This model formulation and associated Python file use advanced data input methods and leverage more of Pyomo's model-building capabilities than the basic version. For a simpler approach with hard-coded data values, refer to the simpler Bubbles Brewery article. This intermediate version facilitates easier expansion to include additional beer types or to read data from external sources.
Download Optimization Models
bubbles_brewery_intermediate.py
Model Description
You are the brewmaster in charge of planning your brewery's next production run. Your task is to decide how many barrels of light beer and dark beer to produce. Each barrel sold will earn your company a profit, as shown below.
- Light beer: $13 per barrel
- Dark beer: $23 per barrel
However, your production is limited by the amount of corn, hops, and malt available. The table below shows the amount of each ingredient required for each type of beer, as well as the total available for this production run.
Corn |
Hops |
Malt |
|
Light beer (per barrel) | 5 | 4 | 35 |
Dark beer (per barrel) | 15 | 4 | 20 |
Amount available | 480 | 160 | 1,190 |
Mathematical Formulation
Sets
\(T\) Set of all types of beer
\(I\) Set of all types of ingredients
Coefficients
\(P_t\) Profit for one barrel of beer type \(t\)
\(R_{ti}\) Based on the recipe, amount of ingredient \(i\) that goes into beer type \(t\)
\(A_{i}\) Amount available at the brewery of ingredient \(i\)
Decision Variables
\(X_t\) Number of barrels of beer type \(t\) to produce
Objective
\( \max \sum_{t \in T} P_t X_t \)
Constraints
Cannot use more ingredients than what is already available at the brewery
\( \sum_{t \in T} R_{ti} X_t \le A_{i} \forall i \in I \)
The amount of barrels of beer produced needs to be non-negative
\( X_t \ge 0 \forall t \in T \)
Solution
When you solve this model using the Rose solver, the optimal solution should yield a profit of $800, achieved by producing 12 barrels of light beer and 28 barrels of dark beer.
Python Pyomo File
Below is the Python code that uses Pyomo to formulate this optimization model.
bubbles_brewery_intermediate.py
import pyomo.environ as pyo
model = pyo.ConcreteModel(name='Bubbles Brewery')
# Data coefficients
beer_types = ['light', 'dark']
ingredients = ['corn', 'hops', 'malt']
profit = {'light': 13, 'dark': 23}
recipe = {'light': {'corn': 5, 'hops': 4, 'malt': 35},
'dark': {'corn': 15, 'hops': 4, 'malt': 20}}
on_hand = {'corn': 480, 'hops': 160, 'malt': 1190}
# Decision variables
model.x = pyo.Var(beer_types, within=pyo.NonNegativeReals)
# Objective function
model.objective = pyo.Objective(expr=sum(profit[bt] * model.x[bt] for bt in beer_types),
sense=pyo.maximize)
# Constraints
def resource_constraint(mdl, ingredient):
return (sum(recipe[beer_type][ingredient] * mdl.x[beer_type]
for beer_type in beer_types)
<= on_hand[ingredient])
model.resources = pyo.Constraint(ingredients, rule=resource_constraint)
# Export the model
model.write('bubbles_brewery.mps', io_options={'symbolic_solver_labels': True})
# Optionally print the model to verify
model.pprint()
To see the resulting .mps file and solution .json file, refer to the bottom of the simpler Bubbles Brewery article. You can also download the .mps file below.
Comments
0 comments
Please sign in to leave a comment.