🎯 Python Decision Science — Objectives and Constraints 🐍
Posted on: November 4, 2026
Description:
A workshop makes standard and premium bags. Premium bags earn more per item, so making only premium bags sounds sensible. But they also require more time. The right question is: which mix earns the most within the available resources?
All figures below are fictional teaching data. “Profit” means contribution after variable costs, before fixed costs. We assume every bag sells and each bag's contribution and resource requirements stay constant.
1. Define the decisions
Let x be the number of standard bags and y the number of premium bags. Both must be nonnegative integers.
| Per bag | Standard | Premium |
|---|---|---|
| Profit contribution | 30 units | 40 units |
| Time | 2 hours | 4 hours |
| Fabric | 3 units | 2 units |
The workshop has 16 hours and 18 fabric units available.
2. Define what “better” means
Our objective is to maximize total profit contribution:
Maximize 30x + 40y
An objective assigns a score to a plan. Choosing a different objective—such as maximizing the number of bags—would be a different problem. The distinction between an objective and feasibility is explained in Google's optimization introduction.
3. Translate the limits into constraints
2x + 4y <= 16 # Time
3x + 2y <= 18 # Fabric
x, y >= 0 # Nonnegative quantities
x, y are integers
Every feasible plan must satisfy every constraint. For example, (6, 4) scores 340 profit units, but needs 28 hours and 26 fabric units. Its score is irrelevant because the plan is infeasible.
4. Search the small decision space with Python
At most six standard bags can fit the resources, and at most four premium bags can fit. These bounds cover all feasible plans because neither product can exceed its resource limit even when made alone.
from itertools import product
feasible = []
for x, y in product(range(7), range(5)):
if 2*x + 4*y <= 16 and 3*x + 2*y <= 18:
feasible.append((x, y, 30*x + 40*y))
best_profit = max(profit for x, y, profit in feasible)
best_plans = [plan for plan in feasible if plan[2] == best_profit]
print(best_plans) # [(4, 2, 200)]
There are 35 candidate pairs and 22 feasible plans. All optimal ties are retained, although this example has only one optimum. The notebook derives the bounds from named resource values and checks the results with assertions.
This is exhaustive enumeration of a tiny integer linear optimization problem. It establishes the global optimum here because every possible feasible integer plan is included. Larger problems need more efficient methods; no external optimization solver is used in this lab.
5. Verify the result
| Measure | Calculation | Result |
|---|---|---|
| Profit contribution | 30×4 + 40×2 | 200 units |
| Time | 2×4 + 4×2 | 16 of 16 hours |
| Fabric | 3×4 + 2×2 | 16 of 18 units |
Making four premium bags alone earns 160 profit units. The mixed plan earns 200 under these assumptions.
Time is a binding constraint at the optimum: no hours remain. Fabric has two units of slack. An optimal plan does not have to use every resource; it has to achieve the best objective while obeying the limits.
6. Check the model, not just the arithmetic
The result is only as appropriate as its assumptions. Limited demand, setup costs, uncertain sales, or changing production requirements would change the model. Before using an optimization result, ask whether the variables, objective, and constraints reflect the real decision.
Open Objectives_and_Constraints.ipynb in Jupyter and run it from top to bottom. It uses Python's standard library only, with saved outputs and verification checks.
Takeaway: decisions → objective → constraints → solution → verification.
No comments yet. Be the first to comment!