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
// Copyright (C) 2005, 2008 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors:  Carl Laird, Andreas Waechter              IBM    2005-08-04

#include "IpPDPerturbationHandler.hpp"

#include <cmath>

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

PDPerturbationHandler::PDPerturbationHandler()
   : reset_last_(false),
     degen_iters_max_(3)
{
}

void PDPerturbationHandler::RegisterOptions(
   SmartPtr<RegisteredOptions> roptions
)
{
   roptions->AddLowerBoundedNumberOption(
      "max_hessian_perturbation",
      "Maximum value of regularization parameter for handling negative curvature.",
      0., true,
      1e20,
      "In order to guarantee that the search directions are indeed proper descent directions, "
      "Ipopt requires that the inertia of the (augmented) linear system for the step computation has "
      "the correct number of negative and positive eigenvalues. "
      "The idea is that this guides the algorithm away from maximizers and makes Ipopt more likely "
      "converge to first order optimal points that are minimizers. "
      "If the inertia is not correct, a multiple of the identity matrix is added to the Hessian of the Lagrangian in the augmented system. "
      "This parameter gives the maximum value of the regularization parameter. "
      "If a regularization of that size is not enough, the algorithm skips this iteration and goes to the restoration phase. "
      "This is delta_w^max in the implementation paper.");
   roptions->AddLowerBoundedNumberOption(
      "min_hessian_perturbation",
      "Smallest perturbation of the Hessian block.",
      0., false,
      1e-20,
      "The size of the perturbation of the Hessian block is never selected smaller than this value, "
      "unless no perturbation is necessary. "
      "This is delta_w^min in implementation paper.");
   roptions->AddLowerBoundedNumberOption(
      "perturb_inc_fact_first",
      "Increase factor for x-s perturbation for very first perturbation.",
      1., true,
      100.,
      "The factor by which the perturbation is increased when a trial value was not sufficient - "
      "this value is used for the computation of the very first perturbation and allows a different value for "
      "the first perturbation than that used for the remaining perturbations. "
      "This is bar_kappa_w^+ in the implementation paper.");
   roptions->AddLowerBoundedNumberOption(
      "perturb_inc_fact",
      "Increase factor for x-s perturbation.",
      1., true,
      8.,
      "The factor by which the perturbation is increased when a trial value was not sufficient - "
      "this value is used for the computation of all perturbations except for the first. "
      "This is kappa_w^+ in the implementation paper.");
   roptions->AddBoundedNumberOption(
      "perturb_dec_fact",
      "Decrease factor for x-s perturbation.",
      0., true,
      1., true,
      1. / 3.,
      "The factor by which the perturbation is decreased when a trial value is deduced from "
      "the size of the most recent successful perturbation. "
      "This is kappa_w^- in the implementation paper.");
   roptions->AddLowerBoundedNumberOption(
      "first_hessian_perturbation",
      "Size of first x-s perturbation tried.",
      0., true,
      1e-4,
      "The first value tried for the x-s perturbation in the inertia correction scheme. "
      "This is delta_0 in the implementation paper.");
   roptions->AddLowerBoundedNumberOption(
      "jacobian_regularization_value",
      "Size of the regularization for rank-deficient constraint Jacobians.",
      0., false,
      1e-8,
      "This is bar delta_c in the implementation paper.");
   roptions->AddLowerBoundedNumberOption(
      "jacobian_regularization_exponent",
      "Exponent for mu in the regularization for rank-deficient constraint Jacobians.",
      0., false,
      0.25,
      "This is kappa_c in the implementation paper.",
      true);
   roptions->AddBoolOption(
      "perturb_always_cd",
      "Active permanent perturbation of constraint linearization.",
      false,
      "Enabling this option leads to using the delta_c and delta_d perturbation for the computation of every search direction. "
      "Usually, it is only used when the iteration matrix is singular.",
      true);
}

bool PDPerturbationHandler::InitializeImpl(
   const OptionsList& options,
   const std::string& prefix
)
{
   options.GetNumericValue("max_hessian_perturbation", delta_xs_max_, prefix);
   options.GetNumericValue("min_hessian_perturbation", delta_xs_min_, prefix);
   options.GetNumericValue("perturb_inc_fact_first", delta_xs_first_inc_fact_, prefix);
   options.GetNumericValue("perturb_inc_fact", delta_xs_inc_fact_, prefix);
   options.GetNumericValue("perturb_dec_fact", delta_xs_dec_fact_, prefix);
   options.GetNumericValue("first_hessian_perturbation", delta_xs_init_, prefix);
   options.GetNumericValue("jacobian_regularization_value", delta_cd_val_, prefix);
   options.GetNumericValue("jacobian_regularization_exponent", delta_cd_exp_, prefix);
   options.GetBoolValue("perturb_always_cd", perturb_always_cd_, prefix);

   hess_degenerate_ = NOT_YET_DETERMINED;
   if( !perturb_always_cd_ )
   {
      jac_degenerate_ = NOT_YET_DETERMINED;
   }
   else
   {
      jac_degenerate_ = NOT_DEGENERATE;
   }
   degen_iters_ = 0;

   delta_x_curr_ = 0.;
   delta_s_curr_ = 0.;
   delta_c_curr_ = 0.;
   delta_d_curr_ = 0.;
   delta_x_last_ = 0.;
   delta_s_last_ = 0.;
   delta_c_last_ = 0.;
   delta_d_last_ = 0.;

   test_status_ = NO_TEST;

   return true;
}

bool PDPerturbationHandler::ConsiderNewSystem(
   Number& delta_x,
   Number& delta_s,
   Number& delta_c,
   Number& delta_d
)
{
   DBG_START_METH("PDPerturbationHandler::ConsiderNewSystem", dbg_verbosity);

   // Check if we can conclude that some components of the system are
   // structurally degenerate
   finalize_test();

   // Store the perturbation from the previous matrix
   if( reset_last_ )
   {
      delta_x_last_ = delta_x_curr_;
      delta_s_last_ = delta_s_curr_;
      delta_c_last_ = delta_c_curr_;
      delta_d_last_ = delta_d_curr_;
   }
   else
   {
      if( delta_x_curr_ > 0. )
      {
         delta_x_last_ = delta_x_curr_;
      }
      if( delta_s_curr_ > 0. )
      {
         delta_s_last_ = delta_s_curr_;
      }
      if( delta_c_curr_ > 0. )
      {
         delta_c_last_ = delta_c_curr_;
      }
      if( delta_d_curr_ > 0. )
      {
         delta_d_last_ = delta_d_curr_;
      }
   }

   DBG_ASSERT((hess_degenerate_ != NOT_YET_DETERMINED || jac_degenerate_ != DEGENERATE) &&
              (jac_degenerate_ != NOT_YET_DETERMINED || hess_degenerate_ != DEGENERATE));

   if( hess_degenerate_ == NOT_YET_DETERMINED || jac_degenerate_ == NOT_YET_DETERMINED )
   {
      if( !perturb_always_cd_ )
      {
         test_status_ = TEST_DELTA_C_EQ_0_DELTA_X_EQ_0;
      }
      else
      {
         test_status_ = TEST_DELTA_C_GT_0_DELTA_X_EQ_0;
      }
   }
   else
   {
      test_status_ = NO_TEST;
   }

   if( jac_degenerate_ == DEGENERATE )
   {
      delta_c = delta_c_curr_ = delta_cd();
      IpData().Append_info_string("l");
   }
   else if( perturb_always_cd_ )
   {
      delta_c = delta_c_curr_ = delta_cd();
   }
   else
   {
      delta_c = delta_c_curr_ = 0.;
   }
   delta_d = delta_d_curr_ = delta_c;

   if( hess_degenerate_ == DEGENERATE )
   {
      delta_x_curr_ = 0.;
      delta_s_curr_ = 0.;
      bool retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
      if( !retval )
      {
         return false;
      }
   }
   else
   {
      delta_x = 0.;
      delta_s = delta_x;
   }

   delta_x_curr_ = delta_x;
   delta_s_curr_ = delta_s;
   delta_c_curr_ = delta_c;
   delta_d_curr_ = delta_d;

   IpData().Set_info_regu_x(delta_x);

   get_deltas_for_wrong_inertia_called_ = false;

   return true;
}

bool PDPerturbationHandler::PerturbForSingularity(
   Number& delta_x,
   Number& delta_s,
   Number& delta_c,
   Number& delta_d
)
{
   DBG_START_METH("PDPerturbationHandler::PerturbForSingularity",
                  dbg_verbosity);

   bool retval;

   // Check for structural degeneracy
   if( hess_degenerate_ == NOT_YET_DETERMINED || jac_degenerate_ == NOT_YET_DETERMINED )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "Degeneracy test for hess_degenerate_ = %d and jac_degenerate_ = %d\n       test_status_ = %d\n",
                     hess_degenerate_, jac_degenerate_, test_status_);
      switch( test_status_ )
      {
         case TEST_DELTA_C_EQ_0_DELTA_X_EQ_0:
            DBG_ASSERT(delta_x_curr_ == 0. && delta_c_curr_ == 0.);
            // in this case we haven't tried anything for this matrix yet
            if( jac_degenerate_ == NOT_YET_DETERMINED )
            {
               delta_d_curr_ = delta_c_curr_ = delta_cd();
               test_status_ = TEST_DELTA_C_GT_0_DELTA_X_EQ_0;
            }
            else
            {
               DBG_ASSERT(hess_degenerate_ == NOT_YET_DETERMINED);
               retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
               if( !retval )
               {
                  return false;
               }
               DBG_ASSERT(delta_c == 0. && delta_d == 0.);
               test_status_ = TEST_DELTA_C_EQ_0_DELTA_X_GT_0;
            }
            break;
         case TEST_DELTA_C_GT_0_DELTA_X_EQ_0:
            DBG_ASSERT(delta_x_curr_ == 0. && delta_c_curr_ > 0.);
            DBG_ASSERT(jac_degenerate_ == NOT_YET_DETERMINED);
            if( !perturb_always_cd_ )
            {
               delta_d_curr_ = delta_c_curr_ = 0.;
               retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
               if( !retval )
               {
                  return false;
               }
               DBG_ASSERT(delta_c == 0. && delta_d == 0.);
               test_status_ = TEST_DELTA_C_EQ_0_DELTA_X_GT_0;
            }
            else
            {
               retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
               if( !retval )
               {
                  return false;
               }
               DBG_ASSERT(delta_c > 0. && delta_d > 0.);
               test_status_ = TEST_DELTA_C_GT_0_DELTA_X_GT_0;
            }
            break;
         case TEST_DELTA_C_EQ_0_DELTA_X_GT_0:
            DBG_ASSERT(delta_x_curr_ > 0. && delta_c_curr_ == 0.);
            delta_d_curr_ = delta_c_curr_ = delta_cd();
            retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
            if( !retval )
            {
               return false;
            }
            test_status_ = TEST_DELTA_C_GT_0_DELTA_X_GT_0;
            break;
         case TEST_DELTA_C_GT_0_DELTA_X_GT_0:
            retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
            if( !retval )
            {
               return false;
            }
            break;
         case NO_TEST:
            DBG_ASSERT(false && "we should not get here.");
      }
   }
   else
   {
      if( delta_c_curr_ > 0. )   // || get_deltas_for_wrong_inertia_called_) {
      {
         // If we already used a perturbation for the constraints, we do
         // the same thing as if we were encountering negative curvature
         retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
         if( !retval )
         {
            Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                           "Can't get_deltas_for_wrong_inertia for delta_x_curr_ = %e and delta_c_curr_ = %e\n", delta_x_curr_,
                           delta_c_curr_);
            return false;
         }
      }
      else
      {
         // Otherwise we now perturb the lower right corner
         delta_d_curr_ = delta_c_curr_ = delta_cd();

         // ToDo - also perturb Hessian?
         IpData().Append_info_string("L");
      }
   }

   delta_x = delta_x_curr_;
   delta_s = delta_s_curr_;
   delta_c = delta_c_curr_;
   delta_d = delta_d_curr_;

   IpData().Set_info_regu_x(delta_x);

   return true;
}

bool PDPerturbationHandler::get_deltas_for_wrong_inertia(
   Number& delta_x,
   Number& delta_s,
   Number& delta_c,
   Number& delta_d
)
{
   if( delta_x_curr_ == 0. )
   {
      if( delta_x_last_ == 0. )
      {
         delta_x_curr_ = delta_xs_init_;
      }
      else
      {
         delta_x_curr_ = Max(delta_xs_min_, delta_x_last_ * delta_xs_dec_fact_);
      }
   }
   else
   {
      if( delta_x_last_ == 0. || 1e5 * delta_x_last_ < delta_x_curr_ )
      {
         delta_x_curr_ = delta_xs_first_inc_fact_ * delta_x_curr_;
      }
      else
      {
         delta_x_curr_ = delta_xs_inc_fact_ * delta_x_curr_;
      }
   }
   if( delta_x_curr_ > delta_xs_max_ )
   {
      Jnlst().Printf(J_DETAILED, J_LINEAR_ALGEBRA,
                     "delta_x perturbation is becoming too large: %e\n", delta_x_curr_);
      delta_x_last_ = 0.;
      delta_s_last_ = 0.;
      IpData().Append_info_string("dx");
      return false;
   }

   delta_s_curr_ = delta_x_curr_;

   delta_x = delta_x_curr_;
   delta_s = delta_s_curr_;
   delta_c = delta_c_curr_;
   delta_d = delta_d_curr_;

   IpData().Set_info_regu_x(delta_x);

   get_deltas_for_wrong_inertia_called_ = true;

   return true;
}

bool PDPerturbationHandler::PerturbForWrongInertia(
   Number& delta_x,
   Number& delta_s,
   Number& delta_c,
   Number& delta_d
)
{
   DBG_START_METH("PDPerturbationHandler::PerturbForWrongInertia",
                  dbg_verbosity);

   // Check if we can conclude that components of the system are
   // structurally degenerate (we only get here if the most recent
   // perturbation for a test did not result in a singular system)
   finalize_test();

   bool retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
   if( !retval && delta_c == 0. )
   {
      DBG_ASSERT(delta_d == 0.);
      delta_c_curr_ = delta_cd();
      delta_d_curr_ = delta_c_curr_;
      delta_x_curr_ = 0.;
      delta_s_curr_ = 0.;
      test_status_ = NO_TEST;
      if( hess_degenerate_ == DEGENERATE )
      {
         hess_degenerate_ = NOT_YET_DETERMINED;
      }
      retval = get_deltas_for_wrong_inertia(delta_x, delta_s, delta_c, delta_d);
   }
   return retval;
}

void PDPerturbationHandler::CurrentPerturbation(
   Number& delta_x,
   Number& delta_s,
   Number& delta_c,
   Number& delta_d
)
{
   delta_x = delta_x_curr_;
   delta_s = delta_s_curr_;
   delta_c = delta_c_curr_;
   delta_d = delta_d_curr_;
}

Number PDPerturbationHandler::delta_cd()
{
   return delta_cd_val_ * std::pow(IpData().curr_mu(), delta_cd_exp_);
}

void PDPerturbationHandler::finalize_test()
{
   switch( test_status_ )
   {
      case NO_TEST:
         return;
      case TEST_DELTA_C_EQ_0_DELTA_X_EQ_0:
         if( hess_degenerate_ == NOT_YET_DETERMINED && jac_degenerate_ == NOT_YET_DETERMINED )
         {
            hess_degenerate_ = NOT_DEGENERATE;
            jac_degenerate_ = NOT_DEGENERATE;
            IpData().Append_info_string("Nhj ");
         }
         else if( hess_degenerate_ == NOT_YET_DETERMINED )
         {
            hess_degenerate_ = NOT_DEGENERATE;
            IpData().Append_info_string("Nh ");
         }
         else if( jac_degenerate_ == NOT_YET_DETERMINED )
         {
            jac_degenerate_ = NOT_DEGENERATE;
            IpData().Append_info_string("Nj ");
         }
         break;
      case TEST_DELTA_C_GT_0_DELTA_X_EQ_0:
         if( hess_degenerate_ == NOT_YET_DETERMINED )
         {
            hess_degenerate_ = NOT_DEGENERATE;
            IpData().Append_info_string("Nh ");
         }
         if( jac_degenerate_ == NOT_YET_DETERMINED )
         {
            degen_iters_++;
            if( degen_iters_ >= degen_iters_max_ )
            {
               jac_degenerate_ = DEGENERATE;
               IpData().Append_info_string("Dj ");
            }
            IpData().Append_info_string("L");
         }
         break;
      case TEST_DELTA_C_EQ_0_DELTA_X_GT_0:
         if( jac_degenerate_ == NOT_YET_DETERMINED )
         {
            jac_degenerate_ = NOT_DEGENERATE;
            IpData().Append_info_string("Nj ");
         }
         if( hess_degenerate_ == NOT_YET_DETERMINED )
         {
            degen_iters_++;
            if( degen_iters_ >= degen_iters_max_ )
            {
               hess_degenerate_ = DEGENERATE;
               IpData().Append_info_string("Dh ");
            }
         }
         break;
      case TEST_DELTA_C_GT_0_DELTA_X_GT_0:
         degen_iters_++;
         if( degen_iters_ >= degen_iters_max_ )
         {
            hess_degenerate_ = DEGENERATE;
            jac_degenerate_ = DEGENERATE;
            IpData().Append_info_string("Dhj ");
         }
         IpData().Append_info_string("L");
         break;
   }
}

} // namespace Ipopt