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

Add solve_time function #1928

Merged
merged 9 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,15 @@ end
set_optimize_hook(model::Model, f) = (model.optimize_hook = f)


"""
solve_time(model::Model)

If available, returns the solve time reported by the solver.
Copy link
Member

Choose a reason for hiding this comment

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

What happens if the solve time is not available? This is useful to document.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, today it returns

ERROR: ArgumentError: ModelLike of type ProxSDP.Optimizer does not support accessing the attribute MathOptInterface.SolveTime()
Stacktrace:
 [1] get(::ProxSDP.Optimizer, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/.julia/packages/MathOptInterface/C3lip/src/attributes.jl:203
 [2] get(::MathOptInterface.Utilities.CachingOptimizer{ProxSDP.Optimizer,MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}}, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/.julia/packages/MathOptInterface/C3lip/src/Utilities/cachingoptimizer.jl:438
 [3] get(::MathOptInterface.Bridges.LazyBridgeOptimizer{MathOptInterface.Utilities.CachingOptimizer{ProxSDP.Optimizer,MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}},MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Bridges.AllBridgedConstraints{Float64}}}, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/.julia/packages/MathOptInterface/C3lip/src/Bridges/bridgeoptimizer.jl:189
 [4] get(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}}, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/.julia/packages/MathOptInterface/C3lip/src/Utilities/cachingoptimizer.jl:438
 [5] _moi_get_result(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}}, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:596
 [6] get(::Model, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:609
 [7] solve_time(::Model) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:448
 [8] top-level scope at none:0

but we could redesign as

function _try_get_solve_time(model_like)
    try
        return MOI.get(model_like, MOI.SolveTime())
    catch ex
        if isa(ex, ArgumentError)
            return "SolveTime() attribute not implemented by the optimizer."
        else
            rethrow(ex)
        end
    end
end

"""
    solve_time(model::Model)

If available, returns the solve time reported by the solver.
Returns "SolveTime() attribute not implemented by the optimizer." if the attribute is
not implemented.
"""
function solve_time(model::Model)
    return _try_get_solve_time(model)
end

@blegat

Copy link
Member

Choose a reason for hiding this comment

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

I don't think 9e40df3 is needed. Catching all ArumentError is too wide and the error message is already quite clear. Adding a sentence in the docstring is enough

"""
function solve_time(model::Model)
return MOI.get(model, MOI.SolveTime())
end

# Abstract base type for all scalar types
abstract type AbstractJuMPScalar end

Expand Down
15 changes: 15 additions & 0 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,21 @@ function test_model()
@test optimizer.b == 2
end

@testset "solve_time" begin
@testset "NoOptimizer()" begin
err = NoOptimizer()
model = Model()
@test_throws err solve_time(model)
end

@testset "OptimizeNotCalled()" begin
err = OptimizeNotCalled()
model = Model(with_optimizer(MOIU.MockOptimizer,
SimpleLPModel{Float64}()))
@test_throws err solve_time(model)
end
Copy link
Member

@blegat blegat Apr 8, 2019

Choose a reason for hiding this comment

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

You can add a test where it works by using a with_optimizer(() -> MockOptimizer (UniversalFallback(JuMP._MOIModel {Float64}())))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not getting exactly how to do this. I have tried

model = Model(with_optimizer(MOIU.MockOptimizer,
                             MOIU.UniversalFallback(JuMP._MOIModel{Float64}())))
JuMP.solve_time(model)

but gives an error

ERROR: OptimizeNotCalled()
Stacktrace:
 [1] _moi_get_result(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}}, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:594
 [2] get(::Model, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:609
 [3] solve_time(::Model) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:448
 [4] top-level scope at none:0

and

model = Model(with_optimizer(MOIU.MockOptimizer,
                             MOIU.UniversalFallback(JuMP._MOIModel{Float64}())))   

optimize!(model)
JuMP.solve_time(model)

gives the same error

ERROR: OptimizeNotCalled()
Stacktrace:
 [1] _moi_get_result(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}}, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:594
 [2] get(::Model, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:609
 [3] solve_time(::Model) at /Users/guilhermebodin/Documents/JuMP.jl/src/JuMP.jl:448
 [4] top-level scope at none:0

Copy link
Member

Choose a reason for hiding this comment

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

You can try

mock = MOIU.MockOptimizer(MOIU.UniversalFallback(JuMP._MOIModel{Float64}())))
mock.optimize! = mock -> begin
    MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
    MOI.set(mock, MOI.SolveTime(), 2.0)
end
model = direct_model(mock)
optimize!(model)
@test solve_time(model) == 2.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This gives this error

ERROR: ArgumentError: `supports` is not defined for MathOptInterface.SolveTime(), it is only defined for attributes such that `is_copyable` returns `true`.
Stacktrace:
 [1] supports(::JuMP._MOIModel{Float64}, ::MathOptInterface.SolveTime) at /Users/guilhermebodin/.julia/packages/MathOptInterface/C3lip/src/attributes.jl:118
 [2] set(::MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}, ::MathOptInterface.SolveTime, ::Float64) at /Users/guilhermebodin/.julia/packages/MathOptInterface/C3lip/src/Utilities/universalfallback.jl:284
 [3] set(::MathOptInterface.Utilities.MockOptimizer{MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}}, ::MathOptInterface.SolveTime, ::Float64) at /Users/guilhermebodin/.julia/packages/MathOptInterface/C3lip/src/Utilities/mockoptimizer.jl:157
 [4] (::getfield(Main, Symbol("##17#18")))(::MathOptInterface.Utilities.MockOptimizer{MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}}) at ./REPL[61]:3
 [5] optimize!(::MathOptInterface.Utilities.MockOptimizer{MathOptInterface.Utilities.UniversalFallback{JuMP._MOIModel{Float64}}}) at /Users/guilhermebodin/.julia/packages/MathOptInterface/C3lip/src/Utilities/mockoptimizer.jl:113
 [6] #optimize!#79(::Bool, ::Bool, ::Function, ::Model, ::Nothing) at /Users/guilhermebodin/Documents/JuMP.jl/src/optimizer_interface.jl:132
 [7] optimize! at /Users/guilhermebodin/Documents/JuMP.jl/src/optimizer_interface.jl:105 [inlined] (repeats 2 times)
 [8] top-level scope at none:0

because of the MOI function

"""
    is_set_by_optimize(::AnyAttribute)

Return a `Bool` indicating whether the value of the attribute is modified
during an [`optimize!`](@ref) call, that is, the attribute is used to query
the result of the optimization.

## Important note when defining new attributes

This function returns `false` by default so it should be implemented for
attributes that are modified by [`optimize!`](@ref).
"""
is_set_by_optimize(::AnyAttribute) = false
function is_set_by_optimize(::Union{ObjectiveValue,
                                    ObjectiveBound,
                                    RelativeGap,
                                    SolveTime,
                                    SimplexIterations,
                                    BarrierIterations,
                                    NodeCount,
                                    RawSolver,
                                    ResultCount,
                                    TerminationStatus,
                                    RawStatusString,
                                    PrimalStatus,
                                    DualStatus,
                                    VariablePrimal,
                                    ConstraintPrimal,
                                    ConstraintDual,
                                    ConstraintBasisStatus})
    return true
end

is there another way to call the optimizer, I mean without having to call the optimize! function?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, you can leave a TODO while jump-dev/MathOptInterface.jl#707 is not solved

end

@testset "solver_name" begin
@testset "Not attached" begin
model = Model()
Expand Down