Creating conditional binary variables
AnsweredHey all
I'm pretty new to mathematical programming, and currently doing so through Python. I have a variable, lets call it X, that I need to be 1 if Y is greater than 0, else 0.
I'm struggling to find a way to linearize this constraint in a simple way as to not increase my solve time too much. Any suggestions are appreciated!
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Summer,
You could try \(Y \leq M \cdot X\), where \(M\) is a number that is at least as large as the upperbound of \(Y\). Ideally you keep \(M\) as tight as possible to avoid running into numerical issues.If you also want to force \(X=0\) when \(Y=0\) then you would need a second constraint. But in many cases this is either irrelevant, or the objective function automatically enforces this relationship (e.g. when there's a cost associated to \(X\).
Kind regards,
Ronald0 -
In addition to Ronald's reply, you can also refer to the Knowledge Base article How do I model conditional statements in Gurobi? which describes how to use indicator constraints.
0
Post is closed for comments.
Comments
3 comments