ipopt-src 0.2.3+3.14.16

Redistribution of Coin-OR Ipopt as a crate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// Copyright (C) 2008, 2011 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors:  Andreas Waechter            IBM    2008-09-19

#include "IpInexactPDTerminationTester.hpp"
#include "IpBlas.hpp"

#include <cmath>

namespace Ipopt
{
#if IPOPT_VERBOSITY > 0
static const Index dbg_verbosity = 0;
#endif

InexactPDTerminationTester::InexactPDTerminationTester()
{
   DBG_START_METH("InexactPDTerminationTester::InexactPDTerminationTester", dbg_verbosity);
}

InexactPDTerminationTester::~InexactPDTerminationTester()
{
   DBG_START_METH("InexactPDTerminationTester::~InexactPDTerminationTester()", dbg_verbosity);
}

void InexactPDTerminationTester::RegisterOptions(
   SmartPtr<RegisteredOptions> roptions)
{
   roptions->AddLowerBoundedNumberOption(
      "tcc_psi",
      "Psi factor in Tangential Component Condition.",
      0.0, true,
      1e-1);
   roptions->AddLowerBoundedNumberOption(
      "tcc_theta",
      "theta factor in Tangential Component Condition.",
      0.0, true,
      1e-12);
   roptions->AddLowerBoundedNumberOption(
      "tcc_theta_mu_exponent",
      "exponent for mu when multiplied with tcc_theta in Tangential Component Condition.",
      0., false,
      0.);
   roptions->AddLowerBoundedNumberOption(
      "tcc_zeta",
      "zeta factor in Tangential Component Condition.",
      0.0, true,
      1e-1);
   roptions->AddLowerBoundedNumberOption(
      "tt_kappa1",
      "kappa1 factor in Termination Test 1 and 3.",
      0.0, true,
      1e-3);
   roptions->AddLowerBoundedNumberOption(
      "tt_kappa2",
      "kappa2 factor in Termination Test 2.",
      0.0, true,
      1e-1);
   roptions->AddLowerBoundedNumberOption(
      "tt_eps2",
      "eps2 factor in Termination Test 2.",
      0.0, true,
      1.);
   roptions->AddLowerBoundedNumberOption(
      "tt_eps3",
      "eps3 factor in Termination Test 3.",
      0.0, true,
      1. - 1e-1);
   roptions->AddLowerBoundedNumberOption(
      "inexact_desired_pd_residual",
      "Desired relative residual tolerance for iterative solver during primal-dual step computation.",
      0.0, true,
      1e-3);
   roptions->AddLowerBoundedIntegerOption(
      "inexact_desired_pd_residual_iter",
      "Number of iterations willing to be spent in obtaining desired primal-dual ration.",
      0,
      1);
}

bool InexactPDTerminationTester::InitializeImpl(
   const OptionsList& options,
   const std::string& prefix)
{
   options.GetNumericValue("tcc_psi", tcc_psi_, prefix);
   options.GetNumericValue("tcc_theta", tcc_theta_, prefix);
   options.GetNumericValue("tcc_theta_mu_exponent", tcc_theta_mu_exponent_, prefix);
   options.GetNumericValue("tcc_zeta", tcc_zeta_, prefix);
   options.GetNumericValue("tt_kappa1", tt_kappa1_, prefix);
   options.GetNumericValue("tt_kappa2", tt_kappa2_, prefix);
   options.GetNumericValue("tt_eps2", tt_eps2_, prefix);
   options.GetNumericValue("tt_eps3", tt_eps3_, prefix);
   options.GetNumericValue("rho", rho_, prefix);
   options.GetNumericValue("inexact_desired_pd_residual", inexact_desired_pd_residual_, prefix);
   options.GetIntegerValue("inexact_desired_pd_residual_iter", inexact_desired_pd_residual_iter_, prefix);

   std::string inexact_linear_system_scaling;
   options.GetStringValue("inexact_linear_system_scaling", inexact_linear_system_scaling, prefix);
   if( inexact_linear_system_scaling == "slack-based" )
   {
      requires_scaling_ = true;
   }
   else
   {
      requires_scaling_ = false;
   }

   return true;
}

bool InexactPDTerminationTester::InitializeSolve()
{
   DBG_START_METH("InexactPDTerminationTester::InitializeSolve",
                  dbg_verbosity);

   bool compute_normal = InexData().compute_normal();

   // compute the current infeasibility
   c_norm_ = IpCq().curr_primal_infeasibility(NORM_2);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: c_norm = %23.16e\n", c_norm_);

   SmartPtr<const Vector> normal_x = InexData().normal_x();
   SmartPtr<const Vector> normal_s = InexData().normal_s();
   if( compute_normal )
   {
      // calculate Jacobian times normal step (no scaling relevant in this space)
      curr_Av_c_ = InexCq().curr_jac_times_normal_c();
      curr_Av_d_ = InexCq().curr_jac_times_normal_d();

      // compute the linearized infeasibility at the normal step
      SmartPtr<const Vector> curr_c = IpCq().curr_c();
      SmartPtr<Vector> tmp1 = curr_c->MakeNew();
      tmp1->AddTwoVectors(1, *curr_c, 1., *curr_Av_c_, 0.);
      SmartPtr<const Vector> curr_d_minus_s = IpCq().curr_d_minus_s();
      SmartPtr<Vector> tmp2 = curr_d_minus_s->MakeNew();
      tmp2->AddTwoVectors(1, *curr_d_minus_s, 1., *curr_Av_d_, 0.);
      c_plus_Av_norm_ = IpCq().CalcNormOfType(NORM_2, *tmp1, *tmp2);
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: c_plus_Av_norm_ = %23.16e\n", c_plus_Av_norm_);

      // compute norm of the normal step in the scaled space
      v_norm_scaled_ = InexCq().slack_scaled_norm(*normal_x, *normal_s);
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: v_norm_scaled_ = %23.16e\n", v_norm_scaled_);

      // compute Wv (Hessian times normal step) in the unscaled space
      curr_Wv_x_ = InexCq().curr_W_times_vec_x(*normal_x);
      curr_Wv_s_ = InexCq().curr_W_times_vec_s(*normal_s);
   }
   else
   {
      curr_Av_c_ = NULL;
      curr_Av_d_ = NULL;
      c_plus_Av_norm_ = -1.;
      v_norm_scaled_ = -1.;
      curr_Wv_x_ = NULL;
      curr_Wv_s_ = NULL;
   }

   // store the previous gradient and Jacobian information
   SmartPtr<const Vector> last_grad_barrier_obj_x = curr_grad_barrier_obj_x_;
   SmartPtr<const Vector> last_grad_barrier_obj_s = curr_grad_barrier_obj_s_;
   SmartPtr<const Matrix> last_jac_c = curr_jac_c_;
   SmartPtr<const Matrix> last_jac_d = curr_jac_d_;
   SmartPtr<const Vector> last_scaling_slacks = curr_scaling_slacks_;
   last_Av_norm_ = curr_Av_norm_;

   // get the current gradient and Jacobian information
   curr_grad_barrier_obj_x_ = IpCq().curr_grad_barrier_obj_x();
   curr_grad_barrier_obj_s_ = IpCq().curr_grad_barrier_obj_s(); // (unscaled)
   curr_jac_c_ = IpCq().curr_jac_c();
   curr_jac_d_ = IpCq().curr_jac_d();
   curr_scaling_slacks_ = InexCq().curr_scaling_slacks();

   // calculate \nabla phi(x_{k}) + A_{k}^Ty_k (in scaled space)
   SmartPtr<const Vector> curr_jac_cT_times_curr_y_c = IpCq().curr_jac_cT_times_curr_y_c();
   SmartPtr<const Vector> curr_jac_cT_times_curr_y_d = IpCq().curr_jac_dT_times_curr_y_d();
   curr_nabla_phi_plus_ATy_x_ = curr_grad_barrier_obj_x_->MakeNewCopy();
   curr_nabla_phi_plus_ATy_x_->AddTwoVectors(1., *curr_jac_cT_times_curr_y_c, 1., *curr_jac_cT_times_curr_y_d, 1.);
   curr_nabla_phi_plus_ATy_s_ = curr_grad_barrier_obj_s_->MakeNew();
   curr_nabla_phi_plus_ATy_s_->AddTwoVectors(1., *curr_grad_barrier_obj_s_, -1., *IpData().curr()->y_d(), 0.);
   curr_nabla_phi_plus_ATy_s_->ElementWiseMultiply(*curr_scaling_slacks_);

   // calculate norms appearing in termination tests
   curr_tt2_norm_ = IpCq().CalcNormOfType(NORM_2, *curr_nabla_phi_plus_ATy_x_, *curr_nabla_phi_plus_ATy_s_);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: curr_tt2_norm_ = %23.16e\n", curr_tt2_norm_);
   if( compute_normal )
   {
      curr_Av_norm_ = IpCq().CalcNormOfType(NORM_2, *curr_Av_c_, *curr_Av_d_);
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: curr_Av_norm_ = %23.16e\n", curr_Av_norm_);
      curr_tt1_norm_ = std::sqrt(std::pow(curr_tt2_norm_, 2) + std::pow(curr_Av_norm_, 2));
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: curr_tt1_norm_ = %23.16e\n", curr_tt1_norm_);
   }
   else
   {
      curr_Av_norm_ = -1.;
      curr_tt1_norm_ = std::sqrt(std::pow(curr_tt2_norm_, 2) + std::pow(c_norm_, 2));
   }

   if( compute_normal && IsValid(last_grad_barrier_obj_x) )
   {
      // calculate \nabla phi(x_{k-1}) + A_{k-1}^Ty_k (in scaled space)
      SmartPtr<Vector> last_nabla_phi_plus_ATy_x = last_grad_barrier_obj_x->MakeNewCopy();
      last_jac_c->TransMultVector(1., *IpData().curr()->y_c(), 1., *last_nabla_phi_plus_ATy_x);
      last_jac_d->TransMultVector(1., *IpData().curr()->y_d(), 1., *last_nabla_phi_plus_ATy_x);
      SmartPtr<Vector> last_nabla_phi_plus_ATy_s = last_grad_barrier_obj_s->MakeNew();
      last_nabla_phi_plus_ATy_s->AddTwoVectors(1., *last_grad_barrier_obj_s, -1., *IpData().curr()->y_d(), 0.);
      last_nabla_phi_plus_ATy_s->ElementWiseMultiply(*last_scaling_slacks);
      last_tt1_norm_ = IpCq().CalcNormOfType(NORM_2, *last_nabla_phi_plus_ATy_x, *last_nabla_phi_plus_ATy_s);
      last_tt1_norm_ = std::sqrt(std::pow(last_tt1_norm_, 2) + std::pow(last_Av_norm_, 2));
   }
   else
   {
      last_tt1_norm_ = 1e100;
   }
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: last_tt1_norm_ = %23.16e\n", last_tt1_norm_);

   // check if we need to test termination test 2
   Number ATc_norm = InexCq().curr_scaled_Ac_norm();
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: current ATc norm = %23.16e\n", ATc_norm);
   try_tt2_ = (ATc_norm <= tt_eps2_ * curr_tt2_norm_);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: will%s try termination test 2.\n", try_tt2_ ? "" : " not");

   return true;
}

InexactPDTerminationTester::ETerminationTest InexactPDTerminationTester::TestTermination(
   Index         ndim,
   const Number* sol,
   const Number* resid,
   Index         iter,
   Number        norm2_rhs
)
{
   DBG_START_METH("InexactPDTerminationTester::TestTermination",
                  dbg_verbosity);

   bool compute_normal = InexData().compute_normal();

   last_iter_ = iter;

   ETerminationTest retval = CONTINUE;

   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "Starting PD Termination Tester for iteration %" IPOPT_INDEX_FORMAT ".\n", iter);
   /*
    if (iter%5 != 4) {
    Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
    "TT: immediately leaving tester for iteration %" IPOPT_INDEX_FORMAT ".\n", iter);
    return retval;
    }
    */
   Number norm2_resid = IpBlasNrm2(ndim, resid, 1);
   Number test_ratio = norm2_resid / norm2_rhs; // Min(norm2_resid/norm2_rhs, norm2_resid);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: test ratio %e (norm2_rhs = %e norm2_resid = %e).\n",
                  test_ratio, norm2_rhs, norm2_resid);
   if( iter < inexact_desired_pd_residual_iter_ && test_ratio > inexact_desired_pd_residual_ )
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: immediately leaving tester with test ratio %e (norm2_rhs = %e norm2_resid = %e).\n", test_ratio,
                     norm2_rhs, norm2_resid);
      return retval;
   }

   SmartPtr<const Vector> sol_x;
   SmartPtr<const Vector> sol_s;
   SmartPtr<const Vector> sol_c;
   SmartPtr<const Vector> sol_d;
   GetVectors(ndim, sol, sol_x, sol_s, sol_c, sol_d);

   DBG_PRINT_VECTOR(2, "sol_x", *sol_x);
   DBG_PRINT_VECTOR(2, "sol_s", *sol_s);
   DBG_PRINT_VECTOR(2, "sol_c", *sol_c);
   DBG_PRINT_VECTOR(2, "sol_d", *sol_d);

   SmartPtr<const Vector> resid_x;
   SmartPtr<const Vector> resid_s;
   SmartPtr<const Vector> resid_c;
   SmartPtr<const Vector> resid_d;
   GetVectors(ndim, resid, resid_x, resid_s, resid_c, resid_d);

   if( requires_scaling_ )
   {
      SmartPtr<const Vector> scaling_vec = curr_scaling_slacks_;
      SmartPtr<Vector> tmp = sol_s->MakeNewCopy();
      tmp->ElementWiseMultiply(*scaling_vec);
      sol_s = ConstPtr(tmp);
      tmp = resid_s->MakeNewCopy();
      tmp->ElementWiseDivide(*scaling_vec);
      resid_s = ConstPtr(tmp);
   }

   //// Set algorithm
   if( !compute_normal )
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "RUNNING TERMINATION TESTS FOR INEXACT NEWTON (which means u=d)\n");
   }
   else
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "RUNNING TERMINATION TESTS FOR INEXACT NEWTON - TRUST REGION\n");
   }

   // Get the tangential step and its scaled norm
   SmartPtr<const Vector> tangential_x;
   SmartPtr<const Vector> tangential_s;
   if( compute_normal )
   {
      SmartPtr<const Vector> normal_x = InexData().normal_x();
      SmartPtr<Vector> tmp = sol_x->MakeNew();
      tmp->AddTwoVectors(1., *sol_x, -1, *normal_x, 0.);
      tangential_x = ConstPtr(tmp);
      SmartPtr<const Vector> normal_s = InexData().normal_s();
      tmp = sol_s->MakeNew();
      tmp->AddTwoVectors(1., *sol_s, -1, *normal_s, 0.);
      tangential_s = ConstPtr(tmp);
   }
   else
   {
      tangential_x = sol_x;
      tangential_s = sol_s;
   }
   Number u_norm_scaled = InexCq().slack_scaled_norm(*tangential_x, *tangential_s);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: u_norm_scaled = %23.16e\n", u_norm_scaled);

   // Compute u^TWu
   DBG_PRINT_VECTOR(2, "tangential_x", *tangential_x);
   DBG_PRINT_VECTOR(2, "tangential_s", *tangential_s);
   SmartPtr<const Vector> Wu_x = InexCq().curr_W_times_vec_x(*tangential_x);
   SmartPtr<const Vector> Wu_s = InexCq().curr_W_times_vec_s(*tangential_s);
   DBG_PRINT_VECTOR(2, "Wu_x", *Wu_x);
   DBG_PRINT_VECTOR(2, "Wu_s", *Wu_s);
   Number uWu = Wu_x->Dot(*tangential_x) + Wu_s->Dot(*tangential_s);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: uWu = %23.16e\n", uWu);

   // Compute norm of c + Ad
   SmartPtr<Vector> c_plus_Ad_c = IpCq().curr_c()->MakeNewCopy();
   curr_jac_c_->MultVector(1., *sol_x, 1., *c_plus_Ad_c);
   SmartPtr<const Vector> curr_d_minus_s = IpCq().curr_d_minus_s();
   SmartPtr<Vector> c_plus_Ad_d = curr_d_minus_s->MakeNew();
   c_plus_Ad_d->AddTwoVectors(1., *curr_d_minus_s, -1., *sol_s, 0.);
   curr_jac_d_->MultVector(1., *sol_x, 1., *c_plus_Ad_d);
   Number c_plus_Ad_norm = IpCq().CalcNormOfType(NORM_2, *c_plus_Ad_c, *c_plus_Ad_d);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: c_plus_Ad_norm  = %23.16e\n", c_plus_Ad_norm);

   // Compute norm of scaled residual rho
   SmartPtr<Vector> tmp = resid_s->MakeNewCopy();
   tmp->ElementWiseMultiply(*curr_scaling_slacks_);
   Number rho_norm = IpCq().CalcNormOfType(NORM_2, *resid_x, *tmp);
   Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                  "TT: rho_norm  = %23.16e\n", rho_norm);
   tmp = NULL;

   // TODO: AW wants to discuss with Frank
   Number Upsilon = -1.;
   Number Nu = -1.;
   if( !compute_normal )
   {
#if 0
      // Compute Nu = ||A*u||^2/||A||^2
      SmartPtr<const Vector> curr_Au_c = IpCq().curr_jac_c_times_vec(*tangential_x);
      SmartPtr<Vector> curr_Au_d = sol_s->MakeNew();
      curr_Au_d->AddTwoVectors(1., *IpCq().curr_jac_d_times_vec(*tangential_x), -1., *tangential_s, 0.);
      Number Nu = IpCq().CalcNormOfType(NORM_2, *curr_Au_c, *curr_Au_d);
      Nu = std::pow(Nu, 2);
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: ||A*u||^2 = %23.16e\n", Nu);
      Number A_norm2 = InexCq().curr_scaled_A_norm2();
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: ||A||^2 = %23.16e\n", A_norm2);
#endif
      Nu = 0; //Nu/A_norm2;
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: Nu = ||A*u||^2/||A||^2 = %23.16e\n", Nu);

      // Compute Upsilon = ||u||^2 - Nu
      Upsilon = u_norm_scaled * u_norm_scaled - Nu;
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: Upsilon = ||u||^2 - ||A*u||^2/||A||^2 = %23.16e\n",  Upsilon);
   }

   // Base value, something on the order of square root of machine epsilon; TODO: find a better base value
   Number BasVal = Max(IpData().curr()->x()->Amax(), IpData().curr()->s()->Amax());

   // Check tangential component condition, part 1
   Number lhs;
   Number rhs;
   if( !compute_normal )
   {
      lhs = Upsilon;
      rhs = std::pow(tcc_psi_, 2) * Nu;
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TCC1 testing Upsilon(=%23.16e) <= (tcc_psi_^2)*Nu(=%23.16e) --> ", lhs, rhs);
   }
   else
   {
      lhs = u_norm_scaled;
      rhs = tcc_psi_ * v_norm_scaled_;
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TCC1 testing u_norm_scaled(=%23.16e) <= tcc_psi_*v_norm_scaled(=%23.16e) --> ", lhs, rhs);
   }
   bool tcc1 = Compare_le(lhs, rhs, BasVal);
   if( tcc1 )
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "satisfied\n");
   }
   else
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "violated\n");
   }

   // Check tangential component condition, part 2a
   const Number mu = IpData().curr_mu();
   rhs = 0.5 * uWu;
   if( !compute_normal )
   {
      lhs = tcc_theta_ * std::pow(mu, tcc_theta_mu_exponent_) * Upsilon;
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TCC2a testing 0.5*uWu(=%23.16e) >= tcc_theta_*pow(mu,tcc_theta_mu_exponent_)*Upsilon(=%23.16e) -->", rhs,
                     lhs);
   }
   else
   {
      lhs = tcc_theta_ * std::pow(mu, tcc_theta_mu_exponent_) * std::pow(u_norm_scaled, 2);
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TCC2a testing 0.5*uWu(=%23.16e) >= tcc_theta_*pow(mu,tcc_theta_mu_exponent_)*u_norm^2(=%23.16e) -->", rhs,
                     lhs);
   }
   bool tcc2a = Compare_le(lhs, rhs, BasVal);
   if( tcc2a )
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "satisfied\n");
   }
   else
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "violated\n");
   }

   // Check tangential component condition, part 2b (only in MIPS)
   bool tcc = tcc1;
   if( !tcc && tcc2a )
   {
      if( !compute_normal )
      {
         tcc = tcc2a;
      }
      else
      {
         lhs = 0.5 * uWu + curr_grad_barrier_obj_x_->Dot(*tangential_x) + curr_grad_barrier_obj_s_->Dot(*tangential_s)
               + curr_Wv_x_->Dot(*tangential_x) + curr_Wv_s_->Dot(*tangential_s);
         rhs = tcc_zeta_ * v_norm_scaled_;
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "TCC2b testing (grad_barr^Tu + v^TWu + 0.5*uWu)(=%23.16e) <= tcc_zeta_*v_norm(=%23.16e) -->", lhs, rhs);
         tcc = Compare_le(lhs, rhs, BasVal);
         if( tcc )
         {
            Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                           "satisfied\n");
         }
         else
         {
            Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                           "violated\n");
         }
      }
   }

   // Check tangential component condition
   if( tcc )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Tangential Component Condition satisfied\n");
   }
   else
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Tangential Component Condition violated\n");
   }

   // Check termination test 1, residual condition
   bool tt1 = tcc;
   bool tt1_kappa1 = tcc;
   if( !compute_normal )
   {
      // Compute scaled norm of entire residual in case there is no step
      // decomposition.  In that case, c_plus_Ad_norm should indeed be
      // the same as what resid_c and resid_d woulod give (TODO:
      // check?!?)
      Number resid_norm = std::sqrt(std::pow(rho_norm, 2) + std::pow(c_plus_Ad_norm, 2));
      lhs = resid_norm;
      rhs = tt_kappa1_ * curr_tt1_norm_;
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT1 testing resid_norm(=%23.16e) <= tt_kappa1_*curr_tt1_norm_(=%23.16e) --> ", lhs, rhs);
      tt1 = Compare_le(lhs, rhs, BasVal);
   }
   else if( tt1 )
   {
      lhs = rho_norm;
      rhs = tt_kappa1_ * Min(curr_tt1_norm_, last_tt1_norm_);
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT1 testing rho_norm(=%23.16e) <= kappa1*min(curr_tt1_norm_, last_tt1_norm_)(=%23.16e) -->", lhs, rhs);
      tt1_kappa1 = Compare_le(lhs, rhs, BasVal);
      tt1 = tt1_kappa1;
   }
   if( tt1 )
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "satisfied\n");
   }
   else
   {
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "violated\n");
   }

   // Check termination test 1, model reduction condition
   // cppcheck-suppress variableScope
   // cppcheck-suppress unreadVariable
   bool model_reduction = false;
   if( !compute_normal || tt1 )
   {
      Number curr_nu = InexData().curr_nu();
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: curr_nu = %23.16e\n", curr_nu);
      Number delta_m = -(curr_grad_barrier_obj_x_->Dot(*sol_x) + curr_grad_barrier_obj_s_->Dot(*sol_s));
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: -grad_barr^Td = %23.16e\n", delta_m);
      delta_m += curr_nu * (c_norm_ - c_plus_Ad_norm);
      Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                     "TT: delta_m = %23.16e\n", delta_m);
      rhs = delta_m;
      Number sigma = rho_ * tt_eps3_;
      if( !compute_normal )
      {
         lhs = Max(0.5 * uWu, tcc_theta_ * Upsilon) + sigma * curr_nu * Max(c_norm_, c_plus_Ad_norm - c_norm_);
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "MRC testing delta_m(=%23.16e) >= max(0.5*uWu,tcc_theta_*Upsilon) + sigma*nu*max(c_norm_, c_plus_Ad_norm - c_norm_)(=%23.16e) -->",
                        rhs, lhs);
         model_reduction = Compare_le(lhs, rhs, BasVal);
         if( model_reduction )
         {
            Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                           "satisfied\n");
         }
         else
         {
            Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                           "violated\n");
         }
         if( tt1 )
         {
            tt1 = model_reduction;
         }
      }
      else
      {
         lhs = Max(0.5 * uWu, tcc_theta_ * std::pow(u_norm_scaled, 2)) + sigma * curr_nu * (c_norm_ - c_plus_Av_norm_);
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "MRC testing delta_m(=%23.16e) >= max(0.5*uWu,tcc_theta_*u_norm^2) + sigma*nu*(c_norm_ - c_plus_Av_norm_)(=%23.16e) -->",
                        rhs, lhs);
         tt1 = Compare_le(lhs, rhs, BasVal);
         if( tt1 )
         {
            Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                           "satisfied\n");
         }
         else
         {
            Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                           "violated\n");
         }
      }
   }

   // Check termination test 1
   if( tt1 )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Termination Test 1 satisfied.\n");
      return TEST_1_SATISFIED;
   }
   else
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Termination Test 1 not satisfied.\n");
   }

   // Check termination test 3, residual condition
   bool tt3 = tcc;
   if( tt3 )
   {
      if( !compute_normal )
      {
         lhs = rho_norm;
         rhs = tt_kappa1_ * c_norm_;
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "TT3 testing rho_norm(=%23.16e) <= tt_kappa1_*c_norm_(=%23.16e) -->", lhs, rhs);
         tt3 = Compare_le(lhs, rhs, BasVal);
      }
      else
      {
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "TT3 with residual condition from TT1 -->");
         tt3 = tt1_kappa1;
      }
      if( tt3 )
      {
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "satisfied\n");
      }
      else
      {
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "violated\n");
      }
   }

   // Check termination test 3, linearized feasibility condition
   if( tt3 )
   {
      if( !compute_normal )
      {
         lhs = c_plus_Ad_norm;
         rhs = tt_kappa1_ * c_norm_;
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "TT3 testing c_plus_Ad_norm(=%23.16e) <= tt_kappa1_*c_norm(=%23.16e) -->", lhs, rhs);
         tt3 = Compare_le(lhs, rhs, BasVal);
      }
      else
      {
         lhs = tt_eps3_ * (c_norm_ - c_plus_Av_norm_);
         rhs = c_norm_ - c_plus_Ad_norm;
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "TT3 testing (c_norm_-c_plus_Ad_norm)(=%23.16e) >= eps3*(c_norm_-c_plus_Av_norm_)(=%23.16e) -->", rhs, lhs);
         tt3 = Compare_le(lhs, rhs, BasVal);
      }
      if( tt3 )
      {
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "satisfied\n");
      }
      else
      {
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "violated\n");
      }
   }

   // Check termination test 3
   if( tt3 )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Termination Test 3 satisfied.\n");
      return TEST_3_SATISFIED;
   }
   else
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Termination Test 3 not satisfied.\n");
   }

   // Check termination test 2
   bool tt2 = try_tt2_;
   if( tt2 )
   {
      DBG_PRINT_VECTOR(2, "curr_nabla_phi_plus_ATy_x_", *curr_nabla_phi_plus_ATy_x_);
      DBG_PRINT_VECTOR(2, "curr_nabla_phi_plus_ATy_s_", *curr_nabla_phi_plus_ATy_s_);
      DBG_PRINT_VECTOR(2, "sol_c", *sol_c);
      DBG_PRINT_VECTOR(2, "sol_d", *sol_d);
      SmartPtr<Vector> sol_d_scaled = sol_d->MakeNewCopy();
      sol_d_scaled->ElementWiseMultiply(*curr_scaling_slacks_);
      DBG_PRINT_VECTOR(2, "sol_d_scaled", *sol_d_scaled);
      SmartPtr<Vector> nabla_phi_plus_ATydelta_x = curr_nabla_phi_plus_ATy_x_->MakeNewCopy();
      curr_jac_c_->TransMultVector(1., *sol_c, 1., *curr_nabla_phi_plus_ATy_x_);
      curr_jac_d_->TransMultVector(1., *sol_d, 1., *curr_nabla_phi_plus_ATy_x_);
      SmartPtr<Vector> nabla_phi_plus_ATydelta_s = curr_nabla_phi_plus_ATy_s_->MakeNew();
      nabla_phi_plus_ATydelta_s->AddTwoVectors(1., *curr_nabla_phi_plus_ATy_s_, -1., *sol_d_scaled, 0.);
      Number nabla_phi_plus_ATydelta_norm = IpCq().CalcNormOfType(NORM_2, *nabla_phi_plus_ATydelta_x,
                                            *nabla_phi_plus_ATydelta_s);
      lhs = nabla_phi_plus_ATydelta_norm;
      if( !compute_normal )
      {
         rhs = tt_kappa2_ * curr_tt2_norm_;
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "TT2 testing ||gamma+A^T(y+delta)||(=%23.16e) <= kappa2*curr_tt2_norm_(=%23.16e) -->", lhs, rhs);
      }
      else
      {
         rhs = tt_kappa2_ * Min(curr_tt2_norm_, last_tt1_norm_);
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "TT2 testing ||gamma+A^T(y+delta)||(=%23.16e) <= kappa2*min(curr_tt2_norm_, last_tt1_norm_)(=%23.16e) -->",
                        lhs, rhs);
      }
      tt2 = Compare_le(lhs, rhs, BasVal);
      if( tt2 )
      {
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "satisfied\n");
      }
      else
      {
         Jnlst().Printf(J_MOREDETAILED, J_LINEAR_ALGEBRA,
                        "violated\n");
      }
   }

   // Check termination test 2
   if( tt2 )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Termination Test 2 satisfied.\n");
      return TEST_2_SATISFIED;
   }
   else if( try_tt2_ )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Termination Test 2 not satisfied.\n");
   }

   // Check if the Hessian should be modified
   if( tcc1 || tcc2a )  // || (!compute_normal && model_reduction)) {
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Hessian Modification not requested.\n");
   }
   else
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Hessian Modification requested.\n");
      return MODIFY_HESSIAN;
   }

   return retval;
}

void InexactPDTerminationTester::Clear()
{
   DBG_START_METH("InexactPDTerminationTester::Clear",
                  dbg_verbosity);

   curr_Av_c_ = NULL;
   curr_Av_d_ = NULL;
   curr_Wv_x_ = NULL;
   curr_Wv_s_ = NULL;
   curr_nabla_phi_plus_ATy_x_ = NULL;
   curr_nabla_phi_plus_ATy_s_ = NULL;
}

} // namespace Ipopt