Skip to main content
Tweeted twitter.com/StackCodeReview/status/1596609745636716544
add link to source
Source Link
Anakhand
  • 645
  • 1
  • 5
  • 16

The up-to-date code, along some documentation, can be found here.


The up-to-date code, along some documentation, can be found here.


added usage example
Source Link
Anakhand
  • 645
  • 1
  • 5
  • 16

Usage example

Consider the following linear programming problem (in standard form):

$$ \begin{cases}\begin{aligned}\min &&&& -x_1 &&-x_2\\ \text{s.t.} &&&& 3x_1 &&+2x_2 &&+x_3 && && = 4\\ &&&& && x_2 && &&+x_4 && = 3\\ \\ &&&& x_1,&&x_2,&&x_3,&&x_4 &&\ge 0\\ \end{aligned}\end{cases} $$

To solve this problem from the Python console, I would write

>>> import numpy as np
>>> from simplex import simplex
>>> A = np.matrix([[3, 2, 1, 0], [0, 1, 0, 1]])
>>> b = np.array([4, 3])
>>> c = np.array([-1, -1, 0, 0])
>>> simplex(A, b, c)

The output would be:

Executing phase I...
    Iteration no. 1:    q =  1  rq =     -3.00  B[p] =  1   theta* = 1.3333     z = 3.00     
    Iteration no. 2:    q =  2  rq =     -1.00  B[p] =  2   theta* = 2.0000     z = 1.00     
    Iteration no. 3:    q =  4  rq =     -1.00  B[p] =  4   theta* = 1.0000     z = 0.00     
    Iteration no. 4:    found optimum
Phase I terminated.
Found initial BFS at x = 
[0. 2. 0. 1.].

Executing phase II...
    Iteration no. 1:    found optimum
Phase II terminated.

---------------------------------
| Found optimal solution at x = |
| [0. 2. 0. 1.].                |
|                               |
| Basic indices: {1, 3}         |
| Nonbasic indices: {0, 2}      |
|                               |
| Optimal cost: -2.0.           |
---------------------------------
4 iterations in phase I, 1 iterations in phase II (5 total).

(0, array([0., 2., 0., 1.]), -2.0, None)

Usage example

Consider the following linear programming problem (in standard form):

$$ \begin{cases}\begin{aligned}\min &&&& -x_1 &&-x_2\\ \text{s.t.} &&&& 3x_1 &&+2x_2 &&+x_3 && && = 4\\ &&&& && x_2 && &&+x_4 && = 3\\ \\ &&&& x_1,&&x_2,&&x_3,&&x_4 &&\ge 0\\ \end{aligned}\end{cases} $$

To solve this problem from the Python console, I would write

>>> import numpy as np
>>> from simplex import simplex
>>> A = np.matrix([[3, 2, 1, 0], [0, 1, 0, 1]])
>>> b = np.array([4, 3])
>>> c = np.array([-1, -1, 0, 0])
>>> simplex(A, b, c)

The output would be:

Executing phase I...
    Iteration no. 1:    q =  1  rq =     -3.00  B[p] =  1   theta* = 1.3333     z = 3.00     
    Iteration no. 2:    q =  2  rq =     -1.00  B[p] =  2   theta* = 2.0000     z = 1.00     
    Iteration no. 3:    q =  4  rq =     -1.00  B[p] =  4   theta* = 1.0000     z = 0.00     
    Iteration no. 4:    found optimum
Phase I terminated.
Found initial BFS at x = 
[0. 2. 0. 1.].

Executing phase II...
    Iteration no. 1:    found optimum
Phase II terminated.

---------------------------------
| Found optimal solution at x = |
| [0. 2. 0. 1.].                |
|                               |
| Basic indices: {1, 3}         |
| Nonbasic indices: {0, 2}      |
|                               |
| Optimal cost: -2.0.           |
---------------------------------
4 iterations in phase I, 1 iterations in phase II (5 total).

(0, array([0., 2., 0., 1.]), -2.0, None)
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
added 1 character in body
Source Link
Anakhand
  • 645
  • 1
  • 5
  • 16
Loading
deleted 151 characters in body
Source Link
Anakhand
  • 645
  • 1
  • 5
  • 16
Loading
Source Link
Anakhand
  • 645
  • 1
  • 5
  • 16
Loading