2016-11-21 81 views
0

我正在編碼優化問題。該模型提供了不可行的解決方案。我想檢查哪個約束條件給出了不可行的解決方案。到目前爲止,我已經在網上查過,但一直沒有能夠想出解決問題的辦法。誰能幫我?例如:在下面的代碼中,由於約束3模型是不可行的。我如何從解決方案中確定它?感謝當模型給出不可行的解決方案時獲取約束的解決方案

from gurobipy import * 

# Create a new model 
m = Model("mip1") 

# Create variables 
x1 = m.addVar(vtype=GRB.INTEGER, name="x1") 
x2 = m.addVar(vtype=GRB.INTEGER, name="x2") 


# Integrate new variables 
m.update() 

# Set objective 
m.setObjective(7*x1 + 2*x2, GRB.MAXIMIZE) 


m.addConstr(-x1 + 2 * x2 <= 4, "constraint-0") 


m.addConstr(5*x1 + x2 <= 20, "constraint-1") 
m.addConstr(-2*x1 -2*x2 <= -7, "constraint-2") 
m.addConstr(x1 <= -2, "constraint-3") 
m.addConstr(x2 <= 4, "constraint-4") 

m.optimize() 

for v in m.getVars(): 
    print('%s %g' % (v.varName, v.x)) 

print('Obj: %g' % m.objVal) 
+0

使用[此](http://www.gurobi.com/documentation/6.5/refman/cpp_grbmodel_computeiis.html)。 – sascha

+0

@sudipta爲什麼x1必須小於或等於-2?只是出於好奇 – user2567806

回答

0

一個爲例:

from gurobipy import * 
 

 
# Create a new model 
 
m = Model("mip1") 
 
# Create variables 
 
x1= m.addVar(lb=0,ub=62,vtype=GRB.INTEGER,name="x1") 
 
x2 = m.addVar(lb=0,ub=50, vtype=GRB.INTEGER,name="x2") 
 
m.update() 
 

 
m.addConstr(-x1 + 2*x2 <= 4, "constraint-0") 
 
m.addConstr(5*x1 + x2 <= 20, "constraint-1") 
 
m.addConstr(-2*x1 -2*x2 <= -25, "constraint-2") 
 
m.addConstr(x1 <= 2, "constraint-3") 
 
#m.addConstr(x2 <= 50, "constraint-4") 
 

 
m.update() 
 

 
# Set objective 
 
m.setObjective(7*x1 + 2*x2, GRB.MAXIMIZE) 
 
m.update() 
 

 
m.optimize() 
 

 
status = m.status 
 
if status == GRB.Status.OPTIMAL: 
 
    for v in m.getVars(): 
 
     print('%s %g' % (v.varName, v.x)) 
 
    print('Obj: %g' % m.objVal) 
 
     
 
elif status == GRB.Status.INFEASIBLE: 
 
    print('Optimization was stopped with status %d' % status) 
 
    # do IIS 
 
    m.computeIIS() 
 
    for c in m.getConstrs(): 
 
     if c.IISConstr: 
 
      print('%s' % c.constrName)