Skip to content

Commit a0b65c3

Browse files
committed
but fix with value
1 parent 58f57ed commit a0b65c3

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

src/Solver.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Solve `T` using the simplex method.
8686
"""
8787
function simplex_solve!(T::Tableau, verbose::Bool=true)
8888
stat = status(T)
89-
if stat == :infeasible
89+
if stat == :infeasible
9090
if verbose
9191
@info "Infeasible basis detected. Restoring tableau to its initial state."
9292
end

src/Status.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ end
2121
status(T::Tableau)::Symbol
2222
2323
Return an indicator for the status of the tableau `T` being one of:
24-
* `:no_basis` -- no basis has been established for this tableau.
25-
* `:optimal` -- the tableau has reached a global minimization point.
24+
* `:no_basis` -- no basis has been established for this tableau
2625
* `:feasible` -- the tableau is in a feasible state, but not optimal (rhs is nonnegative).
2726
* `:infeasible` -- the tableau is in an infeasible state (rhs contains negative values).
27+
* `:optimal` -- the tableau has reached a global minimization point.
28+
* `:unbounded` -- no pivots are possible; objective function can be arbitrarily negative.
2829
"""
2930
function status(T::Tableau)::Symbol
3031
# check for a basis
@@ -40,6 +41,10 @@ function status(T::Tableau)::Symbol
4041

4142
# if not optimal, but feasible
4243
if all(x .>= 0)
44+
i, j = find_pivot(T)
45+
if i==0
46+
return :unbounded
47+
end
4348
return :feasible
4449
end
4550

src/Tableau.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ Return the value of the LP in `T` at the point `x`.
120120
This can also be invoked as `T(x)`.
121121
"""
122122
function value(T::Tableau, x::Vector)
123+
n = T.n_vars
124+
125+
if length(x) > n
126+
x = x[1:n] # trim if too long
127+
end
128+
129+
if length(x) < n # pad if too short
130+
x = vcat(x, zeros(Int, n-length(x)))
131+
end
132+
123133
return T.c' * x
124134
end
125135

@@ -129,8 +139,7 @@ end
129139
Return the value of the LP at the current basic vector.
130140
"""
131141
function value(T::Tableau)
132-
x = basic_vector(T)
133-
return value(T, x)
142+
return T.M[1, end]
134143
end
135144

136145
function (T::Tableau)(x::Vector)

test/runtests.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ using SimplexTableaux
1818

1919
set_basis!(T, [3, 4])
2020
@test !in_feasible_state(T)
21+
22+
lp_solve(T)
2123
end
2224

2325
@testset "Duality" begin
@@ -28,10 +30,25 @@ end
2830
T = Tableau(A, b, c)
2931
dT = dual(T)
3032

31-
simplex_solve!(T, false)
33+
x = simplex_solve!(T, false)
3234
v = value(T)
35+
@test T(x) == v
3336

3437
simplex_solve!(dT, false)
3538
dv = value(dT)
3639
@test v == -dv
3740
end
41+
42+
@testset "Status" begin
43+
A, b, c = ([-1 -2 0 0; -3 -4 0 0; 0 0 1 2; 0 0 3 4], [2, 2, 4, 5], [2, 3, -4, -7])
44+
T = Tableau(A, b, c)
45+
@test status(T) == :no_basis
46+
set_basis!(T, [2, 3, 4, 5])
47+
@test status(T) == :infeasible
48+
49+
T = Tableau([2 1 0 9 -1; 1 1 -1 5 1], [9, 7], [2, 4, 2, 1, -1])
50+
set_basis!(T, [1, 2])
51+
@test status(T) == :feasible
52+
simplex_solve!(T, false)
53+
@test status(T) == :unbounded
54+
end

0 commit comments

Comments
 (0)