Anas
Anas

Reputation: 135

docplex.mp.utils.DOcplexException: Expecting sequence of linear constraints, got: docplex.mp.QuadraticConstraint[]

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.

Upvotes: 0

Views: 640

Answers (2)

Anas
Anas

Reputation: 135

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.

Upvotes: 0

Alex Fleischer
Alex Fleischer

Reputation: 10059

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)

Upvotes: 1

Related Questions