Pyomo 约束声明

Pyomo constraint declaration

pyomo 初学者,我在编写约束时遇到问题

这是我的输入数据:

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}

我做了这样的集合声明:

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)

约束声明,约束是

(在我的模型中 ∼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)

除了我收到错误的约束之一:NameError: name 'd' is not defined,所有单元格都工作正常。给出这个错误,我想知道我的 set 声明 model.N_S_D 是否正确。其次,我不确定如何声明布景要求 (R) 以及我是否应该这样做?谢谢

你的代码有几个问题。

  • 您的班次设置不匹配。 (见下面我的)
  • 当您看到“for each”表示法时,您需要为集合或组合集合中的每个元素创建一个约束...您要求和的东西需要在求和表达式内部并且您需要循环在“for eaches”之上是一个很好的思考方式......我认为你被翻转了。
  • 您的 sum() 表达方式不正确。您需要在要求和的事物中定义索引的来源。我建议你尝试一些 sum() 的练习题——在 pyomo 之外,以便更好地处理它。
  • 将来,如果您将所有代码以工作(或至少会产生错误)的格式粘贴到一个块中,这样会更有帮助,这样其他人就可以直接复制它并对其进行处理。

以下是解决您问题的两种不同方法。首先是将您的方法与 ConstraintList 结合使用。替代方法使用函数约束组合,我发现它更容易,但两者都有效……只是不要同时包含它们,因为它们是多余的。 :)

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