model.T = range(120)
def electrical_grid_block(grid, t):
<containing: bus, transmission line, generator>
model.Grid = Block(model.T,rule=electrical_grid_block)
def storage(s):
s.storage_level = Var(model.T, bounds=(0,300))
s.power = Var(model.T, bounds=(-2,2))
# Constraints
def constr_rule_speicher(s,t):
if t >= 2:
return s.storage_level[t] == - s.power[t] * dt + s.storage_level[t-1]
elif t == 1:
return s.storage_level[t] == 150.0
s.storage_constraints = Constraint(model.T, rule=constr_rule_speicher)
model.storage = Block(rule=storage)I don't like this solution very much because I believe the object oriented manner of problem modeling gets lost.
Any different ideas to mine?
Unfortunately the documentation around Pyomo doesn't give any example for this kind of case.
--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi,
I believe that the Optimal Multi-Period Lot sizing problem in the book addresses this exact issue. Individual blocks are created for each of the time periods, and a constraint is added to link the time periods in the outer level. See that example and, in particular, the constraint:
def i_linking_rule(m, t):
if t == m.T.first():
return m.lsb[t].i0 == i0
return m.lsb[t].i0 == m.lsb[t-1].i
model.i_linking = Constraint(model.T, rule=i_linking_rule)
Hope this helps,
Carl.
From:
<pyomo...@googlegroups.com> on behalf of "becker.ph...@gmail.com" <becker.ph...@gmail.com>
Reply-To: "pyomo...@googlegroups.com" <pyomo...@googlegroups.com>
Date: Wednesday, May 23, 2018 at 4:09 AM
To: Pyomo Forum <pyomo...@googlegroups.com>
Subject: [EXTERNAL] Pyomo Blocks: indexed vs not-indexed blocks on time-dependent problems
The Pyomo book proposes this structure of blocks for a time-dependent problem.
--
Hi Phillip,
I think we are crossing our lines here. I am making exactly the same argument as you (which is exactly the same argument made in the book).
These constraints are NOT added in each of the blocks, rather they are added on the main model and they reference blocks that are below the model in the hierarchy. This is consistent with the quote from the book since it references variables at lower levels in the hierarchy only.
Specifically,
- model.i_linking is the constraint set. It is added to “model”, and not to the block.
- There is no parent lookup here. The “m” that is passed in is the model that contains the constraints.
What you seem to be implying is that these constraints look “up” the tree, but that is not the case here. Sometimes, I have seen code where a rule for a block rule contains a line to get a parent. This looks something like the following:
m = b.parent()
followed by constraints that are added to b, but reference variables on m. This would be an “upwards” reference.
(Having said all of this, there are people that prefer this kind of referencing on purpose. This is a matter of opinion. I don’t recommend it, and in fact, I advocate a model design that is exactly that same as you are advocating here.)
Regards,
def Storage(s):
s.T = Set() #time
s.dt = Set() #time step
++some Variables++
++some Constraints++
def CHP(c):
s.T = Set() #time
s.dt = Set() #time step
++some Variables++
++some Constraints++
import components as C
model= AbstractModel()
#create an abstract Storage with the Storage rule from above
Storage_1 = AbstractModel(rule=C.Storage)
#create a concrete Storage by specifying e.g. size, power etc.
model.Storage_1 = Storage_1.create_instance('storage_1.dat')
#and maybe create another different storage
model.Storage_2 = Storage_2.create_instance('storage_2.dat')
#add CHP (combined power and heat)
CHP = AbstractModel(rule=C.CHP)
model.CHP = CHP.create_instance('chp.dat')
+++ some constraints here, containing Variables from different Storages or CHP +++
+++ here an objective function +++
model_instance = model.create_instace()
+++ and solve it and print results+++set T := 1 2 3 4 5 6 7 8 9 10;
param dt := 1;
param Energie_Max := 50;
param Energie_Initial := 0;
...