[英]Pyomo constraint declaration
begginer to pyomo, I have an issue to code my constraint pyomo的初学者,我有一个问题来编码我的约束
Here're my input data:这是我的输入数据:
S = ['E', 'L'] #shits
N = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #employee ID
D = [1,2,3] #days
R = {(1, 'D'): 1,
(1, 'N'): 1,
(2, 'D'): 1,
(2, 'N'): 1,
(3, 'D'): 2,
(3, 'N'):3} # cover requirement {(day,shift):number of employees required}
I did the sets declaration like this:我做了这样的集合声明:
model.N = Set(initialize = N)
model.S = Set(initialize = S)
model.D = Set(initialize = D)
model.N_S_D = Set(within = model.N * model.S * model.D,
initialize = [(n, s, d) for n in model.N for s in model.S for d in model.D])
decision variable declaration:决策变量声明:
model.x = Var(model.N_S_D, within=Binary, initialize=0)
constraint declaration whith the constraint being约束声明,约束为
(in my model ˜r is R) (在我的 model ~r 是 R)
model.C1 = ConstraintList()
for N in model.N_S_D:
const_expr = sum(model.x[(d, s) == R[(d, s)]])
model.C1.add(expr = const_expr)
All cells works fine except the one of the constraint where I get the error: NameError: name 'd' is not defined
.除了我收到错误的约束之一之外,所有单元格都可以正常工作: NameError: name 'd' is not defined
。 Giving this error, I wonder if my set declaration model.N_S_D
is correct.给出这个错误,我想知道我的设置声明model.N_S_D
是否正确。 Second, I'm not sur on how to declare the set cover requirement ( R
) and if I sould do it?其次,我不知道如何声明套装封面要求( R
),如果我愿意这样做? Thank you谢谢
You have a couple problems in your code.您的代码中有几个问题。
sum()
expression correctly.您没有正确制定sum()
表达式。 You need to define the source of the indices in the thing you are summing.您需要在要求和的事物中定义索引的来源。 I suggest you try some practice problems with sum()
on the side -- outside of pyomo to get a better handle on it.我建议你尝试一些sum()
的练习题——在 pyomo 之外,以便更好地处理它。 Below are 2 different approaches to your question.以下是解决您问题的两种不同方法。 First is using your approach with a ConstraintList
.首先是将您的方法与ConstraintList
一起使用。 The alternate uses a function-Constraint combo, which I find easier, but either works.... Just don't include both as they are redundant.替代使用函数约束组合,我发现它更容易,但任何一个都有效....只是不要同时包含它们,因为它们是多余的。 :) :)
from pyomo.environ import *
S = ['D', 'N'] #shifts
N = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #employee ID
D = [1, 2, 3] #days
R = { (1, 'D'): 1,
(1, 'N'): 1,
(2, 'D'): 1,
(2, 'N'): 1,
(3, 'D'): 2,
(3, 'N'): 3} # cover requirement {(day,shift):number of employees required}
model = ConcreteModel('shift schedule')
model.N = Set(initialize = N)
model.S = Set(initialize = S)
model.D = Set(initialize = D)
model.N_S_D = Set(within = model.N * model.S * model.D,
initialize = [(n, s, d) for n in model.N for s in model.S for d in model.D])
model.x = Var(model.N_S_D, within=Binary) #, initialize=0)
model.C1 = ConstraintList()
shift_day_combos = [(s, d) for s in model.S for d in model.D]
for s, d in shift_day_combos:
const_expr = sum(model.x[(n, s, d)] for n in model.N) == R[(d, s)]
model.C1.add(expr = const_expr)
# alternate approach...
def cover_requirement(model, s, d):
return sum(model.x[n, s, d] for n in model.N) == R[d,s]
model.C2 = Constraint(model.S, model.D, rule=cover_requirement)
model.pprint()
...
2 Constraint Declarations
C1 : Size=6, Index=C1_index, Active=True
Key : Lower : Body : Upper : Active
1 : 1.0 : x[0,D,1] + x[1,D,1] + x[2,D,1] + x[3,D,1] + x[4,D,1] + x[5,D,1] + x[6,D,1] + x[7,D,1] + x[8,D,1] + x[9,D,1] : 1.0 : True
2 : 1.0 : x[0,D,2] + x[1,D,2] + x[2,D,2] + x[3,D,2] + x[4,D,2] + x[5,D,2] + x[6,D,2] + x[7,D,2] + x[8,D,2] + x[9,D,2] : 1.0 : True
3 : 2.0 : x[0,D,3] + x[1,D,3] + x[2,D,3] + x[3,D,3] + x[4,D,3] + x[5,D,3] + x[6,D,3] + x[7,D,3] + x[8,D,3] + x[9,D,3] : 2.0 : True
4 : 1.0 : x[0,N,1] + x[1,N,1] + x[2,N,1] + x[3,N,1] + x[4,N,1] + x[5,N,1] + x[6,N,1] + x[7,N,1] + x[8,N,1] + x[9,N,1] : 1.0 : True
5 : 1.0 : x[0,N,2] + x[1,N,2] + x[2,N,2] + x[3,N,2] + x[4,N,2] + x[5,N,2] + x[6,N,2] + x[7,N,2] + x[8,N,2] + x[9,N,2] : 1.0 : True
6 : 3.0 : x[0,N,3] + x[1,N,3] + x[2,N,3] + x[3,N,3] + x[4,N,3] + x[5,N,3] + x[6,N,3] + x[7,N,3] + x[8,N,3] + x[9,N,3] : 3.0 : True
C2 : Size=6, Index=C2_index, Active=True
Key : Lower : Body : Upper : Active
('D', 1) : 1.0 : x[0,D,1] + x[1,D,1] + x[2,D,1] + x[3,D,1] + x[4,D,1] + x[5,D,1] + x[6,D,1] + x[7,D,1] + x[8,D,1] + x[9,D,1] : 1.0 : True
('D', 2) : 1.0 : x[0,D,2] + x[1,D,2] + x[2,D,2] + x[3,D,2] + x[4,D,2] + x[5,D,2] + x[6,D,2] + x[7,D,2] + x[8,D,2] + x[9,D,2] : 1.0 : True
('D', 3) : 2.0 : x[0,D,3] + x[1,D,3] + x[2,D,3] + x[3,D,3] + x[4,D,3] + x[5,D,3] + x[6,D,3] + x[7,D,3] + x[8,D,3] + x[9,D,3] : 2.0 : True
('N', 1) : 1.0 : x[0,N,1] + x[1,N,1] + x[2,N,1] + x[3,N,1] + x[4,N,1] + x[5,N,1] + x[6,N,1] + x[7,N,1] + x[8,N,1] + x[9,N,1] : 1.0 : True
('N', 2) : 1.0 : x[0,N,2] + x[1,N,2] + x[2,N,2] + x[3,N,2] + x[4,N,2] + x[5,N,2] + x[6,N,2] + x[7,N,2] + x[8,N,2] + x[9,N,2] : 1.0 : True
('N', 3) : 3.0 : x[0,N,3] + x[1,N,3] + x[2,N,3] + x[3,N,3] + x[4,N,3] + x[5,N,3] + x[6,N,3] + x[7,N,3] + x[8,N,3] + x[9,N,3] : 3.0 : True
11 Declarations: N S D N_S_D_domain_index_0 N_S_D_domain N_S_D x C1_index C1 C2_index C2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.