using JuMP
substitute_args(ex, vars) = ex
substitute_args(ex::Symbol, vars) = get(vars, ex, ex)
function substitute_args(ex::Expr, vars)
for (i, arg) in enumerate(ex.args)
ex.args[i] = substitute_args(arg, vars)
end
return ex
end
model = Model()
@variable(model, x[1:2])
str = "x1^3 + 5 * x1 * x2"
ex = Meta.parse(str)
vars = Dict(:x1 => x[1], :x2 => x[2])
set_NL_objective(
model,
MOI.MIN_SENSE,
substitute_args(ex, vars)
)
2 Likes