If condition using Addconstrs() function
回答済みHi,
I am trying to write simple conditional constraints in gurobi on python platform as:
for i in range(N=100):
if ( xf1 - x1[i] <= 0 && yf1 - y1[i] <= 0 )
x1[i] - x2[i] < = 3
y1[i] - y2[i] < = 4
Whereas:
xf1 = final state for x1 in x-axis
yf1 = final state for y1 in y-axis
x1[i] = The position of the point 1 on each iteration of "i" on x-axis
y1[i] = The position of the point 1 on each iteration of "i" on y-axis
x2[i] = The position of the point 2 on each iteration of "i" on x-axis
y2[i] = The position of the point 2 on each iteration of "i" on y-axis
How can I convert these conditions into Gurobi constraints form in the python platform?
Kind regards,
Haris
-
正式なコメント
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 Haris,
You cannot access the variable values when creating the model. Thus, you have to use MIP modelling techniques to formulate and \(\texttt{if}\)-statements. The stackexchange post How to write if else statement in Linear Programing? explains it very well.
First you have to formulate statement to know whether \(x_{f1} - x_1 \leq 0\) and \(y_{f1}-y_1 \leq 0\). To achieve this you can introduce 2 binary variables
\[\begin{align}
x_{f1} - x_1 &\leq M (1-\delta_x)\\
x_{f1} - x_1 &\geq 0.00001 - M \delta_x \\
y_{f1} - y_1 &\leq M (1-\delta_y)\\
y_{f1} - y_1 &\geq 0.00001 - M \delta_y\\
\delta_x,\delta_y &\in \{0,1\}
\end{align}\]
The above inequalities state that if \(\delta_x=1\), then \(x_{f1} - x_1 \leq 0\) and if \(\delta_x=0\), then \(x_{f1} - x_1 \geq 0.00001\). Same for \(\delta_y\). Note that one has to introduce a small tolerance because it is not possible to model strict inequalities in Gurobi.Next, you want to force the inequalities \(x_1 - x_2 \leq 3, y_1 - y_2 \leq 4\) only if both of the previously introduced binaries \(\delta_x,\delta_y\) equal 1. For this you can introduce a new binary variable \(\delta\) and use the addGenConstrAnd function stating that \(\delta = \delta_x \land \delta_y\). You can then use the binary variable \(\delta\) to introduce indicator constraints stating
\[\begin{align}
\delta = 1 &\rightarrow x_1 - x_2 \leq 3\\
\delta = 1 &\rightarrow y_1 - y_2 \leq 4
\end{align}\]Please note that there is no guarantee that the above formulation is best and there may be a more efficient way of formulating your problem.
Best regards,
Jaromił0 -
Thank you so much Najman! It is really helpful!
0
投稿コメントは受け付けていません。
コメント
3件のコメント