Challenge 1 - LP - Powerco (3 Plants, 4 Cities)
from Winston, Goldberg 2004, Pg 127. Nice one!
There are three power-plants and four cities. Each city's demand MUST be met. There is a limit to how much each power-plant CAN supply. There are costs associated with supply to a certain city from a certain power-plant. Find out what the supply breakup should be.
There is an elegant way and an inelegant way.
Inelegant way - use Set only - which means you need to spell out C1, C2, etc.. in a dict. And then you
Elegant way: use RangeSet and then you specify the row and column variable names and maintain the weights (used to compute cost function) in a 2D matrix.
I thought there might be some elegance involved in not having to spell out 7 constraints (capacity limit and demand requirement) but NS did the same thing as me.
To view final variable values:
asdf
This is both elegant in that it's nicer than the first one, which uses only a single Set and multiple variable entry statements. But, don't you think Navid could do better than enter seven constraint functions manually? Come on..
chatGPT agrees:
Yes! You're absolutely right — Pyomo supports indexed constraints, which let you generalize patterns like this without needing to write separate functions for each one.
You can replace your 7 separately defined constraints with just two indexed constraints, one for supply and one for demand, using their indices as set elements.
model.S = pyo.Param(model.j, initialize={1: 35, 2: 50, 3: 40}) # supply at each plant
model.D = pyo.Param(model.i, initialize={1: 45, 2: 20, 3: 30, 4: 30}) # demand at each city
Comments
Post a Comment