Pyomo:值错误:未初始化的NumericValue对象fo [1,1_C1]没有值

时间:2018-06-25 22:17:11

标签: python-3.x optimization constraints pyomo

此链接https://github.com/amianAA/ALNS包含用于优化MINLP铁路网络模型(适用于马德里网络)的ALNS算法。尽管评论是用西班牙语写的,但我相信这不会有太大问题,因为谷歌翻译将在这方面有所帮助。当我运行ALNP.py文件时,它可以正常工作,直到madridALNS.py文件中的代码此阶段为止,其中整个模型是在Pyomo的帮助下开发的:

 #1 // output flow from each origin "wo" 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,rule=resd1_rule)

madridALNS.dat文件包含用于填充模型的数据。我立即遇到的错误是:

 ERROR: evaluating expression: No value for uninitialized NumericValue object
fo[1,1_C1]
    (expression: fo[1,1_C1] + fo[1,2_C2] + fo[1,7_C10] + fo[1,3_C3] +
    fo[1,4_C4] + fo[1,6_C8])
ERROR: evaluating object as numeric value: fo[1,1_C1] + fo[1,2_C2] +
fo[1,7_C10] + fo[1,3_C3] + fo[1,4_C4] + fo[1,6_C8]
    (object: <class 'pyomo.core.kernel.expr_coopr3._SumExpression'>)
No value for uninitialized NumericValue object fo[1,1_C1]
ERROR: evaluating expression: No value for uninitialized NumericValue object
fo[1,1_C1]
    (expression: fo[1,1_C1] + fo[1,2_C2] + fo[1,7_C10] + fo[1,3_C3] +
    fo[1,4_C4] + fo[1,6_C8]  ==  0.0)
ERROR: Rule failed when generating expression for constraint restr1 with index
1: ValueError: No value for uninitialized NumericValue object fo[1,1_C1]
ERROR: Constructing component 'restr1' from data=None failed:
    ValueError: No value for uninitialized NumericValue object fo[1,1_C1]
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 793, in construct
ndx)
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/base/misc.py", line 61, in apply_indexed_rule
return rule(model, index)
 File "/home/bunmalik/Desktop/project/codes/codes/ALNS/ALNS-master/madridALNS.py", line 120, in resd1_rule
if expr!=0:
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/expr_coopr3.py", line 533, in __nonzero__
return bool(self())
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/expr_coopr3.py", line 208, in __call__
exception=exception))
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/expr_coopr3.py", line 545, in _apply_operation
return next(values) == next(values)
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/expr_coopr3.py", line 217, in _evaluate_arglist
yield value(arg, exception=exception)
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/numvalue.py", line 161, in value
tmp = numeric(exception=exception)
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/expr_coopr3.py", line 208, in __call__
exception=exception))
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/expr_coopr3.py", line 812, in _apply_operation
return sum(c*next(values) for c in self._coef) + self._const
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/expr_coopr3.py", line 812, in <genexpr>
return sum(c*next(values) for c in self._coef) + self._const
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/expr_coopr3.py", line 217, in _evaluate_arglist
yield value(arg, exception=exception)
 File "/usr/local/lib/python3.5/dist-packages/pyomo/core/kernel/numvalue.py", line 170, in value
% (obj.name,))

我正在解决我的项目工作中的类似优化问题。但是,我是Pyomo的新手。请有人帮我解决错误。预先感谢

1 个答案:

答案 0 :(得分:0)

此模型有两个注释:

  1. 当前的问题是堆栈跟踪牵涉到if expr!=0:行。在这里,不等式运算符使Pyomo评估expr的值。由于表达式中涉及的某些变量未初始化且没有值,因此观察到No value for uninitialized NumericValue object错误。
  2. 我希望model.b[wo,l]是一个参数。如果它是一个变量,则您可能试图在Constraint声明中表达析取逻辑,代数建模语言通常不支持此声明。有关更多信息,您需要研究Pyomo.GDP。如果它是一个参数,那么就可以了。