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
The maximum: z = 36 at (2, 6), the corner where line 2 and line 3 meet.
Start from
z =x +y
| Where lines meet | x | y | z |
|---|---|---|---|
| x ≥ 0 ∩ y ≥ 0 | 0 | 0 | 0 |
| 1 ∩ y ≥ 0 | 4 | 0 | 12 |
| 1 ∩ 3 | 4 | 3 | 27 |
| 2 ∩ 3 | 2 | 6 | 36 (the maximum) |
| 2 ∩ x ≥ 0 | 0 | 6 | 30 |
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, and :
Each inequality holds on one side of its line , 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 is maximising , so most of what follows is said for a maximum. One convention runs through the page: a constraint's shadow price is the change of the best , as the problem writes , for one unit more of its right side (equation (4) below), so it is at least zero on a row of a maximisation and on a 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:
- If , only the first quadrant counts.
- For each constraint, draw its line : where it crosses the axes is enough, and , and a line with or 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.
- What is left after every constraint is the feasible region. If nothing is left, the problem is infeasible and you stop.
- 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.
- Work out at every corner. The best is the optimum, if the region is bounded.
- Check it with the objective's line: draw for some and slide it parallel to itself in the direction improves. The last point of the region it touches is the optimum. If it never stops touching the region, there is no optimum.
- Write the answer: one corner, a whole edge (two corners tie), or no optimum.
Two lines cross where both hold at once:
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: " means above the line" is true only when , and and 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, , form a straight line, and for other values of the lines are parallel to it; grows in the direction of . 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 lines there are at most 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 decides, never the distance from the origin. Over the product mix with , 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 , and so does every point between them.
- Unbounded: inside the region 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 that no constraint stops, and the problem is unbounded only if improves along one of them:
The class 12 rule says the same in words: on an unbounded region, the best corner value is the maximum only if no feasible point gives more than , which you test by drawing 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 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:
In the figure's Explore, drag a line of the product mix and watch : line 3, , moved out by one unit, takes the optimum along line 2 and raises by 1; line 2 is worth 1.5 a unit; line 1, , 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 is a weighted sum of the rows of the two lines through it, and the weights are their prices:
In Explore the two rows are drawn as arrows from the optimum, at right angles to their lines, with 's arrow between them. A corner of a maximum over rows is optimal exactly while 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 ; past it line 3 has room to spare and is worth nothing. Move it in and the corner slides until it reaches the axis at ; under it each unit is worth 2.5. So is a chain of straight pieces, and the price is the slope of the piece the present sits on, each unit worth no more than the one before it: 2.5, then 1, then 0. At 18, ; at 24, 42; at 30, still 42.
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 the room left on row at the optimum,
The objective's coefficients have ranges too: the present corner stays optimal while stays between the two rows' arrows. For the product mix, (2, 6) stays best while 's coefficient is from 0 to 7.5 and 's is 2 or more. At 7.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):
| Who | What it calls it | Against this page |
|---|---|---|
| This page | shadow price | |
| SciPy linprog (HiGHS) | marginals of ineqlin and eqlin | the derivative of the objective linprog minimises, for rows written : a maximisation solved as minimise turns the sign, and a row entered as turns it again |
| GAMS | an equation's marginal, .m | the same as here for the row as written; a balance written generation less demand gives the price as .m |
| Excel Solver | Shadow Price, with Allowable Increase and Allowable Decrease | the same; the range is given as two distances from the right side |
| Gurobi | Pi, the dual value; SARHSLow and SARHSUp | the same, in a minimisation and a maximisation |
| LINDO, LINGO | dual price | how much the objective improves: the same in a maximisation, the opposite sign in a minimisation |
| Hillier and Lieberman | shadow price , the marginal value of resource | the same (maximise, rows) |
| Energy markets courses | Lagrange multiplier, dual variable, marginal price | the same; the price of a bus is its balance's multiplier |
When three lines meet
Add to the product mix. The mix reaches at most, at its optimum (2, 6), so for 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 the optimum moves along line 2 to (1, 6), , 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 dual
The prices are themselves the answer to a linear programme, the dual of the first, the primal. For a maximisation over rows with , it has one unknown for each constraint and one constraint for each variable:
Read the dual as a buyer's offer: is what the buyer pays for a unit of resource , 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 row of a maximisation has , an equality has of any sign, and the dual of a minimisation is a maximisation with the inequalities turned. For any feasible pair, :
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 are the primal's shadow prices. The product mix's dual is to minimise over and ; its optimum is , 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 (line 1), and a primal variable above zero has its dual row tight (both are here: and , 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 over and . Its optimum is 26 at (2, 6), and its dual, to minimise over and , has 26 at (1, 2). With , 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.
The simplex method, seen
The simplex method turns each inequality into an equality with a slack variable, , the room left on row (a surplus for a row). Now every one of , and the slacks is at least zero, and a variable at zero is a line the point stands on: is the axis, is line . 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 .
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: enters, so the point leaves the axis and runs up the axis; line 2 stops it at (0, 6) and line 3 would at (0, 9), so the nearer wins and leaves.
| Rule | The walk | Pivots |
|---|---|---|
| Largest coefficient (Dantzig) | (0, 0) → (0, 6), z 30 → (2, 6), z 36 | y in, s₂ out; x in, s₃ out |
| First that helps (Bland) | (0, 0) → (4, 0), z 12 → (4, 3), z 27 → (2, 6), z 36 | x 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 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 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 over and : 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 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 ( or ), 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.
Where it comes from
The corner point theorem. On a segment from to , a linear is for 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, with and ; so 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 .
Why (5) is the test of a corner. At a corner where lines and meet, the ways to move that stay inside are the directions with and (for rows): a wedge. is optimal if no direction in the wedge raises , that is for all of them. Write , which two crossing lines always allow; then , which is at most zero for every in the wedge exactly when .
From (5), the rest. The prices: move by and the corner moves by some with and , so moves by : 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 and (with equality where and are above zero, where an axis is one of the two lines), so they are feasible for (7), and : 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
- 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.
- 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.
- 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.
- 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, and 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 subject to , , , . The lines: is upright, level at , and runs through (6, 0) and (0, 9). The origin satisfies all three, so each keeps the origin's side. The region's corners and at each:
| Corner | |
|---|---|
| (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), . Set it up in the figure from Product mix.
B. A minimisation: the diet, and an unbounded region
Two foods, and portions, costing 3 and 2; each portion of gives 2, 1 and 3 units of three nutrients, each of 1, 2 and 1, and at least 6, 8 and 9 are needed; at most 10 portions in all. Minimise subject to , , , , . The origin breaks the three rows, so each keeps the side away from it. The corners:
| Corner | |
|---|---|
| (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 . 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 . Asked for the maximum instead, the largest corner value is 24, but there is no maximum: 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: hectares of wheat and of maize. Each limited thing is a constraint, written in its own unit: land, hectares; work, hours; and neither can be negative. The objective is the money: maximise .
| Corner | , € |
|---|---|
| (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 and that meet the demand at the least cost:
The equality leaves no area: the feasible region is the segment of the line between two corners, one where generator 2 is at its limit and one where generator 1 is (start the figure from Market, two generators):
| Corner | , MW | , MW | Cost, €/h |
|---|---|---|---|
| Generator 1 at its limit | 300 | 100 | 16,000 |
| Generator 2 at its limit | 150 | 250 | 22,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 . Add it in the figure (a constraint ): 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.
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
The mistake
" means above the line": shading by the sign.
Why it's wrong
and are the same half-plane, and one of them is shaded the wrong way by the rule. The sign says above only when .
The right one
Test a point not on the line, the origin if you can, and keep its side if the inequality holds there.
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 (it gives 24).
The right one
A corner is a crossing that satisfies every constraint. Check each one against all of them.
The mistake
The corner farthest from the origin is the maximum.
Why it's wrong
With 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 decides: evaluate at every corner, or slide the objective's line.
The mistake
An unbounded region has no optimum.
Why it's wrong
The diet without runs on for ever, and its minimum is 12 at (2, 3).
The right one
An unbounded region has no optimum only if improves along one of the directions it runs off in, (3).
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: grows without end.
The right one
The best corner value is the answer only if no feasible point does better than .
The mistake
A tie is reported as one corner.
Why it's wrong
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 , the whole edge between them is optimal: say so.
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.
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.
The mistake
A price for a constraint that isn't binding.
Why it's wrong
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).
The mistake
One price where three lines meet.
Why it's wrong
With 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.
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.
The mistake
Assuming 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/MWhSciPy'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 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 for any and slide it parallel to itself in the direction 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: means above the line only when the coefficient of 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 , then ask whether any feasible point does better, by (3) or by drawing 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 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 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 moves, or write 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 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 . 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.
How is the simplex method related to the graphical method?
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 , 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.
- Energy Markets
Course materials, UPC Barcelona
- Power Systems
Course materials, UPC Barcelona
- Power System Operation
Course materials, Prof. dr Andrija T. Sarić
Connects to
- 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
- lpviz: Interactive Linear Programming Visualizationlpviz.net
Draw a region by its corners and watch the simplex, interior-point, PDHG and active-set methods step across it, with the central path; the place to see the algorithms this page only names.
- GILP: an interactive tool for visualizing the simplex algorithmgithub.com
A Python package that draws the simplex's iterations in two and three dimensions with the tableau beside each, and branch and bound as a tree of regions.
- R. J. Vanderbei, Linear Programming: Foundations and Extensionsvanderbei.princeton.edu
The book's own page: its programs, and the author's online pivot tool, in which you pivot a dictionary of any size by clicking.
- SciPy, scipy.optimize.linprogdocs.scipy.org
The reference for the Python on this page: the problem's form, the methods, and the marginals it returns as the prices of the minimised objective.
- HiGHShighs.dev
The open-source solver behind linprog, for linear and mixed-integer programmes of millions of variables, with its simplex and interior-point methods.
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