Created
July 17, 2025 17:29
-
-
Save Ogaday/de60cb42d6c999417580ad1838dcfd87 to your computer and use it in GitHub Desktop.
Pyomo default parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Looking parameter defaults based on another parameter in Pyomo. | |
We have a model described as: | |
min sum(x * Y[s] for s in S) | |
Where Y[s] has a default value of x if not supplied. | |
""" | |
import pyomo.environ as pyo | |
if __name__ == "__main__": | |
model = pyo.AbstractModel() | |
# S is our set which indexes Y | |
model.S = pyo.Set() | |
# x is a scalar param, and our default value. | |
model.x = pyo.Param(domain=pyo.Reals) | |
# Y is our vector param which defaults to x if there is any missing data. | |
model.Y = pyo.Param(model.S, default=lambda model, s: model.x) | |
def objective(model): | |
return pyo.quicksum(model.x * model.Y[s] for s in model.S) | |
model.obj = pyo.Objective(rule=objective) | |
solver = pyo.SolverFactory("glpk") | |
instance = model.create_instance({None: {"S": [1, 2], "x": {None: 2.0}, "Y": {1: 10.0}}}) | |
solver.solve(instance).write() | |
# 24.0 | |
instance = model.create_instance({None: {"S": [1, 2], "x": {None: 2.0}}}) | |
solver.solve(instance).write() | |
# 8.0 | |
instance = model.create_instance({None: {"S": [1, 2], "x": {None: 2.0}, "Y": {1: -2}}}) | |
solver.solve(instance).write() | |
# 0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment