python - Pyomo: How to use the final data point in an abstract model's objective? -
i have pyomo model has form:
from pyomo.environ import * pyomo.dae import * m = abstractmodel() m.t = continuousset(bounds=(0,120)) m.t = param(default=120) m.s = var(m.t, bounds=(0,none)) m.sdot = derivativevar(m.s) m.obj = objective(expr=m.s[120],sense=maximize)
note objective m.obj
relies on parameter m.t
. attempting run gives error:
typeerror: unhashable type: 'simpleparam'
using value, such expr=m.s[120]
gives error:
valueerror: error retrieving component s[120]: component has not been constructed.
in both cases, goal same: optimize largest possible value of s
@ horizon.
how can create abstract model expresses this?
you hitting on 2 separate issues:
typeerror: unhashable type: 'simpleparam'
is due bug in pyomo 4.3 cannot directly use simple param
s indexes other components. said, fix particular problem not fix example model.
the trick fixing objective
declaration encapsulate objective
expression within rule:
def obj_rule(m): return m.s[120] # or better yet: # return m.s[m.t] # or # return m.s[m.t.last()] m.obj = objective(rule=obj_rule,sense=maximize)
the problem when writing abstract model, each component being declared, not defined. so, var s
declared exist, has not been defined (it empty shell no members). causes problem because python (not pyomo) attempts resolve m.s[120]
specific variable before calling objective
constructor. use of rules (functions) in abstract models allows defer resolution of expression until pyomo constructing model instance. pyomo constructs instance components in same order declared them on abstract model, when fires obj_rule
, previous components (s
, t
, , t
) constructed , s
has valid members @ known points of continuousset
(in case, bounds).
Comments
Post a Comment