IndexError:约束'restr1':无法使用单个表达式初始化约束的多个索引

时间:2018-06-24 20:21:44

标签: python-3.x constraints pyomo

请我需要帮助解决我作为标题发布的上述错误。我是pyomo的新手。我最近关注此链接:     https://github.com/amianAA/ALNS。在MadridALNS.py中,一切都可以完美地工作到创建约束的位置:

#2 // arrival flow to each destination "wd" equal to demand of the destination origin pair "w"

    def resd1_rule(model,w):
        wo=model.tabla[w,'wo']
        expr=0
        for l in model.L:
            if model.b[wo,l]==1:
                expr += model.fo[w,l]
        if expr!=0:
                return (expr + model.h[w]==model.tabla[w,'g']*model.factorg)
        else:
                return Constraint.Skip
    model.restr1 = Constraint(model.W,expr=resd1_rule)

这是我每次运行代码时遇到的错误:

ERROR: Constructing component 'restr1' from data=None failed:
    IndexError: Constraint 'restr1': Cannot initialize multiple indices of
    a constraint with a single expression
Traceback (most recent call last):
   File "ALNS.py", line 16, in <module>
   instance = model.create_instance('madridALNS.dat')
   File "/usr/local/lib/python3.5/dist-packages/pyomo/core/base/PyomoModel.py", line 723, in create_instance
   profile_memory=profile_memory )
   File "/usr/local/lib/python3.5/dist-packages/pyomo/core/base/PyomoModel.py", line 806, in load
   profile_memory=profile_memory)
   File "/usr/local/lib/python3.5/dist-packages/pyomo/core/base/PyomoModel.py", line 870, in _load_model_data
   self._initialize_component(modeldata, namespaces, component_name, profile_memory)
  File "/usr/local/lib/python3.5/dist-packages/pyomo/core/base/PyomoModel.py", line 925, in _initialize_component
  declaration.construct(data)
   File "/usr/local/lib/python3.5/dist-packages/pyomo/core/base/constraint.py", line 786, in construct
   (self.name,) )
  IndexError: Constraint 'restr1': Cannot initialize multiple indices of a constraint with a single expression

可以在这里https://github.com/amianAA/ALNS

找到文件

1 个答案:

答案 0 :(得分:0)

expr=参数用于单个非索引表达式。您应该改为尝试rule=resid1_rule