Skip to main content

Making variables equal to each other if the solution of them are non-zero: how to add such constraint?

Answered

Comments

3 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff 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?.
  • Alison Cozad
    • Gurobi Staff Gurobi Staff

    It looks like you have the foundation to do this with your binary variables that show when \(x_i\) is positive. Next, you can create big-M constraints that can turn on or turn off your equality constraint.

    For simple notation, let's say binary variable \(y_i = 1\) if \(x_i\) is positive and \(y_i = 0\) otherwise. Additionally, we will pick a sufficiently large value for our \(M\) constant value. More on this later.

    You can add the following two constraints to your model:
    $$\begin{align} f(x_i) - g(x_j) &\leq M(2 - y_i - y_j) &\forall i = 1,2,\ldots,n \quad j = 1,2,\ldots,n\\ f(x_i) - g(x_j) &\geq -M(2 - y_i - y_j) &\forall i = 1,2,\ldots,n \quad j = 1,2,\ldots,n\end{align} $$

    Let's look at the three scenarios that can happen:
    Scenario 1: If both \(x_i\) and \(x_j\) are positive, then \(y_i=1\) and \(y_j=1\). The constraints simplify to
    $$\begin{align} f(x_i) - g(x_j) &\leq M(2 - 1 - 1) &\longrightarrow \quad f(x_i) - g(x_j) &\leq 0 \\ f(x_i) - g(x_j) &\geq -M(2 - 1 - 1) &\longrightarrow \quad f(x_i) - g(x_j) &\geq 0\end{align} $$
    Here, your equality constraint is enforced.

    Scenario 2: If both \(x_i\) and \(x_j\) are non-positive, then \(y_i=0\) and \(y_j=0\). The constraints simplify to
    $$\begin{align} f(x_i) - g(x_j) &\leq M(2 - 0 - 0) &\longrightarrow \quad f(x_i) - g(x_j) &\leq 2M \\ f(x_i) - g(x_j) &\geq -M(2 - 0 - 0) &\longrightarrow \quad f(x_i) - g(x_j) &\geq -2M\end{align} $$
    Since \(M\) is a large value, neither of these constraints should be binding.  Therefore, your equality constraint will not be enforced.

    Scenario 3: If the signs are mixed for \(x_i\) and \(x_j\), then we have \(y_i = 1\) and \(y_j = 0\). The constraints simplify to
    $$\begin{align} f(x_i) - g(x_j) &\leq M(2 - 1 - 0) &\longrightarrow \quad f(x_i) - g(x_j) &\leq M \\ f(x_i) - g(x_j) &\geq -M(2 - 1 - 0) &\longrightarrow \quad f(x_i) - g(x_j) &\geq -M\end{align} $$
    Here, your equality constraint will not be enforced.

    As you can see in the three scenarios, the equality constraint is only enforced when both \(x_i\) and \(x_j\) are positive.

    When choosing a big-M value, you will need to choose a large enough value that the constraints in scenarios 2 and 3 will not be active. However, overly large values of \(M\) can cause numerical challenges. For this reason, I recommend selecting a big-M value that is larger than \(f(x_i) - g(x_j)\) and \(g(x_i) - f(x_j)\) for all combinations of \(i\) and \(j\).

    Finally, I would recommend reading through Dealing with big-M Constraints as you work through this. 

    1
  • Ruoyun Chen
    • Gurobi-versary
    • First Comment
    • First Question

    Thanks a lot for your detailed response!!!

    0

Post is closed for comments.