Pyomo 제약에'목록은'개체가 없는 특성

0

질문

내가 만드는 pyomo 제도록 Eout_pv[t]<=PV_gen[t]각각의 값 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_genparam 에 따라 달라집 model.T. 그것이 의미하는 것에 대한 각각의 가치 T 가 될 것이 값 PV_gen. 를 초기화해야 합 param 로 dictlist. 예를 들어:

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 의에 대한 기본 지원 numpy/판 데이터 형식이 개선되고 있습니다. 에 Pyomo6.2(로 인해 주변 17Nov2021);다음(자연 구문)작동:

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ý
..................................................................................................................