Pyomo:งดัชนีตัวแปรในเท่านั้นส่วนหนึ่งของเรียบร้อยไหม?

0

คำถาม

ฉันกำลังพยายามติดตามข่าวสารของ SOCs ใน pyomo optimization นคุณครูแต่พอกลับมา ฉันมีเบอร์ของ BEVs และฉันต้องการ keept ตามของ eachs SOC. ที่ xpression ฉันผ่านไป pe.Objective ดูเหมือนว่าต่อไปนี้:

sum(sum(model.SOC[t+1, b] - model.SOC[t, b] for b in model.buses) for t in model.times)

model.buses แล้ว model.times สองคนตั้งค่าฉันต้องเป็นประกาศ pe.Set. เวลาที่จากไป(0, ...., 95). ดังนั้นในเมื่อทำซ้ำสำหรับ model.times มันพยายามจะใช้งาน model.SOC[96, b] ข้อมูลชี้นำอะไรไป KeyError.

มันมีทางที่จะบอก pyomo จะ leafe ออกสุดท้ายของธาตุที่จัดเพื่อป้องกันไม่ให้เกิดข้อผิดพลาด?

อะไรอย่าง:

sum(sum(model.SOC[t+1, b] - model.SOC[t, b] for b in model.buses) for t in model.times[0:-2])

ซึ่งน่าเศร้าที่ยังบังอาจเกิดข้อผิดพลาด:

IndexError: times indices must be integers, not slice

นี่เป็น minmal ตัวอย่างเช่นนั้นควรจะ reproduce ข้อผิดพลาด:

import pyomo.environ as pe

solver = pe.SolverFactory('glpk')
model = pe.ConcreteModel('Test')
model.times = pe.Set(initialize=list(range(96)))
model.buses = pe.Set(initialize=list(range(5)))
model.SOC = pe.Var(model.times*model.buses, domain=pe.PositiveReals)

def example_rule(model):
    return sum(sum(model.SOC[t+1, b] - model.SOC[t, b] for b in model.buses) for t in model.times)

model.obj = pe.Objective(rule=example_rule, sense=pe.maximize)

model.pprint()

หลายขอบคุณล่วงหน้า!

optimization pyomo python
2021-11-16 15:17:42
1

คำตอบที่ดีที่สุด

1

ใช่มันมีสองสามวิธีที่จะทำอย่างนี้ ก่อนหากที่ตั้งที่คุณอยากจะดัชนีในคืนสั่ง(ซึ่งเป็นค่าปริยาย)คุณสามารถใช้ first, lastแล้ว prev วิธีการของที่ตั้งค่าไว้ในหลากหลายเหมือนกันกับที่ฉันทำน่ะล่ะ (ดูมอดูลของฉันต้องแก้ไขรหัสของคุณด้านล่างนี้)

เดี๋ยวคุณสามารถสร้างของตัวเอง subset และเหมือนกัที่ห้องนางแบบหรือไม่ สองรุ่นของด้านล่างแสดงก่อสร้างของ arbitrarily ซับซ้อน subset และใส่มันไว้ในที่คุณครูแต่พอกลับมา นี่ตั้งสามารถใช้เป็นพื้นสำหรับจุดมุ่งหมาหรือ constraint.

นี่แก้ปัญหาคล้ายกับเรื่ องตอบ

import pyomo.environ as pe

solver = pe.SolverFactory('glpk')
model = pe.ConcreteModel('Test')
model.times = pe.Set(initialize=list(range(5)), ordered=True)  # ordered is default, this is for clarity...
model.buses = pe.Set(initialize=list(range(2)))
model.SOC = pe.Var(model.times*model.buses, domain=pe.PositiveReals)

def example_rule(model):
    return sum(sum(model.SOC[t+1, b] - model.SOC[t, b] for b in model.buses) for t in model.times if t != model.times.last())

model.obj = pe.Objective(rule=example_rule, sense=pe.maximize)

model.pprint()


# making your own subset...
times = 10
model2 = pe.ConcreteModel("other")
model2.times = pe.Set(initialize=range(times))
# make a subset of the even values that are no more than 4 values close to the end....
model2.times_subset = pe.Set(initialize=[t for t in model2.times if t%2==0 and t <= times-4])

model2.pprint()

มีค่า:

3 Set Declarations
    SOC_index : Size=1, Index=None, Ordered=True
        Key  : Dimen : Domain      : Size : Members
        None :     2 : times*buses :   10 : {(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1), (4, 0), (4, 1)}
    buses : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    2 : {0, 1}
    times : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    5 : {0, 1, 2, 3, 4}

1 Var Declarations
    SOC : Size=10, Index=SOC_index
        Key    : Lower : Value : Upper : Fixed : Stale : Domain
        (0, 0) :     0 :  None :  None : False :  True : PositiveReals
        (0, 1) :     0 :  None :  None : False :  True : PositiveReals
        (1, 0) :     0 :  None :  None : False :  True : PositiveReals
        (1, 1) :     0 :  None :  None : False :  True : PositiveReals
        (2, 0) :     0 :  None :  None : False :  True : PositiveReals
        (2, 1) :     0 :  None :  None : False :  True : PositiveReals
        (3, 0) :     0 :  None :  None : False :  True : PositiveReals
        (3, 1) :     0 :  None :  None : False :  True : PositiveReals
        (4, 0) :     0 :  None :  None : False :  True : PositiveReals
        (4, 1) :     0 :  None :  None : False :  True : PositiveReals

1 Objective Declarations
    obj : Size=1, Index=None, Active=True
        Key  : Active : Sense    : Expression
        None :   True : maximize : SOC[1,0] - SOC[0,0] + SOC[1,1] - SOC[0,1] + SOC[2,0] - SOC[1,0] + SOC[2,1] - SOC[1,1] + SOC[3,0] - SOC[2,0] + SOC[3,1] - SOC[2,1] + SOC[4,0] - SOC[3,0] + SOC[4,1] - SOC[3,1]

5 Declarations: times buses SOC_index SOC obj
2 Set Declarations
    times : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :   10 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    times_subset : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    4 : {0, 2, 4, 6}

2 Declarations: times times_subset
[Finished in 553ms]
2021-11-16 17:03:25

สมบูรณ์แบบขอบคุณ! :)นี้ prevw แก้ปัญหาของฉัน มีที่ไหนที่อยู่บนอินเทอร์เน็ตที่คนสามารถเรียนรู้เรื่องนี้? ใน pyomo เอกสารคู่มือ@info:shell prevw ไม่แม้แต่จะพูดถึง(หรืออย่างน้อยฉันไม่เคยเห็นมัน)
Andre

ใช่ IMHO ที่ Pyomo เอกสารปลอมตัที่จุดเริ่มต้นอีกครั้งแต่คืนนั้นขาดประสบการณ์อยู่ในศูนย์ควบคุม kde ในโมดูลเอกสารอ้างอิง นี่คือโอเค!"โอเค"แต่ไม่ใช่เยี่ยมมาก...ฉันคงไม่รู้จัก prevw โดยไม่มีที่อื่นซะหน่อย บางครั้งฉันจัดการอยู่ ipython ซึ่งแสดงถึงการเติมให้สมบูรณ์และหมอ stubs. pyomo.readthedocs.io/en/expr_dev/_modules/index.html
AirSquid

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

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

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