# Constraints that do not work together

**URL:** <https://discourse.julialang.org/t/constraints-that-do-not-work-together/87482>\
**Category:** Optimization (Mathematical)\
**Created:** [September 19, 2022, 4:01pm UTC](https://discourse.julialang.org/t/constraints-that-do-not-work-together/87482 "2022-09-19T16:01:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zizilabg](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@Zizilabg](https://discourse.julialang.org/u/Zizilabg)\
**Post date:** [September 19, 2022, 4:01pm UTC](https://discourse.julialang.org/t/constraints-that-do-not-work-together/87482/1 "2022-09-19T16:01:13Z")

</div>

Hello,

I wrote a model about Julia which contains 3 constraints. If I test one constraint at a time, each one works. But if I run the whole program, I get the following error message:

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

Has anyone had this problem before and would know where it could be coming from?

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [September 19, 2022, 7:55pm UTC](https://discourse.julialang.org/t/constraints-that-do-not-work-together/87482/2 "2022-09-19T19:55:59Z")

</div>

Hey @Zizilabg, do you mind sharing your code?  
It would definitely help to know which packages you use besides `MathOptInterface`

---

<div class="post-metadata">

**Author:** ![Zizilabg](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@Zizilabg](https://discourse.julialang.org/u/Zizilabg)\
**Post date:** [September 20, 2022, 4:29am UTC](https://discourse.julialang.org/t/constraints-that-do-not-work-together/87482/3 "2022-09-20T04:29:35Z")

</div>

```julia
using XLSX
using JuMP
using Cbc
using DataFrames

# change working directory to the one containing this file
cd(@ __DIR__ )

fichierDonnees=XLSX.readxlsx("choix élèves julia.xlsx")

ws=fichierDonnees["ELEVES"] # sélection de la feuille du fichier Excel (ws pour "Worksheet")

n=2
while !ismissing(ws[n,2]) # ismissing retourne true si la cellule est vide
    global e=ws[n,1]
    global n=n+1
end

ws2=fichierDonnees["SUJETS"]

l=2
while !ismissing(ws2[l,1]) # ismissing retourne true si la cellule est vide
    global s=ws2[l,1]
    global l=l+1
end

q = [0, 5, 20, 100, 1000]

p=zeros(e,s)
for i in 1:e, j in 1:s
   if j==ws[i+1,4]
        p[i,j]=q[1]
    elseif j==ws[i+1,6]
        p[i,j]= q[2]
    elseif j==ws[i+1,8]
        p[i,j]=q[3]
    elseif j==ws[i+1,10]
        p[i,j]= q[4]
    else
       p[i,j]= q[5]
    end
end

TB=Model(optimizer_with_attributes(Cbc.Optimizer))

@variable(TB,x[1:e, 1:s],Bin)

@objective(TB, Min,sum(sum(x[i,j]*p[i,j] for j in 1:s) for i in 1:e))

@constraint(TB, contraintebase[i=1:e], sum(x[i,j] for j in 1:s ) == 1)

@variable(TB, y1, Bin)
@variable(TB, y2, Bin)
@constraint(TB, contrainte1a, y1+y2 == 1)
@constraint(TB, contrainte1b[j=1:s], sum(x[i,j] for i in 1:e ) >= 3*y1)
@constraint(TB, contrainte1c[j=1:s], sum(x[i,j] for i in 1:e ) <= 1000*(1-y2))

#contrainte 2
for j in 1:s
    if ws2[j+1, 3] == 1
        @constraint(TB, sum(x[i, j] for i in 1:e) <= 8)
    else
        @constraint(TB, sum(x[i, j] for i in 1:e) <= 12)
    end
end

#contrainte 3
for i in 1:e, j in 1:s
    if (ws[i+1,3]==1)
    @constraint(TB, x[i,j]==1)
    end
end

JuMP.optimize!(TB)

println("Attribution des séminaires:")
println()

    for i in 1:e, j in 1:s
        if JuMP.value.(x[i,j])==1
            println(ws[i+1,2]," est attribué à ",ws2[j+1,2])
        end
    end

println("Pénalités totales: ", objective_value(TB))

```

---

<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 20, 2022, 5:10am UTC](https://discourse.julialang.org/t/constraints-that-do-not-work-together/87482/4 "2022-09-20T05:10:45Z")

</div>

You need to check if the solver found a solution:  
[https://jump.dev/JuMP.jl/stable/manual/solutions/#Recommended-workflow](https://jump.dev/JuMP.jl/stable/manual/solutions/#Recommended-workflow)

Your model might be infeasible.

---

<div class="post-metadata">

**Author:** ![Zizilabg](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@Zizilabg](https://discourse.julialang.org/u/Zizilabg)\
**Post date:** [September 28, 2022, 3:53pm UTC](https://discourse.julialang.org/t/constraints-that-do-not-work-together/87482/5 "2022-09-28T15:53:13Z")

</div>

Thank you for your answer.

I wrote this:

```julia
if termination_status(TB) == OPTIMAL
    println("Solution is optimal")
elseif termination_status(TB) == TIME_LIMIT && has_values(model)
    println("Solution is suboptimal due to a time limit, but a primal solution is available")
else
    error("The model was not solved correctly.")
end
println(" objective value = ", objective_value(TB))
if primal_status(TB) == FEASIBLE_POINT
    println(" primal solution: x = ", value(x))
end
if dual_status(TB) == FEASIBLE_POINT
    println(" dual solution: c1 = ", dual(c1))
end

```

But got this error message:

```julia
LoadError: The model was not solved correctly.

```

---

<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 28, 2022, 5:26pm UTC](https://discourse.julialang.org/t/constraints-that-do-not-work-together/87482/6 "2022-09-28T17:26:27Z")

</div>

Yes, because your model doesn’t have a feasible solution. Read the debugging guide I posted in your other question: [Result index of attribute - #2 by odow](https://discourse.julialang.org/t/result-index-of-attribute/87891/2)
