ThanksEdit:
As pointed out by jsiirola, nesting in combination with abstract models can get very cumbersome. So I tried to model it using concrete models and nest the blocks "by hand".
import pyomo.environ as pyo
def func_model():
model = pyo.AbstractModel()
model.idx_lb = pyo.Param()
model.idx_ub = pyo.Param()
model.idx = pyo.RangeSet(model.idx_lb, model.idx_ub)
#model.subblock = pyo.Block(model.idx, rule = subblock)
return model
def subblock():
submodel = pyo.AbstractModel()
submodel.jdx_ub = pyo.Param()
submodel.jdx = pyo.RangeSet(submodel.jdx_ub)
#submodel.subsubblock = pyo.Block(submodel.jdx, rule = subsubblock)
return submodel
def subsubblock():
subsubmodel = pyo.AbstractModel()
subsubmodel.kdx_ub = pyo.Param()
subsubmodel.kdx = pyo.RangeSet(subsubmodel.kdx_ub)
return subsubmodel
Then, creating the subsubmodel
ssb_data = {None: {"kdx_ub" : {None: 3}}}
ss_model = subsubblock()
ss_instance = ss_model.create_instance(ssb_data)
and adding this into the submodel
import copy
sb_data = {None: {"jdx_ub": {None: 2}}}
s_model = subblock()
s_instance = s_model.create_instance(sb_data)
def copy_func(m,j):
return copy.deepcopy(ss_instance)
s_instance.subsubblock = pyo.Block(s_instance.jdx, rule = copy_func)
I would also do the same for helpnesting the subblock into the model.
I think this is much better than the cumbersome abstract model instantiation I would normally do. However, in my real example I have a nested model that is interconnected. E.g., my :(outermost) model puts constraints on the subblock's parameters. Is there any way to handle this with the upper approach?