Skip to content

Commit eda206c

Browse files
authored
Use dict for cone types (#9)
1 parent d64c93d commit eda206c

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/conic_form.jl

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ mutable struct ConicForm{T, AT, VT, C} <: MOI.ModelLike
77
b::VT # `b - Ax in cones`
88
c::VT # `sense c'x + objective_constant`
99
cone_types::C
10+
cone_types_dict::Dict{DataType, Int}
1011

1112
function ConicForm{T, AT, VT}(cone_types) where {T, AT, VT}
1213
model = new{T, AT, VT, typeof(cone_types)}()
1314
model.cone_types = cone_types
15+
model.cone_types_dict = Dict{DataType, Int}(
16+
s => i for (i, s) in enumerate(cone_types)
17+
)
1418
model.num_rows = zeros(Int, length(cone_types))
1519
model.dimension = Dict{Int, Int}()
1620
model.A = nothing
@@ -24,7 +28,7 @@ const CONES = Dict(
2428
MOI.Nonnegatives => 2,
2529
MOI.SecondOrderCone => 3,
2630
MOI.PositiveSemidefiniteConeTriangle => 4,
27-
MOI.ExponentialCone => 5,
31+
MOI.ExponentialCone => 5,
2832
MOI.DualExponentialCone => 6
2933
)
3034

@@ -37,7 +41,7 @@ cons_offset(conic::MOI.PositiveSemidefiniteConeTriangle) = Int64((conic.side_dim
3741
function get_conic_form(::Type{T}, model::M, con_idx) where {T, M <: MOI.AbstractOptimizer}
3842
# reorder constraints
3943
cis = sort(
40-
con_idx,
44+
con_idx,
4145
by = x->CONES[_set_type(x)]
4246
)
4347

@@ -66,21 +70,11 @@ function MOI.empty!(model::ConicForm{T}) where T
6670
model.objective_constant = zero(T)
6771
end
6872

69-
function _first(::Type{S}, idx::Int, ::Type{S}, args::Vararg{Type, N}) where {S, N}
70-
return idx
71-
end
72-
function _first(::Type{S}, idx::Int, ::Type, args::Vararg{Type, N}) where {S, N}
73-
return _first(S, idx + 1, args...)
74-
end
75-
_first(::Type, idx::Int) = nothing
76-
77-
_findfirst(::Type{S}, sets::Tuple) where {S} = _first(S, 1, sets...)
78-
7973
function MOI.supports_constraint(
8074
model::ConicForm,
8175
::Type{MOI.VectorAffineFunction{Float64}},
8276
::Type{S}) where S <: MOI.AbstractVectorSet
83-
return _findfirst(S, model.cone_types) !== nothing
77+
return haskey(model.cone_types_dict, S)
8478
end
8579

8680
function _allocate_variables(model::ConicForm{T, AT, VT}, vis_src, idxmap) where {T, AT, VT}
@@ -176,7 +170,7 @@ function MOI.copy_to(dest::ConicForm, src::MOI.ModelLike; copy_names::Bool=true)
176170

177171
has_constraints = BitSet()
178172
for (F, S) in MOI.get(src, MOI.ListOfConstraints())
179-
i = _findfirst(S, dest.cone_types)
173+
i = get(dest.cone_types_dict, S, nothing)
180174
if i === nothing || F != MOI.VectorAffineFunction{Float64}
181175
throw(MOI.UnsupportedConstraint{F, S}())
182176
end

0 commit comments

Comments
 (0)