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