请原谅我的无知。我在试着理解Docplex。我生成这段代码来建模优化方程,如下图所示。我的主要注意力是约束2c,我找不出错误的根源。
from docplex.mp.model import Model
n=14
T = [i for i in range (4,n+1)]
D= [i for i in range (0,4)]
V = D + T
E= [(i,j) for i in V for j in V if i!=j]
x = [35,41,35,55,55,15,25,20,10,55,30,20,50,30,15]
y = [35,49,17,45,20,30,30,50,43,60,60,65,35,25,10]
c = {(i,j):np.hypot(x[i]-x[j],y[i]-y[j]) for i,j in E}
Omega= [1,2,3,4,5]
Q=[(i,j,w) for i,j in E for w in Omega]
F_w = {(i,j,w):rnd.randint(0,10) for i,j in E for w in Omega}
V_w=[(i,j,w) for i,j in E for w in Omega]
Y_w=[(i,j,w) for i,j in E for w in Omega]
md1= Model('FCMDRP')
q = md1.binary_var_dict(Q,name='q')
f_w = md1.continuous_var_dict(F_w, name='f_w')
y_w = md1.binary_var_dict(Y_w,name='y_w')
v_w = md1.binary_var_dict(V_w,name='v_w')
# objective
md1.minimize(md1.sum(c[i,d]*q[i,d,w]+c[d,i]*q[d,i,w] for i in T for d in D for w in Omega))
# constraint 2c
for w in Omega:
md1.add_constraints(v_w[d,i,w]==f_w[i,j,w]*y_w[i,j,w] for d in D for i in V for j in V if i!=d and i!=j) # 2c
错误是
docplex.mp.utils.DOcplexException: Expecting sequence of linear constraints, got: docplex.mp.QuadraticConstraint[](v_w_0_1_1,EQ,f_w_1_0_1*y_w_1_0_1) at position 0
原来的方程式在这里
我已经浏览过这一页,最近的问题是这和这,但它们都解决不了我的问题。请各位高尚的人民在这个崇高的平台上,我需要你们的帮助才能进步。提前谢谢。
发布于 2022-03-05 08:26:47
将二进制决策变量乘以决策变量。
你要么使用逻辑约束
from docplex.mp.model import Model
mdl = Model(name='mutiply binary by decision variable')
b = mdl.binary_var(name='b')
x = mdl.integer_var(name='x',lb=0,ub=10)
bx= mdl.integer_var(name='bx')
mdl.maximize(x)
mdl.add(bx<=7)
mdl.add(mdl.if_then((b==0),(bx==0)))
mdl.add(mdl.if_then((b==1),(bx==x)))
mdl.solve()
decisionVars=[b,x]
for v in decisionVars:
print(v.name," = ",v.solution_value)
或线性化
from docplex.mp.model import Model
mdl = Model(name='mutiply binary by decision variable')
b = mdl.binary_var(name='b')
x = mdl.integer_var(name='x',lb=0,ub=10)
bx= mdl.integer_var(name='bx')
mdl.maximize(x)
mdl.add(bx<=7)
#Linearization
mdl.add(2*b<=bx)
mdl.add(bx<=10*b)
mdl.add(bx<=x-2*(1-b))
mdl.add(bx>=x-10*(1-b))
mdl.solve()
decisionVars=[b,x]
for v in decisionVars:
print(v.name," = ",v.solution_value)
发布于 2022-03-06 09:00:55
我发现我的代码哪里出错了。回答:亚历克斯·弗莱舍带领我发现了我的错误。这是微不足道的,但我觉得它值得分享。方程2b和2c中的f_ij(w)
和f_di(w)
不是决策变量,但我将它们作为决策变量。因此,我将f_w
注释掉,并使用了最初定义为dict变量的F_w
。即F_w = {(i,j,w):rnd.randint(0,10) for i,j in E for w in Omega}
所以而不是
for w in Omega:
md1.add(v_w[d,i,w]==F_w[i,j,w]*y_w[i,j,w] for d in D for i in V for j in V if i!=d and i!=j) # 2c
我用过
for w in Omega:
md1.add(v_w[d,i,w]==f_w[i,j,w]*y_w[i,j,w] for d in D for i in V for j in V if i!=d and i!=j) # 2c
这就是我在贴出的问题中提到的错误的原因。
https://stackoverflow.com/questions/71356333
复制相似问题