turn on constraints only if a given condition is verified
AnsweredHello, my model has 100 binary variables of which 50 are type "V1" and 50 are type "V2". I have 2 types of constraints, let's say, constraints type "C1" and constraints type "C2". C1 type constraints are always on. I would like to turn on some constraints only if the model selects all the V1 type vars. As an example, I want to turn the constraints type "C2" on, only if the sum of var_type_V1 is equal to 50 in a single optimization process.
How can I do it?
Many thanks for helping!!
-
Official comment
This post is more than three 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 why not try our AI Gurobot?. -
Hi Gabi,
One way would be to introduce an additional binary variable \(y\) and add the following two constraints
\[\begin{align}
\sum_{i \in V_1} b_i &\geq 50y\\
\sum_{i \in V_1} b_i &\leq 49+y\\
b_i \in &\{0,1\} \, i \in V_1,\, y \in \{0,1\}
\end{align}\]This way, if the sum of binaries over the set \(V_1\) equals 50, i.e., \(\sum_{i \in V_1} b_i = 50\), then \(y=1\). But if \(\sum_{i \in V_1} b_i < 50\), then \(y = 0\). You can then use the binary variable \(y\) and indicator constraints to enforce \(C_2\) constraints only if \(y=1\), i.e.,
\[ y = 1 \Rightarrow C_2 \text{ type constraint} \]
Note that this only works because \(\sum_{i \in V_1} b_i \) cannot be greater than \(50\). This is just one option an there might be an easier way to achieve the above.
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments