Pyomo constrângeri - "lista" obiect nu are atribut

0

Problema

Eu voi crea un pyomo constrângere, astfel încât Eout_pv[t] <= PV_gen[t] la fiecare valoare a lui t.

În codul de mai jos, am crea seturi, parametri, variabile. Apoi am crea constrângere.

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)

Atunci când am rula acest lucru, primesc mesajul "AttributeError:" listă " obiect nu are atributul 'is_expression_type'"

Aș fi extrem de recunoscător pentru orice ajutor

Plin mesaj de eroare de mai jos

---------------------------------------------------------------------------
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 este un param care depinde de model.T. aceasta înseamnă că pentru fiecare valoare a T nu va fi o valoare pentru PV_gen. Ai nevoie pentru a inițializa param cu o dict nu o list. De exemplu:

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

multumesc mult! E lucrat. Am aprecia cu adevărat.
Simon Colver
0

@pybegginer răspunsul este complet corect. Cu toate acestea, de asemenea, vreau să rețineți că Pyomo are suport nativ pentru numpy/panda tipuri de date se îmbunătățește. În Pyomo 6.2 (din cauza jurul valorii de 17 Noiembrie 2021); următoarele (mai natural sintaxă) va funcționa:

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)

care vă oferă:

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

Apoi:

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()

ar reveni

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 modelul original al lui ar genera în continuare o eroare; cu toate acestea, ar fi pentru un alt motiv: Inițializarea Setului cu model.T = Set(initialize=df.index.tolist(), ordered=True) ar da un Set cu valorile din DataFrame index (ca în exemplul de mai sus). Cu toate acestea, atunci când inițializați un indexate Param cu o listă, "indici" de lista sunt numere întregi 0..len(list_data)-1 (de exemplu, este o prescurtare pentru inițializarea cu dict(enumerate(list_data)). Ca indicele de PV_gen este DataFrame este indicele de coloană, va termina cu eroare:

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

În alte limbi

Această pagină este în alte limbi

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