24 #ifndef _REDUCED_IPOPT__ALGORITHM_H_
25 #define _REDUCED_IPOPT__ALGORITHM_H_
32 #include "IpIpoptApplication.hpp"
55 template <
typename PROBLEM,
typename VECTOR>
74 std::string vector_behavior,
105 std::string _postindex;
106 std::string _vector_behavior;
110 std::string _lin_solve;
116 using namespace dealii;
120 template <
typename PROBLEM,
typename VECTOR>
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.");
132 template <
typename PROBLEM,
typename VECTOR>
135 std::string vector_behavior,
140 :
ReducedAlgorithm<PROBLEM, VECTOR>(OP,S,param_reader,Except,Output,base_priority)
143 param_reader.
SetSubsection(
"reduced_ipoptalgorithm parameters");
146 _capture_out = param_reader.
get_bool(
"capture ipopt output");
147 _lin_solve = param_reader.
get_string(
"ipopt linsolve");
149 _vector_behavior = vector_behavior;
150 _postindex =
"_"+this->
GetProblem()->GetName();
156 template <
typename PROBLEM,
typename VECTOR>
165 template <
typename PROBLEM,
typename VECTOR>
169 throw DOpEException(
"To use this algorithm you need to have IPOPT installed! To use this set the WITH_IPOPT CompilerFlag.",
"Reduced_IpoptAlgorithm::Solve");
175 this->GetReducedProblem()->GetControlBoxConstraints(q_min,q_max);
177 ConstraintVector<VECTOR> constraints(this->GetReducedProblem()->GetProblem()->GetSpaceTimeHandler(),_vector_behavior);
181 double cost_start=0.;
182 std::stringstream out;
183 this->GetOutputHandler()->InitNewtonOut(out);
184 global_tol = std::max(_tol,global_tol);
186 out <<
"**************************************************\n";
187 out <<
"* Starting Solution using IPOPT *\n";
188 out <<
"* Solving : "<<this->GetProblem()->GetName()<<
"\t*\n";
192 this->GetReducedProblem()->StateSizeInfo(out);
193 out <<
"* Constraints : ";
195 out <<
"**************************************************";
196 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
198 this->GetOutputHandler()->SetIterationNumber(iter,
"Opt_Ipopt"+_postindex);
200 this->GetOutputHandler()->Write(q,
"Control"+_postindex,
"control");
204 cost_start = cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
208 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
211 this->GetOutputHandler()->InitOut(out);
212 out<<
"CostFunctional: " << cost;
213 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
214 this->GetOutputHandler()->InitNewtonOut(out);
217 out<<
"************************************************\n";
218 out<<
"* Calling IPOPT *\n";
220 out<<
"* output will be written to logfile only! *\n";
222 out<<
"* output will not be written to logfile! *\n";
223 out<<
"************************************************\n\n";
224 this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
226 this->GetOutputHandler()->DisallowAllOutput();
228 this->GetOutputHandler()->StartSaveCTypeOutputToLog();
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);
241 Ipopt::SmartPtr<Ipopt::IpoptApplication> app = IpoptApplicationFactory();
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");
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());
260 status = app->OptimizeTNLP(mynlp);
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;
270 out<<
" (success) *\n";
272 out<<
" (unknown error: "<<ret_val<<
") *\n";
273 out<<
"************************************************\n";
275 this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
278 this->GetOutputHandler()->SetIterationNumber(iter,
"Opt_Ipopt"+_postindex);
280 this->GetOutputHandler()->Write(q,
"Control"+_postindex,
"control");
283 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
287 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
290 this->GetOutputHandler()->InitOut(out);
291 out<<
"CostFunctional: " << cost;
292 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
293 this->GetOutputHandler()->InitNewtonOut(out);
296 this->GetReducedProblem()->ComputeReducedFunctionals(q);
300 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
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";
307 out <<
"* Final value: "<<cost<<
" *\n";
308 out <<
"**************************************************";
309 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
311 #endif //Endof ifdef WITH_IPOPT
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 ¶m_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 ¶m_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 ¶m_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