Skip to content

Commit 190ae32

Browse files
authored
Implement MOI.SolveTime (#144)
* Implement MOI.SolveTime * Update MOI_wrapper.jl
1 parent 8fb929e commit 190ae32

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "AmplNLWriter"
22
uuid = "7c4d4715-977e-5154-bfe0-e096adeac482"
3-
version = "0.7.1"
3+
version = "0.7.2"
44

55
[deps]
66
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"

src/AmplNLWriter.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
198198
stdin::Any
199199
stdout::Any
200200
results::_NLResults
201+
solve_time::Float64
201202
# Store MOI.Name().
202203
name::String
203204
# The objective expression.
@@ -295,6 +296,7 @@ function Optimizer(
295296
Dict{MOI.VariableIndex,Float64}(),
296297
Dict{MOI.VariableIndex,Float64}(),
297298
),
299+
NaN,
298300
"",
299301
_NLExpr(false, _NLTerm[], Dict{MOI.VariableIndex,Float64}(), 0.0),
300302
MOI.FEASIBILITY_SENSE,
@@ -328,6 +330,7 @@ function MOI.empty!(model::Optimizer)
328330
Dict{MOI.VariableIndex,Float64}(),
329331
Dict{MOI.VariableIndex,Float64}(),
330332
)
333+
model.solve_time = NaN
331334
model.f = _NLExpr(false, _NLTerm[], Dict{MOI.VariableIndex,Float64}(), 0.0)
332335
empty!(model.g)
333336
model.nlpblock_dim = 0
@@ -1130,6 +1133,7 @@ function _read_sol(io::IO, model::Optimizer)
11301133
end
11311134

11321135
function MOI.optimize!(model::Optimizer)
1136+
start_time = time()
11331137
temp_dir = mktempdir()
11341138
nl_file = joinpath(temp_dir, "model.nl")
11351139
open(io -> write(io, model), nl_file, "w")
@@ -1155,6 +1159,7 @@ function MOI.optimize!(model::Optimizer)
11551159
Dict{MOI.VariableIndex,Float64}(),
11561160
)
11571161
end
1162+
model.solve_time = time() - start_time
11581163
return
11591164
end
11601165

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

1180+
MOI.get(model::Optimizer, ::MOI.SolveTime) = model.solve_time
1181+
11751182
function MOI.get(model::Optimizer, ::MOI.TerminationStatus)
11761183
return model.results.termination_status
11771184
end

test/MOI_wrapper.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,26 @@ function test_AbstractSolverCommand(path)
228228
@test model.solver_command === cmd
229229
end
230230

231+
function test_solve_time(path)
232+
model = optimizer(path)
233+
@test isnan(MOI.get(model, MOI.SolveTime()))
234+
v = MOI.add_variables(model, 4)
235+
l = [1.1, 1.2, 1.3, 1.4]
236+
u = [5.1, 5.2, 5.3, 5.4]
237+
start = [2.1, 2.2, 2.3, 2.4]
238+
MOI.add_constraint.(model, MOI.SingleVariable.(v), MOI.GreaterThan.(l))
239+
MOI.add_constraint.(model, MOI.SingleVariable.(v), MOI.LessThan.(u))
240+
MOI.set.(model, MOI.VariablePrimalStart(), v, start)
241+
lb, ub = [25.0, 40.0], [Inf, 40.0]
242+
evaluator = MOI.Test.HS071(true)
243+
block_data = MOI.NLPBlockData(MOI.NLPBoundsPair.(lb, ub), evaluator, true)
244+
MOI.set(model, MOI.NLPBlock(), block_data)
245+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
246+
MOI.optimize!(model)
247+
@test MOI.get(model, MOI.SolveTime()) > 0.0
248+
return
249+
end
250+
231251
function runtests(path)
232252
for name in names(@__MODULE__; all = true)
233253
if !startswith("$(name)", "test_")

0 commit comments

Comments
 (0)