Skip to content

Commit 1b69b84

Browse files
blegatodow
andauthored
Implement MOI.default_cache (#137)
Co-authored-by: odow <o.dowson@gmail.com>
1 parent d899fd9 commit 1b69b84

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
1010

1111
[compat]
1212
CEnum = "0.3, 0.4"
13-
ECOS_jll = "=2.0.8, 200.0.800"
14-
MathOptInterface = "1"
13+
ECOS_jll = "=2.0.8, =200.0.800"
14+
MathOptInterface = "1.1"
1515
julia = "1.6"
1616

1717
[extras]

src/MOI_wrapper/MOI_wrapper.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const OptimizerCache = MOI.Utilities.GenericModel{
5151
},
5252
}
5353

54+
Base.show(io::IO, ::Type{OptimizerCache}) = print(io, "ECOS.OptimizerCache")
55+
5456
mutable struct _Solution
5557
ret_val::Union{Nothing,idxint}
5658
primal::Vector{pfloat}
@@ -95,6 +97,10 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
9597
Optimizer() = new(nothing, nothing, _Solution(), false, Dict{Symbol,Any}())
9698
end
9799

100+
function MOI.default_cache(::Optimizer, ::Type{pfloat})
101+
return MOI.Utilities.UniversalFallback(OptimizerCache())
102+
end
103+
98104
function MOI.is_empty(optimizer::Optimizer)
99105
return optimizer.zeros === nothing && optimizer.cones === nothing
100106
end
@@ -311,6 +317,14 @@ function MOI.optimize!(dest::Optimizer, src::OptimizerCache)
311317
return MOI.Utilities.identity_index_map(src), false
312318
end
313319

320+
function MOI.copy_to(
321+
dest::Optimizer,
322+
src::MOI.Utilities.UniversalFallback{OptimizerCache},
323+
)
324+
MOI.Utilities.throw_unsupported(src)
325+
return MOI.copy_to(dest, src.model)
326+
end
327+
314328
function MOI.optimize!(dest::Optimizer, src::MOI.ModelLike)
315329
cache = OptimizerCache()
316330
index_map = MOI.copy_to(cache, src)

src/MOI_wrapper/permuted_exponential_cone.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
struct PermutedExponentialCone <: MOI.AbstractVectorSet end
55

66
Base.copy(set::PermutedExponentialCone) = set
7+
function MOI.Utilities.set_with_dimension(::Type{PermutedExponentialCone}, dim)
8+
if dim != 3
9+
error("Cannot create a `PermutedExponentialCone` with dimension $dim.")
10+
end
11+
return PermutedExponentialCone()
12+
end
713

814
struct PermutedExponentialBridge{T,F} <: MOI.Bridges.Constraint.SetMapBridge{
915
T,

test/MOI_wrapper.jl

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ function runtests()
1818
end
1919

2020
function test_runtests()
21-
model = MOI.instantiate(ECOS.Optimizer, with_bridge_type = Float64)
21+
model = MOI.Utilities.CachingOptimizer(
22+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
23+
MOI.instantiate(ECOS.Optimizer; with_bridge_type = Float64),
24+
)
25+
@test model.optimizer.model.model_cache isa
26+
MOI.Utilities.UniversalFallback{ECOS.OptimizerCache}
2227
MOI.set(model, MOI.Silent(), true)
2328
exclude = String[
24-
# Expected test failures:
25-
# Problem is a nonconvex QP (fixed in MOI 0.10.6)
26-
"test_basic_ScalarQuadraticFunction_EqualTo",
27-
"test_basic_ScalarQuadraticFunction_GreaterThan",
28-
"test_basic_ScalarQuadraticFunction_Interval",
29-
"test_basic_VectorQuadraticFunction_",
30-
"test_quadratic_SecondOrderCone_basic",
31-
"test_quadratic_nonconvex_",
32-
# MathOptInterface.jl issue #1431
33-
"test_model_LowerBoundAlreadySet",
34-
"test_model_UpperBoundAlreadySet",
29+
# ZerosBridge does not support ConstraintDual. These are tested below in
30+
# test_runtests_ZerosBridge
31+
"test_conic_RotatedSecondOrderCone_INFEASIBLE_2",
32+
"test_conic_linear_VectorOfVariables_2",
33+
"test_linear_integration",
34+
"test_quadratic_constraint_GreaterThan",
35+
"test_quadratic_constraint_LessThan",
3536
]
3637
if Sys.WORD_SIZE == 32
3738
# These tests fail on x86 Linux, returning ITERATION_LIMIT instead of
@@ -55,6 +56,39 @@ function test_runtests()
5556
return
5657
end
5758

59+
function test_runtests_ZerosBridge()
60+
optimizer = MOI.instantiate(ECOS.Optimizer; with_bridge_type = Float64)
61+
MOI.Bridges.remove_bridge(
62+
optimizer,
63+
MOI.Bridges.Variable.ZerosBridge{Float64},
64+
)
65+
model = MOI.Utilities.CachingOptimizer(
66+
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
67+
optimizer,
68+
)
69+
MOI.Test.runtests(
70+
model,
71+
MOI.Test.Config(
72+
atol = 1e-3,
73+
rtol = 1e-3,
74+
exclude = Any[
75+
MOI.ConstraintBasisStatus,
76+
MOI.VariableBasisStatus,
77+
MOI.ObjectiveBound,
78+
],
79+
),
80+
include = String[
81+
# ZerosBridge does not support ConstraintDual
82+
"test_conic_RotatedSecondOrderCone_INFEASIBLE_2",
83+
"test_conic_linear_VectorOfVariables_2",
84+
"test_linear_integration",
85+
"test_quadratic_constraint_GreaterThan",
86+
"test_quadratic_constraint_LessThan",
87+
],
88+
)
89+
return
90+
end
91+
5892
function test_RawOptimizerAttribute()
5993
model = ECOS.Optimizer()
6094
MOI.set(model, MOI.RawOptimizerAttribute("abstol"), 1e-5)

0 commit comments

Comments
 (0)