Yeah, it works! And it requires less evals than Optim.jl. It would be great if you can address following two minor points:
(1) My external C++ funciton is a very expensive function, and it returns the objective value and gradients in a single call:
f, g[1], g[2] = CppFunc(x1, x2, c, model, config)
Can tron take advantage of this special structure of my problem to reduce the function call? Currently, I wirte
function NLPModels.obj(:: MyProblemNoHess, x :: AbstractVector)
f, _, _ = CppFunc(x[1], x[2], c, model, config)
return f
end
function NLPModels.grad!(:: MyProblemNoHess, x :: AbstractVector, g :: AbstractVector)
_, g[1], g[2] = CppFunc(x[1], x[2], c, model, config)
return g
end
However, if the input x is the same, the funciton shall be evaluated only once to save computation time.
(2) As you can see in the above demo code, I need some extra parameters c, model, config
to call the function. I don’t like these variables lives in the global. But when I put the NLPModels.obj
and NLPModels.grad!
definitions into a function, Julia will complain: ERROR: LoadError: syntax: Global method definition around /path/to/test.jl:273 needs to be placed at the top level, or use “eval”. Is there an elegant way to define these function so they can take local parameters?