How to register functions from an array in JuMP?



The signature to register a function in JuMP is as follows

JuMP.register(m::Model, s::Symbol, dimension::Integer, f::Function, ∇f::Function, ∇²f::Function)

I have an array of unnamed functions, like:

funs = [x -> x^2, x -> x + 1]

that I need to use in the definition of a non-linear programming problem. A priori I do not know what functions I will receive in the array, therefore I need my code to be generic.

I tried to register them like this:

for (i,f) in enumerate(funs)
    JuMP.register(m, Symbol("f$i"), 1, f, autodiff=true)

But then I can’t use them, in macros like @NLobjective:

JuMP.@NLobjective(m, Min, eval(Symbol("f1")) + 100(y-x^2)^2)

gives an error. Is there any other way to do this?


You can use the raw expression input format instead of @NLobjective.

But I’m a bit confused, why not write f1 instead of eval(Symbol("f1"))?


@miles.lubin Because I do not know the length of funs a priori. It could be 10, or 1000, so I need to do loops. For example, I would define an NLobjective that sums f1(x) + f2(x) + ..... I might also need to do constrains that involve some of these functions. To do all of this the only way I can think of is somthing like eval(Symbol("f$i"))