Skip to content

Commit

Permalink
Implement MOI.SolveTime (#144)
Browse files Browse the repository at this point in the history
* Implement MOI.SolveTime

* Update MOI_wrapper.jl
  • Loading branch information
odow authored Jul 7, 2021
1 parent 8fb929e commit 190ae32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AmplNLWriter"
uuid = "7c4d4715-977e-5154-bfe0-e096adeac482"
version = "0.7.1"
version = "0.7.2"

[deps]
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
Expand Down
7 changes: 7 additions & 0 deletions src/AmplNLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
stdin::Any
stdout::Any
results::_NLResults
solve_time::Float64
# Store MOI.Name().
name::String
# The objective expression.
Expand Down Expand Up @@ -295,6 +296,7 @@ function Optimizer(
Dict{MOI.VariableIndex,Float64}(),
Dict{MOI.VariableIndex,Float64}(),
),
NaN,
"",
_NLExpr(false, _NLTerm[], Dict{MOI.VariableIndex,Float64}(), 0.0),
MOI.FEASIBILITY_SENSE,
Expand Down Expand Up @@ -328,6 +330,7 @@ function MOI.empty!(model::Optimizer)
Dict{MOI.VariableIndex,Float64}(),
Dict{MOI.VariableIndex,Float64}(),
)
model.solve_time = NaN
model.f = _NLExpr(false, _NLTerm[], Dict{MOI.VariableIndex,Float64}(), 0.0)
empty!(model.g)
model.nlpblock_dim = 0
Expand Down Expand Up @@ -1130,6 +1133,7 @@ function _read_sol(io::IO, model::Optimizer)
end

function MOI.optimize!(model::Optimizer)
start_time = time()
temp_dir = mktempdir()
nl_file = joinpath(temp_dir, "model.nl")
open(io -> write(io, model), nl_file, "w")
Expand All @@ -1155,6 +1159,7 @@ function MOI.optimize!(model::Optimizer)
Dict{MOI.VariableIndex,Float64}(),
)
end
model.solve_time = time() - start_time
return
end

Expand All @@ -1172,6 +1177,8 @@ function MOI.get(
return model.results.primal_solution[x]
end

MOI.get(model::Optimizer, ::MOI.SolveTime) = model.solve_time

function MOI.get(model::Optimizer, ::MOI.TerminationStatus)
return model.results.termination_status
end
Expand Down
20 changes: 20 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ function test_AbstractSolverCommand(path)
@test model.solver_command === cmd
end

function test_solve_time(path)
model = optimizer(path)
@test isnan(MOI.get(model, MOI.SolveTime()))
v = MOI.add_variables(model, 4)
l = [1.1, 1.2, 1.3, 1.4]
u = [5.1, 5.2, 5.3, 5.4]
start = [2.1, 2.2, 2.3, 2.4]
MOI.add_constraint.(model, MOI.SingleVariable.(v), MOI.GreaterThan.(l))
MOI.add_constraint.(model, MOI.SingleVariable.(v), MOI.LessThan.(u))
MOI.set.(model, MOI.VariablePrimalStart(), v, start)
lb, ub = [25.0, 40.0], [Inf, 40.0]
evaluator = MOI.Test.HS071(true)
block_data = MOI.NLPBlockData(MOI.NLPBoundsPair.(lb, ub), evaluator, true)
MOI.set(model, MOI.NLPBlock(), block_data)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.optimize!(model)
@test MOI.get(model, MOI.SolveTime()) > 0.0
return
end

function runtests(path)
for name in names(@__MODULE__; all = true)
if !startswith("$(name)", "test_")
Expand Down

2 comments on commit 190ae32

@odow
Copy link
Member Author

@odow odow commented on 190ae32 Jul 7, 2021

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/40464

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.2 -m "<description of version>" 190ae320b5e00395d88136ad1ba590bd1e27b654
git push origin v0.7.2

Please sign in to comment.