Rational primal simplex and cyclic entering-variable selection
Definitionfriedmann_simplexRational standard-form LPs, nonsingular feasible bases and their actual basic solutions, reduced costs, minimum-ratio pivots, boundedness, and Cunningham entering-variable order with a retained cursor.
import Mathlib.LinearAlgebra.Matrix.NonsingularInverse
import Mathlib.Data.Rat.Cast.Order
set_option autoImplicit false
/-! Rational standard-form primal simplex: maximize cᵀx subject to Ax=b, x≥0.
No pivot rule or lower bound is included in the data. The inverse is Mathlib's
total matrix inverse; every permitted basis explicitly has nonzero determinant.
-/
namespace FriedmannSimplex
open scoped BigOperators
open Matrix
structure LP (m n : ℕ) where
A : Matrix (Fin m) (Fin n) ℚ
b : Fin m → ℚ
c : Fin n → ℚ
abbrev Basis (m n : ℕ) := Fin m → Fin n
def basisMatrix {m n : ℕ} (P : LP m n) (B : Basis m n) :
Matrix (Fin m) (Fin m) ℚ := fun i j => P.A i (B j)
noncomputable def basicValues {m n : ℕ} (P : LP m n) (B : Basis m n) :
Fin m → ℚ := (basisMatrix P B)⁻¹ *ᵥ P.b
noncomputable def basicPoint {m n : ℕ} (P : LP m n) (B : Basis m n) :
Fin n → ℚ := fun j => ∑ i : Fin m, if B i = j then basicValues P B i else 0
def feasible {m n : ℕ} (P : LP m n) (x : Fin n → ℚ) : Prop :=
P.A *ᵥ x = P.b ∧ ∀ j, 0 ≤ x j
noncomputable def basicFeasible {m n : ℕ} (P : LP m n) (B : Basis m n) : Prop :=
Function.Injective B ∧ (basisMatrix P B).det ≠ 0 ∧ ∀ i, 0 ≤ basicValues P B i
def objective {m n : ℕ} (P : LP m n) (x : Fin n → ℚ) : ℚ :=
∑ j : Fin n, P.c j * x j
noncomputable def basicObjective {m n : ℕ} (P : LP m n) (B : Basis m n) : ℚ :=
objective P (basicPoint P B)
noncomputable def direction {m n : ℕ} (P : LP m n) (B : Basis m n)
(j : Fin n) : Fin m → ℚ :=
(basisMatrix P B)⁻¹ *ᵥ (fun i => P.A i j)
noncomputable def reducedCost {m n : ℕ} (P : LP m n) (B : Basis m n)
(j : Fin n) : ℚ :=
P.c j - ∑ i : Fin m, P.c (B i) * direction P B j i
noncomputable def improving {m n : ℕ} (P : LP m n) (B : Basis m n)
(j : Fin n) : Prop :=
(∀ i, B i ≠ j) ∧ 0 < reducedCost P B j
noncomputable def leaving {m n : ℕ} (P : LP m n) (B : Basis m n)
(j : Fin n) (l : Fin m) : Prop :=
0 < direction P B j l ∧
∀ i, 0 < direction P B j i →
basicValues P B l / direction P B j l ≤
basicValues P B i / direction P B j i
noncomputable def pivot {m n : ℕ} (P : LP m n) (B : Basis m n)
(j : Fin n) (B' : Basis m n) : Prop :=
basicFeasible P B ∧ improving P B j ∧
∃ l, leaving P B j l ∧ B' = Function.update B l j ∧ basicFeasible P B'
noncomputable def optimalBasis {m n : ℕ} (P : LP m n) (B : Basis m n) : Prop :=
basicFeasible P B ∧ ∀ j, ¬ improving P B j
noncomputable def nondegenerate {m n : ℕ} (P : LP m n) : Prop :=
∀ B, basicFeasible P B → ∀ i, 0 < basicValues P B i
def bounded {m n : ℕ} (P : LP m n) : Prop :=
∃ R : ℚ, ∀ x, feasible P x → ∀ j, |x j| ≤ R
/-- Distance from the last selected rank. A basic variable cannot enter again,
so including the last rank in the scan agrees with resuming just after it. -/
def cyclicDistance {n : ℕ} (order : Fin n ≃ Fin n) (last j : Fin n) : ℕ :=
((order j).val + n - last.val) % n
noncomputable def cunninghamChoice {m n : ℕ} (P : LP m n)
(order : Fin n ≃ Fin n) (B : Basis m n) (last j : Fin n) : Prop :=
improving P B j ∧ ∀ k, improving P B k →
cyclicDistance order last j ≤ cyclicDistance order last k
structure State (m n : ℕ) where
basis : Basis m n
last : Fin n
noncomputable def cunninghamStep {m n : ℕ} (P : LP m n)
(order : Fin n ≃ Fin n) (s s' : State m n) : Prop :=
∃ j, cunninghamChoice P order s.basis s.last j ∧
pivot P s.basis j s'.basis ∧ s'.last = order j
/-- A finite complete run. The endpoint is genuinely optimal; every edge is an
actual ratio-test simplex pivot. Strictness is a conclusion of source targets. -/
noncomputable def cunninghamRun {m n : ℕ} (P : LP m n)
(order : Fin n ≃ Fin n) (s : ℕ → State m n) (T : ℕ) : Prop :=
basicFeasible P (s 0).basis ∧
(∀ t, t < T → cunninghamStep P order (s t) (s (t + 1))) ∧
optimalBasis P (s T).basis
end FriedmannSimplex
Read-back
What the Lean code literally says, in plain math · Codex independent auditor (model identifier unavailable)
LP. For any natural numbers , including zero, a rational linear-program datum consists of an rational matrix , a rational vector indexed by , and a rational vector indexed by ; the structure itself imposes no feasibility, rank, sign, or boundedness conditions.
Basis. For any natural numbers , a basis is any function . Injectivity and invertibility are not part of this definition. If the function is the empty function; if and no such function exists.
basisMatrix. Given natural numbers , rational data of the indicated dimensions, and a function , the square matrix has entries , so its columns are selected in the order specified by , with repetitions allowed.
basicValues. Given natural numbers , rational data , and a column-selection function , the basic-value vector is , where . This uses the total matrix inverse: a singular rational matrix has inverse the zero matrix, so the expression is defined without an invertibility hypothesis.
basicPoint. Given natural numbers , rational data , and a column-selection function , put and using the total inverse. The vector is defined by . Repeated selections add their basic values; an unselected column receives zero; an empty sum is zero.
feasible. For any natural dimensions , rational data , and rational vector , feasibility means exactly and for every . Zero dimensions are included, with their corresponding empty equations or coordinate conditions.
basicFeasible. For any natural dimensions , rational data , and function , basic feasibility means that is injective, the matrix has nonzero determinant, and every coordinate of is nonnegative. No additional equation about the embedded vector is included in this predicate; when , the determinant is the empty determinant and the coordinate condition is empty.
objective. For natural dimensions , rational data , and , the objective is , equal to zero when .
basicObjective. For natural dimensions , rational data , and a column-selection function , the basic objective is , with and the total inverse, so it is defined even when is repeated or is singular.
direction. For natural dimensions , rational data , a column-selection function , and , the direction is , with and total matrix inverse. It is defined without basic feasibility or invertibility assumptions.
reducedCost. For natural dimensions , rational data , a column-selection function , and , the reduced cost is , where and the inverse is total.
improving. For natural dimensions , rational data , a column-selection function , and , improvement means both for every and , with . It does not itself require to be basic feasible.
leaving. For natural dimensions , rational data , a column-selection function , an entering column , and a row , let and . Row is leaving exactly when and, for every with , . Ties are permitted. The predicate includes no basic feasibility or entering-improvement requirement. All inverses and rational divisions are total; the displayed denominator comparisons are restricted to positive directions.
pivot. For natural dimensions , rational data , column-selection functions , and , a pivot means that is injective, is nonsingular and ; that is outside the image of and has positive reduced cost for ; and that some row satisfies and for every positive , with obtained by replacing only by and also injective with nonsingular selected matrix and nonnegative basic values. The choice among tied leaving rows is unrestricted.
optimalBasis. For natural dimensions and rational data , a function is an optimal basis exactly when it is injective, is nonsingular, , and no column outside its image has positive reduced cost . This is the literal stopping predicate; it contains no separate universal comparison with all feasible objective values.
nondegenerate. For natural dimensions and rational data , nondegeneracy means that, for every injective column-selection function whose selected matrix is nonsingular and whose vector is coordinatewise nonnegative, every coordinate of that vector is strictly positive. If no basic feasible exists the condition holds vacuously; if its concluding coordinate condition is empty.
bounded. For natural dimensions and rational data , boundedness means that some rational number satisfies for every rational vector with and and every . The witness is not required to be nonnegative; empty feasible sets and empty coordinate sets are included.
cyclicDistance. For a natural number , a permutation of , and indices , the cyclic distance is the natural number , where indices are read as their numerical values and subtraction is natural-number subtraction. The stored is used directly as a rank, without applying to it. Distance zero is included in the scan. When there are no arguments .
cunninghamChoice. For natural dimensions , rational data , a permutation of the columns, a column-selection function , a stored rank , and a column , choice means that lies outside the image of , has positive reduced cost , and minimizes weakly over all columns satisfying those same two improvement conditions. It does not itself require basic feasibility; no improving column means that no choice exists.
State. For any natural dimensions , a state consists of a column-selection function and a stored rank . No feasibility or rank conditions are fields. In particular there are no states if .
cunninghamStep. For natural dimensions , rational data , a column permutation , and states , a step means that some nonbasic column with positive reduced cost has weakly least distance among all such columns; that are both basic feasible and is obtained from by entering and leaving a row minimizing the nonnegative-basic-value to positive-direction ratio; and that . Ties in the ratio test are permitted.
cunninghamRun. For natural dimensions , rational data , a column permutation , a sequence of states indexed by all natural numbers, and , a run means that is basic feasible; for every , some improving nonbasic column of least cyclic distance from is entered by an actual ratio-test pivot to a basic feasible and is its -rank; and is basic feasible with no improving nonbasic column. Here basic feasibility requires injectivity, nonzero selected determinant and nonnegative basic values, directions and reduced costs use the selected inverse, and a leaving row has positive direction and least basic-value/direction ratio. The number counts pivots, may be zero, and imposes no conditions on states after , no prescribed initial basis or initial rank, and no explicit strict increase of the objective.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.