docplex.mp.utils.DOcplexException:期望线性约束序列,得到:docplex.mp.QuadraticConstraint[]
请原谅我的无知。我正在尝试我对 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
我浏览过此页面,我得到的最接近的问题是 这个和这个但他们都没有解决我的问题。拜托,这个崇高平台上的高尚人们,我需要你们的帮助才能进步。提前致谢。
Please pardon my ignorance. I was trying my understanding on Docplex. I produce this code to model optimization equations as shown in the picture below. My main concentration is constraint 2c, I can't figure out the root of the error.
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
The error is
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
The original equations are here
I have surfed this page, the closest questions I got are This and This but none of them solves my problem. Please, noble people of this noble platform, I need your help to be able to progress. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将二元决策变量乘以决策变量。
您应该使用 逻辑约束
或 线性化
you multiply a binary decision variable by a decision variable.
You should either use logical constraints
or linearization
我发现我的代码哪里出了问题。 @Alex Fleischer 的回答让我发现了我的错误。虽然很琐碎,但我觉得值得分享。
方程2b和2c中的
f_ij(w)
和f_di(w)
不是决策变量,但我将它们设置为决策变量。所以我注释掉了f_w
并使用了F_w
,它最初被定义为一个dict变量。 ieF_w = {(i,j,w):rnd.randint(0,10) for i,j in E for w in Omega}
而是
因此,我
没有使用导致错误的原因, 使用了我发布的问题中提到。
I have found where I went wrong in my code. Answer by @Alex Fleischer lead me to find my mistake. It's trivial but I feel its worth sharing.
The
f_ij(w)
andf_di(w)
in equations 2b and 2c are not decision variables but I set them out as decision variables. So I commentedf_w
out and usedF_w
which was originally defined as a dict variable. i.e.F_w = {(i,j,w):rnd.randint(0,10) for i,j in E for w in Omega}
So instead of
I used
which was what caused the error I mentioned in the question I posted.