Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python GurobiError:无法将参数转换为表达式_Python_Gurobi - Fatal编程技术网

Python GurobiError:无法将参数转换为表达式

Python GurobiError:无法将参数转换为表达式,python,gurobi,Python,Gurobi,有人能帮我解决这个错误吗? 当我得到错误时,我应该在哪里查找:gurobierro:无法将参数转换为表达式。 我应该补充一点,我正在python中使用Gurobi库 from gurobipy import* m=Model('mymodel') def label(c): return "x" + str(c).translate(None, " '") shifts = [1,2] hours = [1,2] games = ['bj', 'cr'] pits = [1

有人能帮我解决这个错误吗? 当我得到错误时,我应该在哪里查找:gurobierro:无法将参数转换为表达式。 我应该补充一点,我正在python中使用Gurobi库

from gurobipy import*

m=Model('mymodel')

def label(c):
    return "x" + str(c).translate(None, " '")

shifts = [1,2]    
hours = [1,2]
games = ['bj', 'cr']
pits = [1,2]
order1 = [1,2]
order2 = [1,2,3]
combo, oi = multidict( {
 (1,1,'bj',1,1,1): 100,
 (1,1,'bj',1,1,2):200,
 (1,1,'bj',1,1,3):200,
 (1,1,'bj',1,2,1):50,
 (1,1,'bj',1,2,2):70,
 (1,1,'bj',1,2,3):70,
 (1,1,'cr',1,1,1):400,
 (1,1,'cr',1,1,2):450
})

combo= tuplelist(combo)
for s  in shifts:
    for t in hours:
        for i in games:
            for n in order1:
                     m.addConstr(quicksum(x[s,t,i,p,n,k] for s,t,i,p,n,k in combo.select(s,t,i,'*',n,'*'))- int(1)== 0, name=label((s,t,i,p,n,k))

如果您试图添加一个没有模型变量的约束,Gurobi会抱怨。对于某些变量组合,列表枚举将构造空列表,即:

m.addConstr(quicksum([])-1==0)

阿卡

m.addConstr(-1==0)

这是不可能的。事实上,在下面的示例中,gurobi仍然会抛出一个错误,因为即使表达式不包含变量也是可行的


m.addConstr(-1此问题在Gurobi 6.0.0中得到修复。在那里
quicksum([])
返回一个值为0的线性表达式(而不是浮点0.0),这解决了原始问题

5.6.3版及之前版本中的问题是使用所谓的

当您呼叫时,您可以使用显式的“lefthandside”、“operator”、“righthandside”方法:

m.addConstr(quicksum([]), GRB.EQUAL, 0)
这将毫无问题地起作用

如果您在空列表中使用并使用
quicksum
,实际情况如下:

m.addConstr(quicksum([]) == 0)
quicksum
返回0,如果列表为空,则您的语句为:

m.addConstr(0 == 0)
0==0
被转换为
True
,因此您实际上调用:

m.addConstr(True)
这显然是Gurobi无法处理的(但它可以给出更好的错误描述)

总而言之:如果您使用quicksum,并且有可能列表为空,您可以按照blueenvelope的建议检查列表是否为空,使用显式方法,或者使用小包装方法,如果这种情况经常发生:

def add_constr(model, expression, name=""):
    if expression is True:
        return model.addConstr(0, GRB.EQUAL, 0, name)
    elif expression is False:
        raise Exception('`False` as constraint for {}'.format(name))
    else:
        return model.addConstr(expression, name)
此包装器仅作为表达式适用于tempcontr