🎯 Python Decision Science — Feasibility vs Optimality 🐍
Posted on: November 18, 2026
Description:
A valid plan follows the rules. An optimal plan also gives the best objective value among all valid plans. Let’s separate those two ideas with Python.
The workshop model
Reuse our fictional bag workshop. Let x and y be nonnegative integer counts of standard and premium bags.
- Maximize contribution:
30*x + 40*y. - Time limit:
2*x + 4*y <= 16. - Fabric limit:
3*x + 2*y <= 18.
All bags are assumed to sell. Contributions are illustrative units after variable costs, before fixed costs; resource use is fixed and setup costs are omitted.
Feasibility: does the plan satisfy every constraint?
def feasible(plan):
x, y = plan
return (
type(x) is int and type(y) is int
and x >= 0 and y >= 0
and 2*x + 4*y <= 16
and 3*x + 2*y <= 18
)
Plan (x, y) |
Time | Fabric | Contribution | Feasible? |
|---|---|---|---|---|
| (0, 4) | 16 | 8 | 160 | Yes |
| (4, 2) | 16 | 16 | 200 | Yes |
| (5, 2) | 18 | 19 | 230 | No |
A score of 230 is irrelevant if the plan breaks the constraints. Using all available time also does not establish optimality: both valid examples use 16 time units.
Optimality: is there a better feasible plan?
The resource limits imply x <= 6 and y <= 4. These bounds cover every feasible integer plan.
from itertools import product
def contribution(plan):
x, y = plan
return 30*x + 40*y
valid = [p for p in product(range(7), range(5)) if feasible(p)]
best_value = max(map(contribution, valid))
best_plans = [p for p in valid if contribution(p) == best_value]
The executed notebook checks 35 candidates, finds 22 feasible plans, and returns (4, 2) as the unique contribution optimum, worth 200.
The baseline (0, 4) is feasible but suboptimal by 40 contribution units. The optimal plan leaves two fabric units unused. An optimum does not need to exhaust every resource.
What proves it is optimal?
Checking constraints for one plan only establishes feasibility. Here we check every feasible integer plan within mathematically derived bounds; none has a contribution above 200. That establishes global optimality for this model.
The notebook also compares against a larger enumeration to verify that the bounds omit no feasible plan. Exhaustive enumeration works well for this tiny example; larger problems need suitable optimization methods and an assessment of whether optimality was established.
Change the objective, change the answer
If we maximize premium-bag count instead, the same feasible set has a unique optimum at (0, 4). Its contribution remains 160, but contribution is no longer the objective.
most_premium = max(p[1] for p in valid)
premium_optima = [p for p in valid if p[1] == most_premium]
“Best” depends on the objective, constraints and assumptions. Some models have multiple optimal solutions; this example has one for each objective. A model with no feasible plans has no optimal plan, so handle an empty feasible set before using max().
Feasibility admits a plan. Optimality compares it with every valid alternative.
No comments yet. Be the first to comment!