Skip to content

Commit 88656c6

Browse files
authored
Merge pull request #723 from matbesancon/constant
common constant function
2 parents 2d120d2 + 8b9fdd6 commit 88656c6

File tree

10 files changed

+68
-38
lines changed

10 files changed

+68
-38
lines changed

docs/src/apireference.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,14 @@ VectorQuadraticTerm
212212
VectorQuadraticFunction
213213
```
214214

215-
Functions for getting and setting properties of sets.
215+
Functions for getting and setting properties of functions.
216216

217217
```@docs
218218
output_dimension
219+
constant(f::Union{ScalarAffineFunction, ScalarQuadraticFunction})
220+
constant(f::Union{VectorAffineFunction, VectorQuadraticFunction})
221+
constant(f::SingleVariable, ::DataType)
222+
constant(f::VectorOfVariables, T::DataType)
219223
```
220224

221225
## Sets
@@ -258,6 +262,7 @@ Functions for getting and setting properties of sets.
258262

259263
```@docs
260264
dimension
265+
constant(s::EqualTo)
261266
```
262267

263268
## Modifications

src/Bridges/quadtosocbridge.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ end
6363
function QuadtoSOCBridge{T}(model, func::MOI.ScalarQuadraticFunction{T},
6464
set::Union{MOI.LessThan{T},
6565
MOI.GreaterThan{T}}) where T
66-
set_constant = MOIU.getconstant(set)
66+
set_constant = MOI.constant(set)
6767
less_than = set isa MOI.LessThan
6868
if !less_than
6969
set_constant = -set_constant

src/Bridges/scalarizebridge.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ scalar_set_type(::Type{<:MOI.Zeros}, T::Type) = MOI.EqualTo{T}
44
scalar_set_type(::Type{<:MOI.Nonpositives}, T::Type) = MOI.LessThan{T}
55
scalar_set_type(::Type{<:MOI.Nonnegatives}, T::Type) = MOI.GreaterThan{T}
66

7-
__constant(f::Union{MOI.ScalarAffineFunction, MOI.ScalarQuadraticFunction}, T::DataType) = MOI._constant(f)
8-
__constant(f::Union{MOI.VectorAffineFunction, MOI.VectorQuadraticFunction}, T::DataType) = MOI._constant(f)
9-
__constant(f::MOI.SingleVariable, T::DataType) = zero(T)
10-
__constant(f::MOI.VectorOfVariables, T::DataType) = zeros(T, length(f.variables))
11-
127
"""
138
ScalarizeBridge{T}
149
@@ -23,7 +18,7 @@ function ScalarizeBridge{T, F, S}(model::MOI.ModelLike,
2318
f::MOI.AbstractVectorFunction,
2419
set::VectorLPSet) where {T, F, S}
2520
dimension = MOI.output_dimension(f)
26-
constants = __constant(f, T)
21+
constants = MOI.constant(f, T)
2722
new_f = MOIU.scalarize(f, true)
2823
constraints = Vector{CI{F, S}}(undef, dimension)
2924
for i in 1:dimension
@@ -89,7 +84,7 @@ end
8984
function MOI.set(model::MOI.ModelLike, ::MOI.ConstraintFunction,
9085
bridge::ScalarizeBridge{T}, func) where T
9186
old_constants = bridge.constants
92-
bridge.constants = __constant(func, T)
87+
bridge.constants = MOI.constant(func, T)
9388
new_func = MOIU.scalarize(func, true)
9489
MOI.set.(model, MOI.ConstraintFunction(), bridge.scalar_constraints,
9590
new_func)

src/Bridges/vectorizebridge.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ end
2121
function VectorizeBridge{T, F, S}(model::MOI.ModelLike,
2222
f::MOI.AbstractScalarFunction,
2323
set::MOI.AbstractScalarSet) where {T, F, S}
24-
set_constant = MOIU.getconstant(set)
24+
set_constant = MOI.constant(set)
2525
g = MOIU.operate(-, T, f, set_constant)
26-
if -set_constant != MOI._constant(g)[1]
26+
if -set_constant != MOI.constant(g)[1]
2727
# This means the constant in `f` was not zero
28-
constant = MOI._constant(g)[1] + set_constant
28+
constant = MOI.constant(g)[1] + set_constant
2929
throw(MOI.ScalarFunctionConstantNotZero{typeof(constant), typeof(f),
3030
typeof(set)}(constant))
3131
end
@@ -98,7 +98,7 @@ function MOI.modify(model::MOI.ModelLike, bridge::VectorizeBridge,
9898
end
9999
function MOI.set(model::MOI.ModelLike, ::MOI.ConstraintSet,
100100
bridge::VectorizeBridge, new_set::LPCone)
101-
bridge.set_constant = MOIU.getconstant(new_set)
101+
bridge.set_constant = MOI.constant(new_set)
102102
MOI.modify(model, bridge.vector_constraint,
103103
MOI.VectorConstantChange([-bridge.set_constant]))
104104
end

src/Utilities/functions.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ function mapvariables(varmap::Function, f::MOI.VectorOfVariables)
6464
return MOI.VectorOfVariables(varmap.(f.variables))
6565
end
6666
function mapvariables(varmap::Function, f::Union{SAF, VAF})
67-
typeof(f)(mapvariable.(varmap, f.terms), MOI._constant(f))
67+
typeof(f)(mapvariable.(varmap, f.terms), MOI.constant(f))
6868
end
6969
function mapvariables(varmap::Function, f::Union{SQF, VQF})
7070
lin = mapvariable.(varmap, f.affine_terms)
7171
quad = mapvariable.(varmap, f.quadratic_terms)
72-
return typeof(f)(lin, quad, MOI._constant(f))
72+
return typeof(f)(lin, quad, MOI.constant(f))
7373
end
7474
mapvariables(varmap, f::MOI.AbstractFunction) = mapvariables(vi -> varmap[vi], f)
7575
mapvariables(varmap::Function, change::Union{MOI.ScalarConstantChange, MOI.VectorConstantChange}) = change
@@ -461,11 +461,11 @@ function removevariable(f::VVF, vi)
461461
VVF(_rmvar(f.variables, vi))
462462
end
463463
function removevariable(f::Union{SAF, VAF}, vi)
464-
typeof(f)(_rmvar(f.terms, vi), MOI._constant(f))
464+
typeof(f)(_rmvar(f.terms, vi), MOI.constant(f))
465465
end
466466
function removevariable(f::Union{SQF, VQF}, vi)
467467
terms = _rmvar.((f.affine_terms, f.quadratic_terms), Ref(vi))
468-
typeof(f)(terms..., MOI._constant(f))
468+
typeof(f)(terms..., MOI.constant(f))
469469
end
470470

471471
"""
@@ -1399,7 +1399,7 @@ function scalarize(f::MOI.VectorOfVariables, ignore_constants::Bool = false)
13991399
end
14001400
function scalarize(f::MOI.VectorAffineFunction{T}, ignore_constants::Bool = false) where T
14011401
dimension = MOI.output_dimension(f)
1402-
constants = ignore_constants ? zeros(T, dimension) : MOI._constant(f)
1402+
constants = ignore_constants ? zeros(T, dimension) : MOI.constant(f)
14031403
counting = count_terms(dimension, f.terms)
14041404
functions = MOI.ScalarAffineFunction{T}[
14051405
MOI.ScalarAffineFunction{T}(MOI.ScalarAffineTerm{T}[], constants[i]) for i in 1:dimension]
@@ -1413,7 +1413,7 @@ function scalarize(f::MOI.VectorAffineFunction{T}, ignore_constants::Bool = fals
14131413
end
14141414
function scalarize(f::MOI.VectorQuadraticFunction{T}, ignore_constants::Bool = false) where T
14151415
dimension = MOI.output_dimension(f)
1416-
constants = ignore_constants ? zeros(T, dimension) : MOI._constant(f)
1416+
constants = ignore_constants ? zeros(T, dimension) : MOI.constant(f)
14171417
counting_scalars = count_terms(dimension, f.affine_terms)
14181418
counting_quadratics = count_terms(dimension, f.quadratic_terms)
14191419
functions = MOI.ScalarQuadraticFunction{T}[

src/Utilities/sets.jl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
"""
2-
getconstant(s::Union{MOI.EqualTo, MOI.GreaterThan, MOI.LessThan})
3-
4-
Returns the constant of the set.
5-
"""
6-
getconstant(s::MOI.EqualTo) = s.value
7-
getconstant(s::MOI.GreaterThan) = s.lower
8-
getconstant(s::MOI.LessThan) = s.upper
9-
101
const DimensionUpdatableSets = Union{MOI.Reals,
112
MOI.Zeros,
123
MOI.Nonnegatives,
@@ -36,7 +27,7 @@ function shift_constant(set::Union{MOI.LessThan{T},
3627
MOI.GreaterThan{T},
3728
MOI.EqualTo{T}},
3829
offset::T) where T
39-
return typeof(set)(getconstant(set) + offset)
30+
return typeof(set)(MOI.constant(set) + offset)
4031
end
4132
function shift_constant(set::MOI.Interval, offset)
4233
return MOI.Interval(set.lower + offset, set.upper + offset)

src/functions.jl

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,44 @@ function _dicts(f::Union{ScalarQuadraticFunction, VectorQuadraticFunction})
352352
sum_dict(term_pair.(f.quadratic_terms)))
353353
end
354354

355-
_constant(f::Union{ScalarAffineFunction, ScalarQuadraticFunction}) = f.constant
356-
_constant(f::Union{VectorAffineFunction, VectorQuadraticFunction}) = f.constants
355+
"""
356+
constant(f::Union{ScalarAffineFunction, ScalarQuadraticFunction})
357+
358+
Returns the constant term of the scalar function
359+
"""
360+
constant(f::Union{ScalarAffineFunction, ScalarQuadraticFunction}) = f.constant
361+
362+
"""
363+
constant(f::Union{VectorAffineFunction, VectorQuadraticFunction})
364+
365+
Returns the vector of constant terms of the vector function
366+
"""
367+
constant(f::Union{VectorAffineFunction, VectorQuadraticFunction}) = f.constants
357368

358369
function Base.isapprox(f::F, g::G; kwargs...) where {F<:Union{ScalarAffineFunction, ScalarQuadraticFunction, VectorAffineFunction, VectorQuadraticFunction},
359370
G<:Union{ScalarAffineFunction, ScalarQuadraticFunction, VectorAffineFunction, VectorQuadraticFunction}}
360-
isapprox(_constant(f), _constant(g); kwargs...) && all(dict_isapprox.(_dicts(f), _dicts(g); kwargs...))
371+
isapprox(constant(f), constant(g); kwargs...) && all(dict_isapprox.(_dicts(f), _dicts(g); kwargs...))
361372
end
362373

374+
constant(f::Union{ScalarAffineFunction, ScalarQuadraticFunction}, T::DataType) = constant(f)
375+
constant(f::Union{VectorAffineFunction, VectorQuadraticFunction}, T::DataType) = constant(f)
376+
377+
"""
378+
constant(f::SingleVariable, T::DataType)
379+
380+
The constant term of a `SingleVariable` function is
381+
the zero value of the specified type `T`.
382+
"""
383+
constant(f::SingleVariable, T::DataType) = zero(T)
384+
385+
"""
386+
constant(f::VectorOfVariables, T::DataType)
387+
388+
The constant term of a `VectorOfVariables` function is a
389+
vector of zero values of the specified type `T`.
390+
"""
391+
constant(f::VectorOfVariables, T::DataType) = zeros(T, length(f.variables))
392+
363393
# isbits type, nothing to copy
364394
Base.copy(func::SingleVariable) = func
365395

@@ -371,15 +401,15 @@ Base.copy(func::VectorOfVariables) = VectorOfVariables(copy(func.variables))
371401
Return a new affine function with a shallow copy of the terms and constant(s)
372402
from `func`.
373403
"""
374-
Base.copy(func::F) where {F <: Union{ScalarAffineFunction, VectorAffineFunction}} = F(copy(func.terms), copy(_constant(func)))
404+
Base.copy(func::F) where {F <: Union{ScalarAffineFunction, VectorAffineFunction}} = F(copy(func.terms), copy(constant(func)))
375405

376406
"""
377407
copy(func::Union{ScalarQuadraticFunction, VectorQuadraticFunction})
378408
379409
Return a new quadratic function with a shallow copy of the terms and constant(s)
380410
from `func`.
381411
"""
382-
Base.copy(func::F) where {F <: Union{ScalarQuadraticFunction, VectorQuadraticFunction}} = F(copy(func.affine_terms), copy(func.quadratic_terms), copy(_constant(func)))
412+
Base.copy(func::F) where {F <: Union{ScalarQuadraticFunction, VectorQuadraticFunction}} = F(copy(func.affine_terms), copy(func.quadratic_terms), copy(constant(func)))
383413

384414
# Define shortcuts for
385415
# SingleVariable -> ScalarAffineFunction

src/sets.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ Interval(s::LessThan{<:AbstractFloat}) = Interval(typemin(s.upper), s.upper)
136136
Interval(s::EqualTo{<:Real}) = Interval(s.value, s.value)
137137
Interval(s::Interval) = s
138138

139+
"""
140+
constant(s::Union{EqualTo, GreaterThan, LessThan})
141+
142+
Returns the constant of the set.
143+
"""
144+
constant(s::EqualTo) = s.value
145+
constant(s::GreaterThan) = s.lower
146+
constant(s::LessThan) = s.upper
147+
139148
"""
140149
SecondOrderCone(dimension)
141150

test/Utilities/functions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ end
449449
@testset "Conversion to canonical form" begin
450450
function isapprox_ordered(f1::T, f2::T) where {T <: Union{MOI.ScalarAffineFunction, MOI.VectorAffineFunction}}
451451
((MOI.term_indices.(f1.terms) == MOI.term_indices.(f2.terms)) &&
452-
(MOI._constant(f1) MOI._constant(f2)) &&
452+
(MOI.constant(f1) MOI.constant(f2)) &&
453453
(MOI.coefficient.(f1.terms) MOI.coefficient.(f2.terms)))
454454
end
455455
function test_canonicalization(f::T, expected::T) where {T <: Union{MOI.ScalarAffineFunction, MOI.VectorAffineFunction}}

test/Utilities/sets.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@testset "Constant" begin
2-
@test MOIU.getconstant(MOI.EqualTo(3)) == 3
3-
@test MOIU.getconstant(MOI.GreaterThan(6)) == 6
4-
@test MOIU.getconstant(MOI.LessThan(2)) == 2
2+
@test MOI.constant(MOI.EqualTo(3)) == 3
3+
@test MOI.constant(MOI.GreaterThan(6)) == 6
4+
@test MOI.constant(MOI.LessThan(2)) == 2
55
end
66

77
@testset "Shifts" begin

0 commit comments

Comments
 (0)