Programming lesson
Mastering Linear Programming for COMP331/557: From Standard Form to Simplex with Real-World Analogies
A comprehensive tutorial on linear programming for COMP331/557 class test, covering standard form, slack variables, simplex algorithm, two-phase method, and duality with trend-inspired examples from AI and gaming.
Introduction: Why Linear Programming Matters Now
Linear programming (LP) is a cornerstone of optimization, used everywhere from supply chain logistics to AI model training. In May 2026, as AI continues to reshape industries, the ability to formulate and solve LP problems is more relevant than ever. This tutorial is designed to help you ace the COMP331/557 class test by breaking down each task with clear explanations and timely analogies.
Understanding the Assignment Structure
The class test covers five tasks that test your grasp of LP fundamentals: formulating a problem in standard form, introducing slack variables, performing simplex pivots, handling infeasibility with the two-phase method, and deriving dual programs. Each task is worth 20 marks, totaling 100. You'll need to explain your reasoning clearly—no software allowed!
Task 1: Formulating a Linear Program in Standard Form
The Manufacturing Problem
A manufacturer uses three machines (X, Y, Z) with different resource inputs to produce a product. Given limited iron, copper, and nickel, the goal is to maximize total product output. Let's define decision variables: let x, y, z represent the number of hours (or batches) each machine runs.
Constraints (resource limits):
- Iron:
0.9x + 2y ≤ 50 - Copper:
1.5x + 3z ≤ 50 - Nickel:
0.5y + 0.1z ≤ 50
Objective (maximize product): max 2.5x + 2y + 3z
All variables are non-negative: x, y, z ≥ 0. This is the standard form: maximize a linear function subject to linear inequalities and non-negativity.
Why This Matters in AI and Gaming
Resource allocation problems like this are analogous to training AI models where you allocate GPU time (resources) to different tasks (machines) to maximize accuracy (product). In gaming, think of an RPG where you distribute skill points to maximize damage output under mana constraints.
Task 2: Introducing Slack Variables and the Initial Dictionary
To use the simplex algorithm, we convert inequalities to equalities by adding slack variables. For Task 1:
s1 = 50 - 0.9x - 2y(iron slack)s2 = 50 - 1.5x - 3z(copper slack)s3 = 50 - 0.5y - 0.1z(nickel slack)
The initial dictionary (basic feasible solution) has x=y=z=0 and slacks at their upper bounds: (s1, s2, s3) = (50,50,50). The objective is ζ = 2.5x + 2y + 3z. This is the starting point for the primal simplex algorithm—no pivoting yet!
Task 3: Geometric Interpretation of Simplex with Bland's Rule
The provided graphical LP has constraints in 2D (after slack variables). The feasible region is a polygon. Starting basis (1,2,5,6) means variables x1, x2, x5, x6 are basic (non-zero). Bland's rule selects the entering variable with the smallest index among those with positive reduced cost, and the leaving variable with the smallest index among those that block the increase.
Geometric explanation: At each pivot, we move along an edge of the polygon from one vertex to an adjacent vertex. The objective is to minimize x1 (or maximize -x1), so we move leftwards. The first pivot might move from the current vertex to a neighbor with a smaller x1 value. The process continues until no further improvement is possible—the leftmost vertex is found.
Think of it like a character in a battle royale game moving to the safest zone (minimum x1) while staying within the playable area (feasible region).
Task 4: Performing One Simplex Pivot
Given the dictionary:
max ζ = 5 -4x1 +3x2 -6x3
x4 = 4 - x1 - x2
x5 = 6 - x2 - x3
x6 = 6 - x3
x1,x2,x3,x4,x5,x6 ≥ 0Current BFS: (0,0,0,4,6,6) with objective ζ=5. To pivot, choose an entering variable with a positive coefficient in the objective (since we maximize). Here, x2 has coefficient +3 (positive). Now determine the leaving variable: compute ratios of RHS to positive coefficients in x2's column:
- x4: 4/1 = 4
- x5: 6/1 = 6
- x6: no x2 term (coefficient 0), so unbounded? Actually, x6 equation has no x2, so it's not limiting. The smallest ratio is 4, so x4 leaves.
Perform the pivot: solve the x4 equation for x2: x2 = 4 - x1 - x4. Substitute into other equations. New dictionary:
ζ = 5 -4x1 +3(4 - x1 - x4) -6x3 = 17 -7x1 -3x4 -6x3
x2 = 4 - x1 - x4
x5 = 6 - (4 - x1 - x4) - x3 = 2 + x1 + x4 - x3
x6 = 6 - x3New BFS: (x1,x2,x3,x4,x5,x6) = (0,4,0,0,2,6) with ζ=17. This is one pivot step completed.
Task 5: Two-Phase Simplex for Infeasible Start
Consider the LP: max 4x1 -3x2 +6x3 subject to x1+x2 ≤ -4, x2+x3 ≤ -6, x3 ≤ 6, x1,x2,x3 ≥ 0. The first two constraints have negative RHS, making the origin infeasible. Phase 1 introduces artificial variables to find a feasible solution.
Phase 1 LP: minimize w = a1 + a2 (or maximize -a1 - a2) subject to:
- x1 + x2 - a1 = -4 (or x1 + x2 - a1 + s1 = -4? Actually, we add slack and artificial: x1+x2 - s1 + a1 = -4, but careful with signs). Standard approach: multiply by -1 to get non-negative RHS: -x1 - x2 + s1 = 4, then add artificial? Let's follow the typical method: For ≤ constraints with negative RHS, multiply by -1 to get ≥ with positive RHS, then subtract surplus and add artificial.
Better: Rewrite constraints as: -x1 - x2 ≥ 4, -x2 - x3 ≥ 6, x3 ≤ 6. Then for ≥ constraints, subtract surplus and add artificial. But the assignment asks for the LP that needs to be solved in phase 1. So we write:
max -a1 - a2
subject to:
x1 + x2 - s1 + a1 = -4 (but RHS negative? Actually, we want RHS non-negative for initial dictionary. So multiply by -1: -x1 - x2 + s1 - a1 = 4. But then a1 has coefficient -1, which is messy.Given the complexity, the key point is to set up the auxiliary problem correctly. For the first pivot, we choose the most negative reduced cost (for minimization) or use Bland's rule. This step determines the first basic feasible solution for phase 1.
Task 6: Deriving the Dual Linear Program
Given primal (maximization, ≤ constraints, non-negative variables):
max 12x1 -10x2
subject to:
-4x1 + 3x2 ≤ 10
4x1 - 3x2 ≤ 20
x1 + 4x2 ≤ 30
x1, x2 ≥ 0The dual (minimization, ≥ constraints, unrestricted? Actually, standard dual for a max problem with ≤ constraints and non-negative variables is a min problem with ≥ constraints and non-negative dual variables. Let y1, y2, y3 be dual variables for each constraint.
Dual LP:
min 10y1 + 20y2 + 30y3
subject to:
-4y1 + 4y2 + y3 ≥ 12
3y1 - 3y2 + 4y3 ≥ -10
y1, y2, y3 ≥ 0Notice the second dual constraint has a negative RHS because the primal variable x2 has a negative coefficient in the objective. This is correct.
Conclusion: Practice Makes Perfect
Linear programming is a powerful tool with applications in AI, finance, and gaming. By mastering these tasks, you'll be well-prepared for the COMP331/557 class test. Remember to explain every step—geometric intuition matters as much as algebra. Good luck!