Pyomo限制"列表"的对象没有属性

0

的问题

我创造一个pyomo约束,以便Eout_pv[t]<=PV_gen[t]在每个值吨。

在我的代码下面,我第一个建设、参数、变量。 然后我创造约束。

model.T = Set(initialize=df.index.tolist(), ordered=True)
model.PV_gen = Param(model.T, initialize=df.pv_gen.tolist())

model.Eout_pv = Var(model.T, bounds=(0, 100))

def pv_export(model, t):
    return model.Eout_pv[t] <= model.PV_gen[t]   
model.pv_export = Constraint(model.T, rule=pv_export)

当我运行了这个,我得到消息"AttributeError:"列表"的对象没有属性'is_expression_type'"

我将非常感谢任何的帮助

完全错误的消息下

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-47-3de19250551e> in <module>
      2 
      3 start_time = time.time()
----> 4 output_df = optimize_year(df, first_model_period, last_model_period, result_time_step)
      5 print("--- %s seconds ---" % (time.time() - start_time))

<ipython-input-47-a84557fa2321> in optimize_year(df, first_model_period, last_model_period, time_step)
     72         return model.Eout_pv[t] <= model.PV_gen[t]
     73 
---> 74     model.pv_export = Constraint(model.T, rule=pv_export)
     75 
     76     def total_export(model, t):

C:\ProgramFiles2\Anaconda\lib\site-packages\pyomo\core\base\block.py in __setattr__(self, name, val)
    541                 # Pyomo components are added with the add_component method.
    542                 #
--> 543                 self.add_component(name, val)
    544             else:
    545                 #

C:\ProgramFiles2\Anaconda\lib\site-packages\pyomo\core\base\block.py in add_component(self, name, val)
   1079                              _blockName, str(data))
   1080             try:
-> 1081                 val.construct(data)
   1082             except:
   1083                 err = sys.exc_info()[1]

C:\ProgramFiles2\Anaconda\lib\site-packages\pyomo\core\base\constraint.py in construct(self, data)
    774             for ndx in self._index:
    775                 try:
--> 776                     tmp = apply_indexed_rule(self,
    777                                              _init_rule,
    778                                              _self_parent,

C:\ProgramFiles2\Anaconda\lib\site-packages\pyomo\core\base\misc.py in apply_indexed_rule(obj, rule, model, index, options)
     59                 return rule(model)
     60             else:
---> 61                 return rule(model, index)
     62         else:
     63             if index.__class__ is tuple:

<ipython-input-47-a84557fa2321> in pv_export(model, t)
     70     def pv_export(model, t):
     71         "Maximum PV export within a single period"
---> 72         return model.Eout_pv[t] <= model.PV_gen[t]
     73 
     74     model.pv_export = Constraint(model.T, rule=pv_export)

pyomo\core\expr\numvalue.pyx in pyomo.core.expr.numvalue.NumericValue.__le__()

pyomo\core\expr\logical_expr.pyx in pyomo.core.expr.logical_expr._generate_relational_expression()

AttributeError: 'list' object has no attribute 'is_expression_type'
attributeerror constraints pyomo python
2021-11-15 12:39:53
2
0

model.PV_gen 是一个 param 这取决于 model.T. 这意味着每个的价值 T 会有一个的价值 PV_gen. 你需要初始化param有 dict 不是一个 list. 例如:

from pyomo.environ import *

T=[1,2,3]
PV={1:20, 2:560, 3: 890}
model=ConcreteModel()
model.T = Set(initialize=T, ordered=True)
model.PV_gen = Param(model.T, initialize=PV)

model.Eout_pv = Var(model.T, bounds=(0, 100))

def pv_export(model, t):
    return model.Eout_pv[t] <= model.PV_gen[t]   
model.pv_export = Constraint(model.T, rule=pv_export)
2021-11-15 13:23:38

非常感谢! 它的工作。 我真的很欣赏它。
Simon Colver
0

@pybegginer的回答是完全正确的。 然而,我还想指出,Pyomo地支持顽固/熊猫数据类型的数据正在改善。 在Pyomo6.2(由于周围月17 2021);下面(更多的自然语法)会的工作:

from pyomo.common.dependencies import pandas as pd, pandas_available
from pyomo.environ import *
df = pd.DataFrame(index=['20-1', '20-2', '20-3'],
                  data={'pv_gen': [1, 2, 4]})
print(df)

它提供了:

      pv_gen
20-1       1
20-2       2
20-3       4

然后:

model = ConcreteModel()

model.T = Set(initialize=df.index, ordered=True)
model.PV_gen = Param(model.T, initialize=df.pv_gen)

model.Eout_pv = Var(model.T, bounds=(0, 100))

def pv_export(model, t):
    return model.Eout_pv[t] <= model.PV_gen[t]   
model.pv_export = Constraint(model.T, rule=pv_export)

model.pprint()

将返回

1 Set Declarations
    T : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {'20-1', '20-2', '20-3'}

1 Param Declarations
    PV_gen : Size=3, Index=T, Domain=Any, Default=None, Mutable=False
        Key  : Value
        20-1 :     1
        20-2 :     2
        20-3 :     4

1 Var Declarations
    Eout_pv : Size=3, Index=T
        Key  : Lower : Value : Upper : Fixed : Stale : Domain
        20-1 :     0 :  None :   100 : False :  True :  Reals
        20-2 :     0 :  None :   100 : False :  True :  Reals
        20-3 :     0 :  None :   100 : False :  True :  Reals

1 Constraint Declarations
    pv_export : Size=3, Index=T, Active=True
        Key  : Lower : Body          : Upper : Active
        20-1 :  -Inf : Eout_pv[20-1] :   1.0 :   True
        20-2 :  -Inf : Eout_pv[20-2] :   2.0 :   True
        20-3 :  -Inf : Eout_pv[20-3] :   4.0 :   True

OP的原始模型仍然会产生错误;但是,这将是一个不同的原因:初始的设置 model.T = Set(initialize=df.index.tolist(), ordered=True) 会得到一套价值,从数据框的指数(如上述)。 然而,当你初始化的索引Param有一个列表中,"指标"清单是整数 0..len(list_data)-1 (即,它的简写与初始化 dict(enumerate(list_data)). 作为索引的 PV_gen 是的数据框索引列,最终你的错误:

KeyError: "Index '0' is not valid for indexed component 'PV_gen'"
2021-11-15 16:42:04

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................