24 #ifndef REDUCED_IPOPT__ALGORITHM_H_
25 #define REDUCED_IPOPT__ALGORITHM_H_
32 #include "IpIpoptApplication.hpp"
55 template <
typename PROBLEM,
typename VECTOR>
105 std::string postindex_;
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>
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 "Reduced_IpoptAlgorithm::Reduced_IpoptAlgorithm");
162 template <
typename PROBLEM,
typename VECTOR>
171 template <
typename PROBLEM,
typename VECTOR>
175 throw DOpEException(
"To use this algorithm you need to have IPOPT installed! To use this set the WITH_IPOPT CompilerFlag.",
"Reduced_IpoptAlgorithm::Solve");
181 this->GetReducedProblem()->GetControlBoxConstraints(q_min,q_max);
183 ConstraintVector<VECTOR> constraints(this->GetReducedProblem()->GetProblem()->GetSpaceTimeHandler(),vector_behavior_);
187 double cost_start=0.;
188 std::stringstream out;
189 this->GetOutputHandler()->InitNewtonOut(out);
190 global_tol = std::max(tol_,global_tol);
192 out <<
"**************************************************\n";
193 out <<
"* Starting Solution using IPOPT *\n";
194 out <<
"* Solving : "<<this->GetProblem()->GetName()<<
"\t*\n";
198 this->GetReducedProblem()->StateSizeInfo(out);
199 out <<
"* Constraints : ";
201 out <<
"**************************************************";
202 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
204 this->GetOutputHandler()->SetIterationNumber(iter,
"Opt_Ipopt"+postindex_);
206 this->GetOutputHandler()->Write(q,
"Control"+postindex_,
"control");
210 cost_start = cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
214 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
217 this->GetOutputHandler()->InitOut(out);
218 out<<
"CostFunctional: " << cost;
219 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
220 this->GetOutputHandler()->InitNewtonOut(out);
223 out<<
"************************************************\n";
224 out<<
"* Calling IPOPT *\n";
226 out<<
"* output will be written to logfile only! *\n";
228 out<<
"* output will not be written to logfile! *\n";
229 out<<
"************************************************\n\n";
230 this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
232 this->GetOutputHandler()->DisallowAllOutput();
234 this->GetOutputHandler()->StartSaveCTypeOutputToLog();
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);
247 Ipopt::SmartPtr<Ipopt::IpoptApplication> app = IpoptApplicationFactory();
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");
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());
266 status = app->OptimizeTNLP(mynlp);
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;
276 out<<
" (success) *\n";
278 out<<
" (unknown error: "<<ret_val<<
") *\n";
279 out<<
"************************************************\n";
281 this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
284 this->GetOutputHandler()->SetIterationNumber(iter,
"Opt_Ipopt"+postindex_);
286 this->GetOutputHandler()->Write(q,
"Control"+postindex_,
"control");
289 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
293 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
296 this->GetOutputHandler()->InitOut(out);
297 out<<
"CostFunctional: " << cost;
298 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
299 this->GetOutputHandler()->InitNewtonOut(out);
302 this->GetReducedProblem()->ComputeReducedFunctionals(q);
306 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
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";
313 out <<
"* Final value: "<<cost<<
" *\n";
314 out <<
"**************************************************";
315 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
317 #endif //Endof ifdef WITH_IPOPT
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 ¶m_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 ¶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
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