Skip to content

Commit e5447d7

Browse files
arnavk23tmigot
andauthored
Add SolverCore.reset!(solver) (#133)
* solver * JSOSolver * Update src/NLPModelsIpopt.jl Co-authored-by: Tangi Migot <[email protected]> * Update test/runtests.jl Co-authored-by: Tangi Migot <[email protected]> * Update test/runtests.jl Co-authored-by: Tangi Migot <[email protected]> * Update test/runtests.jl Co-authored-by: Tangi Migot <[email protected]> * Update src/NLPModelsIpopt.jl Co-authored-by: Tangi Migot <[email protected]> * removed TODO comment and set problem.obj_val to Inf * Apply suggestions from code review --------- Co-authored-by: Tangi Migot <[email protected]>
1 parent a3a2235 commit e5447d7

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/NLPModelsIpopt.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ function SolverCore.reset!(solver::IpoptSolver, nlp::AbstractNLPModel)
9393
@assert nlp.meta.nvar == problem.n
9494
@assert nlp.meta.ncon == problem.m
9595

96-
problem.obj_val = 0.0
97-
problem.status = 0
96+
problem.obj_val = Inf
97+
problem.status = -1
9898
problem.x .= nlp.meta.x0
9999
eval_f, eval_g, eval_grad_f, eval_jac_g, eval_h = set_callbacks(nlp)
100100
problem.eval_f = eval_f
@@ -108,6 +108,16 @@ function SolverCore.reset!(solver::IpoptSolver, nlp::AbstractNLPModel)
108108
return problem
109109
end
110110

111+
function SolverCore.reset!(solver::IpoptSolver)
112+
problem = solver.problem
113+
114+
problem.obj_val = Inf
115+
problem.status = -1 # Use -1 to indicate not solved yet
116+
problem.intermediate = nothing
117+
118+
return solver
119+
end
120+
111121
"""
112122
set_callbacks(nlp::AbstractNLPModel)
113123

test/runtests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,24 @@ end
117117
@test stats.iter >= 0
118118
@test isapprox(stats.dual_feas, 0.0; atol = 1e-8)
119119
end
120+
121+
@testset "Test restart with a different initial guess" begin
122+
f(x) = (x[1] - 1)^2 + 4 * (x[2] - x[1]^2)^2
123+
nlp = ADNLPModel(f, [-1.2; 1.0])
124+
stats = GenericExecutionStats(nlp)
125+
solver = IpoptSolver(nlp)
126+
127+
# Solve the problem first
128+
stats = solve!(solver, nlp, stats, print_level = 0)
129+
@test stats.status == :first_order
130+
@test isapprox(stats.solution, [1.0; 1.0], atol = 1e-6)
131+
132+
# Change initial guess and reset solver
133+
nlp.meta.x0 .= 2.0
134+
reset!(solver)
135+
136+
# Solve again with new initial guess
137+
stats = solve!(solver, nlp, stats, print_level = 0)
138+
@test stats.status == :first_order
139+
@test isapprox(stats.solution, [1.0; 1.0], atol = 1e-6)
140+
end

0 commit comments

Comments
 (0)