On Mar 28, 2012, at 12:10 PM, Mathias Vanwolleghem wrote:
I would like to try out the possibilities of this library in a Python code. However I noticed that the Python API does not allow to pass extra parameters to the objective function, contrary to the C API. Did I read it well that the objective function can only take two arguments: the optimization variable numpy array, and the gradient ?
Is there a way to nevertheless pass extra function parameters.

You can pass as many extra parameters as you want to the Python objective, using a lambda. (Because Python has proper closures and lexical scoping, it doesn't need hacks like the extra void* parameter in C or "functor" objects in C++.)

There is an example of this in the NLopt tutorial -- look at the "myconstraint" function, where we pass two extra parameters "a" and "b" via a lambda.

e.g. if your objective is a function f(x,grad,p) that takes an extra parameter p, then you would do:

        opt.set_min_objective(lambda x,grad: f(x,grad,somep))

to pass "somep" for the "p" parameter.

--SGJ

_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss

Reply via email to