0

I have defined the variables and constraints for a scheduling case with CPLEX by using python. I would like to define two continuous variables and add constraints with for loop function. However the error messages as follows. Could anybody give suggestion, please?

'''

!pip install cplex
!pip install docplex==2.23.222
from docplex.mp.model import Model
import docplex.cp.expression
from docplex.mp.model import Model

import numpy as np
import math

P = 8 # number of places
T = 6 # number of transitions


r1 = 100 #processing time for PM1 (second)
r2 = 200 #processing time for PM2 (second)

v = 1 #robot moving time (second)
w = 1 #loading or unloading time (second)

M0 = ([0,0,1,0,0,1,0,1]) #initial marking

AT =([[1,-1,0,0,0,0],  #incident matrix
     [0,1,-1,0,0,0],
     [0,-1,1,0,0,0],
     [0,0,1,-1,0,0],
     [0,0,0,1,-1,0],
     [0,0,0,-1,1,0],
     [0,0,0,0,1,-1],
     [-1,1,-1,1,-1,1]])

MZ = ([[1,0,0,0,0,0], 
       [0,1,0,0,0,0], #1 baris adalah z
       [0,0,1,0,0,0],
       [0,0,0,1,0,0],
       [0,0,0,0,1,0],
       [0,0,0,0,0,1]])

PLACES = ([("p1", v+w,0, None), # index 0 for p, holding time (h), initial marking m0, update m
           ("p2", w+ r1,0, None),
           ("p3", 0,1, None),
           ("p4", v+w,0, None),
           ("p5", w+r2,0, None),
           ("p6", 0,1, None),
           ("p7", w+v,0, None),
           ("p8", 0, 1, None)])

# #structure TRANSITIONS = ['t1', [xi], [xj] untuk setiap k
TRANSITIONS =[("t1", None), #yang diupdate adalah epoch x untuk setiap k
             ("t2", None),
             ("t3", None),
             ("t4", None),
             ("t5", None),
             ("t6", None) ]

#Create the modeler/ solver
m = Model(name='Scheduling')
inf = docplex.cp.expression.INFINITY
bigM= 1000000

url = None
key = None

#No of combination for kth firing
numbers = range(1, math.factorial(6))
K = [number for number in range(1, math.factorial(6)+1)] #720
totalK = len(K)
#places=range(1,10)
#T=range(1,4)
print(K)
print(totalK)

# Continuous var
xi = np.empty(shape=(1,totalK) ,dtype=object)
for k in range(totalK):
    xi[k] = m.continuous_var(0,'xi'+str(k+1))
    #xi.append

usr/local/lib/python3.7/dist-packages/docplex/mp/error_handler.py in fatal(self, msg, args)
    208         resolved_message = resolve_pattern(msg, args)
    209         docplex_error_stop_here()
--> 210         raise DOcplexException(resolved_message)
    211 
    212     def fatal_limits_exceeded(self, nb_vars, nb_constraints):

DOcplexException: Var.ub: Expecting number, got: 'xi1'

# Continuous var
xj = np.empty(shape=(2,totalK) ,dtype=object)
for k in range(totalK):
    xj[k] = m.continuous_var(0,'xj'+str(k+1))

DOcplexException: Var.ub: Expecting number, got: 'xj1'

#Define constraint1
PLACES_IDX = ['p1','p2','p3','p4','p5','p6','p7','p8']
m.add_constraint(m.sub(xj[k] - xi[k]) for p in PLACES_IDX >= p(1) - p(2)*m.obj_lambda)  #p(1) for the 2nd column in PLACES matrix #for p in PLACES is written after the left argument

NameError                                 Traceback (most recent call last)
<ipython-input-9-9b940a7cb0e2> in <module>()
      1 #Define constraint1
      2 PLACES_IDX = ['p1','p2','p3','p4','p5','p6','p7','p8']
----> 3 m.add_constraint(m.sub(xj[k] - xi[k]) for p in PLACES_IDX >= p(1) - p(2)*m.obj_lambda)  #p(1) for the 2nd column in PLACES matrix #for p in PLACES is written after the left argument

NameError: name 'p' is not defined

#Define constraint2
m.add_constraint(m[k] for k in T == m.sum(m[k-1] +  AT*MZ[k])

  File "<ipython-input-10-1661d7997134>", line 2
    m.add_constraint(m[k] for k in T == m.sum(m[k-1] +  AT*MZ[k])
                                                                 ^
SyntaxError: unexpected EOF while parsing

'''

Let me know if any other details needed, thank you.

1 Answers1

0
m.continuous_var(0,'xj')

Is not good

Try

 m.continuous_var(0,40,name="x"+str(i))

If 40 is the Upper bound

And with regards to the other errors the following code works fine:

from docplex.mp.model import Model
import docplex.cp.expression
from docplex.mp.model import Model

import numpy as np
import math

P = 8 # number of places
T = 6 # number of transitions


r1 = 100 #processing time for PM1 (second)
r2 = 200 #processing time for PM2 (second)

v = 1 #robot moving time (second)
w = 1 #loading or unloading time (second)

M0 = ([0,0,1,0,0,1,0,1]) #initial marking

AT =([[1,-1,0,0,0,0],  #incident matrix
     [0,1,-1,0,0,0],
     [0,-1,1,0,0,0],
     [0,0,1,-1,0,0],
     [0,0,0,1,-1,0],
     [0,0,0,-1,1,0],
     [0,0,0,0,1,-1],
     [-1,1,-1,1,-1,1]])

MZ = ([[1,0,0,0,0,0], 
       [0,1,0,0,0,0], #1 baris adalah z
       [0,0,1,0,0,0],
       [0,0,0,1,0,0],
       [0,0,0,0,1,0],
       [0,0,0,0,0,1]])

PLACES = ([("p1", v+w,0, None), # index 0 for p, holding time (h), initial marking m0, update m
           ("p2", w+ r1,0, None),
           ("p3", 0,1, None),
           ("p4", v+w,0, None),
           ("p5", w+r2,0, None),
           ("p6", 0,1, None),
           ("p7", w+v,0, None),
           ("p8", 0, 1, None)])

# #structure TRANSITIONS = ['t1', [xi], [xj] untuk setiap k
TRANSITIONS =[("t1", None), #yang diupdate adalah epoch x untuk setiap k
             ("t2", None),
             ("t3", None),
             ("t4", None),
             ("t5", None),
             ("t6", None) ]

#Create the modeler/ solver
m = Model(name='Scheduling')
inf = docplex.cp.expression.INFINITY
bigM= 1000000



#No of combination for kth firing
numbers = range(1, math.factorial(6))
K = [number for number in range(1, math.factorial(6)+1)] #720
totalK = len(K)
#places=range(1,10)
#T=range(1,4)
print(K)
print(totalK)

# Continuous var
xi = [] #np.empty(shape=(1,totalK+1) ,dtype=object)
for k in range(0,totalK):
    #xi[k] = m.continuous_var(0,40,'xi'+str(k+1))
    xi.append(m.continuous_var(0,40,'xi'+str(k+1)))




# Continuous var
xj = [] #np.empty(shape=(2,totalK) ,dtype=object)
for k in range(totalK):
    xj.append( m.continuous_var(0,1,'xj'+str(k+1)))



#Define constraint1
    
PLACES_IDX = ['p1','p2','p3','p4','p5','p6','p7','p8']
for p in PLACES_IDX:
   m.add_constraint(xj[k] - xi[k] >= 1)   #p(1) for the 2nd column in PLACES matrix #for p in PLACES is written after the left argument
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Thank you, @Alex Fleischer. What about the error to define the p? I think I have define p at PLACES. Thank you. – Nicholas Nicholas Jun 23 '22 at 13:51
  • Hello @Alex Fleischer, good day to you. I have the following error for the objective function. ''' m.obj_lambda = xj - xi for t in TRANSITIONS: m.minimize(m.obj_lambda) ''' AttributeError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/docplex/mp/mfactory.py in _to_expr(self, e) 631 try: --> 632 return e.to_expr() 633 except AttributeError: AttributeError: 'numpy.ndarray' object has no attribute 'to_expr' Can you help me, please? Thank you. – Nicholas Nicholas Jun 28 '22 at 03:21