Skip to content

Commit 07d7c99

Browse files
committed
stop gracefully on SIGINT in scalable problem examples
1 parent 1716ff2 commit 07d7c99

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

examples/ScalableProblems/RegisteredTNLP.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,39 @@ using namespace Ipopt;
1515
class RegisteredTNLP: public TNLP
1616
{
1717
public:
18+
RegisteredTNLP()
19+
: interrupted_(false)
20+
{ }
21+
1822
/** Initialize internal parameters.
1923
*
2024
* @return false, if N has an invalid value
2125
*/
2226
virtual bool InitializeProblem(
2327
Index N /**< determines problems size */
2428
) = 0;
29+
30+
bool intermediate_callback(
31+
AlgorithmMode /*mode*/,
32+
Index /*iter*/,
33+
Number /*obj_value*/,
34+
Number /*inf_pr*/,
35+
Number /*inf_du*/,
36+
Number /*mu*/,
37+
Number /*d_norm*/,
38+
Number /*regularization_size*/,
39+
Number /*alpha_du*/,
40+
Number /*alpha_pr*/,
41+
Index /*ls_trials*/,
42+
const IpoptData* /*ip_data*/,
43+
IpoptCalculatedQuantities* /*ip_cq*/
44+
)
45+
{
46+
/* returning false makes Ipopt stop */
47+
return !interrupted_;
48+
}
49+
50+
bool interrupted_;
2551
};
2652

2753
class RegisteredTNLPs

examples/ScalableProblems/solve_problem.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Authors: Andreas Waechter IBM 2004-11-05
66

77
#include "IpIpoptApplication.hpp"
8+
#include "IpUtils.hpp"
89
#include "RegisteredTNLP.hpp"
910

1011
#include <cstdio>
@@ -202,7 +203,11 @@ int main(
202203
app->Options()->SetNumericValue("max_wall_time", runtime);
203204
#endif
204205

206+
Ipopt::RegisterInterruptHandler(NULL, &tnlp->interrupted_);
207+
205208
status = app->OptimizeTNLP(GetRawPtr(tnlp));
206209

210+
Ipopt::UnregisterInterruptHandler();
211+
207212
return (int) status;
208213
}

0 commit comments

Comments
 (0)