RangeSet in abstract model parametrized by concrete model parameter?

Skip to first unread message

Marek Makowski

Apr 7, 2023, 12:32:36 PMApr 7
to Pyomo Forum
I try to replace the following:
periods = 12 # number of planning periods
m.P = pe.RangeSet(0, periods - 1) # planning periods
by replacing the parameter periods by its value to be defined in the concrete model. I tried:
m.periodq = pe.Param(within=pe.PositiveIntegers, default=3) # number of planning periods
m.Q = pe.RangeSet(0, pe.value(m.periodq) - 1) # doesn't work
however this results in:
ValueError: Evaluating the numeric value of parameter 'periodq' before
the Param has been constructed (there is currently no value to return)

Is there a way to achieve it?

Marek Makowski

Jun 12, 2023, 2:49:09 AMJun 12
to Pyomo Forum
The following, based on the approach suggested by John Sirola, solves the above issue,  although PyCharm shows a warning (see the comment):
m.periods = pe.Param(domain=pe.PositiveIntegers, default=3) # number of planning periods
# noinspection PyTypeChecker
# warning: pe.Param used instead of expected int (but a cast to the latter cannot be used in Abstract model)
m.periods_ = pe.Param(initialize=m.periods-1) # index of the last planning periods
m.P = pe.RangeSet(0, m.periods_) # set of periods; NOTE: RangeSet(0, 1) has two elements, RangeSet(0, -1) is empty

I thank John for the advice, which solved the problem. The PyCharm warning can easily be suppressed, if there is no easy way to cast (in abstract model) pe.Param into int.


Reply all
Reply to author
0 new messages