Skip to content

Commit 0a285e4

Browse files
committed
Ratios table
1 parent 87b0718 commit 0a285e4

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/Ratios.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
"""
3+
_ratio_matrix(T::Tableau, col::Int)
4+
5+
Create a ratio matrix for column `col` of the tableau `T`. The
6+
result is a four-column matrix in which:
7+
* Column 1 is just the numbers 1, 2, 3, and so on.
8+
* Column 2 is column number `col`.
9+
* Column 3 is the RHS.
10+
* Column 4 are the ratios with some set to infinity if not relevant.
11+
"""
12+
function _ratio_matrix(T::Tableau, col::Int)
13+
if !(1 col T.n_vars)
14+
error("Invalid column $col")
15+
end
16+
17+
RM = zeros(Rational{BigInt}, T.n_cons, 4)
18+
for i in 1:T.n_cons
19+
RM[i, 1] = i
20+
a = RM[i, 2] = T[i, col]
21+
b = RM[i, 3] = T.M[i + 1, end]
22+
if a > 0 && b 0
23+
RM[i, 4] = b//a
24+
else
25+
RM[i, 4] = 1//0 # invalid pivot
26+
end
27+
end
28+
29+
return RM
30+
end
31+
32+
function _ratio_matrix_str(T::Tableau, col::Int)
33+
RM = _ratio_matrix(T, col)
34+
result = _pretty_string.(RM)
35+
36+
for i in 1:T.n_cons
37+
if RM[i, 4] == 1//0
38+
result[i, 4] = "---"
39+
end
40+
end
41+
42+
(m, i) = findmin(RM[:, 4]) # get smallest ratio
43+
if m == 1//0
44+
i = 0
45+
end
46+
47+
return result, i
48+
end
49+
50+
"""
51+
ratios(T::Tableau, col::Int)
52+
53+
Display a table for determining a valid pivot for `T` in column `col`.
54+
"""
55+
function ratios(T::Tableau, col::Int)
56+
RMS, i = _ratio_matrix_str(T, col)
57+
58+
header = ["Constraint", "Column $col", "RHS", "Ratio"]
59+
title = "Ratios for column $col headed by " * _pretty_string(T[0, col])
60+
sub = ""
61+
if T[0, col] < 0
62+
sub = "Invalid pivot column"
63+
elseif i>0
64+
sub = "Best pivot is in row $i"
65+
66+
else
67+
sub = "No valid pivot: LP is unbounded"
68+
end
69+
70+
if col T.B
71+
sub = "This is a basic column"
72+
end
73+
74+
pretty_table(RMS; column_labels=header, title=title, subtitle=sub)
75+
end

src/SimplexTableaux.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export Tableau,
3636
is_unbounded,
3737
lp_solve,
3838
pivot!,
39+
ratios,
3940
restore!,
4041
rhs,
4142
scale_row!,
@@ -62,5 +63,6 @@ include("Dual.jl")
6263
include("Status.jl")
6364
include("Bases.jl")
6465
include("big_M.jl")
66+
include("Ratios.jl")
6567

6668
end # module SimpleTableaux

0 commit comments

Comments
 (0)