Skip to content

Commit 71d29f5

Browse files
committed
re-org
1 parent 405a586 commit 71d29f5

File tree

3 files changed

+114
-111
lines changed

3 files changed

+114
-111
lines changed

src/LPsolve.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ function lp_solve(T::Tableau, verbose::Bool=true)
2626
println("Optimum value = $obj_val\n")
2727
end
2828

29-
xval = value.(x)
29+
@show x
30+
31+
xval = JuMP.value.(x)
3032

3133
return xval
3234
end

src/SimplexTableaux.jl

Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -32,117 +32,8 @@ export Tableau,
3232
swap!,
3333
value
3434

35-
# Tableau struture is based on standard minimization LP:
36-
# min c'*x st Ax ≥ b, x≥0
37-
#
38-
# 1 | -c^T | 0 <== single row
39-
# ---+------+----
40-
# 0 | A | b <== m rows
41-
42-
"""
43-
Tableau(A::Matrix, b::Vector, c::Vector)
44-
45-
Create a `Tableau` data structure for the linear program minimize `c' * x` subject to `A * x ≥ b, x ≥ 0`.
46-
47-
If matrix and vectors are already in standard form, then use `Tableau(A, b, c, false)`.
48-
"""
49-
struct Tableau
50-
M::Matrix{_Exact} # place to hold the entire Tableau
51-
A::Matrix # (original) A matrix
52-
b::Vector # (original) RHS, b vector
53-
c::Vector # (original) objective coefficients, c vector
54-
n_vars::Int # number of variables in the LP
55-
n_cons::Int # number of constraints in the LP
56-
57-
function Tableau(A::AbstractMatrix, b::Vector, c::Vector, is_cannonical::Bool=true)
58-
m, n = size(A)
59-
if length(b) m || length(c) n
60-
throw(ArgumentError("Size mismatch"))
61-
end
62-
63-
if is_cannonical
64-
A, b, c = make_standard(A, b, c)
65-
end
66-
m, n = size(A)
67-
top_row = hcat(1, -c', 0)
68-
body = hcat(zeros(Int, m), A, b)
69-
70-
M = vcat(top_row, body)
71-
72-
# test rank of M[2:end,2:end-1]
73-
if rankx(M[2:end, 2:(end - 1)]) ≠ m
74-
@warn("Rank difficient Tableau")
75-
end
76-
77-
return new(M, A, b, c, n, m)
78-
end
79-
end
80-
81-
"""
82-
make_standard(A::AbstractMatrix, b::Vector, c::Vector)
83-
84-
Exand the `A` matrix with an identity matrix on the right and the `b`
85-
vector with zeros to convert a canonical LP min `c'*x` st `A*x b, x 0` into
86-
min `c'*x` st `A*x = b, x ≥ 0`. Returns the new matrices/vectors `A`, `b`, and `c`.
87-
"""
88-
function make_standard(A::AbstractMatrix, b::Vector, c::Vector)
89-
m, n = size(A)
90-
if length(b) ≠ m || length(c) ≠ n
91-
throw(ArgumentError("Size mismatch"))
92-
end
93-
AA = hcat(A, -Matrix(I, m, m))
94-
bb = b
95-
cc = vcat(c, zeros(Int, m))
96-
97-
return AA, bb, cc
98-
end
99-
100-
"""
101-
restore(T::Tableau)
102-
103-
Create a new `Tableau` based on the original data used to create `T`.
104-
105-
See also `restore!`.
106-
"""
107-
function restore(T::Tableau)
108-
return Tableau(T.A, T.b, T.c, false)
109-
end
110-
111-
"""
112-
get_Abc(T::Tableau)
113-
114-
Returns a 3-tuple containing copies of the matrix `A`
115-
and the vectors `b` and `c` used to create `T`.
116-
"""
117-
function get_Abc(T::Tableau)
118-
return copy(T.A), copy(T.b), copy(T.c)
119-
end
120-
121-
"""
122-
restore!(T::Tableau)
123-
124-
Restore a `Tableau` based on the original data used to create it.
125-
"""
126-
function restore!(T::Tableau)
127-
TT = restore(T)
128-
m, n = size(TT.M)
129-
for i in 1:m
130-
for j in 1:n
131-
T.M[i, j] = TT.M[i, j]
132-
end
133-
end
134-
return T
135-
end
136-
137-
"""
138-
value(T::Tableau, x::Vector)
139-
140-
Return the value of the LP in `T` at the point `x`.
141-
"""
142-
function value(T::Tableau, x::Vector)
143-
return T.c' * x
144-
end
14535

36+
include("Tableau.jl")
14637
include("Pivoting.jl")
14738
include("Solver.jl")
14839
include("LPsolve.jl")

src/Tableau.jl

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Tableau struture is based on standard minimization LP:
2+
# min c'*x st Ax ≥ b, x≥0
3+
#
4+
# 1 | -c^T | 0 <== single row
5+
# ---+------+----
6+
# 0 | A | b <== m rows
7+
8+
"""
9+
Tableau(A::Matrix, b::Vector, c::Vector)
10+
11+
Create a `Tableau` data structure for the linear program minimize `c' * x` subject to `A * x ≥ b, x ≥ 0`.
12+
13+
If matrix and vectors are already in standard form, then use `Tableau(A, b, c, false)`.
14+
"""
15+
struct Tableau
16+
M::Matrix{_Exact} # place to hold the entire Tableau
17+
A::Matrix # (original) A matrix
18+
b::Vector # (original) RHS, b vector
19+
c::Vector # (original) objective coefficients, c vector
20+
n_vars::Int # number of variables in the LP
21+
n_cons::Int # number of constraints in the LP
22+
23+
function Tableau(A::AbstractMatrix, b::Vector, c::Vector, is_cannonical::Bool=true)
24+
m, n = size(A)
25+
if length(b) m || length(c) n
26+
throw(ArgumentError("Size mismatch"))
27+
end
28+
29+
if is_cannonical
30+
A, b, c = make_standard(A, b, c)
31+
end
32+
m, n = size(A)
33+
top_row = hcat(1, -c', 0)
34+
body = hcat(zeros(Int, m), A, b)
35+
36+
M = vcat(top_row, body)
37+
38+
# test rank of M[2:end,2:end-1]
39+
if rankx(M[2:end, 2:(end - 1)]) ≠ m
40+
@warn("Rank difficient Tableau")
41+
end
42+
43+
return new(M, A, b, c, n, m)
44+
end
45+
end
46+
47+
"""
48+
make_standard(A::AbstractMatrix, b::Vector, c::Vector)
49+
50+
Exand the `A` matrix with an identity matrix on the right and the `b`
51+
vector with zeros to convert a canonical LP min `c'*x` st `A*x b, x 0` into
52+
min `c'*x` st `A*x = b, x ≥ 0`. Returns the new matrices/vectors `A`, `b`, and `c`.
53+
"""
54+
function make_standard(A::AbstractMatrix, b::Vector, c::Vector)
55+
m, n = size(A)
56+
if length(b) ≠ m || length(c) ≠ n
57+
throw(ArgumentError("Size mismatch"))
58+
end
59+
AA = hcat(A, -Matrix(I, m, m))
60+
bb = b
61+
cc = vcat(c, zeros(Int, m))
62+
63+
return AA, bb, cc
64+
end
65+
66+
"""
67+
restore(T::Tableau)
68+
69+
Create a new `Tableau` based on the original data used to create `T`.
70+
71+
See also `restore!`.
72+
"""
73+
function restore(T::Tableau)
74+
return Tableau(T.A, T.b, T.c, false)
75+
end
76+
77+
"""
78+
get_Abc(T::Tableau)
79+
80+
Returns a 3-tuple containing copies of the matrix `A`
81+
and the vectors `b` and `c` used to create `T`.
82+
"""
83+
function get_Abc(T::Tableau)
84+
return copy(T.A), copy(T.b), copy(T.c)
85+
end
86+
87+
"""
88+
restore!(T::Tableau)
89+
90+
Restore a `Tableau` based on the original data used to create it.
91+
"""
92+
function restore!(T::Tableau)
93+
TT = restore(T)
94+
m, n = size(TT.M)
95+
for i in 1:m
96+
for j in 1:n
97+
T.M[i, j] = TT.M[i, j]
98+
end
99+
end
100+
return T
101+
end
102+
103+
"""
104+
value(T::Tableau, x::Vector)
105+
106+
Return the value of the LP in `T` at the point `x`.
107+
"""
108+
function value(T::Tableau, x::Vector)
109+
return T.c' * x
110+
end

0 commit comments

Comments
 (0)