Skip to content

Commit

Permalink
stop gracefully on SIGINT in scalable problem examples
Browse files Browse the repository at this point in the history
  • Loading branch information
svigerske committed Dec 14, 2024
1 parent 1716ff2 commit 07d7c99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/ScalableProblems/RegisteredTNLP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,39 @@ using namespace Ipopt;
class RegisteredTNLP: public TNLP
{
public:
RegisteredTNLP()
: interrupted_(false)
{ }

/** Initialize internal parameters.
*
* @return false, if N has an invalid value
*/
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
Expand Down
5 changes: 5 additions & 0 deletions examples/ScalableProblems/solve_problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Authors: Andreas Waechter IBM 2004-11-05

#include "IpIpoptApplication.hpp"
#include "IpUtils.hpp"
#include "RegisteredTNLP.hpp"

#include <cstdio>
Expand Down Expand Up @@ -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;
}

0 comments on commit 07d7c99

Please sign in to comment.