Colin T Bowers
Colin T Bowers

Reputation: 18560

How to pass parameters and data to objective function for optimization with NLopt

In Julia v0.3.10 on Ubuntu 14.04, I need to pass parameters and data to an objective function for use in an optimisation routine using NLopt in Julia. The following example code demonstrates how I currently do this:

function estimate(myModel, myData, myInitialValue, nloptAlgorithm, numberOfParameters)
    opt = Opt(nloptAlgorithm, numberOfParameters)
    localObjectiveFunction = ((param, grad) -> generic_objective_function(param, grad, myModel, myData))
    min_objective!(opt, localObjectiveFunction)
    (objFuncOpt, paramOpt, flag) = optimize(opt, myInitialValue)
end
function generic_objective_function(param, grad, myModel, myData)
    #some code
end

This works, although suffers from the issue that localObjectiveFunction is anonymous so the compiler will not be able to determine the output type of the function at run-time, which in turn has performance implications.

I'm simply wondering if there is a better way to deal with this problem? Should I be using FastAnonymous? Or is there another form of magic that gets around this issue?

Upvotes: 1

Views: 788

Answers (1)

Colin T Bowers
Colin T Bowers

Reputation: 18560

From Julia v0.5, this question will be superfluous. This pull request on github fixes the performance issues with anonymous functions, so from v0.5 onwards, just use anonymous functions!

Upvotes: 1

Related Questions