docplex.mp.utils.DOcplexException:期望线性约束序列,得到:docplex.mp.QuadraticConstraint[]

发布于 2025-01-12 05:45:32 字数 1765 浏览 1 评论 0原文

请原谅我的无知。我正在尝试我对 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

原始方程在这里 Original Equations

我浏览过此页面,我得到的最接近的问题是 这个这个但他们都没有解决我的问题。拜托,这个崇高平台上的高尚人们,我需要你们的帮助才能进步。提前致谢。

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 Original Equations

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2025-01-19 05:45:32

将二元决策变量乘以决策变量。

您应该使用 逻辑约束

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)

you multiply a binary decision variable by a decision variable.

You should either use logical constraints

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)

or linearization

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)
木格 2025-01-19 05:45:32

我发现我的代码哪里出了问题。 @Alex Fleischer 的回答让我发现了我的错误。虽然很琐碎,但我觉得值得分享。
方程2b和2c中的f_ij(w)f_di(w)不是决策变量,但我将它们设置为决策变量。所以我注释掉了f_w并使用了F_w,它最初被定义为一个dict变量。 ie 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

没有使用导致错误的原因, 使用了我发布的问题中提到。

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) and f_di(w) in equations 2b and 2c are not decision variables but I set them out as decision variables. So I commented f_w out and used F_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

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

I used

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

which was what caused the error I mentioned in the question I posted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文