【问题标题】:python variable assignment index Error: list index out of range with pulppython变量赋值索引错误:列表索引超出范围与纸浆
【发布时间】:2018-09-01 05:03:33
【问题描述】:

您好,我不明白这个错误是如何产生的。我在左侧有相同数量的约束,在右侧列表中有相同数量的元素。也许我错过了一个小标志或者我的逻辑是错误的。请帮我理解。

Machines = ["A", "B",]
Days= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
        "Saturday", "Sunday"]

desire_num={"A":5, "B":2,}

week1={"Monday":1, "Tuesday":1, "Wednesday":1, "Thursday":1,   
       "Friday":1, "Saturday":1, "Sunday":1}

status_list=['1', '1', '0', '1', '0', '1', '1', '0', '0', '1', '0', 
            '1', '0', '0']



avail = pulp.LpVariable.dicts("var", ((machine, day) for machine in 
           Machines for day in Days), cat="Binary")

##---problem is here.  I have 14 variables on the left and 14 elements in the list on the right.  The error says list index out of range.
status_list_iterator = 0
    for machine, day in avail:
        self.prob += avail[machine, day] ==   
        status_list[status_list_iterator]
        status_list_iterator+=1

再次感谢您的澄清。

【问题讨论】:

    标签: python-3.x list variables indexing pulp


    【解决方案1】:

    约束的rhs 应至少包含一个数值(布尔值也可以)。您正在设置表单的约束:

    self.prob += avail[machine, day] == '1' 
    #or
    self.prob += avail[machine, day] == '0' 
    

    您可以将 status_list 中的元素更改为 numeric values 或执行以下操作:

    for (machine, day), status in zip(avail, status_list):
        prob += avail[(machine, day)] == int(status), "c_{}_{}".format(machine, day)
    

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 2014-11-03
      • 1970-01-01
      相关资源
      最近更新 更多