![](/img/trans.png)
[英]CPLEX Python API how to multiply a decision variable with a dummy in the objective function?
[英]How to multiply decision variable with a matrix in Gurobi Python
我对 Gurobi 很陌生,我正在尝试实现一个分配问题,如下所示:
# Create decision variables for the allocation
x = {}
for s in arr1:
for t in arr2:
x[s,t] = m.addVar(vtype=GRB.BINARY, name="allocation")
x[s,t] == x[s,t] * mymat[s]
m.modelSense = GRB.MAXIMIZE
m.update()
我想知道写上面的线性表达式是可能的,这样我就可以最大限度地将具有相似偏好的人分配给相同的任务。
但是,在使用 x[s,t].x 检索结果时,运行如上编写的模型会产生错误。 错误是
GurobiError:无法检索属性“X”
数据从一个单独的文件进入模型,代码如下:
data=c.execute('select id,pref from data')
result = c.fetchall()
pref_data=dict(result)
mymat=defaultdict(int)
a=1
for i in range(1,10):
row = []
for x in range(0,i):
row.append(0)
for j in range(i+1, 10):
if pref_data[i]==pref_data[j]:
row.append(1)
else:
row.append(0)
mymat[a]=row
a+=1
对于所有 a,除非 mymat[a] = 1,否则您的模型是不可行的。 如果不是这种情况,Gurobi 会告诉您该实例不可行(您可以使用status = Model.optimize() 进行检查)并且没有要从您的变量中检索的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.