Skip to main content

Use objective value from objective-function as 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?.
  • Eli Towle
    Gurobi Staff Gurobi Staff

    You can create a new variable and add a constraint requiring it to be equal to the energy-weighted sum of the \( x \) variables. Then, set your objective function equal to this new variable. You can use this variable in the constraints as needed. For example:

    $$\begin{alignat*}{2} \max_{x,z}\ && z \qquad \qquad \qquad \quad & \\ \textrm{s.t.}\ && e_1 x_1 + \ldots + e_n x_n &= z \\ && c_1 x_1 + \ldots + c_n x_n &\geq b \\ && e_1 x_1 + e_2 x_2 &\geq 0.4 z \\ && x, z &\geq 0. \end{alignat*}$$

    1
  • Vitor Pochmann
    Gurobi-versary
    First Question
    First Comment

    The answer of Eli Towle helped me to solve my problem.

    I've created a variable \(z\) that received the sum of  \(x_i \) foods multiplied with \(e_i\) food's energy and I set this variable into objective-function.

    Then, I implemented the second set of constraints are changeable values that need the objective-function value.

    \[\begin{align} z = \sum_{i=1}^n x_i  e_i \\ \max_{x,z} z \\ \text{s.t } c_1 x_1 + ... + c_n x_n \geq b \\ e_1 x_1 + e_2 x_2 \geq 0.4 z \\ e_3 x_3 + e_4 x_4 \geq 0.6 z \\ x, z \geq 0. \end{align}\]

    In Python, looks like it worked. I have tested it and most of the results respected the restrictions. Of course, the model sometimes results in unfeasible states. Maybe I will work with relaxation functions.

    It was expected that the variable \(z\) updates every time Gurobi tries to do a new calculation during its optimization. Therefore, the values of the mutable constraints change too.

    0

Post is closed for comments.