From: William S F. <ws...@fu...> - 2009-08-31 19:50:20
|
Jörg F. Unger wrote: > I would like to write an optimizer in python and c++. > The optimizer is actually a c++ class Optimizer, whose interface is > exposed to python via swig in module nuto. Within the optimizer, I need > to evaluate the objective function and the gradient of the objective > function. For that purpose, I would like to call a callback routine, so > that I can define the optimization problem on the python level. > > The python file looks something like that > > import nuto > > def objective_function (parameters): > objective = 0 > for i in range(0,parameters.size()): > objective += parameters[i]*parameters[i] > return objective > > def gradient_function(parameters, gradient): > for i in range(0,parameters.size()): > gradient[i] = 2.*parameters[i] > > > #define optimization routine with two variables initialized with zero > Opt = nuto.Optimizer(2) > #set callbacks > Opt.set_callbacks(objective_function,gradient_function) > > #call optimization procedure > Opt.optimize() > > > I define a function objective_function, which calculates from the > current set of parameters to be optimized the objective function (in > this case sum_i parameters[i]^2, where parameters is an std::vector > exposed to python via vector.i in the modul. > > > The problem actually is that when calling the callback function from c > in python, I have to wrap the std::vector to a python object (but I just > want the pointer to be wrapped, so that no data copying is required). > Via this wrapping, the Parameter vector is exposed to the callback > routine in python, which then calculates the objective function. In a > similar way, the callback gradient routine for the gradient is called > and stores the gradient directly in the std::vector, which is just > wrapped on the python level. > > Any ideas including modifications of the algorithm are welcome! The only > important thing is that I want to avoid extensive data copying everytime > a callback routine is called. > > > The header file and the cpp file of the optimizer is are given in > http://pastebin.com/m21d6a04c > http://pastebin.com/d7be2394d > The 2nd pastebin doesn't work so I don't really know what you are doing... pastebin code is not viewable in the email archives, so is best avoided. So all I can suggest is to take a look at directors for the callback and don't %include std_vector.i if you don't want std::vector marshalled into Python lists, as the default handling without std_vector.i is to use a simple pointer. Then maybe you can add some additional helper C++ methods for accessing the std::vector elements, eg: %inline %{ double getNthValue(size_t n, const std::vector<double>& v) { return v[n]; } %} William |