1
votes

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?

1
I'd recommend profiling your script to check for bottlenecks rather than blindly making optimizations.Felipe Lema
@FelipeLema Case 1: A small number of optimisations on a complex objective function. Case 2: A large number of optimisations on a very simple objective function. I agree that for Case 1 the penalty is barely measurable. However, for Case 2 it can be significant (see here). Admittedly, most of my personal problems fall in the Case 1 category. Nonetheless, I'm genuinely curious if there is a work-around for Case 2. So the question stands.Colin T Bowers

1 Answers

1
votes

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!