# Fail to re-add new constraints as a new upper bound of an expression in JuMP model

**URL:** <https://discourse.julialang.org/t/fail-to-re-add-new-constraints-as-a-new-upper-bound-of-an-expression-in-jump-model/128850>\
**Category:** Optimization (Mathematical)\
**Tags:** question, optimization\
**Created:** [May 8, 2025, 7:44pm UTC](https://discourse.julialang.org/t/fail-to-re-add-new-constraints-as-a-new-upper-bound-of-an-expression-in-jump-model/128850 "2025-05-08T19:44:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wendy\_Weng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wendy_weng/32/212020_2.png) [@Wendy\_Weng](https://discourse.julialang.org/u/Wendy_Weng)\
**Post date:** [May 8, 2025, 7:44pm UTC](https://discourse.julialang.org/t/fail-to-re-add-new-constraints-as-a-new-upper-bound-of-an-expression-in-jump-model/128850/1 "2025-05-08T19:44:01Z")

</div>

I have a JuMP model called _EP_. I am trying to delete and re-add constraints called _cMaxCap_ as an upper bound for an expression _eTotalCap_. This upper bound has values _max\_cap\_mw_. However, after deleting and re-adding, my new constraints _cMaxCap_ couldn’t obtain the new values of _max\_cap\_mw_. Below is the detail.

1. At each iteration, delete the old constraints cMaxCap. Check these constraints has being successfully deleted.

if haskey(EP.ext, :cMaxCap)  
println(“Deleting old constraints”)  
for (y, cref) in EP.ext[:cMaxCap]  
delete(EP, cref)  
end  
empty!(EP.ext[:cMaxCap])  
else  
println(“there is no cMaxCap”)  
end

This step is successful.

1. update the input data gen[y].max\_cap\_mw and check the inputs are updated correctly.

Update gen[y].max\_cap\_mw externally (assumed done)

This step is successful.

1. add new cMaxCap constraints and check the new constraints. However, the new value cannot be feed into the new constraints at this step, as screenshot shows.

EP.ext[:cMaxCap] = Dict{Any, ConstraintRef}()

for y in MAX\_CAP  
println("gen[$y].max\_cap\_mw = ", gen[y].max\_cap\_mw) ## results(in screenshot below) shows new values  
cap = gen[y].max\_cap\_mw  
EP.ext[:cMaxCap][y] = @constraint(EP, EP[:eTotalCap][y] \<= cap) ## supposed to be new value on RHS  
println("Constraint for $y: ", EP.ext[:cMaxCap][y]) ## **Bug here: results shows old values on RHS**  
end

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/3/c3e59208509ab7f3bfc33623ff49029fc229fc48.png)

I confirm the old constraints have been successfully deleted. As the screenshot shown, gen[y].max\_cap\_mw are new values, 0.1158 etc., but the constraints remain =\< -0.001. -0.001 is the old initial value of gen[y].max\_cap\_mw. **Why the RHS of constraints remain -0.001 instead of 0.1158?**

I have tried many other forms for deleting and adding constraints. For example, @constraint(EP, cMaxCap[y in MAX\_CAP], EP[:eTotalCap][y]\<=gen[y].max\_cap\_mw), but it remains the same bug. So I doubt it is not a syntax issue.

The LHS of the constraints, EP[:eTotalCap][y], is an complex expression. They are not numeric but symbolic. LHS is an10-element Vector{AffExpr}:  
0.25 \_[40657]  
0.25 \_[40658]  
0.25 \_[40659]  
\_[40660] + 0.1168458845  
\_[40661] + 0.072127429 …

I am really appreciated if anyone could provide some advice on this problem.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 8, 2025, 9:45pm UTC](https://discourse.julialang.org/t/fail-to-re-add-new-constraints-as-a-new-upper-bound-of-an-expression-in-jump-model/128850/2 "2025-05-08T21:45:38Z")

</div>

Hi @Wendy_Weng, welcome to the forum 😄

It’s a bit hard to know what’s going on here. It probably depends a lot on how you have structured your code.

Can you provide a reproducible example that I can run to reproduce your issue?

As an alternative suggestion, rather than deleting and re-adding constraints, you may find it easier to manage the cap as a fixed variable. For example, do something like:

```julia
using JuMP
model = Model()
@variable(model, x[1:10, 1:3] >= 0)
@expression(model, eTotalCap[i in 1:10], sum(x[i, :]))
@variable(model, cap[1:10])
@constraint(model, cMaxCap[i in 1:10], eTotalCap[i] <= cap[i])
for i in 1:10
    fix(cap[i], gen[i].max_cap_mw)
end
optimize!(model)
# change gen[i].max_cap_mw
# Update the cap
for i in 1:10
    fix(cap[i], gen[i].max_cap_mw)
end
# Re-optimize
optimize!(model)

```

Now your constraints don’t change, and you can `JuMP.fix` the cap to the appropriate value in each iteration.

See this part of the JuMP documentation: [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Modify-a-constant-term)

---

<div class="post-metadata">

**Author:** ![Wendy\_Weng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wendy_weng/32/212020_2.png) [@Wendy\_Weng](https://discourse.julialang.org/u/Wendy_Weng)\
**Post date:** [May 12, 2025, 8:39pm UTC](https://discourse.julialang.org/t/fail-to-re-add-new-constraints-as-a-new-upper-bound-of-an-expression-in-jump-model/128850/3 "2025-05-12T20:39:52Z")

</div>

Dear @odow, thank you very much for your advise. You are right that the error comes from my code. After I introducing a new variable as you suggested, by printing out the RHS and the constants in the expression of the LHS of the constraint, finally I found out the constants in LHS and the RHS are cancelled out. That’s why it looks as if the new constraint was failed to be added.

Your help is really appreciated!
