from docplex.mp.model import Model
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
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)