分配关于我的字典索引的决策变量

Assign decision variables regarding the index of my dictionary

我正在使用 pyomo 构建模型,我正在努力将正确的输入分配给我的一个决策变量。

这是我正在使用的 2 个输入:

ListN 这是我的员工列表

N = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

和一个字典 EmployeesWE,其中键是上面列表中的员工 ID,值是包含计划范围内所有周末的元组列表:

EmployeeWE = {'0':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '1':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '2':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '3':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '4':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '5':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '6':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '7':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '8':[(2, 3),(9, 10),(16, 17),(23, 24)],
 '9':[(2, 3),(9, 10),(16, 17),(23, 24)]}

所以 (2,3) 是第一个周末,(9,10) 第二个,(16,17) 第三个,(23,24) 最后一个。

决策变量是二元的,如果工人 n 在周末 i 工作,则为 1,否则为 0。 我的代码如下:

from pyomo.environ import *
model = ConcreteModel()
model.weekends = Var(((workers, weekends) for workers in N for weekends in EmployeeWE[workers]), within=Binary, initialize=0)

这是决策变量的打印函数的摘录:

Key           : Lower : Value : Upper : Fixed : Stale : Domain
      ('0', 2, 3) :     0 :     0 :     1 : False : False : Binary
     ('0', 9, 10) :     0 :     0 :     1 : False : False : Binary

我想要元组的索引,而不是周末。例如,而不是 (2,3) 它将是一个。谢谢

是的,你可以做到。这是下面的示例和几点...

  1. 不确定为什么要使用字符串来保存整数,这并不重要,但是 pyomo 可以从其中任何一个进行设置,如果它们是整数,就用它吧。
  2. 当你声明一个 pyomo 变量时,如果你希望它被 2 个集合索引,你可以按照我下面的方式进行。
  3. 对于周末,不清楚你在用字典做什么......只有“一组周末”所以你不需要为每个员工创建一个,对吧?
  4. 我只是用整数在字典中索引周末。模型计算完成后,我可以使用这些整数返回到周末词典中,以从选定的索引中提取标签或其他任何内容,即集合 model.W
import pyomo.environ as pyo

employees = ['X18', 'E22', 'B9']   # employee ID's.  Could also be integers or names or ____
weekends  = {   1: 'Mar 3-4',
                2: 'Mar 10-11',
                3: 'Mar 17-18'}    # the values here could be tuples of integers or whatever...

model=pyo.ConcreteModel()

model.E = pyo.Set(initialize = employees)
model.W = pyo.Set(initialize = weekends.keys())

model.X = pyo.Var(model.E, model.W, domain=pyo.Binary)

model.pprint()

产量:

3 Set Declarations
    E : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {'X18', 'E22', 'B9'}
    W : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}
    X_index : Size=1, Index=None, Ordered=True
        Key  : Dimen : Domain : Size : Members
        None :     2 :    E*W :    9 : {('X18', 1), ('X18', 2), ('X18', 3), ('E22', 1), ('E22', 2), ('E22', 3), ('B9', 1), ('B9', 2), ('B9', 3)}

1 Var Declarations
    X : Size=9, Index=X_index
        Key        : Lower : Value : Upper : Fixed : Stale : Domain
         ('B9', 1) :     0 :  None :     1 : False :  True : Binary
         ('B9', 2) :     0 :  None :     1 : False :  True : Binary
         ('B9', 3) :     0 :  None :     1 : False :  True : Binary
        ('E22', 1) :     0 :  None :     1 : False :  True : Binary
        ('E22', 2) :     0 :  None :     1 : False :  True : Binary
        ('E22', 3) :     0 :  None :     1 : False :  True : Binary
        ('X18', 1) :     0 :  None :     1 : False :  True : Binary
        ('X18', 2) :     0 :  None :     1 : False :  True : Binary
        ('X18', 3) :     0 :  None :     1 : False :  True : Binary

4 Declarations: E W X_index X