Skip to content

Commit 6011045

Browse files
committed
Phase one basis solver working
1 parent 85eb9e8 commit 6011045

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed

development/random-examples.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
using SimplexTableaux, LinearAlgebra, ProgressMeter
22

33
"""
4-
random_example(n_vars::Int, n_cons::Int, modulus::Int=10)
4+
random_example(n_vars::Int, n_cons::Int, lo::Int, hi::Int, canonical::Bool=true)::Tableau
55
66
Create a random instance of a `Tableau` with `n_vars` variables,
77
`n_cons` constraints in which all data are chosen IID uniformly
8-
from `{1,2,...,modulus}`.
8+
from `{lo, ..., hi}`.
99
"""
10-
function random_example(n_vars::Int, n_cons::Int, modulus::Int=10)::Tableau
11-
f(x::Int) = mod1(x, modulus)
12-
A = f.(rand(Int, n_cons, n_vars))
13-
b = f.(rand(Int, n_cons))
14-
c = f.(rand(Int, n_vars))
15-
return Tableau(A, b, c)
10+
function random_example(
11+
n_vars::Int, n_cons::Int, lo::Int, hi::Int, canonical::Bool=true
12+
)::Tableau
13+
A = rand(lo:hi, n_cons, n_vars)
14+
b = rand(lo:hi, n_cons)
15+
c = rand(lo:hi, n_vars)
16+
return Tableau(A, b, c, canonical)
1617
end
1718

1819
"""

src/Bases.jl

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,52 @@
33
44
Return a feasible basis for the LP in `T` or `nothing` if none exists.
55
"""
6-
function find_a_basis(T::Tableau)::Vector{Int}
7-
r, c = size(T.A)
8-
for B in combinations(1:c, r)
9-
TT = set_basis!(T, B)
10-
if is_feasible(TT)
11-
return B
6+
function find_a_basis(T::Tableau)
7+
A, b, c = get_Abc(T)
8+
m = T.n_cons
9+
n = T.n_vars
10+
11+
# ensure RHS is nonnegative
12+
for i in 1:m
13+
if b[i] < 0
14+
b[i] = -b[i]
15+
A[i, :] = -A[i, :]
1216
end
1317
end
14-
@info "No basis found"
15-
return zeros(Int, r)
18+
19+
# glue an identity matrix to RHS
20+
A = hcat(A, eye(Int, m))
21+
22+
# create artifical c
23+
c = vcat(0*c, ones(Int, m))
24+
25+
TT = Tableau(A, b, c, false)
26+
27+
B = collect((n + 1):(n + m))
28+
set_basis!(TT, B)
29+
30+
simplex_solve!(TT, false)
31+
v = value(TT)
32+
if v>0
33+
@info "No basis found."
34+
return 0*get_basis(TT)
35+
end
36+
37+
return TT.B
1638
end
1739

40+
# function find_a_basis(T::Tableau)::Vector{Int}
41+
# r, c = size(T.A)
42+
# for B in combinations(1:c, r)
43+
# TT = set_basis!(T, B)
44+
# if is_feasible(TT)
45+
# return B
46+
# end
47+
# end
48+
# @info "No basis found"
49+
# return zeros(Int, r)
50+
# end
51+
1852
"""
1953
find_all_bases(T::Tableau)
2054

src/LPsolve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function lp_solve(T::Tableau, verbose::Bool=true)
2323
end
2424
if verbose
2525
obj_val = objective_value(MOD)
26-
println("Optimum value = $obj_val\n")
26+
println("Minimial objective value = $obj_val\n")
2727
end
2828

2929
xval = JuMP.value.(x)

src/Solver.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function simplex_solve!(T::Tableau, verbose::Bool=true)
120120

121121
if verbose
122122
println("Optimality reached. Pivot count = $pivot_count")
123-
println("Value = $v = $(Float64(value(T)))")
123+
println("Minimal value = $v = $(Float64(value(T)))")
124124
end
125125

126126
return x

0 commit comments

Comments
 (0)