Skip to content

Commit 7f68017

Browse files
tmigotdpo
andauthored
Add reset! for solver (#47)
* add `reset!` for solver structure * add restart tests Co-authored-by: Dominique <dominique.orban@gmail.com>
1 parent ef4d83d commit 7f68017

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/solver.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ abstract type AbstractSolver end
55

66
abstract type AbstractOptimizationSolver <: AbstractSolver end
77

8+
"""
9+
reset!(solver::AbstractOptimizationSolver, model::AbstractNLPModel)
10+
11+
Use in the context of restarting or reusing the `solver` structure.
12+
Reset the internal fields of `solver` for the `model` before calling `solve!` on the same structure.
13+
`model` must have the same number of variables, bounds and constraints as that used to instantiate `solver`.
14+
"""
15+
function NLPModels.reset!(::AbstractOptimizationSolver, ::AbstractNLPModel) end
16+
817
"""
918
solve!(solver, model; kwargs...)
1019
solve!(solver, model, stats; kwargs...)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ include("dummy_solver.jl")
1212
include("test_logging.jl")
1313
include("test_stats.jl")
1414
include("test_callback.jl")
15+
include("test_restart.jl")

test/test_restart.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@testset "test restart" begin
2+
nlp = ADNLPModel(x -> dot(x, x) / 2, ones(2), x -> [sum(x .^ 3) - 1], [0.0], [0.0])
3+
solver = DummySolver(nlp)
4+
stats = GenericExecutionStats(nlp)
5+
solve!(solver, nlp, stats, verbose = false)
6+
@test stats.status == :first_order
7+
# Try with a new intial guess
8+
nlp.meta.x0 .= 0.2
9+
reset!(solver, nlp)
10+
solve!(solver, nlp, stats, verbose = false)
11+
@test stats.status == :first_order
12+
# Try with a new problem of the same size
13+
nlp = ADNLPModel(x -> dot(x, x) / 2, ones(2), x -> [sum(x .^ 3)], [0.0], [0.0])
14+
reset!(solver, nlp)
15+
solve!(solver, nlp, stats, verbose = false)
16+
@test stats.status == :first_order
17+
end

0 commit comments

Comments
 (0)