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

Add solve_time function #1928

merged 9 commits into from
Apr 12, 2019

Conversation

guilhermebodin
Copy link
Contributor

Solving #1891

@codecov
Copy link

codecov bot commented Apr 8, 2019

Codecov Report

Merging #1928 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1928      +/-   ##
==========================================
+ Coverage   91.02%   91.03%   +<.01%     
==========================================
  Files          32       32              
  Lines        3979     3980       +1     
==========================================
+ Hits         3622     3623       +1     
  Misses        357      357
Impacted Files Coverage Δ
src/JuMP.jl 84.09% <100%> (+0.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b6554b8...b375de5. Read the comment docs.

@codecov
Copy link

codecov bot commented Apr 8, 2019

Codecov Report

Merging #1928 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1928      +/-   ##
==========================================
+ Coverage   91.06%   91.06%   +<.01%     
==========================================
  Files          32       32              
  Lines        3949     3950       +1     
==========================================
+ Hits         3596     3597       +1     
  Misses        353      353
Impacted Files Coverage Δ
src/JuMP.jl 83.72% <100%> (+0.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1371406...9660ed2. Read the comment docs.

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

"""
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

@mlubin mlubin merged commit 01ee0f9 into jump-dev:master Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants