File: emptynlp.cpp

package info (click to toggle)
coinor-ipopt 3.14.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,796 kB
  • sloc: cpp: 97,169; sh: 4,802; ansic: 2,537; java: 1,289; makefile: 821; fortran: 224; xml: 210
file content (757 lines) | stat: -rw-r--r-- 16,489 bytes parent folder | download | duplicates (2)
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
// Copyright (C) 2021 COIN-OR Foundation
// All Rights Reserved.
// This code is published under the Eclipse Public License.

// get active asserts also if NDEBUG is defined
#ifdef NDEBUG
#undef NDEBUG
#endif

#include "IpIpoptApplication.hpp"
#include "IpSolveStatistics.hpp"
#include "IpTNLP.hpp"

#include <iostream>
#include <cassert>
#include <cmath>

using namespace Ipopt;

/** Empty NLP to test with Ipopt
 *
 * min sum_i x_i
 * s.t. x = 0           if not infeasbounds
 *      1 <= x <= 0     if infeasbounds
 *      sum_i x_i >= 0   if not infeascons and cons
 *      sum_i x_i >= 1   if infeascons
 */
class EmptyNLP: public TNLP
{
private:
   int nvars;
   bool cons;
   bool infeascons;
   bool infeasbounds;

public:
   /** constructor */
   EmptyNLP(
      int  nvars_         = 0,
      bool cons_          = true,
      bool infeascons_    = false,
      bool infeasbounds_  = false
   )
      : nvars(nvars_),
        cons(cons_),
        infeascons(infeascons_),
        infeasbounds(infeasbounds_)
   {
      assert(!infeasbounds || nvars > 0);
      assert(!infeascons || cons);
   }

   /** destructor */
   ~EmptyNLP() { }

   /** Method to return some info about the nlp */
   bool get_nlp_info(
      Index&          n,
      Index&          m,
      Index&          nnz_jac_g,
      Index&          nnz_h_lag,
      IndexStyleEnum& index_style
   )
   {
      n = nvars;
      m = cons ? 1 : 0;
      nnz_jac_g = cons ? n : 0;
      nnz_h_lag = 0;
      index_style = C_STYLE;

      return true;
   }

   /** Method to return the bounds for my problem */
   bool get_bounds_info(
      Index   n,
      Number* x_l,
      Number* x_u,
      Index   m,
      Number* g_l,
      Number* g_u
   )
   {
      for( Index i = 0; i < n; ++i )
      {
         x_l[i] = infeasbounds ? 1.0 : 0.0;
         x_u[i] = 0.0;
      }

      assert(m == (cons ? 1 : 0));
      if( cons )
      {
         g_l[0] = infeascons ? 1.0 : 0.0;
         g_u[0] = 1e300;
      }

      return true;
   }

   /** Method to return the starting point for the algorithm */
   bool get_starting_point(
      Index   n,
      bool    init_x,
      Number* x,
      bool    init_z,
      Number*,
      Number*,
      Index,
      bool    init_lambda,
      Number*
   )
   {
      if( init_x )
         for( Index i = 0; i < n; ++i )
         {
            x[i] = 10.0;
         }

      assert(!init_z);
      assert(!init_lambda);

      return true;
   }

   /** Method to return the objective value */
   bool eval_f(
      Index         n,
      const Number* x,
      bool,
      Number&       obj_value
   )
   {
      obj_value = 0.0;
      for( Index i = 0; i < n; ++i )
      {
         obj_value += x[i];
      }

      return true;
   }

   /** Method to return the gradient of the objective */
   bool eval_grad_f(
      Index         n,
      const Number*,
      bool,
      Number*       grad_f
   )
   {
      for( Index i = 0; i < n; ++i )
      {
         grad_f[i] = 1.0;
      }

      return true;
   }

   /** Method to return the constraint residuals */
   bool eval_g(
      Index         n,
      const Number* x,
      bool,
      Index         m,
      Number*       g
   )
   {
      if( !cons )
      {
         return true;
      }

      assert(m == 1);

      g[0] = 0.0;
      for( Index i = 0; i < n; ++i )
      {
         g[0] += x[i];
      }

      return true;
   }

   /** Method to return:
    *   1) The structure of the Jacobian (if "values" is NULL)
    *   2) The values of the Jacobian (if "values" is not NULL)
    */
   bool eval_jac_g(
      Index         n,
      const Number*,
      bool,
      Index,
      Index         nele_jac,
      Index*        iRow,
      Index*        jCol,
      Number*       values
   )
   {
      assert(nele_jac == (cons ? n : 0));

      if( !cons )
      {
         return true;
      }

      assert((iRow != NULL) == (jCol != NULL));
      assert((iRow != NULL) == (values == NULL));

      if( iRow != NULL )
         for( Index i = 0; i < n; ++i )
         {
            iRow[i] = 0;
            jCol[i] = i;
         }
      else
         for( Index i = 0; i < n; ++i )
         {
            values[i] = 1.0;
         }

      return true;
   }

   /** Method to return:
    *   1) The structure of the Hessian of the Lagrangian (if "values" is NULL)
    *   2) The values of the Hessian of the Lagrangian (if "values" is not NULL)
    */
   bool eval_h(
      Index,
      const Number*,
      bool,
      Number,
      Index,
      const Number*,
      bool,
      Index,
      Index*,
      Index*,
      Number*
   )
   {
      return true;
   }

   /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
   void finalize_solution(
      SolverReturn               status,
      Index                      n,
      const Number*              x,
      const Number*              z_L,
      const Number*              z_U,
      Index                      m,
      const Number*              g,
      const Number*              lambda,
      Number                     obj_value,
      const IpoptData*,
      IpoptCalculatedQuantities*
   )
   {
      std::cout << "Finalize called" << std::endl;
      std::cout << "x =";
      for( Index i = 0; i < n; ++i )
      {
         std::cout << ' ' << x[i];
      }
      std::cout << std::endl;
      std::cout << "z_L =";
      for( Index i = 0; i < n; ++i )
      {
         std::cout << ' ' << z_L[i];
      }
      std::cout << std::endl;
      std::cout << "z_U =";
      for( Index i = 0; i < n; ++i )
      {
         std::cout << ' ' << z_U[i];
      }
      std::cout << std::endl;
      std::cout << "lambda =";
      for( Index i = 0; i < m; ++i )
      {
         std::cout << ' ' << lambda[i];
      }
      std::cout << std::endl;

      if( status != (infeascons ? LOCAL_INFEASIBILITY : SUCCESS) )
      {
         std::cout << "*** Unexpected solver status " << status << " for" << (infeascons ? " infeasible" : "") << " NLP" << std::endl;
      }

      if( status == SUCCESS )
      {
         Number tol = 1e-6;
         assert(std::abs(obj_value) < tol);

         for( Index i = 0; i < n; ++i )
         {
            assert(std::abs(x[i]) < tol);
         }

         for( Index i = 0; i < m; ++i )
         {
            assert(std::abs(g[i]) < tol);
         }
      }
   }
};

bool runEmpty(
   int  nvars,
   bool cons,
   bool infeascons,
   bool infeasbounds
)
{
   std::cout << std::endl << "*** Solve for " << nvars << " variables, "
             << (cons ? 1 : 0) << ' '
             << (infeascons ? "infeasible" : "feasible") << " constraint, "
             << (infeasbounds ? "infeasible" : "feasible") << " bounds"
             << std::endl;

   // Create an instance of your nlp...
   SmartPtr<TNLP> nlp = new EmptyNLP(nvars, cons, infeascons, infeasbounds);

   // Create an instance of the IpoptApplication
   SmartPtr<IpoptApplication> app = new IpoptApplication();

   // Initialize the IpoptApplication and process the options
   ApplicationReturnStatus status;
   status = app->Initialize();
   if( status != Solve_Succeeded )
   {
      std::cout << std::endl << std::endl << "*** Error during initialization!" << std::endl;
      return false;
   }

   status = app->OptimizeTNLP(nlp);

   assert((status == Solve_Succeeded) == (!infeasbounds && !infeascons));
   assert((status == Infeasible_Problem_Detected) == infeascons);
   assert((status == Invalid_Problem_Definition) == infeasbounds);

   if( status > Not_Enough_Degrees_Of_Freedom )
   {
      assert(IsValid(app->Statistics()));

      // Retrieve some statistics about the solve
      Index iter_count = app->Statistics()->IterationCount();
      std::cout << std::endl << std::endl << "The problem solved in " << iter_count << " iterations!" << std::endl;

      Number final_obj = app->Statistics()->FinalObjective();
      std::cout << std::endl << std::endl << "The final value of the objective function is " << final_obj << '.'
                << std::endl;

      assert(app->Statistics()->IterationCount() == 0);
   }

   return true;
}

/** Mostly empty NLP to test reoptimization with Ipopt
 *
 * min sum_i x_i
 * s.t. 0 <= x
 *      x_i <= 0   for i <= nfixed
 *      sum_i x_i = rhs
 *      x_1 = 0    (as constraint)
 */
class ReOptNLP: public TNLP
{
private:
   Number rhs;
public:
   int nvars;
   int nfixed;

   /** constructor */
   ReOptNLP(
      Number rhs_ = 0.0
   )
      : rhs(rhs_),
        nvars(2),
        nfixed(0)
   { }

   /** Method to return some info about the nlp */
   bool get_nlp_info(
      Index&          n,
      Index&          m,
      Index&          nnz_jac_g,
      Index&          nnz_h_lag,
      IndexStyleEnum& index_style
   )
   {
      n = nvars;
      m = 2;
      nnz_jac_g = n + 1;
      nnz_h_lag = 0;
      index_style = C_STYLE;

      return true;
   }

   /** Method to return the bounds for my problem */
   bool get_bounds_info(
      Index   n,
      Number* x_l,
      Number* x_u,
      Index   m,
      Number* g_l,
      Number* g_u
   )
   {
      for( Index i = 0; i < n; ++i )
      {
         x_l[i] = 0.0;
         x_u[i] = i < nfixed ? 0.0 : 1.0;
      }

      assert(m == 2);
      g_l[0] = rhs;
      g_u[0] = rhs;

      g_l[1] = 0.0;
      g_u[1] = 0.0;

      return true;
   }

   /** Method to return the starting point for the algorithm */
   bool get_starting_point(
      Index   n,
      bool    init_x,
      Number* x,
      bool    init_z,
      Number*,
      Number*,
      Index,
      bool    init_lambda,
      Number*
   )
   {
      if( init_x )
         for( Index i = 0; i < n; ++i )
         {
            x[i] = 10.0;
         }

      assert(!init_z);
      assert(!init_lambda);

      return true;
   }

   /** Method to return the objective value */
   bool eval_f(
      Index         n,
      const Number* x,
      bool,
      Number&       obj_value
   )
   {
      obj_value = 0.0;
      for( Index i = 0; i < n; ++i )
      {
         obj_value += x[i];
      }

      return true;
   }

   /** Method to return the gradient of the objective */
   bool eval_grad_f(
      Index         n,
      const Number*,
      bool,
      Number*       grad_f
   )
   {
      for( Index i = 0; i < n; ++i )
      {
         grad_f[i] = 1.0;
      }

      return true;
   }

   /** Method to return the constraint residuals */
   bool eval_g(
      Index         n,
      const Number* x,
      bool,
      Index         m,
      Number*       g
   )
   {
      assert(m == 2);

      g[0] = 0.0;
      for( Index i = 0; i < n; ++i )
      {
         g[0] += x[i];
      }

      g[1] = x[0];

      return true;
   }

   /** Method to return:
    *   1) The structure of the Jacobian (if "values" is NULL)
    *   2) The values of the Jacobian (if "values" is not NULL)
    */
   bool eval_jac_g(
      Index         n,
      const Number*,
      bool,
      Index,
      Index         nele_jac,
      Index*        iRow,
      Index*        jCol,
      Number*       values
   )
   {
      assert((iRow != NULL) == (jCol != NULL));
      assert((iRow != NULL) == (values == NULL));
      assert(nele_jac == n + 1);

      if( iRow != NULL )
      {
         for( Index i = 0; i < n; ++i )
         {
            iRow[i] = 0;
            jCol[i] = i;
         }
         iRow[n] = 1;
         jCol[n] = 0;
      }
      else
         for( Index i = 0; i < n + 1; ++i )
         {
            values[i] = 1.0;
         }

      return true;
   }

   /** Method to return:
    *   1) The structure of the Hessian of the Lagrangian (if "values" is NULL)
    *   2) The values of the Hessian of the Lagrangian (if "values" is not NULL)
    */
   bool eval_h(
      Index,
      const Number*,
      bool,
      Number,
      Index,
      const Number*,
      bool,
      Index,
      Index*,
      Index*,
      Number*
   )
   {
      return true;
   }

   /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
   void finalize_solution(
      SolverReturn               status,
      Index                      n,
      const Number*              x,
      const Number*              z_L,
      const Number*              z_U,
      Index                      m,
      const Number*              g,
      const Number*              lambda,
      Number                     obj_value,
      const IpoptData*,
      IpoptCalculatedQuantities*
   )
   {
      std::cout << "Finalize called" << std::endl;
      std::cout << "x =";
      for( Index i = 0; i < n; ++i )
      {
         std::cout << ' ' << x[i];
      }
      std::cout << std::endl;
      std::cout << "z_L =";
      for( Index i = 0; i < n; ++i )
      {
         std::cout << ' ' << z_L[i];
      }
      std::cout << std::endl;
      std::cout << "z_U =";
      for( Index i = 0; i < n; ++i )
      {
         std::cout << ' ' << z_U[i];
      }
      std::cout << std::endl;
      std::cout << "lambda =";
      for( Index i = 0; i < m; ++i )
      {
         std::cout << ' ' << lambda[i];
      }
      std::cout << std::endl;

      if( status == SUCCESS )
      {
         Number tol = 1e-5;
         assert(std::abs(obj_value - rhs) < tol);

         if( rhs == 0.0 )
         {
            for( Index i = 0; i < n; ++i )
            {
               assert(std::abs(x[i]) < tol);
            }
         }

         assert(std::abs(g[0] - rhs) < tol);
         assert(std::abs(g[1]) < tol);
      }
   }
};

bool runReOpt(
   int nvars1,
   int nvars2,
   int nfixed,
   Number rhs
)
{
   std::cout << std::endl << "*** Solve with " << nvars1 << " variables and rhs=" << rhs << "." << std::endl;

   SmartPtr<ReOptNLP> nlp = new ReOptNLP(rhs);
   nlp->nvars = nvars1;
   SmartPtr<IpoptApplication> app = new IpoptApplication();

   ApplicationReturnStatus status;
   status = app->Initialize();
   if( status != Solve_Succeeded )
   {
      std::cout << std::endl << std::endl << "*** Error during initialization!" << std::endl;
      return false;
   }

   status = app->OptimizeTNLP(GetRawPtr(nlp));

   if( nvars1 < 2 )
   {
      assert(status == Not_Enough_Degrees_Of_Freedom);
   }
   else
   {
      assert((status == Solve_Succeeded) == (rhs <= 1));
      assert((status == Infeasible_Problem_Detected) == (rhs > 1));
   }

   std::cout << std::endl << "*** Resolve with " << nvars2 << " variables, " << nfixed << " variables fixed." << std::endl;

   nlp->nvars = nvars2;
   nlp->nfixed = nfixed;
   status = app->ReOptimizeTNLP(GetRawPtr(nlp));

   if( nvars2 < 2 && nfixed < nvars2 )
   {
      assert(status == Not_Enough_Degrees_Of_Freedom);
   }
   else
   {
      assert((status == Solve_Succeeded) == (rhs <= 1 && (rhs == 0.0 || nfixed < nvars2)));
      assert((status == Infeasible_Problem_Detected) == (rhs > 1 || (rhs != 0.0 && nfixed == nvars2)));
   }

   return true;
}

int main(
   int,
   char**
)
{
   if( !runEmpty(0, false, false, false) )
   {
      return EXIT_FAILURE;
   }

   if( !runEmpty(0, true, false, false) )
   {
      return EXIT_FAILURE;
   }

   if( !runEmpty(5, true, false, false) )
   {
      return EXIT_FAILURE;
   }

   if( !runEmpty(0, true, true, false) )
   {
      return EXIT_FAILURE;
   }

   if( !runEmpty(5, true, true, false) )
   {
      return EXIT_FAILURE;
   }

   if( !runEmpty(5, true, false, true) )
   {
      return EXIT_FAILURE;
   }

   // 2 variables, fix them in resolve, 2 constraints
   if( !runReOpt(2, 2, 2, 0.0) )
   {
      return EXIT_FAILURE;
   }

   if( !runReOpt(2, 2, 2, 1.0) )
   {
      return EXIT_FAILURE;
   }

   // 1 variable, do not fix in resolve, 2 constraints
   // should give a too-few-degree-of-freedom in both solves
   if( !runReOpt(1, 1, 0, 0.0) )
   {
      return EXIT_FAILURE;
   }

   // 2 variables, then 1 variable, do not fix in resolve
   // should give a too-few-degree-of-freedom for second solve  (this case had a bug)
   if( !runReOpt(2, 1, 0, 0.0) )
   {
      return EXIT_FAILURE;
   }

   // 2 variables, then 1 variable, but fixed in resolve
   // should not give a too-few-degree-of-freedom, but feasible
   if( !runReOpt(2, 1, 1, 0.0) )
   {
      return EXIT_FAILURE;
   }

   // 2 variables, then 1 variable, but fixed in resolve
   // should not give a too-few-degree-of-freedom, but infeasible
   if( !runReOpt(2, 1, 1, 1.0) )
   {
      return EXIT_FAILURE;
   }

   std::cout << std::endl << "*** All tests passed" << std::endl;

   return EXIT_SUCCESS;
}