Skip to content

Commit b159d03

Browse files
committed
Update to use MOI.Utilities.RuntimeProductOfSets
1 parent 780b85c commit b159d03

7 files changed

Lines changed: 50 additions & 222 deletions

File tree

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ MathOptInterface = "1.18"
2525
MathOptSetDistances = "0.2.9"
2626
ParametricOptInterface = "0.15.3"
2727
julia = "1.10"
28+
29+
[sources]
30+
MathOptInterface = {url = "https://github.com/jump-dev/MathOptInterface.jl", rev = "od/runtime-product-of-sets"}

src/ConicProgram/ConicProgram.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const Form{T} = MOI.Utilities.GenericModel{
4747
MOI.Utilities.OneBasedIndexing,
4848
},
4949
Vector{T},
50-
DiffOpt.ProductOfSets{T},
50+
MOI.Utilities.RuntimeProductOfSets{T},
5151
},
5252
}
5353

@@ -133,7 +133,8 @@ function MOI.supports_constraint(
133133
F::Type{MOI.VectorAffineFunction{Float64}},
134134
::Type{S},
135135
) where {S<:MOI.AbstractVectorSet}
136-
if DiffOpt.add_set_types(model.model.constraints.sets, S)
136+
if MOI.Utilities.set_index(model.model.constraints.sets, S) === nothing
137+
MOI.Utilities.add_set_type(model.model.constraints.sets, S)
137138
push!(model.model.constraints.caches, Tuple{F,S}[])
138139
push!(model.model.constraints.are_indices_mapped, BitSet())
139140
end

src/DiffOpt.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import ParametricOptInterface as POI
1616
import SparseArrays
1717

1818
include("utils.jl")
19-
include("product_of_sets.jl")
2019
include("diff_opt.jl")
2120
include("moi_wrapper.jl")
2221
include("parameters.jl")

src/diff_opt.jl

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -659,14 +659,22 @@ function MOI.get(
659659
end
660660

661661
"""
662-
π(v::Vector{Float64}, model::MOI.ModelLike, cones::ProductOfSets)
662+
π(
663+
v::Vector{T},
664+
model::MOI.ModelLike,
665+
cones::MOI.Utilities.RuntimeProductOfSets,
666+
) where {T}
663667
664668
Given a `model`, its `cones`, find the projection of the vectors `v`
665669
of length equal to the number of rows in the conic form onto the cartesian
666670
product of the cones corresponding to these rows.
667671
For more info, refer to https://github.com/matbesancon/MathOptSetDistances.jl
668672
"""
669-
function π(v::Vector{T}, model::MOI.ModelLike, cones::ProductOfSets) where {T}
673+
function π(
674+
v::Vector{T},
675+
model::MOI.ModelLike,
676+
cones::MOI.Utilities.RuntimeProductOfSets,
677+
) where {T}
670678
return map_rows(model, cones, Flattened{T}()) do ci, r
671679
return MOSD.projection_on_set(
672680
MOSD.DefaultDistance(),
@@ -677,14 +685,22 @@ function π(v::Vector{T}, model::MOI.ModelLike, cones::ProductOfSets) where {T}
677685
end
678686

679687
"""
680-
Dπ(v::Vector{Float64}, model, cones::ProductOfSets)
688+
Dπ(
689+
v::Vector{T},
690+
model::MOI.ModelLike,
691+
cones::MOI.Utilities.RuntimeProductOfSets,
692+
) where {T}
681693
682694
Given a `model`, its `cones`, find the gradient of the projection of
683695
the vectors `v` of length equal to the number of rows in the conic form onto the
684696
cartesian product of the cones corresponding to these rows.
685697
For more info, refer to https://github.com/matbesancon/MathOptSetDistances.jl
686698
"""
687-
function (v::Vector{T}, model::MOI.ModelLike, cones::ProductOfSets) where {T}
699+
function (
700+
v::Vector{T},
701+
model::MOI.ModelLike,
702+
cones::MOI.Utilities.RuntimeProductOfSets,
703+
) where {T}
688704
return BlockDiagonals.BlockDiagonal(
689705
map_rows(model, cones, Nested{Matrix{T}}()) do ci, r
690706
return MOSD.projection_gradient_on_set(
@@ -716,7 +732,7 @@ function _map_rows!(
716732
f::Function,
717733
x::Vector,
718734
model,
719-
cones::ProductOfSets,
735+
cones::MOI.Utilities.RuntimeProductOfSets,
720736
::Type{F},
721737
::Type{S},
722738
map_mode,
@@ -732,7 +748,10 @@ end
732748

733749
# Allocate a vector for storing the output of `map_rows`.
734750
function _allocate_rows(cones, ::Nested{T}) where {T}
735-
n = mapreduce(length, +, cones.rows; init = 0)
751+
n = 0
752+
for (F, S) in MOI.get(cones, MOI.ListOfConstraintTypesPresent())
753+
n += MOI.get(cones, MOI.NumberOfConstraints{F,S}())
754+
end
736755
return Vector{T}(undef, n)
737756
end
738757

@@ -741,7 +760,12 @@ function _allocate_rows(cones, ::Flattened{T}) where {T}
741760
end
742761

743762
"""
744-
map_rows(f::Function, model, cones::ProductOfSets, map_mode::Union{Nested{T}, Flattened{T}})
763+
map_rows(
764+
f::Function,
765+
model,
766+
cones::MOI.Utilities.RuntimeProductOfSets,
767+
map_mode::Union{Nested{T},Flattened{T}},
768+
)
745769
746770
Given a `model`, its `cones` and `map_mode` of type `Nested` (resp.
747771
`Flattened`), return a `Vector{T}` of length equal to the number of cones (resp.
@@ -753,7 +777,7 @@ form.
753777
function map_rows(
754778
f::Function,
755779
model,
756-
cones::ProductOfSets,
780+
cones::MOI.Utilities.RuntimeProductOfSets,
757781
map_mode::Union{Nested,Flattened},
758782
)
759783
x = _allocate_rows(cones, map_mode)

src/product_of_sets.jl

Lines changed: 0 additions & 193 deletions
This file was deleted.

test/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ HiGHS = "1"
2525
Ipopt = "1.0.2"
2626
SCS = "1"
2727
MLDatasets = "0.7.18"
28+
29+
[sources]
30+
MathOptInterface = {url = "https://github.com/jump-dev/MathOptInterface.jl", rev = "od/runtime-product-of-sets"}

test/conic_program.jl

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,10 @@ function test_simple_psd()
181181
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
182182
MOI.optimize!(model)
183183
x = MOI.get(model, MOI.VariablePrimal(), X)
184-
cone_types = unique([
185-
S for (F, S) in
186-
MOI.get(model.optimizer, MOI.ListOfConstraintTypesPresent())
187-
])
188184
conic_form = DiffOpt.ConicProgram.Form{Float64}()
189-
cones = conic_form.constraints.sets
190-
DiffOpt.set_set_types(cones, cone_types)
185+
for (F, S) in MOI.get(model.optimizer, MOI.ListOfConstraintTypesPresent())
186+
MOI.Utilities.add_set_type(conic_form.constraints.sets, S)
187+
end
191188
index_map = MOI.copy_to(conic_form, model)
192189
# s = DiffOpt.map_rows((ci, r) -> MOI.get(model.optimizer, MOI.ConstraintPrimal(), ci), model.optimizer, cones, index_map, DiffOpt.Flattened{Float64}())
193190
# y = DiffOpt.map_rows((ci, r) -> MOI.get(model.optimizer, MOI.ConstraintDual(), ci), model.optimizer, cones, index_map, DiffOpt.Flattened{Float64}())
@@ -337,13 +334,10 @@ function test_differentiating_conic_with_PSD_and_SOC_constraints()
337334
MOI.optimize!(model)
338335
_x = MOI.get(model, MOI.VariablePrimal(), x)
339336
_X = MOI.get(model, MOI.VariablePrimal(), X)
340-
cone_types = unique([
341-
S for (F, S) in
342-
MOI.get(model.optimizer, MOI.ListOfConstraintTypesPresent())
343-
])
344337
conic_form = DiffOpt.ConicProgram.Form{Float64}()
345-
cones = conic_form.constraints.sets
346-
DiffOpt.set_set_types(cones, cone_types)
338+
for (F, S) in MOI.get(model.optimizer, MOI.ListOfConstraintTypesPresent())
339+
MOI.Utilities.add_set_type(conic_form.constraints.sets, S)
340+
end
347341
index_map = MOI.copy_to(conic_form, model)
348342
# s = DiffOpt.map_rows((ci, r) -> MOI.get(model.optimizer, MOI.ConstraintPrimal(), ci), model.optimizer, cones, index_map, DiffOpt.Flattened{Float64}())
349343
# y = DiffOpt.map_rows((ci, r) -> MOI.get(model.optimizer, MOI.ConstraintDual(), ci), model.optimizer, cones, index_map, DiffOpt.Flattened{Float64}())
@@ -725,13 +719,10 @@ function test_differentiating_simple_PSD_back()
725719
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
726720
MOI.optimize!(model)
727721
x = MOI.get(model, MOI.VariablePrimal(), X)
728-
cone_types = unique([
729-
S for (F, S) in
730-
MOI.get(model.optimizer, MOI.ListOfConstraintTypesPresent())
731-
])
732722
conic_form = DiffOpt.ConicProgram.Form{Float64}()
733-
cones = conic_form.constraints.sets
734-
DiffOpt.set_set_types(cones, cone_types)
723+
for (F, S) in MOI.get(model.optimizer, MOI.ListOfConstraintTypesPresent())
724+
MOI.Utilities.add_set_type(conic_form.constraints.sets, S)
725+
end
735726
index_map = MOI.copy_to(conic_form, model)
736727
@test x ones(3) atol = ATOL rtol = RTOL
737728
MOI.set(model, DiffOpt.ReverseVariablePrimal(), X[1], 1.0)

0 commit comments

Comments
 (0)