Skip to main content

Use objective value from objective-function as constraint




  • 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*}$$

  • Vitor Pochmann
    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.


Please sign in to leave a comment.