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  DOpEtypes::VectorStorageType 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  DOpEtypes::VectorStorageType 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  DOpEtypes::VectorStorageType 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  DOpEtypes::ControlType ct = S->GetProblem()->GetSpaceTimeHandler()->GetControlType();
154  {
155  throw DOpEException("The ControlType: "+ DOpEtypesToString(ct) + " is not supported.",
156  "Reduced_IpoptAlgorithm::Reduced_IpoptAlgorithm");
157  }
158  }
159 
160 /******************************************************/
161 
162 template <typename PROBLEM, typename VECTOR>
164  {
165 
166  }
167 
168 
169 /******************************************************/
170 
171 template <typename PROBLEM, typename VECTOR>
173 {
174 #ifndef WITH_IPOPT
175  throw DOpEException("To use this algorithm you need to have IPOPT installed! To use this set the WITH_IPOPT CompilerFlag.","Reduced_IpoptAlgorithm::Solve");
176 #else
177  q.ReInit();
178 
179  ControlVector<VECTOR> dq(q);
180  ControlVector<VECTOR> q_min(q), q_max(q);
181  this->GetReducedProblem()->GetControlBoxConstraints(q_min,q_max);
182 
183  ConstraintVector<VECTOR> constraints(this->GetReducedProblem()->GetProblem()->GetSpaceTimeHandler(),vector_behavior_);
184 
185  unsigned int iter=0;
186  double cost=0.;
187  double cost_start=0.;
188  std::stringstream out;
189  this->GetOutputHandler()->InitNewtonOut(out);
190  global_tol = std::max(tol_,global_tol);
191 
192  out << "**************************************************\n";
193  out << "* Starting Solution using IPOPT *\n";
194  out << "* Solving : "<<this->GetProblem()->GetName()<<"\t*\n";
195  out << "* CDoFs : ";
196  q.PrintInfos(out);
197  out << "* SDoFs : ";
198  this->GetReducedProblem()->StateSizeInfo(out);
199  out << "* Constraints : ";
200  constraints.PrintInfos(out);
201  out << "**************************************************";
202  this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
203 
204  this->GetOutputHandler()->SetIterationNumber(iter,"Opt_Ipopt"+postindex_);
205 
206  this->GetOutputHandler()->Write(q,"Control"+postindex_,"control");
207 
208  try
209  {
210  cost_start = cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
211  }
212  catch(DOpEException& e)
213  {
214  this->GetExceptionHandler()->HandleCriticalException(e,"Reduced_IpoptAlgorithm::Solve");
215  }
216 
217  this->GetOutputHandler()->InitOut(out);
218  out<< "CostFunctional: " << cost;
219  this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
220  this->GetOutputHandler()->InitNewtonOut(out);
221 
223  out<<"************************************************\n";
224  out<<"* Calling IPOPT *\n";
225  if(capture_out_)
226  out<<"* output will be written to logfile only! *\n";
227  else
228  out<<"* output will not be written to logfile! *\n";
229  out<<"************************************************\n\n";
230  this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
231 
232  this->GetOutputHandler()->DisallowAllOutput();
233  if(capture_out_)
234  this->GetOutputHandler()->StartSaveCTypeOutputToLog();
235 
236  int ret_val = -1;
237  {
238  // Create a new instance of your nlp
239  // (use a SmartPtr, not raw)
240  //Ipopt_Problem<ReducedProblemInterface<PROBLEM,VECTOR, dopedim,dealdim>,VECTOR>
241  // ip_prob((this->GetReducedProblem()),q,&q_min,&q_max,constraints);
242  Ipopt::SmartPtr<Ipopt::TNLP> mynlp = new
243  Ipopt_Problem<ReducedProblemInterface<PROBLEM,VECTOR>,VECTOR>(
244  ret_val, this->GetReducedProblem(),q,&q_min,&q_max,constraints);
245 
246  //Ipopt::IpoptApplication app;
247  Ipopt::SmartPtr<Ipopt::IpoptApplication> app = IpoptApplicationFactory();
248  // Change some options
249  // Note: The following choices are only examples, they might not be
250  // suitable for your optimization problem.
251  app->Options()->SetNumericValue("tol", tol_);
252  app->Options()->SetStringValue("mu_strategy", "adaptive");
253  app->Options()->SetStringValue("output_file", this->GetOutputHandler()->GetResultsDir()+"ipopt.out");
254  app->Options()->SetStringValue("linear_solver", lin_solve_);
255  app->Options()->SetStringValue("hessian_approximation","limited-memory");
256 
257  // Intialize the IpoptApplication and process the options
258  Ipopt::ApplicationReturnStatus status;
259  status = app->Initialize();
260  if (status != Ipopt::Solve_Succeeded) {
261  this->GetOutputHandler()->Write("\n\n*** Error during initialization!\n",1+this->GetBasePriority());
262  abort();
263  }
264 
265  // Ask Ipopt to solve the problem
266  status = app->OptimizeTNLP(mynlp);
267  }
268 
269  if(capture_out_)
270  this->GetOutputHandler()->StopSaveCTypeOutputToLog();
271  this->GetOutputHandler()->ResumeOutput();
272  out<<"\n************************************************\n";
273  out<<"* IPOPT Finished *\n";
274  out<<"* with Exit Code: "<<std::setw(3)<<ret_val;
275  if(ret_val == 1)
276  out<<" (success) *\n";
277  else
278  out<<" (unknown error: "<<ret_val<<") *\n";
279  out<<"************************************************\n";
280  //FIXME What is the result...
281  this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
282 
283  iter++;
284  this->GetOutputHandler()->SetIterationNumber(iter,"Opt_Ipopt"+postindex_);
285 
286  this->GetOutputHandler()->Write(q,"Control"+postindex_,"control");
287  try
288  {
289  cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
290  }
291  catch(DOpEException& e)
292  {
293  this->GetExceptionHandler()->HandleCriticalException(e,"Reduced_IpoptAlgorithm::Solve");
294  }
295  //We are done write total evaluation
296  this->GetOutputHandler()->InitOut(out);
297  out<< "CostFunctional: " << cost;
298  this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
299  this->GetOutputHandler()->InitNewtonOut(out);
300  try
301  {
302  this->GetReducedProblem()->ComputeReducedFunctionals(q);
303  }
304  catch(DOpEException& e)
305  {
306  this->GetExceptionHandler()->HandleCriticalException(e,"Reduced_IpoptAlgorithm::Solve");
307  }
308 
309  out << "**************************************************\n";
310  out << "* Stopping Solution Using IPOPT *\n";
311  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";
312  out.precision(7);
313  out << "* Final value: "<<cost<<" *\n";
314  out << "**************************************************";
315  this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
316  return iter;
317 #endif //Endof ifdef WITH_IPOPT
318 }
319 
320 /*****************************END_OF_NAMESPACE_DOpE*********************************/
321 }
322 #endif
void PrintInfos(std::stringstream &out)
Definition: controlvector.cc:897
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:172
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:133
PROBLEM * GetProblem()
Definition: reducedprobleminterface.h:491
Definition: reducedalgorithm.h:54
void ReInit()
Definition: controlvector.cc:96
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
static std::string DOpEtypesToString(const C &t)
Definition: dopetypes.h:134
VectorStorageType
Definition: dopetypes.h:120
void SetSubsection(const std::string subsection)
Definition: parameterreader.h:93
~Reduced_IpoptAlgorithm()
Definition: reduced_ipopt_algorithm.h:163
Definition: reduced_ipopt_algorithm.h:56
Definition: reducedprobleminterface.h:336
Definition: dopeexception.h:35
Definition: optproblemcontainer.h:72