Skip to content

Commit b54ae5d

Browse files
committed
Move dimension update to MOI
1 parent 6da195d commit b54ae5d

File tree

6 files changed

+69
-36
lines changed

6 files changed

+69
-36
lines changed

docs/src/apireference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ Functions for getting properties of sets.
254254
```@docs
255255
dimension
256256
constant(s::EqualTo)
257+
supports_dimension_update
258+
update_dimension
257259
```
258260

259261
### Scalar sets
@@ -644,8 +646,6 @@ Utilities.vectorize
644646

645647
The following utilities are available for sets:
646648
```@docs
647-
Utilities.supports_dimension_update
648-
Utilities.update_dimension
649649
Utilities.shift_constant
650650
```
651651

src/Test/modellike.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@ function delete_test(model::MOI.ModelLike)
488488
@test MOI.get(model, MOI.ConstraintSet(), cx) == MOI.GreaterThan(0.0)
489489
@test MOI.get(model, MOI.ConstraintFunction(), cy) == MOI.VectorOfVariables(y)
490490
@test MOI.get(model, MOI.ConstraintSet(), cy) == MOI.Nonpositives(4)
491+
@test Set(MOI.get(model, MOI.ListOfConstraints())) ==
492+
Set([(MOI.SingleVariable, MOI.GreaterThan{Float64}),
493+
(MOI.VectorOfVariables, MOI.Nonpositives)])
494+
@test MOI.get(model, MOI.ListOfConstraintIndices{
495+
MOI.SingleVariable, MOI.GreaterThan{Float64}}()) == [cx]
496+
@test MOI.get(model, MOI.ListOfConstraintIndices{
497+
MOI.VectorOfVariables, MOI.Nonpositives}()) == [cy]
491498
MOI.delete(model, y[3])
492499
@test MOI.is_valid(model, x)
493500
@test MOI.is_valid(model, y[1])
@@ -500,6 +507,13 @@ function delete_test(model::MOI.ModelLike)
500507
@test MOI.get(model, MOI.ConstraintSet(), cx) == MOI.GreaterThan(0.0)
501508
@test MOI.get(model, MOI.ConstraintFunction(), cy) == MOI.VectorOfVariables(y[[1, 2, 4]])
502509
@test MOI.get(model, MOI.ConstraintSet(), cy) == MOI.Nonpositives(3)
510+
@test Set(MOI.get(model, MOI.ListOfConstraints())) ==
511+
Set([(MOI.SingleVariable, MOI.GreaterThan{Float64}),
512+
(MOI.VectorOfVariables, MOI.Nonpositives)])
513+
@test MOI.get(model, MOI.ListOfConstraintIndices{
514+
MOI.SingleVariable, MOI.GreaterThan{Float64}}()) == [cx]
515+
@test MOI.get(model, MOI.ListOfConstraintIndices{
516+
MOI.VectorOfVariables, MOI.Nonpositives}()) == [cy]
503517
MOI.delete(model, y[1])
504518
@test MOI.is_valid(model, x)
505519
@test !MOI.is_valid(model, y[1])
@@ -512,6 +526,13 @@ function delete_test(model::MOI.ModelLike)
512526
@test MOI.get(model, MOI.ConstraintSet(), cx) == MOI.GreaterThan(0.0)
513527
@test MOI.get(model, MOI.ConstraintFunction(), cy) == MOI.VectorOfVariables(y[[2, 4]])
514528
@test MOI.get(model, MOI.ConstraintSet(), cy) == MOI.Nonpositives(2)
529+
@test Set(MOI.get(model, MOI.ListOfConstraints())) ==
530+
Set([(MOI.SingleVariable, MOI.GreaterThan{Float64}),
531+
(MOI.VectorOfVariables, MOI.Nonpositives)])
532+
@test MOI.get(model, MOI.ListOfConstraintIndices{
533+
MOI.SingleVariable, MOI.GreaterThan{Float64}}()) == [cx]
534+
@test MOI.get(model, MOI.ListOfConstraintIndices{
535+
MOI.VectorOfVariables, MOI.Nonpositives}()) == [cy]
515536
MOI.delete(model, x)
516537
@test !MOI.is_valid(model, x)
517538
@test !MOI.is_valid(model, y[1])
@@ -522,6 +543,12 @@ function delete_test(model::MOI.ModelLike)
522543
@test MOI.is_valid(model, cy)
523544
@test MOI.get(model, MOI.ConstraintFunction(), cy) == MOI.VectorOfVariables(y[[2, 4]])
524545
@test MOI.get(model, MOI.ConstraintSet(), cy) == MOI.Nonpositives(2)
546+
@test MOI.get(model, MOI.ListOfConstraints()) ==
547+
[(MOI.VectorOfVariables, MOI.Nonpositives)]
548+
@test isempty(MOI.get(model, MOI.ListOfConstraintIndices{
549+
MOI.SingleVariable, MOI.GreaterThan{Float64}}()))
550+
@test MOI.get(model, MOI.ListOfConstraintIndices{
551+
MOI.VectorOfVariables, MOI.Nonpositives}()) == [cy]
525552
MOI.delete(model, y[[2, 4]])
526553
@test !MOI.is_valid(model, x)
527554
@test !MOI.is_valid(model, y[1])
@@ -530,4 +557,9 @@ function delete_test(model::MOI.ModelLike)
530557
@test !MOI.is_valid(model, y[4])
531558
@test !MOI.is_valid(model, cx)
532559
@test !MOI.is_valid(model, cy)
560+
@test isempty(MOI.get(model, MOI.ListOfConstraints()))
561+
@test isempty(MOI.get(model, MOI.ListOfConstraintIndices{
562+
MOI.SingleVariable, MOI.GreaterThan{Float64}}()))
563+
@test isempty(MOI.get(model, MOI.ListOfConstraintIndices{
564+
MOI.VectorOfVariables, MOI.Nonpositives}()))
533565
end

src/Utilities/model.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ remove_variable(f, s, vi::VI) = remove_variable(f, vi), s
115115
function remove_variable(f::MOI.VectorOfVariables, s, vi::VI)
116116
g = remove_variable(f, vi)
117117
if length(g.variables) != length(f.variables)
118-
t = update_dimension(s, length(g.variables))
118+
t = MOI.update_dimension(s, length(g.variables))
119119
else
120120
t = s
121121
end
@@ -144,7 +144,7 @@ function _vector_of_variables_with(
144144
if length(f.variables) > 1
145145
# If `supports_dimension_update(s)` then the variable will be
146146
# removed in `_remove_variable`.
147-
if !supports_dimension_update(typeof(s))
147+
if !MOI.supports_dimension_update(typeof(s))
148148
throw_delete_variable_in_vov(vi)
149149
end
150150
else

src/Utilities/sets.jl

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,3 @@
1-
"""
2-
supports_dimension_update(S::Type{<:MOI.AbstractVectorSet})
3-
4-
Return a `Bool` indicating whether the elimination of any dimension of
5-
`n`-dimensional sets of type `S` give an `n-1`-dimensional set `S`.
6-
By default, this function returns `false` so it should only be implemented
7-
for sets that supports dimension update.
8-
9-
For instance, `supports_dimension_update(MOI.Nonnegatives}` is `true` because
10-
the elimination of any dimension of the `n`-dimensional nonnegative orthant
11-
gives the `n-1`-dimensional nonnegative orthant. However
12-
`supports_dimension_update(MOI.ExponentialCone}` is `false`.
13-
"""
14-
function supports_dimension_update(::Type{<:MOI.AbstractVectorSet})
15-
return false
16-
end
17-
function supports_dimension_update(::Type{<:Union{
18-
MOI.Reals, MOI.Zeros, MOI.Nonnegatives, MOI.Nonpositives}})
19-
return true
20-
end
21-
22-
"""
23-
update_dimension(s::AbstractVectorSet, new_dim)
24-
25-
Returns a set with the dimension modified to `new_dim`.
26-
"""
27-
function update_dimension(set::Union{
28-
MOI.Reals, MOI.Zeros, MOI.Nonnegatives, MOI.Nonpositives}, new_dim)
29-
return typeof(set)(new_dim)
30-
end
31-
321
"""
332
shift_constant(set::MOI.AbstractScalarSet,
343
offset)

src/Utilities/universalfallback.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function _remove_variable(uf::UniversalFallback,
9999
f::MOI.VectorOfVariables, s = constraint
100100
if vi in f.variables
101101
if length(f.variables) > 1
102-
if supports_dimension_update(S)
102+
if MOI.supports_dimension_update(S)
103103
constraints[ci] = remove_variable(f, s, vi)
104104
else
105105
throw_delete_variable_in_vov(vi)

src/sets.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,35 @@ function Base.copy(set::Union{Reals, Zeros, Nonnegatives, Nonpositives,
591591
return set
592592
end
593593
Base.copy(set::S) where {S <: Union{SOS1, SOS2}} = S(copy(set.weights))
594+
595+
"""
596+
supports_dimension_update(S::Type{<:MOI.AbstractVectorSet})
597+
598+
Return a `Bool` indicating whether the elimination of any dimension of
599+
`n`-dimensional sets of type `S` give an `n-1`-dimensional set `S`.
600+
By default, this function returns `false` so it should only be implemented
601+
for sets that supports dimension update.
602+
603+
For instance, `supports_dimension_update(MOI.Nonnegatives}` is `true` because
604+
the elimination of any dimension of the `n`-dimensional nonnegative orthant
605+
gives the `n-1`-dimensional nonnegative orthant. However
606+
`supports_dimension_update(MOI.ExponentialCone}` is `false`.
607+
"""
608+
function supports_dimension_update(::Type{<:AbstractVectorSet})
609+
return false
610+
end
611+
function supports_dimension_update(::Type{<:Union{
612+
Reals, Zeros, Nonnegatives, Nonpositives}})
613+
return true
614+
end
615+
616+
"""
617+
update_dimension(s::AbstractVectorSet, new_dim)
618+
619+
Returns a set with the dimension modified to `new_dim`.
620+
"""
621+
function update_dimension end
622+
function update_dimension(set::Union{
623+
Reals, Zeros, Nonnegatives, Nonpositives}, new_dim)
624+
return typeof(set)(new_dim)
625+
end

0 commit comments

Comments
 (0)