I want to define a function that creates a JuMP model that then calls a function to add a set of additional constraints to that model. That set of constraints would be the same across a number of different models.
The following example gives the error: “VariableNotOwned{VariableRef}(x): the variable x cannot be used in this model because it belongs to a different model.”
function f()
m = Model(Gurobi.Optimizer)
@variable(m, x ≥ 0)
@objective(m, Min, 5*x)
g(m)
return m
end
function g(m)
@constraint(m, eqcon, 2*x-3 == 0)
return m
end
mod = f()
The following modification works, but seems a bit tedious for large numbers of variables. Is there a better way to do this?
function g(m)
@constraint(m, eqcon, 2*m[:x]-3 == 0)
return m
end