How to Assign Variables in a Model Based on Conditions

29 views
Skip to first unread message

Leonardo Ariani

unread,
May 19, 2024, 10:03:43 AMMay 19
to Python-MIP
I'm implementing a scheduling problem in MIP. In the model I have the decision variables x_i_j which determines if j is the next job after i. In order for a job to be the next candidate, it must fulfil certain conditions. For each job i there is a set S_i which contains all successor jobs j, that fulfil those conditions. If a job j is not in this set, then the corresponding variable must be set to zero.  

condition.png
I implemented this as follows:

code.png

But I get the SyntaxError: invalid syntax

How can I add this condition to my model and assign decision variables accordingly?

Any help would be appreciated.

Thiago Giachetto

unread,
May 20, 2024, 6:57:02 AMMay 20
to Python-MIP
Hello,

you should use:
```
...
       model += decision_var[i][j] == 0
```

Take a look on "The Traveling Salesman Problem" example.

Luciano Rigolin de Almeida

unread,
May 20, 2024, 8:18:25 PMMay 20
to Leonardo Ariani, Python-MIP
Hello

Two equals (model += decision_var[i][j] == 0)?

Att,

Luciano Almeida

--
You received this message because you are subscribed to the Google Groups "Python-MIP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-mip+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-mip/3f6c12b0-8531-4fed-8713-0b967cb073cbn%40googlegroups.com.

Thiago Giachetto

unread,
May 21, 2024, 3:08:56 AMMay 21
to Python-MIP
Exactly, with two equals, as is possible to see here -> https://python-mip.readthedocs.io/en/latest/quickstart.html#constraints.

Best regards,
Thiago.

Luciano Rigolin de Almeida

unread,
May 21, 2024, 6:27:37 AMMay 21
to Thiago Giachetto, Python-MIP
Then. My answer was a question. laughter

Leonardo Ariani

unread,
May 22, 2024, 3:01:32 AMMay 22
to Python-MIP
Thank you for your answers. I'm a bit confused on that. This is my understanding of the constraints. 

The first constraint in TSP for example says, the summation of routes going from city i to city j is equal to 1, which ensures each city is left only once. This is a comparison implemented with == in programming languages. In my case, the decision variable must be assigned to zero if the condition is not met, which ensures job j is not selected after job i

Isn't the constrain in TSP a comparison and in my case an assignment ? That's why I used = instead of ==.

Thank you for elaboration.

Luciano Rigolin de Almeida

unread,
May 22, 2024, 3:41:22 AMMay 22
to Leonardo Ariani, Python-MIP
I understood your doubt.

In fact, in linear programming, there is no comparison or assignment. There are restrictions. The solver is the one who assigns values.

When you say "the sum of the routes that go from city i to city j is equal to 1", you are including a restriction in your solution.

When it says model += decision_var[i][j] == 0, that is also a restriction. Here it can be said that, in the end, you are assigning a zero value to decision_var[i][j], as your restriction includes only one variable. But this is a restriction.

In the case of the sum of routes, the solver will assign a value of 1 to any of the variables involved in the sum to guarantee the restriction.

So, you can include ==.

Suggestion, include the line model.write('my_sol.lp') at the end of your code. This will generate a file with your complete solution. You can analyze and check whether your constraints were generated correctly.

Att,

Luciano Almeida

Thiago Giachetto

unread,
May 22, 2024, 4:07:44 AMMay 22
to Luciano Rigolin de Almeida, Leonardo Ariani, Python-MIP
Leonardo, I believe that I understand your doubt and I'll complement Luciano's answer. Looking at Model documentation [link] you have this equivalency:
  • model += x1 + x2 <= 1
  • model.add_constr( x1 + x2 <= 1 )
Both are ways to add a new constraint to the model. It's not a way to assign a new value to a variable (in the sense of programming), but to add a constraint to the model.
In your case, you want to fix a variable of the model: one way to do this is add a constraint to the model that forces it to be zero.

Best regards,
Thiago.

You received this message because you are subscribed to a topic in the Google Groups "Python-MIP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-mip/HUZZyOkm50k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-mip+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-mip/CAOxPKHDotWpq5VyCwHMZucPQ2eOGg_RwJm_q1dhk4yeRQ2D3_w%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages