Skip to main content

Gurobi counting: set constraint to a percentage of grid values

Answered

Comments

2 comments

  • Official comment
    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 Tobias,

    For simplicity, let's assume that your grid variables are given by positive and bounded continuous variables \(x_{i,j}\), \(i \in \{1,\dots,N\}, j \in \{1,\dots,M\}\).

    For each variable \(x_{i,j}\), we introduce a binary variable \(\delta_{i,j}\). Let \(T\) be the constant threshold value. The constraints

    \[\begin{align}
    x_{i,j} &\leq T + \delta_{i,j}\cdot C\\
    x_{i,j} &\geq T - C \cdot (1-\delta_{i,j})
    \end{align}\]

    state that if \(delta_{i,j} = 1\) then \(x_{i,j} \geq T\) and if \(delta_{i,j} = 0\) then \(x_{i,j} \leq T\). \(C\) is a large constant, preferably something not too large, e.g., \(C= 2\cdot T\) or the maximal upper bound of the \(x_{i,j}\) variables. We now additionally need the constraint

    \[\sum \delta_{i,j} \leq 0.05\cdot N\cdot M\]

    to make sure that at most 5% of \(x_{i,j}\) variables is \(\geq T\). Note that Gurobi cannot work with strict inequality constraints, thus if you really need \(> T\), then you would have to introduce a small tolerance, e.g., \(10^{-5}\). However, in your case, the tolerance should not be necessary.

    Please note that there might be a better formulation for your particular problem and is not guaranteed to be the best.

    Best regards,
    Jaromił

    0

Post is closed for comments.