メインコンテンツへスキップ

If condition using Addconstrs() function

回答済み

コメント

3件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    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?.
  • Jaromił Najman
    • Gurobi Staff

    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
  • Muhammad Haris
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you so much Najman! It is really helpful!

    0

投稿コメントは受け付けていません。