IPOPTSolver.cc 15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//=============================================================================
//
//  CLASS IPOPTSolver - IMPLEMENTATION
//
//=============================================================================

//== INCLUDES =================================================================

//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
#if COMISO_IPOPT_AVAILABLE
//=============================================================================


#include "IPOPTSolver.hh"
16

17
18
19
20
21
22
23
24
//== NAMESPACES ===============================================================

namespace COMISO {

//== IMPLEMENTATION ========================================================== 


// Constructor
25
26
IPOPTSolver::
IPOPTSolver()
27
{
28
29
  // Create an instance of the IpoptApplication
  app_ = IpoptApplicationFactory();
30
31

  // Switch to HSL if available in Comiso
32
33
34
  #if COMISO_HSL_AVAILABLE
    app_->Options()->SetStringValue("linear_solver", "ma57");
  #endif
35
36
37

  // Restrict memory to be able to run larger problems on windows
  // with the default mumps solver
38
39
40
  #ifdef WIN32
    app_->Options()->SetIntegerValue("mumps_mem_percent", 5);
  #endif
41
42

  // set default parameters
43
  app_->Options()->SetIntegerValue("max_iter", 100);
44
45
46
47
  //  app->Options()->SetStringValue("derivative_test", "second-order");
  //  app->Options()->SetIntegerValue("print_level", 0);
  //  app->Options()->SetStringValue("expect_infeasible_problem", "yes");

48
  print_level_ = 5;
49
50
}

51

52
//-----------------------------------------------------------------------------
53

54
55


56
57
58
int
IPOPTSolver::
solve(NProblemInterface* _problem, const std::vector<NConstraintInterface*>& _constraints)
59
{
60
61
62
63
64
65
66
67
68
69
70
71
72
  //----------------------------------------------------------------------------
  // 0. Check whether hessian_approximation is active
  //----------------------------------------------------------------------------
  bool hessian_approximation = false;
  std::string ha, p;
  app().Options()->GetStringValue("hessian_approximation", ha, p);
  if(ha != "exact")
  {
    if(print_level_>=2)
      std::cerr << "Hessian approximation is enabled" << std::endl;
    hessian_approximation = true;
  }

73
74
75
  //----------------------------------------------------------------------------
  // 1. Create an instance of IPOPT NLP
  //----------------------------------------------------------------------------
76
  Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, _constraints, hessian_approximation);
77
78
79
80
81
82
  NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));

  //----------------------------------------------------------------------------
  // 2. exploit special characteristics of problem
  //----------------------------------------------------------------------------

83
84
  if(print_level_>=2)
    std::cerr << "exploit detected special properties: ";
85
86
  if(np2->hessian_constant())
  {
87
88
89
    if(print_level_>=2)
      std::cerr << "*constant hessian* ";
    app().Options()->SetStringValue("hessian_constant", "yes");
90
91
92
93
  }

  if(np2->jac_c_constant())
  {
94
95
96
    if(print_level_>=2)
      std::cerr << "*constant jacobian of equality constraints* ";
    app().Options()->SetStringValue("jac_c_constant", "yes");
97
98
99
100
  }

  if(np2->jac_d_constant())
  {
101
102
103
    if(print_level_>=2)
      std::cerr << "*constant jacobian of in-equality constraints*";
    app().Options()->SetStringValue("jac_d_constant", "yes");
104
  }
105
106
107

  if(print_level_>=2)
    std::cerr << std::endl;
108
109
110
111
112
113

  //----------------------------------------------------------------------------
  // 3. solve problem
  //----------------------------------------------------------------------------

  // Initialize the IpoptApplication and process the options
114
115
116
117
118
119
120
  Ipopt::ApplicationReturnStatus status;
  status = app_->Initialize();
  if (status != Ipopt::Solve_Succeeded)
  {
    if(print_level_>=2)
      printf("\n\n*** Error IPOPT during initialization!\n");
  }
121

122
  status = app_->OptimizeTNLP( np);
123
124
125
126

  //----------------------------------------------------------------------------
  // 4. output statistics
  //----------------------------------------------------------------------------
127
128
129
130
131
132
133
134
135
136
137
138
  if(print_level_>=2)
    if (status == Ipopt::Solve_Succeeded || status == Ipopt::Solved_To_Acceptable_Level)
    {
      // Retrieve some statistics about the solve
      Ipopt::Index iter_count = app_->Statistics()->IterationCount();
      printf("\n\n*** IPOPT: The problem solved in %d iterations!\n", iter_count);

      Ipopt::Number final_obj = app_->Statistics()->FinalObjective();
      printf("\n\n*** IPOPT: The final value of the objective function is %e.\n", final_obj);
    }

  return status;
139
140
141
142
143
144
}


//-----------------------------------------------------------------------------


145
146
147
148

int
IPOPTSolver::
solve(NProblemInterface*                        _problem,
149
150
151
152
153
154
      const std::vector<NConstraintInterface*>& _constraints,
      const std::vector<NConstraintInterface*>& _lazy_constraints,
      const double                              _almost_infeasible,
      const int                                 _max_passes        )
{
  //----------------------------------------------------------------------------
155
156
157
158
159
160
161
162
163
164
165
166
  // 0. Check whether hessian_approximation is active
  //----------------------------------------------------------------------------
  bool hessian_approximation = false;
  std::string ha, p;
  app().Options()->GetStringValue("hessian_approximation", ha, p);
  if(ha != "exact")
  {
    if(print_level_>=2)
      std::cerr << "Hessian approximation is enabled" << std::endl;
    hessian_approximation = true;
  }

167
  //----------------------------------------------------------------------------
168
169
170
171
  // 0. Initialize IPOPT Applicaiton
  //----------------------------------------------------------------------------

  StopWatch sw; sw.start();
172
173
174

  // Initialize the IpoptApplication and process the options
  Ipopt::ApplicationReturnStatus status;
175
  status = app_->Initialize();
176
  if (status != Ipopt::Solve_Succeeded)
177
178
179
  {
    printf("\n\n*** Error IPOPT during initialization!\n");
  }
180
181
182
183
184
185
186
187
188
189
190
191
192
193

  bool feasible_point_found = false;
  int  cur_pass = 0;
  double acceptable_tolerance = 0.01; // hack: read out from ipopt!!!
  // copy default constraints
  std::vector<NConstraintInterface*> constraints = _constraints;
  std::vector<bool> lazy_added(_lazy_constraints.size(),false);

  // cache statistics of all iterations
  std::vector<int> n_inf;
  std::vector<int> n_almost_inf;

  while(!feasible_point_found && cur_pass <(_max_passes-1))
  {
194

195
196
197
198
    ++cur_pass;
    //----------------------------------------------------------------------------
    // 1. Create an instance of current IPOPT NLP
    //----------------------------------------------------------------------------
199
    Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, constraints, hessian_approximation);
200
201
202
203
204
205
206
207
    NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));
    // enable caching of solution
    np2->store_solution() = true;

    //----------------------------------------------------------------------------
    // 2. exploit special characteristics of problem
    //----------------------------------------------------------------------------

208
    if(print_level_>=2) std::cerr << "detected special properties which will be exploit: ";
209
210
    if(np2->hessian_constant())
    {
211
212
      if(print_level_>=2) std::cerr << "*constant hessian* ";
      app().Options()->SetStringValue("hessian_constant", "yes");
213
214
215
216
    }

    if(np2->jac_c_constant())
    {
217
218
      if(print_level_>=2) std::cerr << "*constant jacobian of equality constraints* ";
      app().Options()->SetStringValue("jac_c_constant", "yes");
219
220
221
222
    }

    if(np2->jac_d_constant())
    {
223
224
      if(print_level_>=2) std::cerr << "*constant jacobian of in-equality constraints*";
      app().Options()->SetStringValue("jac_d_constant", "yes");
225
    }
226
    if(print_level_>=2) std::cerr << std::endl;
227
228
229
230

    //----------------------------------------------------------------------------
    // 3. solve problem
    //----------------------------------------------------------------------------
231
    status = app_->OptimizeTNLP( np);
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

    // check lazy constraints
    n_inf.push_back(0);
    n_almost_inf.push_back(0);
    feasible_point_found = true;
    for(unsigned int i=0; i<_lazy_constraints.size(); ++i)
      if(!lazy_added[i])
      {
        NConstraintInterface* lc = _lazy_constraints[i];
        double v = lc->eval_constraint(&(np2->solution()[0]));
        bool inf        = false;
        bool almost_inf = false;

        if(lc->constraint_type() == NConstraintInterface::NC_EQUAL)
        {
          v = std::abs(v);
          if(v>acceptable_tolerance)
            inf = true;
          else
            if(v>_almost_infeasible)
              almost_inf = true;
        }
        else
          if(lc->constraint_type() == NConstraintInterface::NC_GREATER_EQUAL)
          {
            if(v<-acceptable_tolerance)
              inf = true;
            else
              if(v<_almost_infeasible)
                almost_inf = true;
          }
          else
            if(lc->constraint_type() == NConstraintInterface::NC_LESS_EQUAL)
            {
              if(v>acceptable_tolerance)
                inf = true;
              else
                if(v>-_almost_infeasible)
                  almost_inf = true;
            }

        // infeasible?
        if(inf)
        {
          constraints.push_back(lc);
          lazy_added[i] = true;
          feasible_point_found = false;
          ++n_inf.back();
        }

        // almost violated or violated? -> add to constraints
        if(almost_inf)
        {
          constraints.push_back(lc);
          lazy_added[i] = true;
          ++n_almost_inf.back();
        }
      }
  }

  // no termination after max number of passes?
  if(!feasible_point_found)
  {
    ++cur_pass;

297
    std::cerr << "*************** could not find feasible point after " << _max_passes-1 << " -> solving with all lazy constraints..." << std::endl;
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    for(unsigned int i=0; i<_lazy_constraints.size(); ++i)
      if(!lazy_added[i])
        constraints.push_back(_lazy_constraints[i]);

    //----------------------------------------------------------------------------
    // 1. Create an instance of current IPOPT NLP
    //----------------------------------------------------------------------------
    Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemIPOPT(_problem, constraints);
    NProblemIPOPT* np2 = dynamic_cast<NProblemIPOPT*> (Ipopt::GetRawPtr(np));
    // enable caching of solution
    np2->store_solution() = true;

    //----------------------------------------------------------------------------
    // 2. exploit special characteristics of problem
    //----------------------------------------------------------------------------

314
    if(print_level_>=2) std::cerr << "exploit detected special properties: ";
315
316
    if(np2->hessian_constant())
    {
317
318
      if(print_level_>=2) std::cerr << "*constant hessian* ";
      app().Options()->SetStringValue("hessian_constant", "yes");
319
320
321
322
    }

    if(np2->jac_c_constant())
    {
323
324
      if(print_level_>=2) std::cerr << "*constant jacobian of equality constraints* ";
      app().Options()->SetStringValue("jac_c_constant", "yes");
325
326
327
328
    }

    if(np2->jac_d_constant())
    {
329
330
      if(print_level_>=2) std::cerr << "*constant jacobian of in-equality constraints*";
      app().Options()->SetStringValue("jac_d_constant", "yes");
331
    }
332
    if(print_level_>=2) std::cerr << std::endl;
333
334
335
336

    //----------------------------------------------------------------------------
    // 3. solve problem
    //----------------------------------------------------------------------------
337
    status = app_->OptimizeTNLP( np);
338
339
  }

340
341
  const double overall_time = sw.stop()/1000.0;

342
343
344
  //----------------------------------------------------------------------------
  // 4. output statistics
  //----------------------------------------------------------------------------
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  if (status == Ipopt::Solve_Succeeded || status == Ipopt::Solved_To_Acceptable_Level)
  {
    if(print_level_>=2)
    {
      // Retrieve some statistics about the solve
      Ipopt::Index iter_count = app_->Statistics()->IterationCount();
      printf("\n\n*** IPOPT: The problem solved in %d iterations!\n", iter_count);

      Ipopt::Number final_obj = app_->Statistics()->FinalObjective();
      printf("\n\n*** IPOPT: The final value of the objective function is %e.\n", final_obj);
    }
  }

  if(print_level_>=2)
  {
    std::cerr <<"############# IPOPT with lazy constraints statistics ###############" << std::endl;
    std::cerr << "overall time: " << overall_time << "s" << std::endl;
    std::cerr << "#passes     : " << cur_pass << "( of " << _max_passes << ")" << std::endl;
    for(unsigned int i=0; i<n_inf.size(); ++i)
      std::cerr << "pass " << i << " induced " << n_inf[i] << " infeasible and " << n_almost_inf[i] << " almost infeasible" << std::endl;
  }

  return status;
368
369
370
371
372
373
}


//-----------------------------------------------------------------------------


374
375
376
int
IPOPTSolver::
solve(NProblemInterface*    _problem)
377
378
{
  std::vector<NConstraintInterface*> constraints;
379
  return this->solve(_problem, constraints);
380
381
382
383
384
385
}


//-----------------------------------------------------------------------------


386
387
388
int
IPOPTSolver::
solve(NProblemGmmInterface* _problem, std::vector<NConstraintInterface*>& _constraints)
389
{
390
  std::cerr << "****** Warning: NProblemGmmInterface is deprecated!!! -> use NProblemInterface *******\n";
391
392
393
394
395
396
397
398
399
400
401

  //----------------------------------------------------------------------------
  // 1. Create an instance of IPOPT NLP
  //----------------------------------------------------------------------------
  Ipopt::SmartPtr<Ipopt::TNLP> np = new NProblemGmmIPOPT(_problem, _constraints);

  //----------------------------------------------------------------------------
  // 2. solve problem
  //----------------------------------------------------------------------------

  // Initialize the IpoptApplication and process the options
402
403
  Ipopt::ApplicationReturnStatus status;
  status = app_->Initialize();
404
  if (status != Ipopt::Solve_Succeeded)
405
406
407
  {
    printf("\n\n*** Error IPOPT during initialization!\n");
  }
408
409
410
411

  //----------------------------------------------------------------------------
  // 3. solve problem
  //----------------------------------------------------------------------------
412
  status = app_->OptimizeTNLP(np);
413
414
415
416

  //----------------------------------------------------------------------------
  // 4. output statistics
  //----------------------------------------------------------------------------
417
418
419
420
421
  if (status == Ipopt::Solve_Succeeded || status == Ipopt::Solved_To_Acceptable_Level)
  {
    // Retrieve some statistics about the solve
    Ipopt::Index iter_count = app_->Statistics()->IterationCount();
    printf("\n\n*** IPOPT: The problem solved in %d iterations!\n", iter_count);
422

423
424
425
    Ipopt::Number final_obj = app_->Statistics()->FinalObjective();
    printf("\n\n*** IPOPT: The final value of the objective function is %e.\n", final_obj);
  }
426

427
  return status;
428
429
430
431
432
433
434
435
}


//=============================================================================
} // namespace COMISO
//=============================================================================
#endif // COMISO_IPOPT_AVAILABLE
//=============================================================================