"All different" constraint
回答済みHello,
I'm trying to model a problem when I have a list consisting of integers and Gurobi variables.
[<gurobi.Var C0>, 4, <gurobi.Var C1>, 6, <gurobi.Var C2>, <gurobi.Var C3>, 2, 7, 8]
I want to add a constraint so that the final list contains only one of each number from 1-9, similar to a sudoku puzzle. When I try to add this constraint by setting the count of each number 1-9 in the list to 1, I get the error:
GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
I understand that double inequality constraints are not supported in the Python API, and I was wondering if there is a different modeling approach I can take so that no integers are repeated. In Excel's solver it's possible to apply an "alldifferent" (unique value) constraint, and I'm curious if there's something similar in Gurobi. Any help would be greatly appreciated
Matt
-
正式なコメント
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?. -
It looks like you're using integer variables \( x_i \in [1, 9] \) to represent the number assigned to location \( i = 1, \ldots, 9 \). The typical way to model this logic is to instead use binary variables \( x_{ik} \) for \( i = 1, \ldots 9 \), \(k = 1, \ldots 9 \), where \( x_{ik} = 1 \) if and only if number \( k \) is assigned to location \( i \). Then, you can model the "all different" condition with the following constraints:
$$\sum_{i = 1}^{9} x_{ik} = 1 \quad k = 1, \ldots, 9.$$
The full Gurobi installation includes a sudoku.py example that uses constraints of this form.
0
投稿コメントは受け付けていません。
コメント
2件のコメント