From 07d7c997998470335aebc1373249892186636550 Mon Sep 17 00:00:00 2001 From: Stefan Vigerske Date: Sat, 14 Dec 2024 14:16:28 +0100 Subject: [PATCH] stop gracefully on SIGINT in scalable problem examples --- examples/ScalableProblems/RegisteredTNLP.hpp | 26 ++++++++++++++++++++ examples/ScalableProblems/solve_problem.cpp | 5 ++++ 2 files changed, 31 insertions(+) diff --git a/examples/ScalableProblems/RegisteredTNLP.hpp b/examples/ScalableProblems/RegisteredTNLP.hpp index be1efb910..c0fa321ee 100644 --- a/examples/ScalableProblems/RegisteredTNLP.hpp +++ b/examples/ScalableProblems/RegisteredTNLP.hpp @@ -15,6 +15,10 @@ using namespace Ipopt; class RegisteredTNLP: public TNLP { public: + RegisteredTNLP() + : interrupted_(false) + { } + /** Initialize internal parameters. * * @return false, if N has an invalid value @@ -22,6 +26,28 @@ class RegisteredTNLP: public TNLP virtual bool InitializeProblem( Index N /**< determines problems size */ ) = 0; + + bool intermediate_callback( + AlgorithmMode /*mode*/, + Index /*iter*/, + Number /*obj_value*/, + Number /*inf_pr*/, + Number /*inf_du*/, + Number /*mu*/, + Number /*d_norm*/, + Number /*regularization_size*/, + Number /*alpha_du*/, + Number /*alpha_pr*/, + Index /*ls_trials*/, + const IpoptData* /*ip_data*/, + IpoptCalculatedQuantities* /*ip_cq*/ + ) + { + /* returning false makes Ipopt stop */ + return !interrupted_; + } + + bool interrupted_; }; class RegisteredTNLPs diff --git a/examples/ScalableProblems/solve_problem.cpp b/examples/ScalableProblems/solve_problem.cpp index 5257b5c46..06005fd5f 100644 --- a/examples/ScalableProblems/solve_problem.cpp +++ b/examples/ScalableProblems/solve_problem.cpp @@ -5,6 +5,7 @@ // Authors: Andreas Waechter IBM 2004-11-05 #include "IpIpoptApplication.hpp" +#include "IpUtils.hpp" #include "RegisteredTNLP.hpp" #include @@ -202,7 +203,11 @@ int main( app->Options()->SetNumericValue("max_wall_time", runtime); #endif + Ipopt::RegisterInterruptHandler(NULL, &tnlp->interrupted_); + status = app->OptimizeTNLP(GetRawPtr(tnlp)); + Ipopt::UnregisterInterruptHandler(); + return (int) status; }