"""
Python implementation of the simplex algorithm for solving linear programs in
tabular form with
- `>=`, `<=`, and `=` constraints and
- each variable `x1, x2, ...>= 0`.
See https://gist.github.com/imengus/f9619a568f7da5bc74eaf20169a24d98 for how to
convert linear programs to simplex tableaus, and the steps taken in the simplex
algorithm.
Resources:
https://en.wikipedia.org/wiki/Simplex_algorithm
https://tinyurl.com/simplex4beginners
"""
from typing import Any
import numpy as np
class Tableau:
"""Operate on simplex tableaus
>>> t = Tableau(np.array([[-1,-1,0,0,-1],[1,3,1,0,4],[3,1,0,1,4.]]), 2)
Traceback (most recent call last):
...
ValueError: RHS must be > 0
"""
def __init__(self, tableau: np.ndarray, n_vars: int) -> None:
if np.any(tableau[:, -1], where=tableau[:, -1] < 0):
raise ValueError("RHS must be > 0")
self.tableau = tableau
self.n_rows, _ = tableau.shape
self.n_vars = n_vars
self.n_art_vars = len(np.where(tableau[self.n_vars : -1] == -1)[0])
self.n_stages = (self.n_art_vars > 0) + 1
self.n_slack = self.n_rows - self.n_stages
self.objectives = ["max"]
if self.n_art_vars:
self.objectives.append("min")
self.col_titles = [""]
self.row_idx = None
self.col_idx = None
self.stop_iter = False
@staticmethod
def generate_col_titles(*args: int) -> list[str]:
"""Generate column titles for tableau of specific dimensions
>>> Tableau.generate_col_titles(2, 3, 1)
['x1', 'x2', 's1', 's2', 's3', 'a1', 'RHS']
>>> Tableau.generate_col_titles()
Traceback (most recent call last):
...
ValueError: Must provide n_vars, n_slack, and n_art_vars
>>> Tableau.generate_col_titles(-2, 3, 1)
Traceback (most recent call last):
...
ValueError: All arguments must be non-negative integers
"""
if len(args) != 3:
raise ValueError("Must provide n_vars, n_slack, and n_art_vars")
if not all(x >= 0 and isinstance(x, int) for x in args):
raise ValueError("All arguments must be non-negative integers")
string_starts = ["x", "s", "a"]
titles = []
for i in range(3):
for j in range(args[i]):
titles.append(string_starts[i] + str(j + 1))
titles.append("RHS")
return titles
def find_pivot(self, tableau: np.ndarray) -> tuple[Any, Any]:
"""Finds the pivot row and column.
>>> t = Tableau(np.array([[-2,1,0,0,0], [3,1,1,0,6], [1,2,0,1,7.]]), 2)
>>> t.find_pivot(t.tableau)
(1, 0)
"""
objective = self.objectives[-1]
sign = (objective == "min") - (objective == "max")
col_idx = np.argmax(sign * tableau[0, : self.n_vars])
if sign * self.tableau[0, col_idx] <= 0:
self.stop_iter = True
return 0, 0
s = slice(self.n_stages, self.n_rows)
dividend = tableau[s, -1]
divisor = tableau[s, col_idx]
nans = np.full(self.n_rows - self.n_stages, np.nan)
quotients = np.divide(dividend, divisor, out=nans, where=divisor > 0)
row_idx = np.nanargmin(quotients) + self.n_stages
return row_idx, col_idx
def pivot(self, tableau: np.ndarray, row_idx: int, col_idx: int) -> np.ndarray:
"""Pivots on value on the intersection of pivot row and column.
>>> t = Tableau(np.array([[-2,-3,0,0,0],[1,3,1,0,4],[3,1,0,1,4.]]), 2)
>>> t.pivot(t.tableau, 1, 0).tolist()
... # doctest: +NORMALIZE_WHITESPACE
[[0.0, 3.0, 2.0, 0.0, 8.0],
[1.0, 3.0, 1.0, 0.0, 4.0],
[0.0, -8.0, -3.0, 1.0, -8.0]]
"""
piv_row = tableau[row_idx].copy()
piv_val = piv_row[col_idx]
piv_row *= 1 / piv_val
for idx, coeff in enumerate(tableau[:, col_idx]):
tableau[idx] += -coeff * piv_row
tableau[row_idx] = piv_row
return tableau
def change_stage(self, tableau: np.ndarray) -> np.ndarray:
"""Exits first phase of the two-stage method by deleting artificial
rows and columns, or completes the algorithm if exiting the standard
case.
>>> t = Tableau(np.array([
... [3, 3, -1, -1, 0, 0, 4],
... [2, 1, 0, 0, 0, 0, 0.],
... [1, 2, -1, 0, 1, 0, 2],
... [2, 1, 0, -1, 0, 1, 2]
... ]), 2)
>>> t.change_stage(t.tableau).tolist()
... # doctest: +NORMALIZE_WHITESPACE
[[2.0, 1.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 2.0, -1.0, 0.0, 1.0, 2.0],
[2.0, 1.0, 0.0, -1.0, 0.0, 2.0]]
"""
self.objectives.pop()
if not self.objectives:
return tableau
s = slice(-self.n_art_vars - 1, -1)
tableau = np.delete(tableau, s, axis=1)
tableau = np.delete(tableau, 0, axis=0)
self.n_stages = 1
self.n_rows -= 1
self.n_art_vars = 0
self.stop_iter = False
return tableau
def run_simplex(self) -> dict[Any, Any]:
"""Operate on tableau until objective function cannot be
improved further.
# Standard linear program:
Max: x1 + x2
ST: x1 + 3x2 <= 4
3x1 + x2 <= 4
>>> Tableau(np.array([[-1,-1,0,0,0],[1,3,1,0,4],[3,1,0,1,4.]]),
... 2).run_simplex()
{'P': 2.0, 'x1': 1.0, 'x2': 1.0}
# Optimal tableau input:
>>> Tableau(np.array([
... [0, 0, 0.25, 0.25, 2],
... [0, 1, 0.375, -0.125, 1],
... [1, 0, -0.125, 0.375, 1]
... ]), 2).run_simplex()
{'P': 2.0, 'x1': 1.0, 'x2': 1.0}
# Non-standard: >= constraints
Max: 2x1 + 3x2 + x3
ST: x1 + x2 + x3 <= 40
2x1 + x2 - x3 >= 10
- x2 + x3 >= 10
>>> Tableau(np.array([
... [2, 0, 0, 0, -1, -1, 0, 0, 20],
... [-2, -3, -1, 0, 0, 0, 0, 0, 0],
... [1, 1, 1, 1, 0, 0, 0, 0, 40],
... [2, 1, -1, 0, -1, 0, 1, 0, 10],
... [0, -1, 1, 0, 0, -1, 0, 1, 10.]
... ]), 3).run_simplex()
{'P': 70.0, 'x1': 10.0, 'x2': 10.0, 'x3': 20.0}
# Non standard: minimisation and equalities
Min: x1 + x2
ST: 2x1 + x2 = 12
6x1 + 5x2 = 40
>>> Tableau(np.array([
... [8, 6, 0, -1, 0, -1, 0, 0, 52],
... [1, 1, 0, 0, 0, 0, 0, 0, 0],
... [2, 1, 1, 0, 0, 0, 0, 0, 12],
... [2, 1, 0, -1, 0, 0, 1, 0, 12],
... [6, 5, 0, 0, 1, 0, 0, 0, 40],
... [6, 5, 0, 0, 0, -1, 0, 1, 40.]
... ]), 2).run_simplex()
{'P': 7.0, 'x1': 5.0, 'x2': 2.0}
"""
for _ in range(100):
if not self.objectives:
self.col_titles = self.generate_col_titles(
self.n_vars, self.n_slack, self.n_art_vars
)
return self.interpret_tableau(self.tableau, self.col_titles)
row_idx, col_idx = self.find_pivot(self.tableau)
if self.stop_iter:
self.tableau = self.change_stage(self.tableau)
else:
self.tableau = self.pivot(self.tableau, row_idx, col_idx)
return {}
def interpret_tableau(
self, tableau: np.ndarray, col_titles: list[str]
) -> dict[str, float]:
"""Given the final tableau, add the corresponding values of the basic
decision variables to the `output_dict`
>>> tableau = np.array([
... [0,0,0.875,0.375,5],
... [0,1,0.375,-0.125,1],
... [1,0,-0.125,0.375,1]
... ])
>>> t = Tableau(tableau, 2)
>>> t.interpret_tableau(tableau, ["x1", "x2", "s1", "s2", "RHS"])
{'P': 5.0, 'x1': 1.0, 'x2': 1.0}
"""
output_dict = {"P": abs(tableau[0, -1])}
for i in range(self.n_vars):
nonzero = np.nonzero(tableau[:, i])
n_nonzero = len(nonzero[0])
nonzero_rowidx = nonzero[0][0]
nonzero_val = tableau[nonzero_rowidx, i]
if n_nonzero == nonzero_val == 1:
rhs_val = tableau[nonzero_rowidx, -1]
output_dict[col_titles[i]] = rhs_val
for title in col_titles:
if title[0] not in "R-s-a":
output_dict.setdefault(title, 0)
return output_dict
if __name__ == "__main__":
import doctest
doctest.testmod()