Dear all,
I have a MWE where I want to add new constraints into a model inside a function:
m,n=10,20
A=rand(-10:10,m,n)
b=[sum(A[i,:]) for i in 1:m]/2
function Basic_model(A,b,n)
modelo = Model(Gurobi.Optimizer)
@variable(modelo, x[1:n] >=0, Bin)
@objective(modelo, Min, sum(x[i] for i in 1:n))
@constraint(modelo,[i in 1:m],
A*x .>= b
)
@constraint(modelo, con, x[3] + x[2] == 1)
return (modelo)
end
modelo = Basic_model(A,b,n)
@constraint(modelo, x[1] + x[5] ==1)
optimize!(modelo)
The function Basic_model
only build the model to optimize in the future. I want to add new constraints, like this:
@constraint(modelo, x[1] + x[5] ==1)
but this does not work.
Could be possible I build the main model and add/remove new constraints iteratively, outside the function? What is the wrong in the above code?
For some reason, I need to add/remove constraints after the construction of the main model.
O don’t want to rebuild my model several times (because the insertion/remotion of constraints) from initial stage to save computational effort.
Thanks.