MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / run_simplex

Method run_simplex

linear_programming/simplex.py:210–305  ·  view source on GitHub ↗

Operate on tableau until objective function cannot be improved further. # Standard linear program: Max: x1 + x2 ST: x1 + 3x2 <= 4 3x1 + x2 <= 4 >>> {key: float(value) for key, value in Tableau(np.array([[-1,-1,0,0,0], ... [1,3,1,0,4]

(self)

Source from the content-addressed store, hash-verified

208 return self.tableau
209
210 def run_simplex(self) -> dict[Any, Any]:
211 """Operate on tableau until objective function cannot be
212 improved further.
213
214 # Standard linear program:
215 Max: x1 + x2
216 ST: x1 + 3x2 <= 4
217 3x1 + x2 <= 4
218 >>> {key: float(value) for key, value in Tableau(np.array([[-1,-1,0,0,0],
219 ... [1,3,1,0,4],[3,1,0,1,4.]]), 2, 0).run_simplex().items()}
220 {'P': 2.0, 'x1': 1.0, 'x2': 1.0}
221
222 # Standard linear program with 3 variables:
223 Max: 3x1 + x2 + 3x3
224 ST: 2x1 + x2 + x3 ≤ 2
225 x1 + 2x2 + 3x3 ≤ 5
226 2x1 + 2x2 + x3 ≤ 6
227 >>> {key: float(value) for key, value in Tableau(np.array([
228 ... [-3,-1,-3,0,0,0,0],
229 ... [2,1,1,1,0,0,2],
230 ... [1,2,3,0,1,0,5],
231 ... [2,2,1,0,0,1,6.]
232 ... ]),3,0).run_simplex().items()} # doctest: +ELLIPSIS
233 {'P': 5.4, 'x1': 0.199..., 'x3': 1.6}
234
235
236 # Optimal tableau input:
237 >>> {key: float(value) for key, value in Tableau(np.array([
238 ... [0, 0, 0.25, 0.25, 2],
239 ... [0, 1, 0.375, -0.125, 1],
240 ... [1, 0, -0.125, 0.375, 1]
241 ... ]), 2, 0).run_simplex().items()}
242 {'P': 2.0, 'x1': 1.0, 'x2': 1.0}
243
244 # Non-standard: >= constraints
245 Max: 2x1 + 3x2 + x3
246 ST: x1 + x2 + x3 <= 40
247 2x1 + x2 - x3 >= 10
248 - x2 + x3 >= 10
249 >>> {key: float(value) for key, value in Tableau(np.array([
250 ... [2, 0, 0, 0, -1, -1, 0, 0, 20],
251 ... [-2, -3, -1, 0, 0, 0, 0, 0, 0],
252 ... [1, 1, 1, 1, 0, 0, 0, 0, 40],
253 ... [2, 1, -1, 0, -1, 0, 1, 0, 10],
254 ... [0, -1, 1, 0, 0, -1, 0, 1, 10.]
255 ... ]), 3, 2).run_simplex().items()}
256 {'P': 70.0, 'x1': 10.0, 'x2': 10.0, 'x3': 20.0}
257
258 # Non standard: minimisation and equalities
259 Min: x1 + x2
260 ST: 2x1 + x2 = 12
261 6x1 + 5x2 = 40
262 >>> {key: float(value) for key, value in Tableau(np.array([
263 ... [8, 6, 0, 0, 52],
264 ... [1, 1, 0, 0, 0],
265 ... [2, 1, 1, 0, 12],
266 ... [6, 5, 0, 1, 40.],
267 ... ]), 2, 2).run_simplex().items()}

Callers

nothing calls this directly

Calls 4

interpret_tableauMethod · 0.95
find_pivotMethod · 0.95
change_stageMethod · 0.95
pivotMethod · 0.95

Tested by

no test coverage detected