DOpE
reduced_ipopt_algorithm.h
Go to the documentation of this file.
1 
24 #ifndef _REDUCED_IPOPT__ALGORITHM_H_
25 #define _REDUCED_IPOPT__ALGORITHM_H_
26 
27 #include "reducedalgorithm.h"
28 #include "parameterreader.h"
29 #include "ipopt_problem.h"
30 
31 #ifdef WITH_IPOPT
32 #include "IpIpoptApplication.hpp"
33 #endif
34 
35 #include <iostream>
36 #include <assert.h>
37 #include <iomanip>
38 
39 namespace DOpE
40 {
55  template <typename PROBLEM, typename VECTOR>
56  class Reduced_IpoptAlgorithm : public ReducedAlgorithm<PROBLEM, VECTOR>
57  {
58  public:
72  Reduced_IpoptAlgorithm(PROBLEM* OP,
74  std::string vector_behavior,
75  ParameterReader &param_reader,
76  DOpEExceptionHandler<VECTOR>* Except=NULL,
77  DOpEOutputHandler<VECTOR>* Output=NULL,
78  int base_priority=0);
80 
86  static void declare_params(ParameterReader &param_reader);
87 
100  virtual int Solve(ControlVector<VECTOR>& q,double global_tol=-1.);
101 
102  protected:
103 
104  private:
105  std::string _postindex;
106  std::string _vector_behavior;
107 
108  double _tol;
109  bool _capture_out;
110  std::string _lin_solve;
111  };
112 
113  /***************************************************************************************/
114  /****************************************IMPLEMENTATION*********************************/
115  /***************************************************************************************/
116  using namespace dealii;
117 
118  /******************************************************/
119 
120 template <typename PROBLEM, typename VECTOR>
122  {
123  param_reader.SetSubsection("reduced_ipoptalgorithm parameters");
124  param_reader.declare_entry("tol","1.e-5",Patterns::Double(0,1),"Tolerance");
125  param_reader.declare_entry("capture ipopt output","true",Patterns::Bool(),"Select if the ipopt output should be stored in log file");
126  param_reader.declare_entry("ipopt linsolve","ma27",Patterns::Selection("ma27|ma57|ma77|ma86|pardiso|wsmp|mumps"),"Linear Solver to be used in ipopt.");
128  }
129 
130 /******************************************************/
131 
132 template <typename PROBLEM, typename VECTOR>
135  std::string vector_behavior,
136  ParameterReader &param_reader,
139  int base_priority)
140  : ReducedAlgorithm<PROBLEM, VECTOR>(OP,S,param_reader,Except,Output,base_priority)
141  {
142 
143  param_reader.SetSubsection("reduced_ipoptalgorithm parameters");
144 
145  _tol = param_reader.get_double("tol");
146  _capture_out = param_reader.get_bool("capture ipopt output");
147  _lin_solve = param_reader.get_string("ipopt linsolve");
148 
149  _vector_behavior = vector_behavior;
150  _postindex = "_"+this->GetProblem()->GetName();
151 
152  }
153 
154 /******************************************************/
155 
156 template <typename PROBLEM, typename VECTOR>
158  {
159 
160  }
161 
162 
163 /******************************************************/
164 
165 template <typename PROBLEM, typename VECTOR>
167 {
168 #ifndef WITH_IPOPT
169  throw DOpEException("To use this algorithm you need to have IPOPT installed! To use this set the WITH_IPOPT CompilerFlag.","Reduced_IpoptAlgorithm::Solve");
170 #else
171  q.ReInit();
172 
173  ControlVector<VECTOR> dq(q);
174  ControlVector<VECTOR> q_min(q), q_max(q);
175  this->GetReducedProblem()->GetControlBoxConstraints(q_min,q_max);
176 
177  ConstraintVector<VECTOR> constraints(this->GetReducedProblem()->GetProblem()->GetSpaceTimeHandler(),_vector_behavior);
178 
179  unsigned int iter=0;
180  double cost=0.;
181  double cost_start=0.;
182  std::stringstream out;
183  this->GetOutputHandler()->InitNewtonOut(out);
184  global_tol = std::max(_tol,global_tol);
185 
186  out << "**************************************************\n";
187  out << "* Starting Solution using IPOPT *\n";
188  out << "* Solving : "<<this->GetProblem()->GetName()<<"\t*\n";
189  out << "* CDoFs : ";
190  q.PrintInfos(out);
191  out << "* SDoFs : ";
192  this->GetReducedProblem()->StateSizeInfo(out);
193  out << "* Constraints : ";
194  constraints.PrintInfos(out);
195  out << "**************************************************";
196  this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
197 
198  this->GetOutputHandler()->SetIterationNumber(iter,"Opt_Ipopt"+_postindex);
199 
200  this->GetOutputHandler()->Write(q,"Control"+_postindex,"control");
201 
202  try
203  {
204  cost_start = cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
205  }
206  catch(DOpEException& e)
207  {
208  this->GetExceptionHandler()->HandleCriticalException(e,"Reduced_IpoptAlgorithm::Solve");
209  }
210 
211  this->GetOutputHandler()->InitOut(out);
212  out<< "CostFunctional: " << cost;
213  this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
214  this->GetOutputHandler()->InitNewtonOut(out);
215 
217  out<<"************************************************\n";
218  out<<"* Calling IPOPT *\n";
219  if(_capture_out)
220  out<<"* output will be written to logfile only! *\n";
221  else
222  out<<"* output will not be written to logfile! *\n";
223  out<<"************************************************\n\n";
224  this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
225 
226  this->GetOutputHandler()->DisallowAllOutput();
227  if(_capture_out)
228  this->GetOutputHandler()->StartSaveCTypeOutputToLog();
229 
230  int ret_val = -1;
231  {
232  // Create a new instance of your nlp
233  // (use a SmartPtr, not raw)
234  //Ipopt_Problem<ReducedProblemInterface<PROBLEM,VECTOR, dopedim,dealdim>,VECTOR>
235  // ip_prob((this->GetReducedProblem()),q,&q_min,&q_max,constraints);
236  Ipopt::SmartPtr<Ipopt::TNLP> mynlp = new
237  Ipopt_Problem<ReducedProblemInterface<PROBLEM,VECTOR>,VECTOR>(
238  ret_val, this->GetReducedProblem(),q,&q_min,&q_max,constraints);
239 
240  //Ipopt::IpoptApplication app;
241  Ipopt::SmartPtr<Ipopt::IpoptApplication> app = IpoptApplicationFactory();
242  // Change some options
243  // Note: The following choices are only examples, they might not be
244  // suitable for your optimization problem.
245  app->Options()->SetNumericValue("tol", _tol);
246  app->Options()->SetStringValue("mu_strategy", "adaptive");
247  app->Options()->SetStringValue("output_file", this->GetOutputHandler()->GetResultsDir()+"ipopt.out");
248  app->Options()->SetStringValue("linear_solver", _lin_solve);
249  app->Options()->SetStringValue("hessian_approximation","limited-memory");
250 
251  // Intialize the IpoptApplication and process the options
252  Ipopt::ApplicationReturnStatus status;
253  status = app->Initialize();
254  if (status != Ipopt::Solve_Succeeded) {
255  this->GetOutputHandler()->Write("\n\n*** Error during initialization!\n",1+this->GetBasePriority());
256  abort();
257  }
258 
259  // Ask Ipopt to solve the problem
260  status = app->OptimizeTNLP(mynlp);
261  }
262 
263  if(_capture_out)
264  this->GetOutputHandler()->StopSaveCTypeOutputToLog();
265  this->GetOutputHandler()->ResumeOutput();
266  out<<"\n************************************************\n";
267  out<<"* IPOPT Finished *\n";
268  out<<"* with Exit Code: "<<std::setw(3)<<ret_val;
269  if(ret_val == 1)
270  out<<" (success) *\n";
271  else
272  out<<" (unknown error: "<<ret_val<<") *\n";
273  out<<"************************************************\n";
274  //FIXME What is the result...
275  this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
276 
277  iter++;
278  this->GetOutputHandler()->SetIterationNumber(iter,"Opt_Ipopt"+_postindex);
279 
280  this->GetOutputHandler()->Write(q,"Control"+_postindex,"control");
281  try
282  {
283  cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
284  }
285  catch(DOpEException& e)
286  {
287  this->GetExceptionHandler()->HandleCriticalException(e,"Reduced_IpoptAlgorithm::Solve");
288  }
289  //We are done write total evaluation
290  this->GetOutputHandler()->InitOut(out);
291  out<< "CostFunctional: " << cost;
292  this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
293  this->GetOutputHandler()->InitNewtonOut(out);
294  try
295  {
296  this->GetReducedProblem()->ComputeReducedFunctionals(q);
297  }
298  catch(DOpEException& e)
299  {
300  this->GetExceptionHandler()->HandleCriticalException(e,"Reduced_IpoptAlgorithm::Solve");
301  }
302 
303  out << "**************************************************\n";
304  out << "* Stopping Solution Using IPOPT *\n";
305  out << "* Relative reduction in cost functional:"<<std::scientific << std::setw(11) << this->GetOutputHandler()->ZeroTolerance((cost-cost_start)/fabs(0.5*(cost_start+cost)),1.0) <<" *\n";
306  out.precision(7);
307  out << "* Final value: "<<cost<<" *\n";
308  out << "**************************************************";
309  this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
310  return iter;
311 #endif //Endof ifdef WITH_IPOPT
312 }
313 
314 /*****************************END_OF_NAMESPACE_DOpE*********************************/
315 }
316 #endif
void PrintInfos(std::stringstream &out)
Definition: controlvector.cc:542
Definition: constraintvector.h:47
double get_double(const std::string &entry_name)
Definition: parameterreader.h:115
bool get_bool(const std::string &entry_name)
Definition: parameterreader.h:148
Definition: parameterreader.h:36
Definition: optproblemcontainer.h:70
void PrintInfos(std::stringstream &out)
Definition: constraintvector.cc:450
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation=std::string())
Definition: parameterreader.h:98
Definition: controlvector.h:48
virtual int Solve(ControlVector< VECTOR > &q, double global_tol=-1.)
Definition: reduced_ipopt_algorithm.h:166
PROBLEM * GetProblem()
Definition: reducedalgorithm.h:187
Definition: reducedalgorithm.h:54
void ReInit()
Definition: controlvector.cc:60
static void declare_params(ParameterReader &param_reader)
Definition: reduced_ipopt_algorithm.h:121
std::string get_string(const std::string &entry_name)
Definition: parameterreader.h:137
static void declare_params(ParameterReader &param_reader)
Definition: reducedalgorithm.h:241
void SetSubsection(const std::string subsection)
Definition: parameterreader.h:93
~Reduced_IpoptAlgorithm()
Definition: reduced_ipopt_algorithm.h:157
Definition: reduced_ipopt_algorithm.h:56
Reduced_IpoptAlgorithm(PROBLEM *OP, ReducedProblemInterface< PROBLEM, VECTOR > *S, std::string vector_behavior, ParameterReader &param_reader, DOpEExceptionHandler< VECTOR > *Except=NULL, DOpEOutputHandler< VECTOR > *Output=NULL, int base_priority=0)
Definition: reduced_ipopt_algorithm.h:133
Definition: reducedprobleminterface.h:338
Definition: dopeexception.h:35
Definition: optproblemcontainer.h:72