# How to determine if a constraint is actually applied to the model?

**URL:** <https://discourse.julialang.org/t/how-to-determine-if-a-constraint-is-actually-applied-to-the-model/47176>\
**Category:** Optimization (Mathematical)\
**Created:** [September 24, 2020, 4:12am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-constraint-is-actually-applied-to-the-model/47176 "2020-09-24T04:12:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 24, 2020, 4:12am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-constraint-is-actually-applied-to-the-model/47176/1 "2020-09-24T04:12:24Z")

</div>

I have yet an another question. I have a working costraint:

```julia
            @variable(prmx, PRODAMOUNT[op_k in keys(_ORDER_PRODUCTs_ALL), u_k in keys(UNITS), t in TIME], Int, lower_bound = 0)

            for f_f_k in _FORB
                for (op_k1, op1) in _ORDER_PRODUCTs_ALL
                    if _PRODUCTs_ALL[op1["product"]]["familly"] == split(f_f_k,"-")[1] 
                        @constraint(
                            prmx, 
                            [u_k in keys(UNITS), t in TIME[1:end - 3]],
                            PRODAMOUNT[op_k1, u_k, t]
                            => {
                                sum(PRODAMOUNT[op_k, u_k, t + a]
                                for (op_k, op) in _ORDER_PRODUCTs_ALL, a in [1,2]
                                    if _PRODUCTs_ALL[op["product"]]["familly"] == split(f_f_k,"-")[2]
                                ) == 0
                            }
                        )                 
                    end
                end
            end   

```

This constraint will prohibit moving from forbidden product families. Now my question is how to denote that this actually happens? I mean if this constraint actually is used in the model.

---

<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:** [September 24, 2020, 6:28am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-constraint-is-actually-applied-to-the-model/47176/2 "2020-09-24T06:28:26Z")

</div>

All constraints are added to the model by definition.

If you find a case where the solution violates a constraint, that is a bug, and you should provide a reproducible example.

---

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 24, 2020, 7:24am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-constraint-is-actually-applied-to-the-model/47176/3 "2020-09-24T07:24:08Z")

</div>

The customer wants us to make a constraint so between 2 forbidden families of products we have 2 time units of different conditions. Something like this:

If A and B families are forbidden to use one after another then:

t1 → use family A  
t2 → sum(family B) == 0, but anything else can apply  
t3 → sum(family B) == 0, but anything else can apply  
t…-\> can be anything

Placing constraint as above worked (as I did in the model too), but when I want to print the constraint in the output I don’t know how.

Here is the extract of the solution printout:

```julia
1,V500,500,o12,45,p34,216,G7,216,1
1,CINTAS,1000,o8,63,p8,760,G5,760,3
1,V150,160,o5,49,p6,112,G13,112,1
2,BUHLER1,1500,o11,38,p27,1380,G2,1380,3
2,V150,160,o14,25,p10,150,G2,150,1
3,CINTAS,1000,o6,50,p31,880,G3,880,3
3,BUHLER2,2000,o18,60,p25,3000,G13,1980,3
4,V150,160,o4,36,p44,100,G2,100,1
4,BUHLER2,2000,o17,36,p42,3300,G4,1980,3
5,V500,500,o15,29,p1,480,G13,80,1
5,V500,500,o1,50,p1,480,G13,400,1
5,V150,160,o14,25,p12,60,G2,60,1
5,BUHLER2,2000,o3,63,p26,3000,G2,1980,3
6,CINTAS,1000,o11,38,p45,540,G5,60,3
6,CINTAS,1000,o7,55,p45,540,G5,100,3

```

How can I know if **that constraint** applied to, for example, G3 family (3,CINTAS) and G5 family (6,CINTAS)?

So I want to know where the model actually applied the constraint?

Look if you are about to come to my house, when you arrive I would ask you how did you manage to find out where I am and you would say that my neighbor told you where I live 😄 Now the same thing is here, I want to ask the model why 3,CINTAS and 6,CINTAS are 2 time units apart? Is this because of the above constraint?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 24, 2020, 7:26am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-constraint-is-actually-applied-to-the-model/47176/4 "2020-09-24T07:26:40Z")

</div>

Re-solve the problem without the constraint, and see if the objective value changes?

---

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [September 24, 2020, 7:51am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-constraint-is-actually-applied-to-the-model/47176/5 "2020-09-24T07:51:19Z")

</div>

As re-solving the model might take too much time (if not, I totally agree with @Per that would probably solve your problem) you might look into removing the constraint and then solve (See Sensitivity analysis solutions e.g. [reference request - Sensitivity analysis: Removing one decision variable or constraint from the previous problem. - Mathematics Stack Exchange](https://math.stackexchange.com/a/3629888))

---

<div class="post-metadata">

**Author:** ![sogrbilja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sogrbilja/32/17521_2.png) [@sogrbilja](https://discourse.julialang.org/u/sogrbilja)\
**Post date:** [September 24, 2020, 10:08am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-constraint-is-actually-applied-to-the-model/47176/6 "2020-09-24T10:08:38Z")

</div>

I will then just iterate through the results and based on them try to figure out if the situation is the same as the constraint applied to it.
