2017-08-02 98 views
0

可能有人请帮我这个错误:Gurobi蟒蛇:错误与索引中的var回调懒约束

当我使用添加懒约束回调,我收到这个后续消息:

Exception TypeError: 'Var' object does not support indexing in 'gurobipy.callbackstub' ignored

错误来自包含“selected”的行。我尝试测试2个回调版本,但没有成功。我有alos尝试在Gurobi上运行TSP的例子,但model._vars.keys()不工作。我使用Gurobi 6.5.2和Python 2.7。打印model._vZ得到:

[<gurobi.Var vZ_j((9, 6, 6), 3)_i((4, 7, 6), 3)_e(3, 'P2')>, <gurobi.Var vZ_j((9, 6, 6), 3)_i((4, 7, 6), 3)_e(3, 'P3')>, <gurobi.Var vZ_j((9, 6, 6), 3)_i((4, 7, 6), 3)_e(3, 'P4')>, <gurobi.Var vZ_j((9, 6, 6), 3)_i((4, 7, 6), 3)_e(3, 'P5')>, <gurobi.Var vZ_j((3, 8, 6), 2)_i((10, 7, 5), 3)_e(3, 'P1')>, <gurobi.Var vZ_j((3, 8, 6), 2)_i((10, 7, 5), 3)_e(3, 'P2')>, <gurobi.Var vZ_j((3, 8, 6), 2)_i((10, 7, 5), 3)_e(3, 'P3')>, <gurobi.Var vZ_j((3, 8, 6), 2)_i((10, 7, 5), 3)_e(3, 'P4')>, <gurobi.Var vZ_j((3, 8, 6), 2)_i((10, 7, 5), 3)_e(3, 'P5')>, <gurobi.Var vZ_j((10, 10, 6), 3)_i((4, 7, 6), 3)_e(3, 'P1')>, <gurobi.Var vZ_j((10, 10, 6), 3)_i((4, 7, 6), 3)_e(3, 'P2')>, <gurobi.Var vZ_j((10, 10, 6), 3)_i((4, 7, 6), 3)_e(3, 'P3')>, <gurobi.Var vZ_j((10, 10, 6), 3)_i((4, 7, 6), 3)_e(3, 'P4')>, <gurobi.Var vZ_j((10, 10, 6), 3)_i((4, 7, 6), 3)_e(3, 'P5')>] 

[,,,,,,,,,,,,,]

由于

def subtourelim(model, where): 
    if where == GRB.Callback.MIPSOL: 
     vZs = model.cbGetSolution(model._vZ) 
     vXs = model.cbGetSolution(model._vX) 
     opeqp= model._OpEqP 
     nz=model._nz 
     for e in opeqp: 
      selected = [(model._vZ[i][0],model._vZ[i][1]) for i in range(nz) if model._vZ[i][2]==e and model._vZ[i][1]!=(b1,T)]    
      bti=b0 
      chaine_primo=[]    
      while True: 
       bti=[bt for bt in BlocsEPT if vZs[bti,bt,e]>0.5][0] 
       if bit!=(b1,T): 
        chaine_primo.append(bti) 
       else: 
        break 
      unvisit=[bt for bt in Blocs_EP if vXs[bt,e]>0.5 and bt not in chaine_primo] 
      if len(unvisit)>0: 
       tour = subtour(selected,unvisit,chaine_primo,Blocs_EP) 
       ArcsTour=[(bt0,bt1) for bt in tour for bt1 in tour if (bt0,bt1) in CBloc_T and vZs[bt0,bt1,e]>0.5] 
       model.cbLazy(quicksum(model._vZ[ct[0],ct[1],e] for ct in ArcsTour) <= len(tour)-1) 

回答

0

谢谢,我找到了解决办法:

selected = tuplelist((cte[0],cte[1]) for cte in model._vZ if cte[2]==e if model.cbGetSolution(model._vZ[cte]) > 0.5 and cte[1]!=(Fb1,FT)) 

“model._vZ”来自“model.getVars()”,这是一个列表。我将其更改为“model._vZ = varZ”,它是词典,然后应用评估“选定”