I’m trying to build a toy program using the nonlinear facility of JuMP. The general structure looks like this:
function solve(constraint::String)
model = Model()
vars = @variable(model, [1:10], base_name="x")
…
cons_expr = Meta.parse(constraint)
add_NL_constraint(model, cons_expr)
end
The problem here is: function add_NL_constraint
accepts an Expr
of JuMP variables, which are created in solve
. Therefore, directly parsing the input string (such as the above code) will not work.
To be more specific, that means:
add_NL_constraint(model, :($(vars[1])==0)) # OK
add_NL_constraint(model, Meta.parse("vars[1]==0")) # not OK, not JuMP variables
add_NL_constraint(model, Meta.parse("\$(vars[1])==0")) # not OK, does not trigger interpolation
Is there a way to enable interpolation in Meta.parse()
, so that the last approach can work?