Skip to content

Commit 76c446b

Browse files
authored
[Utilities] improve docstrings of shift_constant (#2467)
1 parent 7743c96 commit 76c446b

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/Utilities/sets.jl

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,36 @@
1010
Returns a new scalar set `new_set` such that `func`-in-`set` is equivalent to
1111
`func + offset`-in-`new_set`.
1212
13-
Only define this function if it makes sense to.
14-
1513
Use [`supports_shift_constant`](@ref) to check if the set supports shifting:
1614
```Julia
17-
if supports_shift_constant(typeof(old_set))
18-
new_set = shift_constant(old_set, offset)
19-
f.constant = 0
20-
add_constraint(model, f, new_set)
15+
if MOI.Utilities.supports_shift_constant(typeof(set))
16+
new_set = MOI.Utilities.shift_constant(set, -func.constant)
17+
func.constant = 0
18+
MOI.add_constraint(model, func, new_set)
2119
else
22-
add_constraint(model, f, old_set)
20+
MOI.add_constraint(model, func, set)
2321
end
2422
```
2523
26-
See also [`supports_shift_constant`](@ref).
24+
### Note for developers
25+
26+
Only define this function if it makes sense and you have implemented
27+
[`supports_shift_constant`](@ref) to return `true`.
2728
2829
## Examples
2930
30-
The call `shift_constant(MOI.Interval(-2, 3), 1)` is equal to
31-
`MOI.Interval(-1, 4)`.
31+
```jldoctest
32+
julia> import MathOptInterface as MOI
33+
34+
julia> set = MOI.Interval(-2.0, 3.0)
35+
MathOptInterface.Interval{Float64}(-2.0, 3.0)
36+
37+
julia> MOI.Utilities.supports_shift_constant(typeof(set))
38+
true
39+
40+
julia> MOI.Utilities.shift_constant(set, 1.0)
41+
MathOptInterface.Interval{Float64}(-1.0, 4.0)
42+
```
3243
"""
3344
function shift_constant end
3445

@@ -38,32 +49,49 @@ function shift_constant end
3849
Return `true` if [`shift_constant`](@ref) is defined for set `S`.
3950
4051
See also [`shift_constant`](@ref).
52+
53+
## Examples
54+
55+
```jldoctest
56+
julia> import MathOptInterface as MOI
57+
58+
julia> MOI.Utilities.supports_shift_constant(MOI.Interval{Float64})
59+
true
60+
61+
julia> MOI.Utilities.supports_shift_constant(MOI.ZeroOne)
62+
false
63+
```
4164
"""
4265
supports_shift_constant(::Type{S}) where {S<:MOI.AbstractSet} = false
4366

4467
function shift_constant(set::MOI.LessThan, offset)
4568
return MOI.LessThan(MOI.constant(set) + offset)
4669
end
70+
4771
supports_shift_constant(::Type{<:MOI.LessThan}) = true
4872

4973
function shift_constant(set::MOI.GreaterThan, offset)
5074
return MOI.GreaterThan(MOI.constant(set) + offset)
5175
end
76+
5277
supports_shift_constant(::Type{<:MOI.GreaterThan}) = true
5378

5479
function shift_constant(set::MOI.EqualTo, offset)
5580
return MOI.EqualTo(MOI.constant(set) + offset)
5681
end
82+
5783
supports_shift_constant(::Type{<:MOI.EqualTo}) = true
5884

5985
function shift_constant(set::MOI.Interval, offset)
6086
return MOI.Interval(set.lower + offset, set.upper + offset)
6187
end
88+
6289
supports_shift_constant(::Type{<:MOI.Interval}) = true
6390

6491
function shift_constant(set::MOI.Parameter, offset)
6592
return MOI.Parameter(MOI.constant(set) + offset)
6693
end
94+
6795
supports_shift_constant(::Type{<:MOI.Parameter}) = true
6896

6997
"""

0 commit comments

Comments
 (0)