From efebdcd8afffab9fa95429e7329daf7304a1d3bf Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 10 Apr 2023 08:58:47 +0200 Subject: [PATCH] outsource solver code to separate file --- src/Initialization/init_Polyhedra.jl | 8 ----- .../AbstractPolyhedron_functions.jl | 33 ------------------- src/Interfaces/LazySet.jl | 10 ++++++ src/LazySets.jl | 1 + src/Utils/lp_solvers.jl | 27 +++++++++++++++ 5 files changed, 38 insertions(+), 41 deletions(-) create mode 100644 src/Utils/lp_solvers.jl diff --git a/src/Initialization/init_Polyhedra.jl b/src/Initialization/init_Polyhedra.jl index c4866d6461..2b32b61fae 100644 --- a/src/Initialization/init_Polyhedra.jl +++ b/src/Initialization/init_Polyhedra.jl @@ -4,14 +4,6 @@ eval(quote using .Polyhedra: HRep, VRep, removehredundancy!, removevredundancy! - function default_polyhedra_backend(P::LazySet{N}) where {N} - if LazySets.dim(P) == 1 - return default_polyhedra_backend_1d(N) - else - return default_polyhedra_backend_nd(N) - end - end - function default_polyhedra_backend_1d(N::Type{<:Number}, solver=nothing) return Polyhedra.IntervalLibrary{N}() end diff --git a/src/Interfaces/AbstractPolyhedron_functions.jl b/src/Interfaces/AbstractPolyhedron_functions.jl index 75a66e7b39..675a7a9494 100644 --- a/src/Interfaces/AbstractPolyhedron_functions.jl +++ b/src/Interfaces/AbstractPolyhedron_functions.jl @@ -8,39 +8,6 @@ export constrained_dimensions, an_element, vertices_list -# default LP solver for floating-point numbers -function default_lp_solver(N::Type{<:AbstractFloat}) - JuMP.optimizer_with_attributes(() -> GLPK.Optimizer(method=GLPK.SIMPLEX)) -end - -# default LP solver for rational numbers -function default_lp_solver(N::Type{<:Rational}) - JuMP.optimizer_with_attributes(() -> GLPK.Optimizer(method=GLPK.EXACT)) -end - -# helper function given two possibly different numeric types -function default_lp_solver(M::Type{<:Number}, N::Type{<:Number}) - return default_lp_solver(promote_type(M, N)) -end - -# Polyhedra backend (fallback method) -function default_polyhedra_backend(P::LazySet{N}) where {N} - require(@__MODULE__, :Polyhedra; fun_name="default_polyhedra_backend") - error("no default backend for numeric type $N") -end - -# default LP solver for Polyhedra (fallback method) -# NOTE: exists in parallel to `default_lp_solver` because we use different -# interfaces (see #1493) -function default_lp_solver_polyhedra(N; kwargs...) - require(@__MODULE__, :Polyhedra; fun_name="default_lp_solver_polyhedra") - error("no default solver for numeric type $N") -end - -function _is_polyhedra_backend(backend) - return false -end - isconvextype(::Type{<:AbstractPolyhedron}) = true is_polyhedral(::AbstractPolyhedron) = true diff --git a/src/Interfaces/LazySet.jl b/src/Interfaces/LazySet.jl index 64f4c8cc31..7a3c80eb31 100644 --- a/src/Interfaces/LazySet.jl +++ b/src/Interfaces/LazySet.jl @@ -212,6 +212,16 @@ true """ isconvextype(X::Type{<:LazySet}) = false +# Polyhedra backend (fallback method) +function default_polyhedra_backend(P::LazySet{N}) where {N} + require(@__MODULE__, :Polyhedra; fun_name="default_polyhedra_backend") + if LazySets.dim(P) == 1 + return default_polyhedra_backend_1d(N) + else + return default_polyhedra_backend_nd(N) + end +end + # Note: this method cannot be documented due to a bug in Julia function low(X::LazySet, i::Int) return _low(X, i) diff --git a/src/LazySets.jl b/src/LazySets.jl index ba777af1b6..6b70843716 100644 --- a/src/LazySets.jl +++ b/src/LazySets.jl @@ -52,6 +52,7 @@ include("Utils/numbers.jl") include("Utils/helper_functions.jl") include("Utils/macros.jl") include("Utils/matrix_exponential.jl") +include("Utils/lp_solvers.jl") include("Utils/file_formats.jl") # ================== diff --git a/src/Utils/lp_solvers.jl b/src/Utils/lp_solvers.jl new file mode 100644 index 0000000000..299b295fe4 --- /dev/null +++ b/src/Utils/lp_solvers.jl @@ -0,0 +1,27 @@ +# default LP solver for floating-point numbers +function default_lp_solver(::Type{<:AbstractFloat}) + JuMP.optimizer_with_attributes(() -> GLPK.Optimizer(method=GLPK.SIMPLEX)) +end + +# default LP solver for rational numbers +function default_lp_solver(::Type{<:Rational}) + JuMP.optimizer_with_attributes(() -> GLPK.Optimizer(method=GLPK.EXACT)) +end + +# default LP solver given two possibly different numeric types +function default_lp_solver(M::Type{<:Number}, N::Type{<:Number}) + return default_lp_solver(promote_type(M, N)) +end + +# check for Polyhedra backend (fallback method) +function _is_polyhedra_backend(backend) + return false +end + +# default LP solver for Polyhedra (fallback method) +# NOTE: exists in parallel to `default_lp_solver` because we use different +# interfaces (see #1493) +function default_lp_solver_polyhedra(N, kwargs...) + require(@__MODULE__, :Polyhedra; fun_name="default_lp_solver_polyhedra") + error("no default solver for numeric type $N") +end