# Unable to delete constraints stored in dictionaries using JuMP@.constraint macro

**URL:** https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920
**Category:** Optimization (Mathematical)
**Tags:** jump, optimization
**Created:** [July 29, 2020, 8:45pm UTC](https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920 "2020-07-29T20:45:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![srajan76](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srajan76/32/16690_2.png) [@srajan76](https://discourse.julialang.org/u/srajan76)
#### Post date: [July 29, 2020, 8:45pm UTC](https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920/1 "2020-07-29T20:45:12Z")

</div>

I am unable to delete the constraints which is created and stored in a dictionary using the JuMP.@constraint macro.  
the constraints are added in the form  
for i in array  
d[i] = JuMP.@constraint(model, variable[i] == constant)  
end  
when i delete them using  
for i in array  
delete(model, d[i])  
end  
it doesnt seem to work. any Idea where i am going wrong or what can be done to fix it?

---

<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: [July 29, 2020, 8:48pm UTC](https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920/2 "2020-07-29T20:48:48Z")

</div>

This is a known issue. Please read: [https://github.com/jump-dev/JuMP.jl/issues/1956](https://github.com/jump-dev/JuMP.jl/issues/1956)

---

<div class="post-metadata">

### Author: ![srajan76](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srajan76/32/16690_2.png) [@srajan76](https://discourse.julialang.org/u/srajan76)
#### Post date: [July 29, 2020, 8:54pm UTC](https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920/3 "2020-07-29T20:54:28Z")

</div>

So from what i gather there is no way to delete it… only modification is the option?  
Or do i have to reformulate the whole problem each time before I solve it? I have an equality constraint on some variables which i want to remove , add a few different constraints and re run the optimization

---

<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: [July 29, 2020, 9:46pm UTC](https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920/4 "2020-07-29T21:46:56Z")

</div>

Ah, I mis-read your question. This should work:

```nohighlight
julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> array = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> constant = 1
1

julia> @variable(model, x[array])
1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:
    Dimension 1, [1, 2, 3]
And data, a 3-element Array{VariableRef,1}:
 x[1]
 x[2]
 x[3]

julia> d = Dict()
Dict{Any,Any} with 0 entries

julia> for i in array
           d[i] = @constraint(model, x[i] == constant)
       end

julia> println(model)
Feasibility
Subject to
 x[1] = 1.0
 x[2] = 1.0
 x[3] = 1.0

julia> for i in array
           delete(model, d[i])
       end

julia> println(model)
Feasibility
Subject to

```

---

<div class="post-metadata">

### Author: ![srajan76](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srajan76/32/16690_2.png) [@srajan76](https://discourse.julialang.org/u/srajan76)
#### Post date: [July 29, 2020, 11:18pm UTC](https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920/5 "2020-07-29T23:18:27Z")

</div>

Thats useful…Can you solve the given model-  
julia\> println(model)  
Feasibility  
Subject to  
x[1] = 1.0  
x[2] = 1.0  
x[3] = 1.0  
write its solution and then delete the constraints? I for some reason am still unable to do so  
Also i was using JuMP.@constraint is it different than using @constraint?

---

<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: [July 30, 2020, 1:04am UTC](https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920/6 "2020-07-30T01:04:34Z")

</div>

Please show a minimal version of the code you are trying to run the demonstrates the error.

The first post of [Please read: make it easier to help you - #81](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/81) has some good tips to read first.

---

<div class="post-metadata">

### Author: ![srajan76](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srajan76/32/16690_2.png) [@srajan76](https://discourse.julialang.org/u/srajan76)
#### Post date: [July 30, 2020, 1:15am UTC](https://discourse.julialang.org/t/unable-to-delete-constraints-stored-in-dictionaries-using-jump-constraint-macro/43920/7 "2020-07-30T01:15:18Z")

</div>

Ok.iwas actually thinking perhaps I should use a call back routine… given I an solving the problem then using the solution to add delete constraints and solving again iteratively… will post code shortly
