Skip to main content

Optimisation / linear programming: how do I specify a constraint that a combination of some variables should be different from another

Answered

Comments

2 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?.
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Hi Justin,

    The first step is to introduce binary variables that are equal to 1 if and only if you eat a food product on a particular day. We should assume that if you eat a food, you must eat at least \( m \) of it. If this does not hold, then the optimal solution would be to eat the same foods every day, while additionally eating some negligible \( \epsilon > 0 \) amount of a different food product each day to satisfy the condition that the exact same set of food products are not consumed.

    So, if \( P \) is the set of food products and \( Q \) the set of days, we introduce variables \( z \in \{0, 1\}^{P \times Q} \) and add the constraints:

    $$\begin{align*}m z_{ij} \leq y_{ij} \leq M_i z_{ij} \qquad \forall i \in P,\ j \in Q, \end{align*}$$

    where \( M_i \) is an upper bound on the amount of food \( i \in P \) you can consume. You can derive reasonable values for the \( M_i \) by considering your daily nutritional requirements and the nutrients provided by food \( i \).

    Then, we add constraints to ensure that the same foods are not eaten each day. I.e.:

    $$\begin{align*} \sum_{i \in P} | z_{ij} - z_{ik} | \geq 1 \qquad \forall j, k \in Q,\ j \neq k. \end{align*}$$

    You can model the absolute value using binary variables, or (e.g.) the abs_() helper function in the Python API.

    However, I think there is a better way to solve this problem. Let's consider the problem of optimizing food consumption for a single day. After adding variables \( z \in \{0, 1\}^P \) and the "variable bound" constraints, we solve the problem. Let \( (y^*, z^*) \) be the optimal solution. We add the following inequality to the problem:

    $$\begin{align*} \sum_{\substack{i \in P \colon \\ z^*_i = 1}} (1 - z_i) + \sum_{\substack{i \in P \colon \\ z^*_i = 0}} z_i \geq 1, \end{align*}.$$

    This inequality says that a different set of \( z \) variables must be used in the optimal solution. This is because the only vector in \( \{0,1\}^P \) cut off by the above inequality is precisely the vector \( z^* \). Thus, when we solve the model a second time, the new solution will correspond to a different set of foods. If we repeat this process for each day of the week, we will end up with seven solutions corresponding to the seven best food combinations.

    I hope this helps. Thanks!

    Eli

     

    0

Post is closed for comments.