Using addGenConstrAnd leads to "max general constraint violation"
回答済みRecently I tested my Gurobi implementation on larger problem instances. But I got:
Warning: max constraint violation (1.0000e+00) exceeds tolerance
Warning: max general constraint violation (1.0000e+00) exceeds tolerance
(model may be infeasible or unbounded - try turning presolve off)
So I decided to fix the variables to their solution after the first optimization step and optimize() again:
for v in m.getVars():
v.LB = v.X
v.UB = v.X
m.optimize()
And indeed, Gurobi claims "Model is infeasible". Therefore, I decided to compute the IIS.
This is the resulting model.ilp file (with renamed variable names):
Minimize
0 a + 0 b + 0 c
Subject To
Bounds
b = 0
c = 1
Binaries
a
b
c
General Constraints
GC73678: c = AND ( a , b )
End
Using this information I quickly found the code that generates this AND-constraint:
m.addConstr(c == and_(a, b))
For some reason I decided to replace the general constraint with my own implementation for the logical AND:
def logical_and(m, y, vars):
n = len(vars)
m.addConstr(quicksum(vars) - n * y >= 0)
m.addConstr(quicksum(vars) - n * y <= n - 1)
logical_and(m, c, [a, b])
And suddenly the model was feasible and no warnings were printed.
My questions:
- How is it possible that Gurobi violates a general-AND-constraint for a feasible model and just prints a warning?
- Why did my AND-implementation work better than the built-in one?
-
正式なコメント
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?. -
I have a relatively small minimal example, once with the built-in "_and" and once with my custom "logical_and". Both models are clearly feasible but with the built-in "_and" I get this "max general constraint violation" warning.
I'm happy to send it to you if you are interested.
0 -
Hi Hans,
Could you post your minimal working example? Thanks!
Eli
0 -
I uploaded my model with general-AND constraints to your file uploader at https://www.gurobi.com/upload-a-model/ I hope that was the correct way.
The file is named: minimal_example_genconstr_and.rew.gz
When you run it like this:
m = read("minimal_example_genconstr_and.rew.gz")
m.optimize()for v in m.getVars():
v.LB = v.X
v.UB = v.Xm.optimize()
You will get this for the first optimize():
Solution count 1: 0
Optimal solution found (tolerance 1.00e-04)
Warning: max constraint violation (1.0000e+00) exceeds tolerance
Warning: max general constraint violation (1.0000e+00) exceeds tolerance
(model may be infeasible or unbounded - try turning presolve off)
Best objective 0.000000000000e+00, best bound 0.000000000000e+00, gap 0.0000%And then this for the second optimize():
Solution count 0
Model is infeasible
However, when you replace all the AND-constraints with my custom implementation:
from gurobipy import *
def main():
m = read("minimal_example_genconstr_and.rew.gz")
gencons = m.getGenConstrs()
for gc in gencons:
if gc.getAttr(GRB.Attr.GenConstrType) == GRB.GENCONSTR_AND:
resvar, vars = m.getGenConstrAnd(gc)
m.remove(gc)
logical_and(m, resvar, vars)
m.optimize()
def logical_and(m, y, vars):
n = len(vars)
m.addConstr(quicksum(vars) - n * y >= 0)
m.addConstr(quicksum(vars) - n * y <= n - 1)
if __name__ == "__main__":
main()Suddenly, it is feasible and no warning is printed:
Solution count 1: 0
Optimal solution found (tolerance 1.00e-04)
Best objective 0.000000000000e+00, best bound 0.000000000000e+00, gap 0.0000%0 -
It should be noted that running the model with Presolve turned off does not make the model infeasible or unbounded:
gurobi_cl Presolve=0 minimal_example_genconstr_and.rew.gz
Optimal solution found (tolerance 1.00e-04)
Best objective 0.000000000000e+00, best bound 0.000000000000e+00, gap 0.0000%0 -
Hi Hans,
Thanks for bringing this to our attention! The underlying issue has been fixed for subsequent releases of Gurobi.
Eli
0
投稿コメントは受け付けていません。
コメント
6件のコメント