I’m trying to use a user-defined function that takes in several distinct arguments, and then a splatted N-dim vector of parameters as an @NLexpression to be used in a constraint of an optimization problem.
I described the problem in this thread.
I got the following to work:
function my_constraint_i(q_b_i, q_b_arr...)
q_b= [q_b_arr...]
....
STUFF INVOLVING q_b
constriant_i_out = __function_of_q_b_vector__
....
return constriant_i_out
end
JuMP.register(m, :my_constraint_i, (N+1), my_constraint_i, autodiff=true)
foc_constraint = @eval @NLexpression(m, [i=1:N], my_constraint_i(i, $(q_b...)))
@NLconstraint(m, [i=1:N, foc_constraint[i] == 0)
where my_constraint_i
is a function that takes in an index i
, and a splatted N-dim vector of parameters and outputs the i’th constraint (for constraining an optimizer w/ a a set of N nonlinear equations in N unknowns).
However, @eval appears to be defined in global scope, and I’m hoping to run the solver inside a function (this is an inner loop of a bigger optimization problem). Is it possible to rewrite this with Expr
?
The following did not work
foc_constraint = @NLexpression(m, [i=1:N], Expr(:call, :my_constraint_i, [i, (q_b[j] for j=1:N)...]...))