Documentation

CslibFrontier.Algorithms.Lean.Simplex.TwoPhase

The two-phase simplex method #

This module formalizes the two-phase simplex method ([Chv83], Chapter 3) for linear programs max c ⬝ᵥ x subject to x ≥ 0, A *ᵥ x ≤ b over a linearly ordered field K, with no sign restriction on b.

Algorithm #

Given A : Matrix (Fin m) (Fin n) K, b : Fin m → K and c : Fin n → K:

  1. Phase I. Form the auxiliary program max -x₀ subject to A *ᵥ x - x₀ • 1 ≤ b, x ≥ 0, x₀ ≥ 0, whose slack dictionary is auxDictionary A b on Fin (n + 1 + m) (with x₀ at Fin.castAdd m (Fin.last n)). If some b i < 0, phaseOneDict A b performs the special first pivot entering x₀ and leaving the slack of the smallest row minimizing b i, producing a feasible dictionary. Otherwise auxDictionary A b is already feasible.
  2. Phase I solve. Run Bland's rule (Dictionary.simplex) on phaseOneDict A b. Because the auxiliary objective -x₀ is bounded above by 0, the unbounded and out-of-fuel branches are unreachable.
    • If the optimal auxiliary value is < 0, the original program is infeasible, and D.slackDual is a Farkas certificate (0 ≤ y, 0 ≤ y ᵥ* A, y ⬝ᵥ b < 0).
    • Otherwise the optimal auxiliary value is 0. If x₀ is still basic, forcePivotX₀ D performs a degenerate pivot removing x₀ from the basis (a nonzero row coefficient always exists because x₀ can vary freely in the affine solution space of auxDictionary A b). Then dropX₀ deletes column x₀ and installs the original objective c, producing a feasible dictionary equivalent to slackDictionary A b c.
  3. Phase II. Run Bland's rule on the resulting Phase II dictionary. The out-of-fuel branch is unreachable by Dictionary.simplex_ne_outOfFuel; an optimal dictionary yields a primal–dual optimal pair (x, y) with c ⬝ᵥ x = y ⬝ᵥ b, and an unbounded dictionary yields a feasible point x and recession ray d with 0 ≤ d, A *ᵥ d ≤ 0, 0 < c ⬝ᵥ d.

Main definitions #

Main results #

Limitations #

The method is a pure Lean function over an exact linearly ordered field; no machine-level running time or bit complexity is certified.

References #

The certified outcome of the two-phase simplex method on a linear program with m constraints and n variables over K.

  • infeasible {K : Type u_1} {m n : ℕ} (y : Fin m → K) : TwoPhaseResult K m n

    Infeasible, certified by a Farkas vector y with 0 ≤ y, 0 ≤ y ᵥ* A, y ⬝ᵥ b < 0.

  • unbounded {K : Type u_1} {m n : ℕ} (x d : Fin n → K) : TwoPhaseResult K m n

    Unbounded, certified by a feasible point x and ray d with 0 ≤ d, A *ᵥ d ≤ 0, 0 < c ⬝ᵥ d.

  • optimal {K : Type u_1} {m n : ℕ} (x : Fin n → K) (y : Fin m → K) : TwoPhaseResult K m n

    Optimal, certified by a primal optimum x and a dual optimum y with c ⬝ᵥ x = y ⬝ᵥ b.

def Cslib.Algorithms.Lean.Simplex.auxDictionary {K : Type u_1} [Field K] {m n : ℕ} (A : Matrix (Fin m) (Fin n) K) (b : Fin m → K) :
Dictionary (Fin (n + 1 + m)) K

The slack dictionary of the Phase I auxiliary program max -x₀ subject to A *ᵥ x - x₀ • 1 ≤ b, x ≥ 0, x₀ ≥ 0 ([Chv83], Chapter 3), with x₀ at Fin.castAdd m (Fin.last n). Exposed so twoPhase reduces in the kernel.

def Cslib.Algorithms.Lean.Simplex.phaseOneDict {K : Type u_1} [Field K] [LinearOrder K] {m n : ℕ} (A : Matrix (Fin m) (Fin n) K) (b : Fin m → K) :
Dictionary (Fin (n + 1 + m)) K

The initial feasible Phase I dictionary ([Chv83], Chapter 3): if some b i < 0, pivot x₀ into the basis and the slack of the smallest row minimizing b i out; otherwise auxDictionary A b is already feasible. Exposed so twoPhase reduces in the kernel.

def Cslib.Algorithms.Lean.Simplex.forcePivotX₀ {K : Type u_1} [Field K] [LinearOrder K] {m n : ℕ} (D : Dictionary (Fin (n + 1 + m)) K) :
Dictionary (Fin (n + 1 + m)) K

If the auxiliary variable x₀ is still basic at the end of Phase I, pivot it out using the smallest nonbasic variable with a nonzero coefficient in its row ([Chv83], Chapter 3). Exposed so twoPhase reduces in the kernel.

def Cslib.Algorithms.Lean.Simplex.dropX₀ {K : Type u_1} [Field K] {m n : ℕ} (D : Dictionary (Fin (n + 1 + m)) K) (c : Fin n → K) :
Dictionary (Fin (n + m)) K

Drop the auxiliary variable x₀ from a Phase I dictionary (with x₀ ∉ D.basis) and install the original objective c ⬝ᵥ x expressed in terms of the nonbasic variables ([Chv83], Chapter 3). Exposed so twoPhase reduces in the kernel.

def Cslib.Algorithms.Lean.Simplex.twoPhase {K : Type u_1} [Field K] [LinearOrder K] {m n : ℕ} (A : Matrix (Fin m) (Fin n) K) (b : Fin m → K) (c : Fin n → K) :

The two-phase simplex method with Bland's rule ([Chv83], Chapter 3; [Bla77]) for max c ⬝ᵥ x subject to x ≥ 0, A *ᵥ x ≤ b with arbitrary b. It is total and terminating, returning an infeasibility, unboundedness or primal–dual optimality certificate (twoPhase_spec); the Phase I unbounded and out-of-fuel branches are unreachable.

Phase I invariants #

theorem Cslib.Algorithms.Lean.Simplex.phaseOneDict_feasible {K : Type u_1} [Field K] [LinearOrder K] [IsStrictOrderedRing K] {m n : ℕ} (A : Matrix (Fin m) (Fin n) K) (b : Fin m → K) :

Phase I initialization is always feasible ([Chv83], Chapter 3): for arbitrary b, phaseOneDict A b is a feasible dictionary.

phaseOneDict A b is equivalent to auxDictionary A b.

Phase I → Phase II transition #

Phase II unboundedness certificate #

Main specification and corollaries #

theorem Cslib.Algorithms.Lean.Simplex.twoPhase_spec {K : Type u_1} [Field K] [LinearOrder K] [IsStrictOrderedRing K] {m n : ℕ} (A : Matrix (Fin m) (Fin n) K) (b : Fin m → K) (c : Fin n → K) :
match twoPhase A b c with | TwoPhaseResult.infeasible y => 0 ≤ y ∧ 0 ≤ Matrix.vecMul y A ∧ y ⬝ᵥ b < 0 | TwoPhaseResult.unbounded x d => 0 ≤ x ∧ A.mulVec x ≤ b ∧ 0 ≤ d ∧ A.mulVec d ≤ 0 ∧ 0 < c ⬝ᵥ d | TwoPhaseResult.optimal x y => 0 ≤ x ∧ A.mulVec x ≤ b ∧ 0 ≤ y ∧ c ≤ Matrix.vecMul y A ∧ c ⬝ᵥ x = y ⬝ᵥ b

Correctness and termination of the two-phase simplex method ([Chv83], Chapter 3): for arbitrary A, b and c over a linearly ordered field, twoPhase A b c returns a valid Farkas infeasibility certificate, an unboundedness ray certificate, or an optimal primal–dual pair with equal objective values.

theorem Cslib.Algorithms.Lean.Simplex.feasibleSet_eq_empty_of_twoPhase_eq_infeasible {K : Type u_1} [Field K] [LinearOrder K] [IsStrictOrderedRing K] {m n : ℕ} {A : Matrix (Fin m) (Fin n) K} {b : Fin m → K} {c : Fin n → K} {y : Fin m → K} (h : twoPhase A b c = TwoPhaseResult.infeasible y) :
{x : Fin n → K | 0 ≤ x ∧ A.mulVec x ≤ b} = ∅

An infeasible result of twoPhase certifies that the primal feasible set is empty.

theorem Cslib.Algorithms.Lean.Simplex.not_bddAbove_of_twoPhase_eq_unbounded {K : Type u_1} [Field K] [LinearOrder K] [IsStrictOrderedRing K] {m n : ℕ} {A : Matrix (Fin m) (Fin n) K} {b : Fin m → K} {c x d : Fin n → K} (h : twoPhase A b c = TwoPhaseResult.unbounded x d) :
¬BddAbove ((fun (x : Fin n → K) => c ⬝ᵥ x) '' {x : Fin n → K | 0 ≤ x ∧ A.mulVec x ≤ b})

An unbounded result of twoPhase certifies that the objective is not bounded above on the primal feasible set.

theorem Cslib.Algorithms.Lean.Simplex.isGreatest_isLeast_of_twoPhase_eq_optimal {K : Type u_1} [Field K] [LinearOrder K] [IsStrictOrderedRing K] {m n : ℕ} {A : Matrix (Fin m) (Fin n) K} {b : Fin m → K} {c x : Fin n → K} {y : Fin m → K} (h : twoPhase A b c = TwoPhaseResult.optimal x y) :
IsGreatest ((fun (x : Fin n → K) => c ⬝ᵥ x) '' {x : Fin n → K | 0 ≤ x ∧ A.mulVec x ≤ b}) (c ⬝ᵥ x) ∧ IsLeast ((fun (x : Fin m → K) => x ⬝ᵥ b) '' {y : Fin m → K | 0 ≤ y ∧ c ≤ Matrix.vecMul y A}) (y ⬝ᵥ b)

An optimal result of twoPhase certifies both that c ⬝ᵥ x is the primal maximum (IsGreatest) and that y ⬝ᵥ b is the dual minimum (IsLeast).