簡體   English   中英

CPLEX Python for Scheduling Case:如何定義連續變量和約束(for循環)?

[英]CPLEX Python for Scheduling Case: How to define the continuous variable and constraint (for loop)?

我已經使用 python 為帶有 CPLEX 的調度案例定義了變量和約束。 我想定義兩個連續變量並使用 for 循環函數添加約束。 但是錯誤信息如下。 請問有人可以給點建議嗎?

'''

!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

'''

如果需要任何其他細節,請告訴我,謝謝。

m.continuous_var(0,'xj')

不好

嘗試

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

如果 40 是上限

關於其他錯誤,以下代碼可以正常工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM