Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utilities] improve docstrings of shift_constant #2467

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions src/Utilities/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,36 @@
Returns a new scalar set `new_set` such that `func`-in-`set` is equivalent to
`func + offset`-in-`new_set`.

Only define this function if it makes sense to.

Use [`supports_shift_constant`](@ref) to check if the set supports shifting:
```Julia
if supports_shift_constant(typeof(old_set))
new_set = shift_constant(old_set, offset)
f.constant = 0
add_constraint(model, f, new_set)
if MOI.Utilities.supports_shift_constant(typeof(set))
new_set = MOI.Utilities.shift_constant(set, -func.constant)
func.constant = 0
MOI.add_constraint(model, func, new_set)
else
add_constraint(model, f, old_set)
MOI.add_constraint(model, func, set)
end
```

See also [`supports_shift_constant`](@ref).
### Note for developers

Only define this function if it makes sense and you have implemented
[`supports_shift_constant`](@ref) to return `true`.

## Examples

The call `shift_constant(MOI.Interval(-2, 3), 1)` is equal to
`MOI.Interval(-1, 4)`.
```jldoctest
julia> import MathOptInterface as MOI

julia> set = MOI.Interval(-2.0, 3.0)
MathOptInterface.Interval{Float64}(-2.0, 3.0)

julia> MOI.Utilities.supports_shift_constant(typeof(set))
true

julia> MOI.Utilities.shift_constant(set, 1.0)
MathOptInterface.Interval{Float64}(-1.0, 4.0)
```
"""
function shift_constant end

Expand All @@ -38,32 +49,49 @@ function shift_constant end
Return `true` if [`shift_constant`](@ref) is defined for set `S`.

See also [`shift_constant`](@ref).

## Examples

```jldoctest
julia> import MathOptInterface as MOI

julia> MOI.Utilities.supports_shift_constant(MOI.Interval{Float64})
true

julia> MOI.Utilities.supports_shift_constant(MOI.ZeroOne)
false
```
"""
supports_shift_constant(::Type{S}) where {S<:MOI.AbstractSet} = false

function shift_constant(set::MOI.LessThan, offset)
return MOI.LessThan(MOI.constant(set) + offset)
end

supports_shift_constant(::Type{<:MOI.LessThan}) = true

function shift_constant(set::MOI.GreaterThan, offset)
return MOI.GreaterThan(MOI.constant(set) + offset)
end

supports_shift_constant(::Type{<:MOI.GreaterThan}) = true

function shift_constant(set::MOI.EqualTo, offset)
return MOI.EqualTo(MOI.constant(set) + offset)
end

supports_shift_constant(::Type{<:MOI.EqualTo}) = true

function shift_constant(set::MOI.Interval, offset)
return MOI.Interval(set.lower + offset, set.upper + offset)
end

supports_shift_constant(::Type{<:MOI.Interval}) = true

function shift_constant(set::MOI.Parameter, offset)
return MOI.Parameter(MOI.constant(set) + offset)
end

supports_shift_constant(::Type{<:MOI.Parameter}) = true

"""
Expand Down
Loading