1

I am trying to use CPLEX module via python for the scheduling case. However there is an error as follows.

model = Model(name='scheduling')
    # --- decision variables ---

numbers = range(1, math.factorial(6))
K = [number for number in range(1, math.factorial(6)+1)] #720

model.x_i = model.continuous_var_dict(keys=K) #keys untuk siapin tempat x dari 1 sampai 
720
model.x_j = model.continuous_var_dict(keys=K)
#mdl.s = mdl.continuous_var_dict(keys=K).sort() #check sort di cplex 


for k in K:
    mdl.x_i[k] = mdl.continuous_var_dict(TRANSITIONS) 
    mdl.x_i[k] = mdl.x_i[k] #K
    if k < max(K)-1: # bukan angka 5, tetapi maksimal nilai K yang terakhir
      mdl.x_j[k] = mdl.x_i[k+1] #K+1
    print('k[1]',mdl.x_i[1],mdl.x_j[1])
    print('k[2]',mdl.x_i[2],mdl.x_j[2])
    print('k[3]',mdl.x_i[3], mdl.x_j[3]) #check X
    mdl.obj_lambda = mdl.continuous_var_dict(keys=K) #define the lambda before 
proceeding into the constraint
    mdl.add_constraint(mdl.x_j[k] - mdl.x_i[k]  >= p(1) - p(2)*mdl.obj_lambda) for p in 
PLACES #p(1) for holding time (h), m0 from p(2) #ERROR invalid syntax
    mdl.add_constraint(m[k] == mdl.sum(m[k-1] +  AT*MZ[k]) for k in T) #Error object is not iterable
    mdl.add_constraint(mdl.sum(MZ[i]) == 1 for i in T) #Error int object is not iterable
    mdl.add_constraint(mdl.sum(MZ[k]) == 1) #define iteration k #list index out of range
    mdl.add_constraint(mdl.sum(MZ[k]) == 1) #list index out of range

File "<ipython-input-14-a8eac2458627>", line 22
mdl.add_constraint(mdl.x_j[k] - mdl.x_i[k]  >= p(1) - p(2)*mdl.obj_lambda) for p in PLACES #p(1) for holding time (h), m0 from p(2) #ERROR
                                                                             ^

SyntaxError: invalid syntax

Could anyone have any reference on how to write the code for CPLEX, especially the constraint, please?

Thank you.

1 Answers1

0
from docplex.mp.model import Model
import math

mdl = Model(name='scheduling')
    # --- decision variables ---

numbers = range(1, math.factorial(6))
K = [number for number in range(1, math.factorial(6)+1)] #720
PLACES=range(1,10)
T=range(1,4)

mdl.x_i = mdl.continuous_var_dict(keys=K) #keys untuk siapin tempat x dari 1 sampai 
720
mdl.x_j = mdl.continuous_var_dict(keys=K)
mdl.MZ = mdl.continuous_var_dict(keys=T)
#mdl.s = mdl.continuous_var_dict(keys=K).sort() #check sort di cplex 


for k in K:
    #mdl.x_i[k] = mdl.continuous_var_dict(TRANSITIONS) 
    mdl.x_i[k] = mdl.x_i[k] #K
    if k < max(K)-1: # bukan angka 5, tetapi maksimal nilai K yang terakhir
      mdl.x_j[k] = mdl.x_i[k+1] #K+1
    print('k[1]',mdl.x_i[1],mdl.x_j[1])
    print('k[2]',mdl.x_i[2],mdl.x_j[2])
    print('k[3]',mdl.x_i[3], mdl.x_j[3]) #check X
    mdl.obj_lambda = mdl.continuous_var_dict(keys=K) #define the lambda before 

    #mdl.add_constraint(mdl.x_j[k] - mdl.x_i[k]  >= p(1) - p(2)*mdl.obj_lambda for p in PLACES) #p(1) for holding time (h), m0 from p(2) #ERROR invalid syntax
    #mdl.add_constraint(m[k] == mdl.sum(m[k-1] +  AT*MZ[k]) for k in T) #Error object is not iterable
    mdl.add_constraint(mdl.sum(mdl.MZ[k] for k in T) == 1 ) #Error int object is not iterable
    mdl.add_constraint(mdl.sum(mdl.MZ[k] for k in T) == 1) #define iteration k #list index out of range
    mdl.add_constraint(mdl.sum(mdl.MZ[k] for k in T) == 1) #list index out of range

works fine

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Hi @Alex Fleischer, thank you for your prompt response. By the way, I encounter the error again for defining the objective function as follows. ,,,, mdl.obj_lambda = mdl.x_j - mdl.x_i for p = [number for number in TRANSITIONS] #define lambda mdl.minimize(mdl.obj_lambda) error message: mdl.obj_lambda = mdl.x_j - mdl.x_i for p = [number for number in TRANSITIONS] #define lambda ^ SyntaxError: invalid syntax In this case, do you know where could I get the documentation for the python syntax of CPLEX? Thank you. – Nicholas TI Jun 03 '22 at 12:18
  • See documentation https://ibmdecisionoptimization.github.io/docplex-doc/mp/refman.html and some examples in https://github.com/AlexFleischerParis/zoodocplex – Alex Fleischer Jun 03 '22 at 12:24