Pyomo โครงสราง-'รายการ'วัตถุไม่มีแอททริบิวต์

0

คำถาม

ฉันกำลังสร้างเป็น pyomo constraint ดังนั้น Eout_pv[t]<=PV_gen[t]ในแต่ละค่าของที.

ขอรหัสทางด้านล่าฉันก่อสร้างตั้งค่า,พารามิเตอร์,ตัวแปร. แล้วผมก็สร้าง constraint.

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 นเป็นชนพื้นเมืองที่สนับสนุนสำหรับ numpy/pandas ข้อมูลประเภทคืออะไรเปลี่ยนแปลงเลย ใน Pyomo 6.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

คนปฏิบัติการต้นฉบับของนางแบบคงยังยอมจำนนต่อการเกิดข้อผิดพลาดอย่างไรก็ตามมันจะเป็นด้วยเหตุผลที่แตกต่างจา:กำลังเริ่มการเรียกใช้งานตัวกับ model.T = Set(initialize=df.index.tolist(), ordered=True) จะให้ใช้กับค่าจาก DataFrame เป็นดัชนี(ตัวอย่างเช่นในตัวอย่างอยู่ด้านบน). อย่างไรก็ตามเมื่อคุณเริ่มการใช้การสร้างดัชนีค้นหา Param กับรายชื่อที่"indices"ของรายการเป็น integers 0..len(list_data)-1 (i.e. มันคือช่วยย่อมันให้กำลังเริ่มการทำงานกับ dict(enumerate(list_data)). เป็นดัชนีของ PV_gen คือ DataFrame เป็นดัชนีคอลัมน์สุดท้ายก็ด้วยข้อผิดพลาด:

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่