Passing an array of variables to a user-defined non-linear function

Perhaps what you are looking for can be achieved with @eval. For example, taking n = 3, the code looks like:

m = Model(solver=IpoptSolver(print_level=0))
n=3
@variable(m, 0 <= x[1:n] <= 1)
f(x...)  = rand()
df(g,x...) = g[:] = rand(n)
JuMP.register(m, :obj, n, f, df)

The problematic @NLobjective could be specialized for n=3 as:

@NLobjective(m, Max, obj(x[1],x[2],x[3]))

and this would work. To make it work for other n defined at runtime, we could build-up this expression and @eval it, as follows:

@eval @NLobjective(m, Max, $(Expr(:call, :obj, [Expr(:ref,:x,i) for i=1:n]...)))

This works, but I’m not really sure, it’s the way to solve the underlying optimization problem.