Slurping and splatting in JuMP

question

#1

I need to define a function that takes a large number of arguments. I am trying to follow JuMP documentation:

All arguments to user-defined functions are scalars, not vectors. To define a function which takes a large number of arguments, you may use the splatting syntax f(x…) = …

So did this, but it does not work:

m = JuMP.Model(solver=Ipopt.IpoptSolver())
f(x...) = x[1] + x[2]
JuMP.register(m, :f, 2, f, autodiff=true)
JuMP.@variable(m, x[i=1:2], start = 0.)
JuMP.@NLobjective(m, :Min, f(x...) + 100(x[2]-x[1]^2)^2)

I also tried different things using setNLobjective and raw expression input, but it also didn’t work.

What am I doing wrong?


How to register functions from an array in JuMP?
#2

I found the answer I was looking for here:

https://stackoverflow.com/a/44711305/855050

To gist is to use something like:

JuMP.setNLobjective(m, :Min, Expr(:call, :myf, x...))