Skip to content

Commit 53aaf99

Browse files
committed
move 'isfeasible' to util section
1 parent d5bba94 commit 53aaf99

File tree

4 files changed

+45
-47
lines changed

4 files changed

+45
-47
lines changed

src/Interfaces/AbstractPolyhedron.jl

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ function _linear_map_hrep_helper(M::AbstractMatrix, P::LazySet,
7676
end
7777

7878
# internal functions; defined here due to dependency SymEngine and submodules
79-
function isfeasible end
8079
function remove_redundant_constraints end
8180
function remove_redundant_constraints! end
8281
function _ishalfspace end

src/Interfaces/AbstractPolyhedron_functions.jl

-44
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export constrained_dimensions,
22
remove_redundant_constraints,
33
remove_redundant_constraints!,
4-
isfeasible,
54
addconstraint!,
65
ishyperplanar
76

@@ -952,49 +951,6 @@ function _project_polyhedron(P::LazySet, block; kwargs...)
952951
return constraints_list(πP)
953952
end
954953

955-
"""
956-
isfeasible(A::AbstractMatrix, b::AbstractVector, [witness]::Bool=false;
957-
[solver]=nothing)
958-
959-
Check for feasibility of linear constraints given in matrix-vector form.
960-
961-
### Input
962-
963-
- `A` -- constraints matrix
964-
- `b` -- constraints vector
965-
- `witness` -- (optional; default: `false`) flag for witness production
966-
- `solver` -- (optional; default: `nothing`) LP solver
967-
968-
### Output
969-
970-
If `witness` is `false`, the result is a `Bool`.
971-
972-
If `witness` is `true`, the result is a pair `(res, w)` where `res` is a `Bool`
973-
and `w` is a witness point/vector.
974-
975-
### Algorithm
976-
977-
This implementation solves the corresponding feasibility linear program.
978-
"""
979-
function isfeasible(A::AbstractMatrix, b::AbstractVector, witness::Bool=false;
980-
solver=nothing)
981-
N = promote_type(eltype(A), eltype(b))
982-
# feasibility LP
983-
lbounds, ubounds = -Inf, Inf
984-
sense = '<'
985-
obj = zeros(N, size(A, 2))
986-
if isnothing(solver)
987-
solver = default_lp_solver(N)
988-
end
989-
lp = linprog(obj, A, sense, b, lbounds, ubounds, solver)
990-
if is_lp_optimal(lp.status)
991-
return witness ? (true, lp.sol) : true
992-
elseif is_lp_infeasible(lp.status)
993-
return witness ? (false, N[]) : false
994-
end
995-
return error("LP returned status $(lp.status) unexpectedly")
996-
end
997-
998954
# convenience function to invert the result of `isfeasible` while still including the witness result
999955
function _isinfeasible(constraints::AbstractVector{<:HalfSpace},
1000956
witness::Bool=false; solver=nothing)

src/Sets/HalfSpace/isfeasible.jl

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
"""
42
isfeasible(constraints::AbstractVector{<:HalfSpace}, [witness]::Bool=false;
53
[solver]=nothing)

src/Utils/lp_solvers.jl

+45
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export isfeasible
2+
13
function default_lp_solver(::Type{T}) where {T}
24
key = task_local_lp_solver_key(T)
35
LP = get!(() -> JuMP.Model(default_lp_solver_factory(T)), task_local_storage(), key)
@@ -35,3 +37,46 @@ function default_lp_solver_polyhedra(N; kwargs...)
3537
require(@__MODULE__, :Polyhedra; fun_name="default_lp_solver_polyhedra")
3638
return error("no default solver for numeric type $N")
3739
end
40+
41+
"""
42+
isfeasible(A::AbstractMatrix, b::AbstractVector, [witness]::Bool=false;
43+
[solver]=nothing)
44+
45+
Check for feasibility of linear constraints given in matrix-vector form.
46+
47+
### Input
48+
49+
- `A` -- constraints matrix
50+
- `b` -- constraints vector
51+
- `witness` -- (optional; default: `false`) flag for witness production
52+
- `solver` -- (optional; default: `nothing`) LP solver
53+
54+
### Output
55+
56+
If `witness` is `false`, the result is a `Bool`.
57+
58+
If `witness` is `true`, the result is a pair `(res, w)` where `res` is a `Bool`
59+
and `w` is a witness point/vector.
60+
61+
### Algorithm
62+
63+
This implementation solves the corresponding feasibility linear program.
64+
"""
65+
function isfeasible(A::AbstractMatrix, b::AbstractVector, witness::Bool=false;
66+
solver=nothing)
67+
N = promote_type(eltype(A), eltype(b))
68+
# feasibility LP
69+
lbounds, ubounds = -Inf, Inf
70+
sense = '<'
71+
obj = zeros(N, size(A, 2))
72+
if isnothing(solver)
73+
solver = default_lp_solver(N)
74+
end
75+
lp = linprog(obj, A, sense, b, lbounds, ubounds, solver)
76+
if is_lp_optimal(lp.status)
77+
return witness ? (true, lp.sol) : true
78+
elseif is_lp_infeasible(lp.status)
79+
return witness ? (false, N[]) : false
80+
end
81+
return error("LP returned status $(lp.status) unexpectedly")
82+
end

0 commit comments

Comments
 (0)