Skip to content

Commit d5627c9

Browse files
authored
Remove unneeded files in src (#62)
1 parent f66c9ff commit d5627c9

10 files changed

Lines changed: 147 additions & 135 deletions

src/Bridges/Bridges.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import MathOptInterface as MOI
1010
using ..MathOptComplements:
1111
ComplementsWithSetType,
1212
AbstractComplementarityRelaxation,
13-
ComplementarityReformulation,
14-
_remove_bounds!
13+
ComplementarityReformulation
1514

1615
include("VerticalBridge.jl")
1716
include("SpecifySetTypeBridge.jl")

src/Bridges/ComplementsVectorizeBridge.jl

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ struct ComplementsVectorizeBridge{T,F,S,SV} <:
4040
set_constant::T
4141
end
4242

43-
function _vector_set_type(::Type{<:MOI.GreaterThan})
44-
return MOI.Nonnegatives
45-
end
46-
function _vector_set_type(::Type{<:MOI.LessThan})
47-
return MOI.Nonpositives
48-
end
49-
function _vector_set_type(::Type{<:MOI.EqualTo})
50-
return MOI.Zeros
51-
end
43+
_vector_set_type(::Type{<:MOI.GreaterThan}) = MOI.Nonnegatives
44+
45+
_vector_set_type(::Type{<:MOI.LessThan}) = MOI.Nonpositives
46+
47+
_vector_set_type(::Type{<:MOI.EqualTo}) = MOI.Zeros
5248

5349
function _set_constant(
5450
::Type{T},
@@ -58,6 +54,7 @@ function _set_constant(
5854
) where {T}
5955
return MOI.Utilities.get_bounds(model, T, x2)[1]
6056
end
57+
6158
function _set_constant(
6259
::Type{T},
6360
model,
@@ -66,6 +63,7 @@ function _set_constant(
6663
) where {T}
6764
return MOI.Utilities.get_bounds(model, T, x2)[2]
6865
end
66+
6967
function _set_constant(
7068
::Type{T},
7169
model,
@@ -94,12 +92,8 @@ end
9492
function MOI.supports_constraint(
9593
::Type{<:ComplementsVectorizeBridge},
9694
::Type{MOI.VectorOfVariables},
97-
::Type{
98-
<:ComplementsWithSetType{
99-
<:Union{MOI.GreaterThan,MOI.LessThan,MOI.EqualTo},
100-
},
101-
},
102-
)
95+
::Type{ComplementsWithSetType{S}},
96+
) where {S<:Union{MOI.GreaterThan,MOI.LessThan,MOI.EqualTo}}
10397
return true
10498
end
10599

src/Bridges/FlipSignBridge.jl

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,13 @@ struct FlipSignBridge{T,F,S1,S2,G} <: MOI.Bridges.Constraint.AbstractBridge
3838
original_func::G
3939
end
4040

41-
function _flip_set_type(::Type{MOI.Nonnegatives})
42-
return MOI.Nonpositives
43-
end
44-
function _flip_set_type(::Type{MOI.Nonpositives})
45-
return MOI.Nonnegatives
46-
end
47-
function _flip_set_type(::Type{MOI.GreaterThan{T}}) where {T}
48-
return MOI.LessThan{T}
49-
end
50-
function _flip_set_type(::Type{MOI.LessThan{T}}) where {T}
51-
return MOI.GreaterThan{T}
52-
end
41+
_flip_set_type(::Type{MOI.Nonnegatives}) = MOI.Nonpositives
42+
43+
_flip_set_type(::Type{MOI.Nonpositives}) = MOI.Nonnegatives
44+
45+
_flip_set_type(::Type{MOI.GreaterThan{T}}) where {T} = MOI.LessThan{T}
46+
47+
_flip_set_type(::Type{MOI.LessThan{T}}) where {T} = MOI.GreaterThan{T}
5348

5449
const _FlippableSets =
5550
Union{MOI.Nonnegatives,MOI.Nonpositives,MOI.GreaterThan,MOI.LessThan}

src/Bridges/NonlinearBridge.jl

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ function _complementarity_bounds(
155155
) where {T}
156156
return (zero(T), T(Inf))
157157
end
158+
158159
function _complementarity_bounds(
159160
::Type{MOI.Nonpositives},
160161
model,
@@ -163,6 +164,7 @@ function _complementarity_bounds(
163164
) where {T}
164165
return (T(-Inf), zero(T))
165166
end
167+
166168
function _complementarity_bounds(
167169
::Type{MOI.Zeros},
168170
model,
@@ -171,6 +173,7 @@ function _complementarity_bounds(
171173
) where {T}
172174
return (zero(T), zero(T))
173175
end
176+
174177
function _complementarity_bounds(
175178
::Type{<:MOI.GreaterThan},
176179
model,
@@ -179,6 +182,7 @@ function _complementarity_bounds(
179182
) where {T}
180183
return (MOI.Utilities.get_bounds(model, T, x2)[1], T(Inf))
181184
end
185+
182186
function _complementarity_bounds(
183187
::Type{<:MOI.LessThan},
184188
model,
@@ -187,6 +191,7 @@ function _complementarity_bounds(
187191
) where {T}
188192
return (T(-Inf), MOI.Utilities.get_bounds(model, T, x2)[2])
189193
end
194+
190195
function _complementarity_bounds(
191196
::Type{<:MOI.Interval},
192197
model,
@@ -197,11 +202,15 @@ function _complementarity_bounds(
197202
end
198203

199204
"""
200-
reformulate_as_nonlinear_program!(model, relaxation, fun, set::ComplementsWithSetType{S})
205+
reformulate_as_nonlinear_program!(
206+
model,
207+
relaxation,
208+
fun,
209+
set::ComplementsWithSetType{S},
210+
)
201211
202212
Reformulate complementarity constraints as a nonlinear program using the given
203213
relaxation. The set type `S` determines which bound case to use.
204-
205214
"""
206215
function reformulate_as_nonlinear_program!(
207216
model::MOI.ModelLike,
@@ -334,7 +343,6 @@ For `epsilon ≥ 0`, the complementarity constraint `0 ≤ a ⟂ b ≥ 0` is ref
334343
0 ≤ b
335344
a + b - sqrt((a + b)^2 + epsilon) ≤ 0
336345
```
337-
338346
"""
339347
struct FischerBurmeisterRelaxation{T} <: AbstractComplementarityRelaxation
340348
epsilon::T
@@ -439,13 +447,27 @@ For `epsilon ≥ 0`, the complementarity constraint `0 ≤ a ⟂ b ≥ 0` is ref
439447
```
440448
a . b ≤ epsilon^2
441449
(a + epsilon) . (b + epsilon) ≥ epsilon^2
442-
443450
```
444451
"""
445452
struct LiuFukushimaRelaxation{T} <: AbstractComplementarityRelaxation
446453
epsilon::T
447454
end
448455

456+
function _remove_bounds!(model::MOI.ModelLike, x::MOI.VariableIndex)
457+
for cidx in [
458+
MOI.ConstraintIndex{MOI.VariableIndex,MOI.Interval{Float64}}(x.value),
459+
MOI.ConstraintIndex{MOI.VariableIndex,MOI.LessThan{Float64}}(x.value),
460+
MOI.ConstraintIndex{MOI.VariableIndex,MOI.GreaterThan{Float64}}(
461+
x.value,
462+
),
463+
]
464+
if MOI.is_valid(model, cidx)
465+
MOI.delete(model, cidx)
466+
end
467+
end
468+
return
469+
end
470+
449471
function _relax_complementarity_lower_bound!(
450472
model::MOI.ModelLike,
451473
relaxation::LiuFukushimaRelaxation,
@@ -528,7 +550,6 @@ with the function `ϕ`:
528550
-0.5 ((a -epsilon)^2 + (b - epsilon)^2) otherwise
529551
530552
```
531-
532553
"""
533554
struct KanzowSchwarzRelaxation{T} <: AbstractComplementarityRelaxation
534555
epsilon::T

src/Bridges/VerticalBridge.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ function _is_single_variable(func::MOI.ScalarAffineFunction)
187187
func.terms[1].coefficient == 1.0 &&
188188
iszero(func.constant)
189189
end
190+
190191
function _is_single_variable(func::MOI.ScalarQuadraticFunction)
191192
return (
192193
length(func.quadratic_terms) == 0 &&
@@ -195,13 +196,17 @@ function _is_single_variable(func::MOI.ScalarQuadraticFunction)
195196
iszero(func.constant)
196197
)
197198
end
199+
198200
function _is_single_variable(func::MOI.ScalarNonlinearFunction)
199201
return func.head == :+ &&
200202
length(func.args) == 1 &&
201203
isa(func.args[1], MOI.VariableIndex)
202204
end
205+
203206
_get_variable(func::MOI.ScalarAffineFunction) = func.terms[1].variable
207+
204208
_get_variable(func::MOI.ScalarQuadraticFunction) = func.affine_terms[1].variable
209+
205210
_get_variable(func::MOI.ScalarNonlinearFunction) = func.args[1]
206211

207212
# TODO: add support for ScalarNonlinearTerm
@@ -211,10 +216,8 @@ function _parse_complementarity_constraint(
211216
)
212217
exprs = MOI.Utilities.scalarize(fun)
213218
@assert length(exprs) == 2*n_comp
214-
215219
cc_lhs = MOI.AbstractScalarFunction[]
216220
cc_rhs = MOI.VariableIndex[]
217-
218221
for i in 1:n_comp
219222
# Parse LHS
220223
t1 = exprs[i]
@@ -224,7 +227,6 @@ function _parse_complementarity_constraint(
224227
else
225228
push!(cc_lhs, t1)
226229
end
227-
228230
# Parse RHS
229231
isvar2 = _is_single_variable(t2)
230232
if !isvar2
@@ -237,7 +239,6 @@ function _parse_complementarity_constraint(
237239
end
238240
push!(cc_rhs, _get_variable(t2))
239241
end
240-
241242
return cc_lhs, cc_rhs
242243
end
243244

@@ -250,7 +251,6 @@ expressions are rewritten with a slack. `T` is the coefficient type used for
250251
the generated equality constraints.
251252
252253
Once reformulated, the complementarity constraints involve only single variables.
253-
254254
"""
255255
function reformulate_to_vertical!(
256256
model::MOI.ModelLike,

src/MOI_wrapper.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mutable struct Optimizer{T,O<:MOI.ModelLike} <:
1010
constraint_map::MOI.Bridges.Constraint.Map
1111
con_to_name::Dict{MOI.ConstraintIndex,String}
1212
name_to_con::Union{Dict{String,MOI.ConstraintIndex},Nothing}
13+
1314
function Optimizer{T}(model::MOI.ModelLike) where {T}
1415
return new{T,typeof(model)}(
1516
model,
@@ -45,12 +46,14 @@ MOI.Bridges.is_bridged(::Optimizer, ::Type{<:MOI.AbstractSet}) = false
4546

4647
# Complements and ComplementsWithSetType are bridged
4748
MOI.Bridges.is_bridged(::Optimizer, ::Type{MOI.Complements}) = true
49+
4850
function MOI.Bridges.supports_bridging_constrained_variable(
4951
::Optimizer,
5052
::Type{MOI.Complements},
5153
)
5254
return true
5355
end
56+
5457
function MOI.Bridges.bridge_type(
5558
::Optimizer{T},
5659
::Type{MOI.Complements},
@@ -59,6 +62,7 @@ function MOI.Bridges.bridge_type(
5962
end
6063

6164
MOI.Bridges.is_bridged(::Optimizer, ::Type{<:ComplementsWithSetType}) = true
65+
6266
function MOI.Bridges.supports_bridging_constrained_variable(
6367
::Optimizer,
6468
::Type{<:ComplementsWithSetType},
@@ -86,13 +90,15 @@ function MOI.Bridges.is_bridged(
8690
)
8791
return true
8892
end
93+
8994
function MOI.Bridges.supports_bridging_constraint(
9095
::Optimizer,
9196
::Type{<:MOI.AbstractVectorFunction},
9297
::Type{MOI.Complements},
9398
)
9499
return true
95100
end
101+
96102
function MOI.Bridges.bridge_type(
97103
::Optimizer{T},
98104
::Type{<:MOI.AbstractVectorFunction},
@@ -118,6 +124,7 @@ function MOI.Bridges.is_bridged(
118124
)
119125
return true
120126
end
127+
121128
function MOI.Bridges.supports_bridging_constraint(
122129
::Optimizer,
123130
::Type{<:MOI.AbstractVectorFunction},
@@ -251,8 +258,10 @@ function _additional_arguments(
251258
return (model.reformulation,)
252259
end
253260

254-
# TODO it would be nice if MOI was defining this `MOI.Bridges.additional_arguments` function and
255-
# already had this implementation of `add_bridged_constraint` so that I don't have to reimplement it
261+
# TODO(blegat): it would be nice if MOI was defining this
262+
# `MOI.Bridges.additional_arguments` function and already had this
263+
# implementation of `add_bridged_constraint` so that I don't have to reimplement
264+
# it
256265
function MOI.Bridges.add_bridged_constraint(b::Optimizer, BridgeType, f, s)
257266
bridge = MOI.Bridges.Constraint.Constraint.bridge_constraint(
258267
BridgeType,
@@ -261,7 +270,8 @@ function MOI.Bridges.add_bridged_constraint(b::Optimizer, BridgeType, f, s)
261270
s,
262271
_additional_arguments(b, BridgeType)...,
263272
)
264-
# The rest is copy-pasted from the default implementation of `add_bridged_constraint` in MOI
273+
# The rest is copy-pasted from the default implementation of
274+
# `add_bridged_constraint` in MOI
265275
ci = MOI.Bridges.Constraint.add_key_for_bridge(
266276
MOI.Bridges.Constraint.bridges(b)::MOI.Bridges.Constraint.Map,
267277
bridge,

0 commit comments

Comments
 (0)