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

adding ObjectiveLimit attribute #2257

Merged
merged 6 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions docs/src/manual/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ The following attributes are available:
* [`SolverVersion`](@ref)
* [`SolveTimeSec`](@ref)
* [`TimeLimitSec`](@ref)
* [`ObjectiveLimit`](@ref)
1 change: 1 addition & 0 deletions docs/src/reference/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ SolverName
SolverVersion
Silent
TimeLimitSec
ObjectiveLimit
RawOptimizerAttribute
NumberOfThreads
RawSolver
Expand Down
1 change: 1 addition & 0 deletions docs/src/tutorials/implementing.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ method for each attribute.
| [`Name`](@ref) | Yes | Yes | Yes |
| [`Silent`](@ref) | Yes | Yes | Yes |
| [`TimeLimitSec`](@ref) | Yes | Yes | Yes |
| [`ObjectiveLimit`](@ref) | Yes | Yes | Yes |
| [`RawOptimizerAttribute`](@ref) | Yes | Yes | Yes |
| [`NumberOfThreads`](@ref) | Yes | Yes | Yes |
| [`AbsoluteGapTolerance`](@ref) | Yes | Yes | Yes |
Expand Down
24 changes: 24 additions & 0 deletions src/Test/test_attribute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@
return
end

function test_attribute_ObjectiveLimit(model::MOI.AbstractOptimizer, ::Config)
@requires MOI.supports(model, MOI.ObjectiveLimit())

Check warning on line 194 in src/Test/test_attribute.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_attribute.jl#L193-L194

Added lines #L193 - L194 were not covered by tests
# Get the current value to restore it at the end of the test
value = MOI.get(model, MOI.ObjectiveLimit())
MOI.set(model, MOI.ObjectiveLimit(), 0.0)
@test MOI.get(model, MOI.ObjectiveLimit()) == 0.0
odow marked this conversation as resolved.
Show resolved Hide resolved
MOI.set(model, MOI.ObjectiveLimit(), 1.0)
@test MOI.get(model, MOI.ObjectiveLimit()) == 1.0
MOI.set(model, MOI.ObjectiveLimit(), value)
@test value == MOI.get(model, MOI.ObjectiveLimit()) # Equality should hold
_test_attribute_value_type(model, MOI.ObjectiveLimit())
return

Check warning on line 204 in src/Test/test_attribute.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_attribute.jl#L196-L204

Added lines #L196 - L204 were not covered by tests
end
test_attribute_ObjectiveLimit(::MOI.ModelLike, ::Config) = nothing

Check warning on line 206 in src/Test/test_attribute.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_attribute.jl#L206

Added line #L206 was not covered by tests

function setup_test(

Check warning on line 208 in src/Test/test_attribute.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_attribute.jl#L208

Added line #L208 was not covered by tests
::typeof(test_attribute_ObjectiveLimit),
model::MOIU.MockOptimizer,
::Config,
)
MOI.set(model, MOI.ObjectiveLimit(), nothing)
return

Check warning on line 214 in src/Test/test_attribute.jl

View check run for this annotation

Codecov / codecov/patch

src/Test/test_attribute.jl#L213-L214

Added lines #L213 - L214 were not covered by tests
end

"""
test_attribute_AbsoluteGapTolerance(model::MOI.AbstractOptimizer, config::Config)

Expand Down
12 changes: 12 additions & 0 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,18 @@

attribute_value_type(::TimeLimitSec) = Union{Nothing,Float64}

"""
ObjectiveLimit()

odow marked this conversation as resolved.
Show resolved Hide resolved

odow marked this conversation as resolved.
Show resolved Hide resolved
An optimizer attribute for setting a limit on the objective value.
odow marked this conversation as resolved.
Show resolved Hide resolved
The solver may stop when the `ObjectiveValue` is better (lower for minimization, higher for maximization) than the `ObjectiveLimit`.
odow marked this conversation as resolved.
Show resolved Hide resolved
When `set` to `nothing`, it removes the solver limit. The default value is `nothing`.
odow marked this conversation as resolved.
Show resolved Hide resolved
"""
struct ObjectiveLimit <: AbstractOptimizerAttribute end

attribute_value_type(::ObjectiveLimit) = Union{Nothing,Float64}

Check warning on line 863 in src/attributes.jl

View check run for this annotation

Codecov / codecov/patch

src/attributes.jl#L863

Added line #L863 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An implicit assumption is that this applies only to scalar-valued objective functions, and that the objective is Float64. What if the optimizer is BigFloat, or Rational{BigFloat}? And what about vector-valued objectives?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vetor-valued objectives is another story I wouldn't touch at first. I would be okay upgrading this to:

Union{Nothing,Real}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just remove this for now. It's only implemented for a subset of attributes, and the fallback is Any.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure yes

matbesancon marked this conversation as resolved.
Show resolved Hide resolved

matbesancon marked this conversation as resolved.
Show resolved Hide resolved
"""
RawOptimizerAttribute(name::String)

Expand Down
1 change: 1 addition & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function precompile_model(model, constraints)
ObjectiveValue,
Silent,
TimeLimitSec,
ObjectiveBound,
odow marked this conversation as resolved.
Show resolved Hide resolved
odow marked this conversation as resolved.
Show resolved Hide resolved
NumberOfVariables,
)
Base.precompile(get, (model, attr))
Expand Down
9 changes: 9 additions & 0 deletions test/Utilities/cachingoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function test_default_attributes()
MOI.SolverName(),
MOI.Silent(),
MOI.TimeLimitSec(),
MOI.ObjectiveLimit(),
MOI.NumberOfThreads(),
MOI.ResultCount(),
)
Expand All @@ -209,22 +210,28 @@ function test_copyable_solver_attributes()
cached = MOIU.CachingOptimizer(cache, MOIU.MANUAL)
MOI.set(cached, MOI.Silent(), true)
MOI.set(cached, MOI.TimeLimitSec(), 0.0)
MOI.set(cached, MOI.ObjectiveLimit(), 42.0)
MOI.set(cached, MOI.NumberOfThreads(), 1)
mock = MOIU.MockOptimizer(MOIU.UniversalFallback(MOIU.Model{Float64}()))
MOIU.reset_optimizer(cached, mock)
@test MOI.get(mock, MOI.Silent())
@test MOI.get(cached, MOI.Silent())
@test MOI.get(mock, MOI.TimeLimitSec()) == 0.0
@test MOI.get(cached, MOI.TimeLimitSec()) == 0.0
@test MOI.get(mock, MOI.ObjectiveLimit()) == 42.0
@test MOI.get(cached, MOI.ObjectiveLimit()) == 42.0
@test MOI.get(mock, MOI.NumberOfThreads()) == 1
@test MOI.get(cached, MOI.NumberOfThreads()) == 1
MOI.set(cached, MOI.Silent(), false)
MOI.set(cached, MOI.TimeLimitSec(), 1.0)
MOI.set(cached, MOI.ObjectiveLimit(), 1.0)
MOI.set(cached, MOI.NumberOfThreads(), 2)
@test !MOI.get(mock, MOI.Silent())
@test !MOI.get(cached, MOI.Silent())
@test MOI.get(mock, MOI.TimeLimitSec()) 1.0
@test MOI.get(mock, MOI.ObjectiveLimit()) 1.0
@test MOI.get(cached, MOI.TimeLimitSec()) 1.0
@test MOI.get(cached, MOI.ObjectiveLimit()) 1.0
@test MOI.get(mock, MOI.NumberOfThreads()) == 2
@test MOI.get(cached, MOI.NumberOfThreads()) == 2
mock = MOIU.MockOptimizer(MOIU.UniversalFallback(MOIU.Model{Float64}()))
Expand All @@ -233,6 +240,8 @@ function test_copyable_solver_attributes()
@test !MOI.get(cached, MOI.Silent())
@test MOI.get(mock, MOI.TimeLimitSec()) 1.0
@test MOI.get(cached, MOI.TimeLimitSec()) 1.0
@test MOI.get(mock, MOI.ObjectiveLimit()) 1.0
@test MOI.get(cached, MOI.ObjectiveLimit()) 1.0
@test MOI.get(mock, MOI.NumberOfThreads()) == 2
@test MOI.get(cached, MOI.NumberOfThreads()) == 2
MOI.set(cached, MOI.Silent(), true)
Expand Down
1 change: 1 addition & 0 deletions test/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ function test_missing_attribute()
model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
@test MOI.get(model, MOI.Test.UnknownModelAttribute()) === nothing
@test MOI.get(model, MOI.TimeLimitSec()) === nothing
@test MOI.get(model, MOI.ObjectiveLimit()) === nothing
return
end

Expand Down
Loading