MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / __init__

Method __init__

linear_programming/simplex.py:43–84  ·  view source on GitHub ↗
(
        self, tableau: np.ndarray, n_vars: int, n_artificial_vars: int
    )

Source from the content-addressed store, hash-verified

41 maxiter = 100
42
43 def __init__(
44 self, tableau: np.ndarray, n_vars: int, n_artificial_vars: int
45 ) -> None:
46 if tableau.dtype != "float64":
47 raise TypeError("Tableau must have type float64")
48
49 # Check if RHS is negative
50 if not (tableau[:, -1] >= 0).all():
51 raise ValueError("RHS must be > 0")
52
53 if n_vars < 2 or n_artificial_vars < 0:
54 raise ValueError(
55 "number of (artificial) variables must be a natural number"
56 )
57
58 self.tableau = tableau
59 self.n_rows, n_cols = tableau.shape
60
61 # Number of decision variables x1, x2, x3...
62 self.n_vars, self.n_artificial_vars = n_vars, n_artificial_vars
63
64 # 2 if there are >= or == constraints (nonstandard), 1 otherwise (std)
65 self.n_stages = (self.n_artificial_vars > 0) + 1
66
67 # Number of slack variables added to make inequalities into equalities
68 self.n_slack = n_cols - self.n_vars - self.n_artificial_vars - 1
69
70 # Objectives for each stage
71 self.objectives = ["max"]
72
73 # In two stage simplex, first minimise then maximise
74 if self.n_artificial_vars:
75 self.objectives.append("min")
76
77 self.col_titles = self.generate_col_titles()
78
79 # Index of current pivot row and column
80 self.row_idx = None
81 self.col_idx = None
82
83 # Does objective row only contain (non)-negative values?
84 self.stop_iter = False
85
86 def generate_col_titles(self) -> list[str]:
87 """Generate column titles for tableau of specific dimensions

Callers

nothing calls this directly

Calls 2

generate_col_titlesMethod · 0.95
appendMethod · 0.45

Tested by

no test coverage detected