【问题标题】:Assign decision variables regarding the index of my dictionary分配有关我的字典索引的决策变量
【发布时间】:2021-02-25 11:49:05
【问题描述】:

我正在使用 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 在周末工作,则为 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) 它将是一个。谢谢

【问题讨论】:

    标签: python dictionary indexing tuples pyomo


    【解决方案1】:

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

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

    【讨论】:

    • 对于您的第 3 点,这取决于实例。例如,员工 3 和 4 可能有不同的 WE,例如周五、周六、周日。这就是为什么我需要为每个员工创建一个。顺便说一句,感谢您的完整回复!
    猜你喜欢
    • 2019-02-15
    • 2021-10-17
    • 1970-01-01
    • 2016-09-11
    • 2019-12-12
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多