|
| 1 | +# Creating and Solving Linear Programs |
| 2 | + |
| 3 | +## Create the Tableau |
| 4 | + |
| 5 | +### Canonical LPs |
| 6 | + |
| 7 | +A canonical LP has the form $\min c^T x$ s.t. $Ax ≥ b, x \ge 0$. To set up a tableau for this problem simply create the matrix `A` and the vectors `b` and `c`, and call `Tableau(A,b,c)`. |
| 8 | + |
| 9 | +For example, let `A`, `b`, and `c` be as follows: |
| 10 | +``` |
| 11 | +julia> A = [3 10; 5 6; 10 2]; |
| 12 | +
|
| 13 | +julia> b = [100, 100, 100]; |
| 14 | +
|
| 15 | +julia> c = [25, 10]; |
| 16 | +
|
| 17 | +julia> Tableau(A, b, c) |
| 18 | +┌──────────┬───┬─────┬─────┬─────┬─────┬─────┬─────┐ |
| 19 | +│ │ z │ x_1 │ x_2 │ x_3 │ x_4 │ x_5 │ RHS │ |
| 20 | +│ Obj Func │ 1 │ -25 │ -10 │ 0 │ 0 │ 0 │ 0 │ |
| 21 | +├──────────┼───┼─────┼─────┼─────┼─────┼─────┼─────┤ |
| 22 | +│ Cons 1 │ 0 │ 3 │ 10 │ -1 │ 0 │ 0 │ 100 │ |
| 23 | +│ Cons 2 │ 0 │ 5 │ 6 │ 0 │ -1 │ 0 │ 100 │ |
| 24 | +│ Cons 3 │ 0 │ 10 │ 2 │ 0 │ 0 │ -1 │ 100 │ |
| 25 | +└──────────┴───┴─────┴─────┴─────┴─────┴─────┴─────┘ |
| 26 | +``` |
| 27 | +Notice that extra variables $x_3$, $x_4$, and $x_5$ are added to the `Tableau` |
| 28 | +as slack variables to convert inequalities into equations. That is, canonical |
| 29 | +form LPs are automatically converted into standard form. |
| 30 | + |
| 31 | +### Standard LPs |
| 32 | + |
| 33 | +A linear program in standard form is $\min c^T x$ s.t. $Ax = b$, $x ≥ 0$. |
| 34 | +For example, |
| 35 | +``` |
| 36 | +julia> A = [2 1 0 9 -1; 1 1 -1 5 1] |
| 37 | +2×5 Matrix{Int64}: |
| 38 | + 2 1 0 9 -1 |
| 39 | + 1 1 -1 5 1 |
| 40 | +
|
| 41 | +julia> b = [9, 7] |
| 42 | +2-element Vector{Int64}: |
| 43 | + 9 |
| 44 | + 7 |
| 45 | +
|
| 46 | +julia> c = [2, 4, 2, 1, -1] |
| 47 | +5-element Vector{Int64}: |
| 48 | + 2 |
| 49 | + 4 |
| 50 | + 2 |
| 51 | + 1 |
| 52 | + -1 |
| 53 | +
|
| 54 | +julia> T = Tableau(A, b, c, false) |
| 55 | +┌──────────┬───┬─────┬─────┬─────┬─────┬─────┬─────┐ |
| 56 | +│ │ z │ x_1 │ x_2 │ x_3 │ x_4 │ x_5 │ RHS │ |
| 57 | +│ Obj Func │ 1 │ -2 │ -4 │ -2 │ -1 │ 1 │ 0 │ |
| 58 | +├──────────┼───┼─────┼─────┼─────┼─────┼─────┼─────┤ |
| 59 | +│ Cons 1 │ 0 │ 2 │ 1 │ 0 │ 9 │ -1 │ 9 │ |
| 60 | +│ Cons 2 │ 0 │ 1 │ 1 │ -1 │ 5 │ 1 │ 7 │ |
| 61 | +└──────────┴───┴─────┴─────┴─────┴─────┴─────┴─────┘ |
| 62 | +``` |
| 63 | +The fourth argument `false` means that the constraints are already equalities and slack variables should not be appended. |
| 64 | + |
| 65 | + |
| 66 | +## Specify a Basis |
| 67 | + |
| 68 | +Use `set_basis!(T, B)` to specify a staring basis for the tableau. Here, `B` is a list (`Vector`) |
| 69 | +of integers specifying the columns that are in the basis. |
| 70 | + |
| 71 | +``` |
| 72 | +julia> set_basis!(T,[1,4,5]) |
| 73 | +┌──────────┬───┬─────┬───────┬───────┬─────┬─────┬────────┐ |
| 74 | +│ │ z │ x_1 │ x_2 │ x_3 │ x_4 │ x_5 │ RHS │ |
| 75 | +│ Obj Func │ 1 │ 0 │ 220/3 │ -25/3 │ 0 │ 0 │ 2500/3 │ |
| 76 | +├──────────┼───┼─────┼───────┼───────┼─────┼─────┼────────┤ |
| 77 | +│ Cons 1 │ 0 │ 1 │ 10/3 │ -1/3 │ 0 │ 0 │ 100/3 │ |
| 78 | +│ Cons 2 │ 0 │ 0 │ 32/3 │ -5/3 │ 1 │ 0 │ 200/3 │ |
| 79 | +│ Cons 3 │ 0 │ 0 │ 94/3 │ -10/3 │ 0 │ 1 │ 700/3 │ |
| 80 | +└──────────┴───┴─────┴───────┴───────┴─────┴─────┴────────┘ |
| 81 | +``` |
| 82 | +> Note: On the screen, the headings for the basis (in this case, `x_1`, `x_3`, and `x_4`) appear in green. |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +### Tools to find a basis |
| 87 | + |
| 88 | +The function `find_all_bases(T)` returns a list of all feasible bases for `T`: |
| 89 | +``` |
| 90 | +julia> find_all_bases(T) |
| 91 | +4-element Vector{Vector{Int64}}: |
| 92 | + [1, 2, 3] |
| 93 | + [1, 2, 5] |
| 94 | + [1, 4, 5] |
| 95 | + [2, 3, 4] |
| 96 | +``` |
| 97 | +The function `find_a_basis(T)` returns a feasible basis for `T` (the first it finds). |
| 98 | +``` |
| 99 | +julia> find_a_basis(T) |
| 100 | +3-element Vector{Int64}: |
| 101 | + 1 |
| 102 | + 2 |
| 103 | + 3 |
| 104 | +``` |
| 105 | + |
| 106 | +These are inefficient functions. We plan to change the implementation of `find_a_basis` to something more performant. |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +## Perform the Simplex Algorithm |
| 111 | + |
| 112 | +Once a tableau has been set up with a feasible basis, use `simplex_solve!(T)` to run the simplex algorithm and return solution to the LP. |
| 113 | +``` |
| 114 | +julia> simplex_solve!(T) |
| 115 | +Starting tableau |
| 116 | +
|
| 117 | +┌──────────┬───┬─────┬───────┬───────┬─────┬─────┬────────┐ |
| 118 | +│ │ z │ x_1 │ x_2 │ x_3 │ x_4 │ x_5 │ RHS │ |
| 119 | +│ Obj Func │ 1 │ 0 │ 220/3 │ -25/3 │ 0 │ 0 │ 2500/3 │ |
| 120 | +├──────────┼───┼─────┼───────┼───────┼─────┼─────┼────────┤ |
| 121 | +│ Cons 1 │ 0 │ 1 │ 10/3 │ -1/3 │ 0 │ 0 │ 100/3 │ |
| 122 | +│ Cons 2 │ 0 │ 0 │ 32/3 │ -5/3 │ 1 │ 0 │ 200/3 │ |
| 123 | +│ Cons 3 │ 0 │ 0 │ 94/3 │ -10/3 │ 0 │ 1 │ 700/3 │ |
| 124 | +└──────────┴───┴─────┴───────┴───────┴─────┴─────┴────────┘ |
| 125 | +
|
| 126 | +Column 4 leaves basis and column 2 enters |
| 127 | +
|
| 128 | +┌──────────┬───┬─────┬─────┬───────┬────────┬─────┬──────┐ |
| 129 | +│ │ z │ x_1 │ x_2 │ x_3 │ x_4 │ x_5 │ RHS │ |
| 130 | +│ Obj Func │ 1 │ 0 │ 0 │ 25/8 │ -55/8 │ 0 │ 375 │ |
| 131 | +├──────────┼───┼─────┼─────┼───────┼────────┼─────┼──────┤ |
| 132 | +│ Cons 1 │ 0 │ 1 │ 0 │ 3/16 │ -5/16 │ 0 │ 25/2 │ |
| 133 | +│ Cons 2 │ 0 │ 0 │ 1 │ -5/32 │ 3/32 │ 0 │ 25/4 │ |
| 134 | +│ Cons 3 │ 0 │ 0 │ 0 │ 25/16 │ -47/16 │ 1 │ 75/2 │ |
| 135 | +└──────────┴───┴─────┴─────┴───────┴────────┴─────┴──────┘ |
| 136 | +
|
| 137 | +Column 5 leaves basis and column 3 enters |
| 138 | +
|
| 139 | +┌──────────┬───┬─────┬─────┬─────┬────────┬───────┬─────┐ |
| 140 | +│ │ z │ x_1 │ x_2 │ x_3 │ x_4 │ x_5 │ RHS │ |
| 141 | +│ Obj Func │ 1 │ 0 │ 0 │ 0 │ -1 │ -2 │ 300 │ |
| 142 | +├──────────┼───┼─────┼─────┼─────┼────────┼───────┼─────┤ |
| 143 | +│ Cons 1 │ 0 │ 1 │ 0 │ 0 │ 1/25 │ -3/25 │ 8 │ |
| 144 | +│ Cons 2 │ 0 │ 0 │ 1 │ 0 │ -1/5 │ 1/10 │ 10 │ |
| 145 | +│ Cons 3 │ 0 │ 0 │ 0 │ 1 │ -47/25 │ 16/25 │ 24 │ |
| 146 | +└──────────┴───┴─────┴─────┴─────┴────────┴───────┴─────┘ |
| 147 | +
|
| 148 | +Optimality reached |
| 149 | +Value = 300 |
| 150 | +5-element Vector{Rational}: |
| 151 | + 8 |
| 152 | + 10 |
| 153 | + 24 |
| 154 | + 0 |
| 155 | + 0 |
| 156 | +``` |
| 157 | + |
0 commit comments