Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Linear programming: the simplex method

ProblemMinimise a linear function subject to linear equalities and inequalities and bounds on the variables
Devised byLeonid Kantorovich (1939) and Tjalling Koopmans (1940s), formulation; George Dantzig (1947), the simplex method; John von Neumann (1947), duality; Robert Bland (1977), the anti-cycling rule
AccuracyExact in exact arithmetic: the method finds an optimal vertex, or proves the problem infeasible or unbounded, in finitely many steps
CostEach pivot costs for constraints and variables; the number of pivots is usually a small multiple of , but exponential in the worst case
Read firstConstrained optimisation: SQP and the augmented Lagrangian method
APILinearProgram, LpSolution, LpStatus

Overview

A linear program (LP) optimises a linear objective subject to linear constraints:

Linear programming is the workhorse of operations research: production planning, transportation and logistics, blending, scheduling, network flows, and the relaxations used to solve integer programs. It is solved routinely with millions of variables.

The constraints define a convex polyhedron, and a linear function on a polyhedron attains its minimum (if it has one) at a vertex. The simplex method moves from vertex to adjacent vertex, improving the objective at each step, until no neighbour is better.

Where it is used

Linear programming is the foundation of operations research.

  • Production and logistics. Choosing quantities to produce, routes for trucks or blends of ingredients at the lowest cost within capacity limits.
  • Energy. Scheduling power plants and blending fuels.
  • Network flows and assignment. Special cases include shortest paths and maximum flow.
  • Engineering. Chebyshev (minimax) filter design and approximation reduces to linear programming.
  • Nutrition and finance. The diet problem, and portfolios with linear constraints.

History

Leonid Kantorovich formulated linear programming in 1939 to plan production at a Leningrad plywood trust, and Tjalling Koopmans developed it for transportation problems during the Second World War; they shared the 1975 Nobel Prize in economics. George Dantzig, planning for the US Air Force, invented the simplex method in 1947. On hearing of it John von Neumann immediately described duality, drawing on his theory of games. The method was one of the first great applications of computers.

Its efficiency puzzled theorists for decades. Victor Klee and George Minty (1972) built problems on which it visits all vertices, but in practice it takes a small multiple of pivots, a discrepancy explained by Daniel Spielman and Shang-Hua Teng’s smoothed analysis (2001). Leonid Khachiyan’s ellipsoid method (1979) proved that LP is solvable in polynomial time, and Narendra Karmarkar’s interior-point method (1984) made polynomial-time methods practical. E. M. L. Beale (1955) had shown that the textbook simplex method can cycle forever on degenerate problems; Robert Bland’s rule (1977) is the simplest guaranteed cure.

The idea

The simplex method

The constraints of a linear program cut out a region with flat faces, the feasible region: a polygon in two dimensions, a polyhedron in general. The objective is linear, so its contours are parallel lines (or planes), and sliding a contour in the direction of improvement as far as it will go ends at a corner of the region. An optimum, if there is one, is always attained at a vertex. The simplex method exploits this: it starts at a vertex and repeatedly moves along an edge to a neighbouring vertex with a better objective, until no neighbour is better.

Figure 5.10. A linear program in two variables.

In the figure (the “Wyndor Glass” problem of Hillier and Lieberman’s textbook), the feasible region is the polygon, the dashed lines are contours of the objective, and the optimum is the vertex where the contour last touches the region.

To turn this picture into arithmetic we need an algebraic description of a vertex, a test for optimality, and a rule for moving.

Standard form. First the program is rewritten with equality constraints and non-negative variables. An inequality such as becomes the equation with a new slack variable , which measures how far the constraint is from being tight. A variable that may be negative is shifted, or split into the difference of two non-negative ones. The result is

with of size and more variables than equations ().

Vertices. With unknowns and equations there are degrees of freedom. A vertex is a point where as many variables as possible are zero, that is, where of the constraints are tight. So: choose of the columns of , forming an invertible matrix ; call the corresponding variables basic; set all the other (non-basic) variables to zero; and solve for the basic ones. This is a basic solution, and it is a vertex of the feasible region if in addition , a basic feasible solution.

The optimality test. Standing at a vertex, consider increasing one non-basic variable from zero while keeping the other non-basic variables at zero. The basic variables must change to keep satisfied: , where is column of . Substituting into the objective,

The number is the reduced cost of variable : the rate at which the objective changes as increases, once the forced changes in the basic variables are accounted for. If every , moving along any edge makes the objective worse, and the vertex is optimal.

Moving to a neighbour. Otherwise pick a variable with to enter the basis, and increase it. As it grows, the basic variables change linearly, and some of them decrease. The first one to reach zero stops the move, since it must not become negative; it leaves the basis. Finding it means comparing, for each decreasing basic variable, its current value with its rate of decrease, which is called the ratio test. We have arrived at a neighbouring vertex. If no basic variable decreases, can grow for ever and the objective decreases without bound: the program is unbounded.

Pivoting. In practice is never formed. The method keeps the tableau in a form where the basic columns are columns of the identity, so that the basic solution and the reduced costs can be read off. Exchanging the entering and leaving variables is one step of Gauss-Jordan elimination on the tableau, called a pivot.

In the Wyndor problem, starting from the origin, the textbook rule (enter the variable with the most negative reduced cost) moves to and then to the optimum. The library, with Bland’s rule and its phase one, takes three pivots in all.

Two phases

The simplex method needs a vertex to start from. When all constraints are of the form with , the origin is one, with the slack variables as the basis. In general a first basic feasible solution is not obvious, and finding one is itself a linear program.

Phase one adds an artificial variable to each constraint, so that row reads . Now , is an obvious vertex of the enlarged problem. From there the simplex method minimises the sum of the artificials.

  • If the minimum is positive, the artificials cannot all be driven to zero, which means the original constraints cannot all be satisfied: the LP is infeasible.
  • If the minimum is zero, every artificial is zero and the remaining variables satisfy the original constraints. After pivoting out any artificials that are still in the basis at value zero, the final basis is a basic feasible solution of the original problem.

Phase two discards the artificials and optimises the true objective from that vertex.

Degeneracy and Bland’s rule

The argument that the simplex method terminates is that every pivot improves the objective, so no vertex is visited twice, and there are finitely many vertices. The argument has a gap.

A vertex is degenerate when a basic variable happens to be zero, which occurs when more constraints than necessary pass through the same point. At a degenerate vertex the ratio test can stop the entering variable immediately, at zero. The pivot then changes which variables are called basic without moving the point or improving the objective. A sequence of such pivots can return to an earlier basis and repeat for ever. This is cycling, and the textbook rule of entering the most negative reduced cost is vulnerable to it.

Bland’s rule closes the gap: among the candidates, always choose the entering variable of lowest index and the leaving variable of lowest index. Bland proved that with this rule no basis is ever repeated. It is slower on average than smarter rules, but simple and safe; this library uses it.

The library’s results on a few programs:

Table 5.10. Linear programs solved by the simplex method.

programstatussolutionobjectivepivots
Wyndor: max (above)Optimal(2, 6)36.00003
transportation, 2 × 3 (min cost)Optimal(0, 20, 0, 10, 5, 15)465.00006
Beale’s cycling example (min)Optimal(0.04, 0, 1, 0)-0.05006
infeasible: , Infeasiblen/an/a1
unbounded: max , Unboundedn/an/a1
free variable: min as min , Optimal(3, 0)0.00002

Beale’s example is a famous degenerate problem on which the textbook rule cycles; with Bland’s rule it is solved in six pivots, to its optimum . The last program shows how an absolute value is minimised by linear programming, with a free variable split into two non-negative ones.

Duality

Every linear program has a partner, its dual. For the primal problem

the dual is

There is one dual variable for each primal constraint and one dual constraint for each primal variable. The two problems bound each other. If is feasible for the primal and for the dual, then

using with for the first inequality and with for the second. Every feasible dual value is a lower bound on every feasible primal value (weak duality). The deeper result, strong duality, is that at the optimum the two values are equal, so a dual solution is a certificate that a primal solution cannot be improved.

The optimal dual variables have a practical meaning: is the rate at which the optimal cost changes when the right-hand side is changed, the shadow price of constraint . They are the same as the Lagrange multipliers of constrained optimisation.

Algorithm

Input: an objective to minimise, constraints of the forms , and , and bounds on the variables.

  1. Convert to standard form, so that every variable is non-negative and every constraint an equation with a non-negative right-hand side:
    • a variable with a finite lower bound is replaced by ;
    • a variable with only an upper bound is replaced by ;
    • a free variable is replaced by the difference of two non-negative ones;
    • a finite upper bound on a shifted variable becomes an extra constraint row;
    • each inequality receives a slack variable;
    • a row with a negative right-hand side is multiplied by .
  2. Phase one. Add an artificial variable to each row, take them as the starting basis, and minimise their sum by simplex pivots. If the minimum is positive, report the program as infeasible.
  3. Clean the basis. An artificial variable may remain basic at value zero. Pivot it out on any nonzero entry of its row in a real column; if the row has none, it is a redundant constraint and is dropped.
  4. Phase two. Restore the true objective and pivot:
    • compute the reduced costs; if all are non-negative, the basis is optimal;
    • otherwise the entering variable is the one of lowest index with a negative reduced cost (Bland’s rule);
    • apply the ratio test to its column; if no entry is positive, report the program as unbounded; otherwise the leaving variable is the one with the smallest ratio, ties broken by lowest index;
    • pivot, and repeat.
  5. Map back. Undo the substitutions of step 1 to express the solution in the original variables.

A pivot on a tableau with rows and columns costs operations. The number of pivots is usually a small multiple of , although in the worst case it can be exponential.

Implementation

        // Reduced costs: c_j - c_B · column_j.
        let entering = (0..limit).find(|&j| {
            let reduced = cost[j] - basis.iter().enumerate().map(|(i, &b)| cost[b] * t[i][j]).sum::<f64>();
            reduced < -eps
        });
        let Some(col) = entering else { return Some(LpStatus::Optimal) };
        // Ratio test with Bland's tie-break (lowest basic variable index).
        let mut best: Option<(f64, usize, usize)> = None;
        for (i, row) in t.iter().enumerate() {
            if row[col] > eps {
                let ratio = row[total] / row[col];
                let better = match best {
                    None => true,
                    Some((r, _, b)) => ratio < r - 1e-12 || (ratio <= r + 1e-12 && basis[i] < b),
                };
                if better {
                    best = Some((ratio, i, basis[i]));
                }
            }
        }
        let Some((_, row, _)) = best else { return Some(LpStatus::Unbounded) };
        pivot(t, basis, row, col, total);
  • Programs are built with LinearProgram::new(c) and the with_less_equal, with_greater_equal, with_equal, with_bounds and with_free_variable methods; variables default to . To maximise, negate the objective.
  • Infeasible and unbounded programs are reported through LpSolution::status, not as errors.
  • The dense tableau suits problems of up to a few hundred variables and constraints. Large, sparse problems need the revised simplex method with LU factorisation, or interior-point methods.
  • A safety limit of 50 000 pivots, which Bland’s rule makes unreachable in exact arithmetic, used to return the current basis as optimal when hit; it is now an error.

In practice

//! Linear programming by the simplex method.
//!
//! Run with `cargo run -p lib_jsl_optimization --example optimization_linear_programming`.

use lib_jsl_optimization::linear_programming::{LinearProgram, LpStatus};

fn main() {
    // A diet problem: choose servings of bread, milk and cheese, costing 2, 3.5 and 8 each, to
    // provide at least 300 calories (65, 120 and 106 per serving) and 20 g of protein (2, 8 and
    // 7 g per serving) at the least cost.
    let lp = LinearProgram::new(vec![2.0, 3.5, 8.0])
        .with_greater_equal(vec![65.0, 120.0, 106.0], 300.0)
        .with_greater_equal(vec![2.0, 8.0, 7.0], 20.0)
        .with_bounds(1, 0.0, 2.0); // at most two servings of milk
    let s = lp.solve().unwrap();
    assert_eq!(s.status, LpStatus::Optimal);
    println!("servings {:?}, cost {:.4}, {} pivots", s.x, s.objective, s.iterations);

    // Infeasible and unbounded programs are reported through the status.
    let impossible = LinearProgram::new(vec![1.0]).with_greater_equal(vec![1.0], 5.0).with_bounds(0, 0.0, 2.0);
    println!("x ≥ 5 with x ≤ 2: {:?}", impossible.solve().unwrap().status);
}

Strengths

  • Finds the global optimum, or proves infeasibility or unboundedness, in finitely many steps.
  • Bland’s rule makes it immune to cycling.

Weaknesses

  • Dense tableau: memory and time grow as per pivot.
  • Bland’s rule takes more pivots than the rules used in production solvers.
  • Exponential worst case (Klee-Minty), though rarely seen in practice.

Exercises

  1. Solve the Wyndor problem by hand with the simplex tableau, starting from the origin.
  2. Write the dual of the Wyndor problem and verify strong duality.
  3. Construct the Klee-Minty cube for and count the pivots taken by the most-negative-reduced-cost rule and by Bland’s rule.
  4. Formulate the problem of fitting a line minimising the largest absolute residual (the minimax or Chebyshev fit) as an LP, and solve it for a few points.
  5. The transportation problem in the table has integer data. Why is its optimal solution integral?
Hints and answers
  1. The Wyndor problem is subject to , , . Starting with the three slack variables basic, enter (the most negative reduced cost, ), which the second row limits to ; then enter , limited by the third row to . The optimum is with value .
  2. The dual is subject to , , . Its optimum is with value , equal to the primal value.
  3. Dantzig’s rule on the Klee-Minty cube visits all vertices, i.e. 7 pivots; count Bland’s rule’s pivots on the same data and compare.
  4. Minimise over subject to for every : two linear inequalities per point.
  5. The constraint matrix of a transportation problem is totally unimodular, so every vertex of the feasible region has integer coordinates when the supplies and demands are integers, and the simplex method ends at a vertex.

Further reading

  • G. B. Dantzig, Linear Programming and Extensions, Princeton University Press, 1963.
  • V. Chvátal, Linear Programming, W. H. Freeman, 1983. Contains the version of Beale’s example used here.
  • R. G. Bland, “New finite pivoting rules for the simplex method”, Mathematics of Operations Research 2 (1977) 103–107.
  • F. S. Hillier and G. J. Lieberman, Introduction to Operations Research, McGraw-Hill (many editions).
  • D. A. Spielman and S.-H. Teng, “Smoothed analysis of algorithms: why the simplex algorithm usually takes polynomial time”, Journal of the ACM 51 (2004) 385–463.