Skip to content

Interactive explainer

Linear programming by the graphical method

In two variables a linear programme can be solved by drawing it, and the figure below is a calculator you can drag: each constraint cuts the plane in half, what's left is the feasible region, and the best point is one of its corners. Drag the lines, add and delete constraints, and see when the answer is one point, a whole edge, unbounded or impossible. Explore shows what one more unit of each constraint is worth; Step by step walks the method as you would do it by hand; Simplex walks the corners the simplex method visits.

From the courses Energy Markets (UPC Barcelona), Power Systems (UPC Barcelona) and Power System Operation (Prof. dr Andrija T. Sarić).

The feasible region and the objective's line

024680510xyz = 36(2, 6)123
How to look at it

The maximum: z = 36 at (2, 6), the corner where line 2 and line 3 meet.

Start from

The objective

z =x +y

Subject to (drag a line by its number, or type)
  1. x +y
  2. x +y
  3. x +y
x, y ≥ 0
The corners
Where lines meetxyz
x ≥ 0 ∩ y ≥ 0000
1 ∩ y ≥ 04012
1 ∩ 34327
2 ∩ 32636 (the maximum)
2 ∩ x ≥ 00630
The plane of the two variables: each constraint's line with its number, the feasible region shaded, its corners, and the objective's dashed line with an arrow the way z improves, at the optimum or wherever the reader drags it. In Explore, each constraint's shadow price and the range it holds over are written under it, the objective's coefficients' ranges under the objective, and at the optimum the binding constraints' arrows are drawn with the objective's between them. Step by step draws the hand method one step at a time; Simplex walks from the origin, one pivot a step, under either entering rule.

What it is

A linear programme asks for the best value of a linear objective over the points that satisfy a set of linear constraints. In two variables, xx and yy:

maximise (or minimise)z=c1x+c2ysubject toaix+biy ≤, ≥ or = ri,i=1…m,x,y≥0\begin{aligned} &\text{maximise (or minimise)} && z = c_1x + c_2y \\ &\text{subject to} && a_ix + b_iy \ \le,\ \ge \text{ or } = \ r_i, \quad i = 1 \ldots m, \qquad x, y \ge 0 \end{aligned}
(1)

Each inequality holds on one side of its line aix+biy=ria_ix + b_iy = r_i, and each equality on the line itself. The points that satisfy all of them at once are the feasible region: the intersection of half-planes, so always convex, and never in two pieces. It can be a polygon, an unbounded region, a segment (where there is an equality), or nothing at all. Minimising zz is maximising −z-z, so most of what follows is said for a maximum. One convention runs through the page: a constraint's shadow price λi\lambda_i is the change of the best zz, as the problem writes zz, for one unit more of its right side rir_i (equation (4) below), so it is at least zero on a ≤\le row of a maximisation and on a ≥\ge row of a minimisation.

The method, step by step

The graphical method, as it is done by hand and as Step by step in the figure draws it:

  1. If x,y≥0x, y \ge 0, only the first quadrant counts.
  2. For each constraint, draw its line aix+biy=ria_ix + b_iy = r_i: where it crosses the axes is enough, (ri/ai,0)(r_i/a_i, 0) and (0,ri/bi)(0, r_i/b_i), and a line with ai=0a_i = 0 or bi=0b_i = 0 is level or upright. Then pick a test point not on the line, the origin if you can, and put it into the inequality: if it holds, keep the test point's side; if not, keep the other. An equality keeps only the line itself.
  3. What is left after every constraint is the feasible region. If nothing is left, the problem is infeasible and you stop.
  4. Find its corners: each is where two of the lines cross, by (2) below, and a crossing counts only if it satisfies every other constraint.
  5. Work out zz at every corner. The best is the optimum, if the region is bounded.
  6. Check it with the objective's line: draw c1x+c2y=zc_1x + c_2y = z for some zz and slide it parallel to itself in the direction zz improves. The last point of the region it touches is the optimum. If it never stops touching the region, there is no optimum.
  7. Write the answer: one corner, a whole edge (two corners tie), or no optimum.

Two lines cross where both hold at once:

{aix+biy=riajx+bjy=rj⇒x=ribj−birjaibj−biaj,y=airj−ajriaibj−biaj\left\{\begin{aligned} a_ix + b_iy &= r_i \\ a_jx + b_jy &= r_j \end{aligned}\right. \quad\Rightarrow\quad x = \frac{r_ib_j - b_ir_j}{a_ib_j - b_ia_j}, \qquad y = \frac{a_ir_j - a_jr_i}{a_ib_j - b_ia_j}
(2)

School books give steps 4 and 5 the name corner point method and step 6 the iso-profit line method (iso-cost for a minimum); they are two ways to finish the same drawing, and doing both is the check. Never shade by the sign alone: "≥\ge means above the line" is true only when bi>0b_i > 0, and −x+2y≤4-x + 2y \le 4 and x−2y≥−4x - 2y \ge -4 are the same half-plane. In the product mix (worked example A), three crossings of its lines, (4, 6), (0, 9), (6, 0), are not corners: each breaks a constraint.

Why the optimum is at a corner

The points where the objective takes one value, c1x+c2y=zc_1x + c_2y = z, form a straight line, and for other values of zz the lines are parallel to it; zz grows in the direction of c=(c1,c2)c = (c_1, c_2). Slide the line that way: the last point of the region it touches is the optimum. A convex polygon is last touched by a straight line at a corner, or along a whole edge, which has corners at its ends. So if a linear programme has an optimum, one of the region's corners is optimal: the corner point theorem, and the whole of the graphical method. With mm lines there are at most m(m−1)/2m(m-1)/2 crossings to try by (2), which is nothing in two variables and hopeless in two thousand. The simplex method does better by walking from a corner along an edge to a neighbouring corner that is better, and stopping when none is.

The direction of cc decides, never the distance from the origin. Over the product mix with 9x+5y9x + 5y, the corner farthest from the origin is (2, 6), 6.32 away, and gives 48; the optimum is (4, 3), 5 away, with 51.

The four outcomes

  • One optimum, at one corner.
  • Many optima: the objective's line is parallel to the edge where the region is last touched, and every point of that edge is as good. Two corners give the same zz, and so does every point between them.
  • Unbounded: inside the region zz grows without end, so there is no maximum.
  • Infeasible: the constraints contradict each other and the region is empty.

An unbounded region is not yet an unbounded problem. The region runs to infinity along the directions dd that no constraint stops, and the problem is unbounded only if zz improves along one of them:

unbounded  ⟺  ∃ d≠0:c1dx+c2dy>0andaidx+bidy ≤0, ≥0 or =0 as its constraint is ≤, ≥ or =\begin{aligned} \text{unbounded} \iff \exists\, d \ne 0: \quad & c_1d_x + c_2d_y > 0 \quad \text{and} \\ & a_id_x + b_id_y \ \le 0,\ \ge 0 \text{ or } = 0 \ \text{as its constraint is}\ \le,\ \ge \text{ or } = \end{aligned}
(3)

The class 12 rule says the same in words: on an unbounded region, the best corner value MM is the maximum only if no feasible point gives more than MM, which you test by drawing c1x+c2y>Mc_1x + c_2y > M and seeing whether it meets the region. The diet of worked example B, without its last constraint, is unbounded and still has a minimum, 12; its largest corner value is 24, and it has no maximum.

A solver that looks for the region by testing a grid of points, or that finds corners inside a drawing's box, gets three of these wrong: it misses the region of an equality, which falls between the grid's points; it calls an unbounded problem optimal at the edge of its box; and it shows one corner of a tie. The figure here finds the corners exactly by (2) and decides boundedness by (3).

Shadow prices and their ranges

Every constraint has a price: how much the optimum z∗z^* changes for one unit more of its right side, its shadow price, also called its dual value, marginal value or (in the language of the KKT conditions) its Lagrange multiplier:

λi=∂z∗∂ri\lambda_i = \frac{\partial z^*}{\partial r_i}
(4)

In the figure's Explore, drag a line of the product mix and watch zz: line 3, 3x+2y≤183x + 2y \le 18, moved out by one unit, takes the optimum along line 2 and raises zz by 1; line 2 is worth 1.5 a unit; line 1, x≤4x \le 4, is worth nothing, since the optimum (2, 6) has 2 to spare on it. The prices can also be read off the corner itself. At an optimal corner, the objective's row cc is a weighted sum of the rows ni=(ai,bi)n_i = (a_i, b_i) of the two lines through it, and the weights are their prices:

c=λi ni+λj nj,(3,5)=1.5 (0,2)+1 (3,2)c = \lambda_i\, n_i + \lambda_j\, n_j, \qquad (3, 5) = 1.5\,(0, 2) + 1\,(3, 2)
(5)

In Explore the two rows are drawn as arrows from the optimum, at right angles to their lines, with cc's arrow between them. A corner of a maximum over ≤\le rows is optimal exactly while cc points between its two rows' arrows, so that both weights are at least zero; this is the course books' KKT condition, drawn.

A price holds only over a range. Move line 3 out and the corner slides along line 2 until it meets line 1 at (4, 6), at r3=24r_3 = 24; past it line 3 has room to spare and is worth nothing. Move it in and the corner slides until it reaches the yy axis at r3=12r_3 = 12; under it each unit is worth 2.5. So z∗(r3)z^*(r_3) is a chain of straight pieces, and the price is the slope of the piece the present r3r_3 sits on, each unit worth no more than the one before it: 2.5, then 1, then 0. At 18, z∗=36z^* = 36; at 24, 42; at 30, still 42.

The product mix with its third line, 3x + 2y ≤ r, and beside it the optimum z* as r runs from 0 to 30: three straight pieces, worth 2.5, 1 and 0 a unit. Move r: the price is the slope of the piece you are on, and its range is that piece's ends. Past 24 the line has room to spare and its price is 0.

The figure shows the rule called complementary slackness: a constraint with room to spare has a price of zero, and a constraint with a price above zero has no room to spare. With sis_i the room left on row ii at the optimum,

λi si=0for every i,si=ri−aix∗−biy∗  (for a≤row)\lambda_i\, s_i = 0 \quad \text{for every } i, \qquad s_i = r_i - a_ix^* - b_iy^* \ \ (\text{for a} \le \text{row})
(6)

The objective's coefficients have ranges too: the present corner stays optimal while cc stays between the two rows' arrows. For the product mix, (2, 6) stays best while xx's coefficient is from 0 to 7.5 and yy's is 2 or more. At 7.5, c=(7.5,5)c = (7.5, 5) points along line 3's row and the whole edge from (4, 3) to (2, 6) ties; above it (4, 3) is best. These are the numbers a solver's sensitivity report prints as a constraint's allowable increase and decrease, and a variable's.

Who writes the price which way

The numbers agree everywhere; the signs don't. To map another tool's price onto this page's (4):

WhoWhat it calls itAgainst this page
This pageshadow price λi=∂z∗/∂ri\lambda_i = \partial z^*/\partial r_i
SciPy linprog (HiGHS)marginals of ineqlin and eqlinthe derivative of the objective linprog minimises, for rows written ≤\le: a maximisation solved as minimise −z-z turns the sign, and a ≥\ge row entered as −aix−biy≤−ri-a_ix - b_iy \le -r_i turns it again
GAMSan equation's marginal, .mthe same as here for the row as written; a balance written generation less demand gives the price as −-.m
Excel SolverShadow Price, with Allowable Increase and Allowable Decreasethe same; the range is given as two distances from the right side
GurobiPi, the dual value; SARHSLow and SARHSUpthe same, in a minimisation and a maximisation
LINDO, LINGOdual pricehow much the objective improves: the same in a maximisation, the opposite sign in a minimisation
Hillier and Liebermanshadow price yi∗y_i^*, the marginal value of resource iithe same (maximise, ≤\le rows)
Energy markets coursesLagrange multiplier, dual variable, marginal pricethe same; the price of a bus is its balance's multiplier

When three lines meet

Add x+y≤rx + y \le r to the product mix. The mix reaches x+y=8x + y = 8 at most, at its optimum (2, 6), so for rr above 8 the new line misses the region: it is redundant, its price is 0 and every other price is as before. Below 8 it cuts the region: at r=7r = 7 the optimum moves along line 2 to (1, 6), z=33z = 33, and line 3 goes slack. At exactly 8 three lines run through (2, 6), more than the two that name a corner: the corner is degenerate. Then a price has two sides. One more unit of line 3 is worth 0, since line 4 stops the corner at once, but one less costs 1; line 2's one more is worth 1 and one less costs 1.5. A solver prints one set of multipliers, valid but one of several, and for one of the questions it seems to answer it is wrong.

The product mix with a fourth line, x + y ≤ r, and its optimum seen close. Above 8 the line is redundant; at 8 it runs through the optimum with lines 2 and 3, and each binding price splits into one more and one less; under 8 it cuts the region and line 3 goes slack.

The dual

The prices are themselves the answer to a linear programme, the dual of the first, the primal. For a maximisation over ≤\le rows with x,y≥0x, y \ge 0, it has one unknown ui≥0u_i \ge 0 for each constraint and one constraint for each variable:

maximise c1x+c2yminimise ∑iriuiaix+biy≤ri,  x,y≥0∑iaiui≥c1,  ∑ibiui≥c2,  ui≥0\begin{aligned} &\text{maximise } c_1x + c_2y && \qquad && \text{minimise } \textstyle\sum_i r_iu_i \\ &a_ix + b_iy \le r_i,\ \ x, y \ge 0 && \qquad && \textstyle\sum_i a_iu_i \ge c_1,\ \ \sum_i b_iu_i \ge c_2,\ \ u_i \ge 0 \end{aligned}
(7)

Read the dual as a buyer's offer: uiu_i is what the buyer pays for a unit of resource ii, and its rows say that the resources in one unit of each product are worth at least that product's profit, so selling them pays as well as using them. Its other forms follow the same rule: a ≥\ge row of a maximisation has ui≤0u_i \le 0, an equality has uiu_i of any sign, and the dual of a minimisation is a maximisation with the inequalities turned. For any feasible pair, z≤wz \le w:

c1x+c2y ≤ ∑iui(aix+biy) ≤ ∑iriuic_1x + c_2y \ \le\ \textstyle\sum_i u_i (a_ix + b_iy) \ \le\ \sum_i r_iu_i
(8)

That is weak duality: every feasible point of the dual bounds the primal. Strong duality says the best of each are equal when either has an optimum, and the dual's optimal uu are the primal's shadow prices. The product mix's dual is to minimise 4u1+12u2+18u34u_1 + 12u_2 + 18u_3 over u1+3u3≥3u_1 + 3u_3 \ge 3 and 2u2+2u3≥52u_2 + 2u_3 \ge 5; its optimum is u=(0,1.5,1)u = (0, 1.5, 1), the three prices, and its value is 36, the primal's optimum. Complementary slackness holds both ways: a primal constraint with room to spare has ui=0u_i = 0 (line 1), and a primal variable above zero has its dual row tight (both are here: u1+3u3=3u_1 + 3u_3 = 3 and 2u2+2u3=52u_2 + 2u_3 = 5, with no room over 3 and 5). An unbounded primal has an infeasible dual, and an infeasible primal's dual is unbounded or infeasible.

The starts of the figure have three or four constraints, so their duals have three or four unknowns and can't be drawn. A primal of two constraints can: maximise 4x+3y4x + 3y over 2x+y≤102x + y \le 10 and x+y≤8x + y \le 8. Its optimum is 26 at (2, 6), and its dual, to minimise 10u1+8u210u_1 + 8u_2 over 2u1+u2≥42u_1 + u_2 \ge 4 and u1+u2≥3u_1 + u_2 \ge 3, has 26 at (1, 2). With b1=8b_1 = 8, three lines meet at the primal's corner (0, 8), and the dual's optimum becomes a whole edge, from (3, 0) to (1, 2): a degenerate corner on one side is a tie on the other.

A primal on the left and its dual on the right. b₁ moves the primal's first line and turns the dual's objective; c₁ turns the primal's objective and moves the dual's first line. The two optima are always equal, the dual's corner is the primal's two prices, and where three lines meet on one side the other has a whole edge of optima.

The simplex method, seen

The simplex method turns each inequality into an equality with a slack variable, si=ri−aix−biy≥0s_i = r_i - a_ix - b_iy \ge 0, the room left on row ii (a surplus for a ≥\ge row). Now every one of xx, yy and the slacks is at least zero, and a variable at zero is a line the point stands on: x=0x = 0 is the yy axis, si=0s_i = 0 is line ii. A corner is where two of them are zero; the others, the basis, are what the equalities solve for. One step, a pivot, lets one variable that is zero rise (it enters) and moves the point along an edge until another variable hits zero (it leaves): the line that stops the move first, found by the ratio test. Each pivot is one edge to a better corner, and the method stops when no edge improves zz.

Which variable enters is a choice. The textbook's rule (Dantzig's) takes the largest coefficient in the objective row; Bland's takes the first one that helps, which can never cycle. On the product mix they make different walks to the same optimum. The figure's Simplex mode has the switch. Its first pivot under Dantzig's rule: yy enters, so the point leaves the xx axis and runs up the yy axis; line 2 stops it at (0, 6) and line 3 would at (0, 9), so the nearer wins and s2s_2 leaves.

RuleThe walkPivots
Largest coefficient (Dantzig)(0, 0) → (0, 6), z 30 → (2, 6), z 36y in, s₂ out; x in, s₃ out
First that helps (Bland)(0, 0) → (4, 0), z 12 → (4, 3), z 27 → (2, 6), z 36x in, s₁ out; y in, s₃ out; s₁ in, s₂ out

Where the origin breaks a constraint, the method first has to find a corner. Phase 1 adds an artificial variable to each row the origin breaks and minimises their sum, the total by which the constraints are broken; it still walks from crossing to crossing, but outside the region, and when the total reaches zero it is at a corner and phase 2 improves zz from there. On the diet, the broken total runs 23, 5, 5, 0, the second pivot not moving the point at all, since two lines cross the xx axis at (3, 0): a stalled pivot at a degenerate corner. Real solvers walk the same way over millions of variables, with better rules and much better linear algebra. Interior-point methods, the other family, cross the inside of the region instead of its edges; lpviz draws both on a plane you shape.

Whole numbers only

Often the unknowns must be whole: machines, trucks, plants started. Rounding the linear optimum does not give the answer. Maximise x+4yx + 4y over x+y≤6x + y \le 6 and x+5y≤20x + 5y \le 20: the linear optimum is 16.5 at (2.5, 3.5). Rounded to the nearest, (3, 4) breaks both constraints; of its four roundings the best feasible is (3, 3), 15. The best whole point is (0, 4), 16, two and a half units of xx away and not one of the roundings. The linear optimum is still useful: 16.5 bounds the whole-number optimum from above, and that is what branch and bound works with: it splits the region at a fractional variable (x≤2x \le 2 or x≥3x \ge 3), solves each part as a linear programme, and drops a part whose bound can't beat the best whole point found. GILP draws its tree.

Maximise x + 4y over x + y ≤ 6 and x + 5y ≤ r₂, x and y whole: every whole point, those inside dark, the linear optimum, its nearest rounding (a red cross where it breaks a constraint) and the best whole point, with the objective's line through each optimum. Move r₂ and watch the best whole point jump.

Where it comes from

The corner point theorem. On a segment from pp to qq, a linear zz is z(p)+t (z(q)−z(p))z(p) + t\,(z(q) - z(p)) for tt from 0 to 1, so it is largest at one end, or the same along the whole segment. A bounded feasible region is a convex polygon, and every point of it is a mixture of its corners, ∑ktkvk\sum_k t_k v_k with tk≥0t_k \ge 0 and ∑ktk=1\sum_k t_k = 1; so zz there is the same mixture of the corners' values, and no larger than the largest of them. An unbounded region adds directions it runs off along, and (3) is the test that none of them improves zz.

Why (5) is the test of a corner. At a corner vv where lines ii and jj meet, the ways to move that stay inside are the directions dd with ni⋅d≤0n_i \cdot d \le 0 and nj⋅d≤0n_j \cdot d \le 0 (for ≤\le rows): a wedge. vv is optimal if no direction in the wedge raises zz, that is c⋅d≤0c \cdot d \le 0 for all of them. Write c=λini+λjnjc = \lambda_in_i + \lambda_jn_j, which two crossing lines always allow; then c⋅d=λi ni⋅d+λj nj⋅dc \cdot d = \lambda_i\, n_i \cdot d + \lambda_j\, n_j \cdot d, which is at most zero for every dd in the wedge exactly when λi,λj≥0\lambda_i, \lambda_j \ge 0.

From (5), the rest. The prices: move rir_i by δ\delta and the corner moves by some dd with ni⋅d=δn_i \cdot d = \delta and nj⋅d=0n_j \cdot d = 0, so zz moves by c⋅d=λiδc \cdot d = \lambda_i\delta: λi\lambda_i is (4). The ranges: that holds while the same two lines name the corner, until it runs into a third line. Complementary slackness: a line not through the corner has no weight in (5), so its price is zero. The dual: the weights, with zero for every other row, satisfy ∑iaiui≥c1\sum_i a_iu_i \ge c_1 and ∑ibiui≥c2\sum_i b_iu_i \ge c_2 (with equality where xx and yy are above zero, ≥\ge where an axis is one of the two lines), so they are feasible for (7), and ∑iriui=λiri+λjrj=c⋅v=z∗\sum_i r_iu_i = \lambda_i r_i + \lambda_j r_j = c \cdot v = z^*: strong duality in two variables.

Where it came from. L. V. Kantorovich wrote down linear programmes for planning production in 1939; G. B. Dantzig gave the simplex method in 1947, with the duality theory worked out with J. von Neumann soon after. V. Klee and G. Minty showed in 1972 that Dantzig's rule can visit every corner of a cube bent the right way, an exponential number; L. Khachiyan's ellipsoid method (1979) was the first proven to take polynomial time, and N. Karmarkar's interior-point method (1984) the first such method to compete with the simplex in practice.

Try this

  1. In Explore, on the product mix, grab line 3's handle and move it with the arrow keys. The tag over the handle says each unit is worth 1 until 24, where the corner reaches line 1 and the price drops to 0. Then set the profit of x to 7.5: the end of x's range, where the whole edge from (4, 3) to (2, 6) ties.
  2. Start from Diet, minimise and choose Step by step. Ten steps, the quadrant, four lines, the region, its corners, z at each, the check and the answer: 12 at (2, 3). Line 1, 2x + y ≥ 6, is drawn and kept and never touches the region: in Explore its price is 0 up to 7.
  3. On the product mix choose Simplex and press Next to the end, once under each rule. Largest coefficient: (0, 0) → (0, 6), z 30 → (2, 6), z 36, two pivots. First that helps: (0, 0) → (4, 0), z 12 → (4, 3), z 27 → (2, 6), z 36, three. The same optimum, by different edges.
  4. Start from Market, two generators in Explore. Under the demand's equality: each MW costs 70, from 300 to 550 MW: the price of electricity. Under generator 1's limit: each MW of it saves 40, 70 − 30, what replacing generator 2's power with generator 1's is worth.

Worked examples

A. A maximisation, by hand: the product mix. Two products made in three plants, xx and yy batches a week; the plants have 4, 12 and 18 hours, and the profit is 3 and 5 a batch (Hillier and Lieberman's case, the figure's first start): maximise 3x+5y3x + 5y subject to x≤4x \le 4, 2y≤122y \le 12, 3x+2y≤183x + 2y \le 18, x,y≥0x, y \ge 0. The lines: x=4x = 4 is upright, 2y=122y = 12 level at y=6y = 6, and 3x+2y=183x + 2y = 18 runs through (6, 0) and (0, 9). The origin satisfies all three, so each keeps the origin's side. The region's corners and zz at each:

Cornerz=3x+5yz = 3x + 5y
(0, 0)0
(4, 0)12
(4, 3)27
(2, 6)36
(0, 6)30

The crossings (4, 6), (0, 9), (6, 0) are not corners: (4, 6) breaks line 3, (0, 9) line 2 and (6, 0) line 1. The optimum is 36 at (2, 6), on lines 2 and 3. What one more hour of each plant is worth: plant 1, nothing (it has 2 hours to spare); plant 2, 1.5, for its hours from 6 to 18; plant 3, 1, from 12 to 24. By (5), (3,5)=1.5 (0,2)+1 (3,2)(3, 5) = 1.5\,(0, 2) + 1\,(3, 2). Set it up in the figure from Product mix.

B. A minimisation: the diet, and an unbounded region

Two foods, xx and yy portions, costing 3 and 2; each portion of xx gives 2, 1 and 3 units of three nutrients, each of yy 1, 2 and 1, and at least 6, 8 and 9 are needed; at most 10 portions in all. Minimise 3x+2y3x + 2y subject to 2x+y≥62x + y \ge 6, x+2y≥8x + 2y \ge 8, 3x+y≥93x + y \ge 9, x+y≤10x + y \le 10, x,y≥0x, y \ge 0. The origin breaks the three ≥\ge rows, so each keeps the side away from it. The corners:

Cornerz=3x+2yz = 3x + 2y
(8, 0)24
(10, 0)30
(0, 10)20
(0, 9)18
(2, 3)12

The minimum is 12 at (2, 3), where the second and third nutrients bind. Their prices are 0.6 and 0.8: one more unit required costs that much more. The first nutrient never binds: the other rows already give at least 7 of it, so its price is 0. Now delete x+y≤10x + y \le 10. The region runs on for ever up and to the right, its corners are (8, 0), (0, 9), (2, 3), and the minimum is still 12: by (3), no direction the region runs along lowers 3x+2y3x + 2y. Asked for the maximum instead, the largest corner value is 24, but there is no maximum: zz grows without end along the region.

C. A word problem turned into constraints: a farm

A farm has 40 hectares and 240 hours of work for the season. Wheat takes 4 hours a hectare and earns 300 €; maize takes 8 hours and earns 450 €. How much of each? Name the unknowns first: ww hectares of wheat and mm of maize. Each limited thing is a constraint, written in its own unit: land, w+m≤40w + m \le 40 hectares; work, 4w+8m≤2404w + 8m \le 240 hours; and neither can be negative. The objective is the money: maximise 300w+450m300w + 450m.

Corner (w,m)(w, m)zz, €
(40, 0)12,000
(20, 20)15,000
(0, 30)13,500
(0, 0)0

The best plan is (20, 20): 20 hectares of each, 15,000 €. Both resources bind, and their prices say what more of each is worth: a hectare 150 € (while the land is from 30 to 60 hectares), an hour 37.5 € (from 160 to 320 hours). So land offered at 120 € a hectare is worth renting, and help at 40 € an hour is not. The plan stays best while wheat earns 225 to 450 € a hectare and maize 300 to 600. The simplex from the origin, by the largest coefficient: (0, 0) → (0, 30), z 13,500 → (20, 20), z 15,000, m in, s₂ out; w in, s₁ out. Type it into the figure as two constraints and check each number.

D. An electricity market, cleared

Generator 1 at bus 1 offers up to 300 MW at 30 €/MWh; generator 2 at bus 2 offers up to 250 MW at 70 €/MWh; 400 MW of demand is at bus 2. The market operator chooses the outputs g1g_1 and g2g_2 that meet the demand at the least cost:

minimise  30g1+70g2subject tog1+g2=400,g1≤300,g2≤250,g1,g2≥0\text{minimise}\ \ 30g_1 + 70g_2 \quad \text{subject to} \quad g_1 + g_2 = 400, \quad g_1 \le 300, \quad g_2 \le 250, \quad g_1, g_2 \ge 0

The equality leaves no area: the feasible region is the segment of the line g1+g2=400g_1 + g_2 = 400 between two corners, one where generator 2 is at its limit and one where generator 1 is (start the figure from Market, two generators):

Cornerg1g_1, MWg2g_2, MWCost, €/h
Generator 1 at its limit30010016,000
Generator 2 at its limit15025022,000

The cheaper corner wins: 16,000 €/h, with generator 1 at full output and generator 2 making up the rest. The price of electricity is the shadow price of the demand's equality: one more MW of demand moves the optimum to (300, 101) and adds 70 € an hour, so the market price is 70 €/MWh, the offer of the marginal unit, the one not at a limit. It holds while the demand stays from 300 to 550 MW: under 300 generator 1 is marginal and the price is 30, past 550 nothing more can be had. Generator 1's limit has a price too, −40 €/MWh: one more MW of it saves 40 € an hour, its offer's distance from the price, which is what it earns on each MW it sells.

With a line limit

Generator 1 reaches the demand over a line from bus 1 to bus 2 that carries at most 200 MW, so g1≤200g_1 \le 200. Add it in the figure (a constraint 1⋅g1+0⋅g2≤2001 \cdot g_1 + 0 \cdot g_2 \le 200): the segment is cut short at (200, 200), and the cost rises to 20,000 €/h. Now the prices differ by bus. One more MW at bus 2 must come from generator 2, so it costs 70 €/MWh; one more MW at bus 1 can come from generator 1 without using the line, 30 €/MWh. These are locational marginal prices, or nodal prices, and their difference, 40 €/MWh, is the line's own shadow price: times its 200 MW it is the money the market collects from buyers beyond what it pays sellers, the congestion rent or merchandising surplus, 8,000 €/h. In a meshed network the line's flow comes from the DC power flow, and the same price splits there across three buses.

With a third generator, the plane of two outputs is no longer enough, and the offers drawn as a staircase are. Add generator 3 at bus 1, 50 €/MWh up to 150 MW. With no line limit and 400 MW, generator 3 is marginal and the price is 50 €/MWh, the cost 14,000 €/h. With the line at 350 MW, bus 1's two units can send only 350, generator 2 gives the other 50, and the prices split, 50 at bus 1 and 70 at bus 2, with 7,000 €/h of surplus. At 450 MW without a limit the demand stands exactly on the edge between generator 3's step and generator 2's, and the price has two sides: one more MW costs 70, one less saves 50.

Three generators' offers as a staircase, cheapest first, as bus 2 sees them; the demand a vertical line, the price where it meets the stairs. Move the line's limit below what bus 1 can send: bus 1's steps past it are greyed, generator 2's step moves in behind them, and the one price becomes two.

Energy markets courses write the same auction with demand bids as well as supply offers, a stair of blocks for each, and maximise social welfare, what the accepted bids are worth less what the accepted offers cost, subject to one balance a bus. The price is still the balance's multiplier, and the conditions of the optimum say what the staircase shows: a block offered under the price is fully accepted, one over it is refused, and the marginal block is the one at the price.

Mistakes people make

  1. The mistake

    "≥\ge means above the line": shading by the sign.

    Why it's wrong

    −x+2y≤4-x + 2y \le 4 and x−2y≥−4x - 2y \ge -4 are the same half-plane, and one of them is shaded the wrong way by the rule. The sign says above only when bi>0b_i > 0.

    The right one

    Test a point not on the line, the origin if you can, and keep its side if the inequality holds there.

  2. The mistake

    Every crossing of two lines is a corner.

    Why it's wrong

    (4, 6), where lines 1 and 2 of the product mix cross, would give 42; it breaks 3x+2y≤183x + 2y \le 18 (it gives 24).

    The right one

    A corner is a crossing that satisfies every constraint. Check each one against all of them.

  3. The mistake

    The corner farthest from the origin is the maximum.

    Why it's wrong

    With 9x+5y9x + 5y over the product mix, (2, 6) is farthest (6.32 against 5) and gives 48; (4, 3) gives 51.

    The right one

    The direction of cc decides: evaluate zz at every corner, or slide the objective's line.

  4. The mistake

    An unbounded region has no optimum.

    Why it's wrong

    The diet without x+y≤10x + y \le 10 runs on for ever, and its minimum is 12 at (2, 3).

    The right one

    An unbounded region has no optimum only if zz improves along one of the directions it runs off in, (3).

  5. The mistake

    On an unbounded region, the best corner is the answer.

    Why it's wrong

    On the same region the largest corner value is 24, and there is no maximum: 3x+2y3x + 2y grows without end.

    The right one

    The best corner value MM is the answer only if no feasible point does better than MM.

  6. The mistake

    A tie is reported as one corner.

    Why it's wrong

    x+yx + y over the Many optima start is 6 at (1, 5), at (4, 2) and at every point between them.

    The right one

    When two corners give the same best zz, the whole edge between them is optimal: say so.

  7. The mistake

    Whole numbers by rounding the linear optimum.

    Why it's wrong

    (2.5, 3.5) rounds to (3, 4), which is infeasible; the best whole point is (0, 4), not one of the roundings.

    The right one

    Solve it as an integer programme (branch and bound, or a solver's integer mode); the linear optimum is only a bound.

  8. The mistake

    A shadow price used outside its range.

    Why it's wrong

    Six more hours of plant 3 are worth 6; twelve more are worth 6 too, not 12: past 24 the price is 0.

    The right one

    Read the range with the price, and solve again past its end.

  9. The mistake

    A price for a constraint that isn't binding.

    Why it's wrong

    x≤4x \le 4 has 2 to spare at (2, 6): one more unit of it changes nothing.

    The right one

    A constraint with room to spare has a price of 0: complementary slackness, (6).

  10. The mistake

    One price where three lines meet.

    Why it's wrong

    With x+y≤8x + y \le 8 added, a solver may print 1 for line 3; one more unit of it is worth 0, and one less costs 1.

    The right one

    At a degenerate corner, ask for one more and one less separately, or solve again with the right side moved.

  11. The mistake

    The sign of a price, copied from another tool.

    Why it's wrong

    Generator 1's limit is worth −40 by (4): the cost falls. LINDO prints its dual price as 40, and linprog's marginal depends on how the row was written.

    The right one

    Map the tool's convention onto (4) first, with the table above.

  12. The mistake

    Assuming x,y≥0x, y \ge 0 when the problem doesn't say so.

    Why it's wrong

    Without it the product mix still has its maximum, 36, but minimised it has no answer at all: z falls for ever down and to the left.

    The right one

    Write the variables' signs as constraints of their own; in the figure, the switch under the constraints.

The same in Python

from scipy.optimize import linprog

# The product mix: maximise 3x + 5y, so linprog minimises -3x - 5y
res = linprog([-3, -5], A_ub=[[1, 0], [0, 2], [3, 2]], b_ub=[4, 12, 18])
prices = ", ".join(f"{-m + 0:g}" for m in res.ineqlin.marginals)
print(f"product mix: z = {-res.fun:.0f} at ({res.x[0]:.0f}, {res.x[1]:.0f}), prices {prices}")

cost = [30, 70]                           # offers of generators 1 and 2, EUR/MWh
limits = [(0, 300), (0, 250)]             # their outputs, MW
demand = 400                              # MW, at bus 2
for line in (None, 200):                  # the line from bus 1 to bus 2, MW
    ub = dict(A_ub=[[1, 0]], b_ub=[line]) if line else {}
    res = linprog(cost, A_eq=[[1, 1]], b_eq=[demand], bounds=limits, **ub)
    g1, g2 = res.x
    price2 = res.eqlin.marginals[0]       # what one more MW at bus 2 costs
    price1 = price2 + (res.ineqlin.marginals[0] if line else 0)
    print(f"line {line}: g1 = {g1:.0f} MW, g2 = {g2:.0f} MW, "
          f"cost {res.fun:,.0f} EUR/h, prices {price1:.0f} and {price2:.0f} EUR/MWh")

It prints:

product mix: z = 36 at (2, 6), prices 0, 1.5, 1
line None: g1 = 300 MW, g2 = 100 MW, cost 16,000 EUR/h, prices 70 and 70 EUR/MWh
line 200: g1 = 200 MW, g2 = 200 MW, cost 20,000 EUR/h, prices 30 and 70 EUR/MWh

SciPy's linprog (the HiGHS solver) on the product mix and on the market of worked example D, without and with the line limit. linprog minimises, so the product mix's marginals come back with their sign turned; turned back, they are the three plants' prices. The market's prices are its marginals: the demand's for bus 2, and at bus 1 the same less what one more MW of line would save.

Questions people ask

What are the steps of the graphical method?

Draw each constraint's line from where it crosses the axes, keep the side a test point says, and find the region all of them leave. Mark its corners, where two lines cross by (2), keeping only those that satisfy every constraint. Work out zz at each and take the best, checking with the objective's line slid across the region. The method, step by step has all seven, and the figure's Step by step draws them on your own problem.

What is the corner point method?

The graphical method's way to finish: list the feasible region's corners and evaluate the objective at each; the best is the optimum, if the region is bounded. For the product mix the values are 0, 12, 27, 36, 30, so 36 at (2, 6). It rests on the corner point theorem: a linear objective is best at a corner.

What is the iso-profit (or iso-cost) line method?

The other way to finish: draw the objective's line c1x+c2y=zc_1x + c_2y = z for any zz and slide it parallel to itself in the direction zz improves; the last point of the region it touches is the optimum. All points on one such line give the same profit (or cost), hence the name. On an unbounded region it also shows when there is no optimum: the line never leaves the region.

Which side of the line do I shade?

Put a point that isn't on the line into the inequality, the origin if the line misses it. If the inequality holds there, the point's side is the one kept; if not, the other. Don't go by the sign: ≥\ge means above the line only when the coefficient of yy is positive.

What is a feasible region, and can it be in two parts?

The points that satisfy every constraint at once. It can't be in two parts: each constraint keeps a half-plane (or a line), every half-plane is convex, and an intersection of convex sets is convex, so the segment between any two feasible points is feasible. A problem that needs "this region or that one" is not a linear programme; it needs a whole-number variable to choose.

Why is the optimum always at a corner?

Along any segment a linear function is largest at one of the ends, and every point of a bounded convex polygon is a mixture of its corners, so no point does better than the best corner. When an edge is parallel to the objective's line, every point of that edge ties, and its ends are corners. Where it comes from has the proof.

What if the feasible region is unbounded?

Then there may or may not be an optimum. Find the best corner value MM, then ask whether any feasible point does better, by (3) or by drawing z>Mz > M and seeing whether it meets the region. The diet of example B without its last constraint has a minimum, 12, and no maximum.

Can a linear programme have more than one optimal solution?

Yes: when the objective's line is parallel to the edge where it last touches the region, the whole edge is optimal. Set the product mix's profit of xx to 7.5 and every point from (4, 3) to (2, 6) gives 45. A linear programme has no optimum, exactly one, or infinitely many; never exactly two.

What is a shadow price, and how do I find it on the graph?

How much the best zz changes for one unit more of a constraint's right side, (4). On the graph, move the constraint's line out by one unit and see how far z∗z^* moves, or write cc as the weighted sum of the two binding lines' rows at the optimum, (5): the weights are the prices. For the product mix, plant 3's hour is worth 1 from 12 to 24 hours. The figure's Explore writes each one under its constraint.

What is a binding, a slack and a redundant constraint?

Binding: the optimum lies on its line, so it holds with equality and usually has a price. Slack (or non-binding): the optimum has room to spare on it, and its price is 0. Redundant: removing it changes the region not at all, like x+y≤9x + y \le 9 added to the product mix. A redundant constraint is always slack, but a slack constraint need not be redundant: it may cut off part of the region away from the optimum.

What is the dual of a linear programme?

A second linear programme built from the same numbers, one unknown for each constraint of the first and one constraint for each variable, (7). Its best value equals the first's, and its optimal unknowns are the first's shadow prices: the product mix's dual has its optimum 36 at u=(0,1.5,1)u = (0, 1.5, 1). The dual draws a pair side by side.

Why can't the graphical method solve three variables, and what does?

With three variables each constraint is a plane in space and the region a polyhedron, which can be drawn but not worked on by hand; past three there is nothing to draw. The ideas carry over unchanged: the optimum is at a corner, and the simplex method walks the polyhedron's edges with the same pivots, whatever the number of variables. Solvers such as HiGHS, Gurobi or CPLEX use it, or interior-point methods.

It is the corner point method without the drawing. Each slack variable at zero is a line the point stands on, so a basis is a corner, and a pivot is a move along one edge to the next corner. It visits only corners that improve zz, instead of every crossing. The figure's Simplex mode draws its walk on the plane.

Why is the price of electricity a shadow price?

The market operator clears the market as a linear programme: meet the demand at the least cost of the offers, within each generator's and each line's limits. The price at a bus is the shadow price of that bus's balance, what one more MW of demand there would cost the market: 70 €/MWh in example D, the offer of the marginal generator. With a congested line, each bus gets its own price.

Where it's used

Linear programming is the workhorse of operations research: production planning, blending and diet problems, transport and assignment, scheduling. In power systems it clears the day-ahead electricity markets, where the network enters as the DC power flow's linear constraints (a DC optimal power flow) and the shadow prices of the buses' balances are the locational marginal prices; and it is the relaxation inside the mixed-integer unit commitment that decides which plants start. Real problems have thousands to millions of variables and are solved by the simplex method or by interior-point methods (HiGHS, Gurobi, CPLEX). The graphical method is where the ideas are first seen: the region, its corners, the four outcomes and the prices.

It belongs to three courses, two of them mine; their materials are linked below. Energy Markets, my UPC course, teaches it as the tool that clears a market. In T2: Optimization of electricity market operations, "1. Mathematical and Computational Optimization.pdf" (in Course site as "OEMO 1 Mathematical Optimization r20.pdf") writes a linear programme with ≤, ≥ and = rows, draws one in two variables with its level lines, and gives the optimum by the KKT conditions, whose multipliers it calls Lagrange multipliers, dual variables or marginal prices, zero on a row that doesn't bind: equation (5) and complementary slackness, in its words. "2. Market Clearing Models.pdf" (in Course site as "OEMO 2 market clearing models r13.pdf") clears the auction as a programme that maximises social welfare, the price the multiplier of the balance and, on a network, a nodal price at each bus and the merchandising surplus between them: the market example. In the same folder are my hand-ins: Exercise 1 - optimality conditions for LP writes the KKT conditions of a two-variable programme, draws its region, slides the objective's level lines to the best corner and reads the multipliers from GAMS's MARGINAL column, this page's method by hand; Exercise 2 solves a programme with a whole-number variable beside its linear relaxation, the subject of whole numbers only. In Exercise solutions, ex1_LP1 to ex6_TCMPA are the six exercises solved in GAMS, the last on two buses across one limited line; in Exams, Part 2's papers ask for a dispatch's KKT conditions and an auction's prices by hand; and Group project 2: market clearing with a wind power plant holds my group's GAMS model, five generators and three demands bidding in blocks over five periods, with a wind plant that offers at zero and lowers the price. Power Systems comes to it from the network: in Retake, "T3_00_Introduction_2024.pdf" names economic dispatch, unit commitment and the optimal load flow among a power system's studies and shows the daily sequence of the electricity markets, and "T3_05_Matpower introduction.pdf" enters the generators' costs Matpower needs for an optimal power flow. The network constraints of such a flow are the DC power flow's, the linear form of the course's load flow.

Andrija T. Sarić taught it in Power System Operation, whose lectures are on the site as text in Serbian. Lecture 13, Optimalni tokovi snaga (OPF) i linearno programiranje, solves the optimal power flow as a chain of linear programmes on the DC model, each by a dual or primal algorithm of linear programming. Lecture 15, Tržište električne energije, deregulacija, ISO i LMP, clears the market as a linear programme that maximises social welfare, reads the price of energy as the dual variable of the power balance, and builds each bus's locational marginal price from it and the congested lines' dual variables, weighted by the shift factors. Lectures 09, 11 and 12, Ekonomski dispečing i optimalno angažovanje agregata, name linear programming as the method most used for committing units.

  • DC power flow

    gives the network constraints a DC optimal power flow adds to a market like this one, and with them a price at every bus

Further reading

References

  • G. B. Dantzig, Linear Programming and Extensions, Princeton University Press, 1963
  • F. S. Hillier, G. J. Lieberman, Introduction to Operations Research, 11th ed., McGraw-Hill, 2021
  • R. J. Vanderbei, Linear Programming: Foundations and Extensions, 5th ed., Springer, 2020
  • D. Bertsimas, J. N. Tsitsiklis, Introduction to Linear Optimization, Athena Scientific, 1997
  • V. Klee, G. J. Minty, “How good is the simplex algorithm?”, in O. Shisha (ed.), Inequalities III, Academic Press, 1972
  • N. Karmarkar, “A new polynomial-time algorithm for linear programming”, Combinatorica, 4(4), 1984
  • D. S. Kirschen, G. Strbac, Fundamentals of Power System Economics, 2nd ed., Wiley, 2018
  • F. D. Galiana, A. J. Conejo, “Economics of Electricity Generation”, in A. Gómez-Expósito, A. J. Conejo, C. Cañizares (eds.), Electric Energy Systems: Analysis and Operation, CRC Press, 2009