-
-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deleting constraint in order to modify it not possible due to name registered in model #1956
Comments
JuMP has functions to modify constraints directly. See That said, it looks like you have found a bug. Once all of the constraints have been deleted, the name should probably be deleted as well. |
It also seems to break model = Model()
@variable(model, x)
@constraint(model, constr, x >= 1)
delete(model, constr)
copy(model) produces the error :
|
This doesn't quite achieve what I want, and setting a variable to be fixed turns my problem into a bilinear problem which is also an issue. My current solution is to just remake the entire model since speed is currently not an issue. |
Why not? What part are you modifying? |
I have power system expansion model (i.e. it tells you what technologies to invest in which meet electrical power demand while minimising operational and investment costs). I rerun my model every time with an increasing CO2 price, so I have a term similar to this:
If I want to change that CO2 price in a simpleish way, I could use the method showed here but you get a bilinear term. I'm sure I could find a way around this, e.g. make (Note that I use model and optimisation problem interchangeably) |
Would just like to bring this up again. I'm currently programming a progressive hedging algorithm and it would make my life easier if I could just redefine one constraint, reformulate my objective and then solve, instead of rebuilding my entire model every time. |
There is really no need to keep rebuilding the model. This will kill the performance of you PH. See https://www.juliaopt.org/JuMP.jl/v0.20.0/constraints/#Modifying-a-variable-coefficient-1 function build_model()
CO2Price = 1
model = Model()
@variable(model, C02EmissionsInTonnesPerMWH >= 0)
@variable(model, OPEX)
@constraint(model, opex_constraint, OPEX == CO2Price * CO2EmissionsInTonnesPerMWH)
return model
end
function set_co2_price(model, price)
# !! IMPORTANT !!
# JuMP will normalize:
# OPEX == emissions * price
# to
# OPEX - price * emissions == 0,
# moving all variables to the LHS, and all constants to the RHS.
# set_normalized_coefficient sets the coefficient in the normalized constraint
# so we need to set -price.
set_normalized_coefficient(
model[:opex_constraint], model[:CO2EmissionsInTonnesPerMWH], -price
)
return
end
model = build_model()
set_price(model, 2)
optimize!(model)
set_price(model, 3)
optimize!(model) If you're implementing PH, see https://github.com/martinbiel/ProgressiveHedgingSolvers.jl. cc @martinbiel. |
If you don't want to modify the coefficients for some reason, another workaround is to use anonymous constraints: con = @constraint(model, [i=1:2,j=1:2], x[i,j] >= 1)
for i=1:2
for j=1:2
delete(model, con[i,j])
end
end
con = @constraint(model, [i=1:2,j=1:2], x[i,j] >= 2) Given the various workarounds, we haven't prioritized addressing this issue so far. |
Indeed there are a lot of workarounds, and I haven't implemented any (yet) due to sheer laziness. Still, I hope that eventually something similar to |
I believe this bug is still present in v0.21.2 @NLparameter(pm.model,x_p[i = 1:N] == x_hat[i]) # this is done only once
...
for i = 1:len
x_hat = JuMP.lower_bound.(vars) + factor.*rand(rng, Float64, (N,1)); # new values
JuMP.set_value.(x_p, x_hat) # iteratively update the values # update
# optimize, etc.```
|
Hello, I wonder if there is any news or plan in addressing this issue. however, the expression is too large and complex that setting all specific parameters to every variable would be so complex that it would be easier to rebuild the model. Thank you, |
You can still get around this. Good workflow tip: save your expressions, variables and constraints in model.ext[:variables] = Dict()
model.ext[:expressions] = Dict()
model.ext[:constraints] = Dict()
model.ext[:constraints][:con1] = @constraint(model, ...)
delete!(model[:constraints][:con1]) Or something along those lines. That way if you pass your model to a function you can modify it. |
I run an MILP several times with different parameter values. Instead of recreating the model every time, I would like to be able to just change the relevant constraints. From the documentation, I figured that I'd be able to do this by deleting the constraint and then reformulating it.
Here's a toy example:
This produces the error:
One workaround I can see is to delete the name
con
in model, but I don't know how / cannot do this. Ideally I would like a simpler way of modifying constraints, for example as:However it would appear that this is quite difficult judging from this issue: #1241
The text was updated successfully, but these errors were encountered: