Skip to content

Commit 4742e76

Browse files
committed
Lots o fixes
1 parent 7921c41 commit 4742e76

File tree

10 files changed

+109
-20
lines changed

10 files changed

+109
-20
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ At present it will fail if:
1515
* The LP is unbounded.
1616
* Other unidentified reasons. (In other words, still buggy.)
1717

18-
It is also set up for maximization problems only. To solve a minimization problem use the dual LP by
19-
replacing the inputs `A`, `b`, and `c` with `A'`, `c`, and `b` respectively.
2018

19+
This module also set up for maximization problems only. To solve a minimization problem use the dual LP by
20+
replacing the inputs `A`, `b`, and `c` with `A'`, `c`, and `b` respectively.
2121

22+
This module solves LPs using the simplex algorithm which is not the most performant method. Further, all data is stored using arbitrary precision integers (that is, `Rational{BigInt}`) which gives exact answer, but is much slower than floating point arithmetic. These issues are negligible for small problems.
2223

2324

2425
# Quick Start Instructions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"documenter":{"julia_version":"1.11.4","generation_timestamp":"2025-04-03T14:22:49","documenter_version":"1.10.0"}}
1+
{"documenter":{"julia_version":"1.11.4","generation_timestamp":"2025-04-03T17:53:48","documenter_version":"1.10.0"}}

docs/build/index.html

Lines changed: 20 additions & 3 deletions
Large diffs are not rendered by default.

docs/build/objects.inv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# Project: SimpleTableaux
33
# Version: 0.0.2
44
# The remainder of this file is compressed using zlib.
5-
x�}���0��>�u�GWW���6���-�ޖ�(��w����R�܃u��6`p�����������qR_FFQ�*R��8][�δ�����I�?�7f��^T�C��4��)>C�I��mB�`B��xA�h���Xx��'AP� q�֓�Z�6%w�ׅ�y���֐3oM�����q�W)�` ��ȋ'un��
5+
x�}���1 D�|�P���� *��@���"�d�NJ��I ���>3������fޘ� `#O��-Պ)���H� ���q)]��Ǖ�'�7���U�'PX�,ddg|ڽ�Pl��e�����yi���L�f*Ρh�L��2�cH��|f�dD�6�1*$�{l��)�=H�KH� T��]y����_��K���IG�ȱ�)�`�@ҝ/���[

docs/build/search_index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/index.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# SimpleTableaux
22

3-
4-
53
This is an illustration project for solving
64
feasible optimization problems (Linear Programs) of the form
75
$\max c^t x$ subject to $Ax ≤ b$ and $x \ge 0$.
@@ -123,7 +121,7 @@ julia> lp_solve(T)
123121
1.599999999999999
124122
```
125123

126-
## The `restore!` function
124+
## The `restore!` Function
127125

128126
As mentioned, the `pivot!` and `pivot_solve!` functions modify the `Tableau`. Use `restore!` to return a `Tableau` to its original state like this: `restore!(T)`.
129127

@@ -174,4 +172,34 @@ julia> visualize(T)
174172
```
175173

176174

177-
![](visual.png)
175+
![](visual.png)
176+
177+
## Changing the Display Format
178+
179+
The usual way a `Tableau` is printed looks like this:
180+
```
181+
4×7 DataFrame
182+
Row │ x1 x2 s1 s2 s3 val RHS
183+
│ Exact Exact Exact Exact Exact Exact Exact
184+
─────┼─────────────────────────────────────────────────
185+
1 │ 8 3 1 0 0 0 24
186+
2 │ 1 1 0 1 0 0 4
187+
3 │ 1 4 0 0 1 0 12
188+
4 │ -2 -1 0 0 0 1 0
189+
```
190+
191+
The column headings are (should be) self-explanatory. As usual we begin with the LP variables, $x_1$, $x_2$, and so forth (rendered as `x1`, `x2`, etc.), and then the slack variables, and the right hand side. There are no row labels. To remedy that, set the (nonexported) variable `SimpleTableaux.show_row_labels` to true.
192+
```
193+
julia> SimpleTableaux.show_row_labels=true;
194+
195+
julia> T
196+
4×8 DataFrame
197+
Row │ Row Name x1 x2 s1 s2 s3 val RHS
198+
│ String Exact Exact Exact Exact Exact Exact Exact
199+
─────┼───────────────────────────────────────────────────────────
200+
1 │ cons1 8 3 1 0 0 0 24
201+
2 │ cons2 1 1 0 1 0 0 4
202+
3 │ cons3 1 4 0 0 1 0 12
203+
4 │ obj -2 -1 0 0 0 1 0
204+
```
205+
We now see that rows 1, 2, and 3 correspond to constraints and row 4 to the objective function.

src/DataFrame.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
show_row_labels::Bool = false
2+
13
"""
24
DataFrame(T::Tableau)
35
@@ -6,10 +8,12 @@ Present a `Tableau` as a `DataFrame`.
68
function DataFrame(T::Tableau)
79
df = DataFrame()
810

9-
# # Name the rows
10-
# rownames = ["cons" * string(k) for k in 1:(T.n_cons)]
11-
# push!(rownames, "obj")
12-
# df[:, "Row Name"] = rownames
11+
if show_row_labels
12+
# Name the rows
13+
rownames = ["cons" * string(k) for k in 1:(T.n_cons)]
14+
push!(rownames, "obj")
15+
df[:, "Row Name"] = rownames
16+
end
1317

1418
# Variable columns
1519
for i in 1:(T.n_vars)

src/Pivoting.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ function find_pivot_column(T::Tableau)
1010
return v < 0 ? j : 0
1111
end
1212

13+
"""
14+
find_least_nonnegative(v::Vector)
15+
16+
Find the index of the least nonnegative entry in `v` or `0` if all entries are negative.
17+
"""
18+
function find_least_nonnegative(v::Vector)
19+
m = maximum(v)
20+
if m < 0
21+
return 0 # no nonegative values
22+
end
23+
idx = 0
24+
for k in 1:length(v)
25+
if v[k] >= 0 && v[k] < m
26+
m = v[k]
27+
idx = k
28+
end
29+
end
30+
return idx
31+
end
32+
1333
"""
1434
find_pivot_row(T::Tableau, j::Int)
1535
@@ -19,7 +39,8 @@ function find_pivot_row(T::Tableau, j::Int)
1939
rhs = T.M[1:(end - 1), end]
2040
col = T.M[1:(end - 1), j]
2141
vals = rhs .// col
22-
_, i = findmin(vals)
42+
# _, i = findmin(vals)
43+
i = find_least_nonnegative(vals)
2344
return i
2445
end
2546

src/SimpleTableaux.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct Tableau
4545
end
4646
body = hcat(A, Matrix(I, m, m), zeros(Int, m), b)
4747

48-
obj = [-c; zeros(Int, m); 1; 0]'
48+
obj = collect([-c; zeros(Int, m); 1; 0]')
4949

5050
return new(vcat(body, obj), A, b, c, n, m)
5151
end
@@ -69,8 +69,9 @@ Restore a `Tableau` based on the original data used to create it.
6969
"""
7070
function restore!(T::Tableau)
7171
TT = restore(T)
72-
for i in 1:(T.n_cons)
73-
for j in 1:(T.n_vars)
72+
m, n = size(TT.M)
73+
for i in 1:m
74+
for j in 1:n
7475
T.M[i, j] = TT.M[i, j]
7576
end
7677
end

test/bad.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using SimpleTableaux
2+
3+
"""
4+
bad()
5+
6+
Create a `Tableau` that `pivot_solve` can't handle.
7+
"""
8+
function bad()
9+
A = [
10+
2 2 6
11+
3 9 8
12+
9 7 3
13+
]
14+
b = [9; 1; 2]
15+
c = [2; 1; 4]
16+
return Tableau(A, b, c)
17+
end

0 commit comments

Comments
 (0)