I'm considering implementing a schema as below for one of my larger pyomo
projects. (Yes, I'm intentionally using m
instead of the ubiquitous self
for consistency in model.) In the project the model creation / solving / post-processing are all separate modules and I'd like to be able to employ the IDE's auto-complete and type hinting on the model, which would make it quicker / more accurate to locate key variables, sets, etc. in the post-processing and tidy up the project.
I'm considering sub-classing ConcreteModel
to do this.
Is this a good/horrible idea?
Are there other options for achieving this?
I've seen this posting which indicates sub-classing has been considered for other reasons.
import pyomo.environ as pyo
class MyModel(pyo.ConcreteModel):
def __init__(m):
super().__init__()
m.S = pyo.Set(initialize=[1, 2, 3])
m.x = pyo.Var(m.S)
@m.Constraint(m.S)
def upper_limit(m, s):
return m.x[s] <= 5
# ... etc.
m1 = MyModel()
# meanwhile.... somewhere else in post-processing:
def list_set_vals(m: MyModel) -> list[int]:
return list(m.S) # <-- auto completion available
print(list_set_vals(m1))