24 #ifndef REDUCED_IPOPT__ALGORITHM_H_
25 #define REDUCED_IPOPT__ALGORITHM_H_
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wunused-parameter"
35 #include "IpIpoptApplication.hpp"
36 #pragma GCC diagnostic pop
59 template <
typename PROBLEM,
typename VECTOR>
109 std::string postindex_;
114 std::string lin_solve_;
120 using namespace dealii;
124 template <
typename PROBLEM,
typename VECTOR>
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.");
136 template <
typename PROBLEM,
typename VECTOR>
144 :
ReducedAlgorithm<PROBLEM, VECTOR>(OP,S,param_reader,Except,Output,base_priority)
147 param_reader.
SetSubsection(
"reduced_ipoptalgorithm parameters");
150 capture_out_ = param_reader.
get_bool(
"capture ipopt output");
151 lin_solve_ = param_reader.
get_string(
"ipopt linsolve");
153 vector_behavior_ = vector_behavior;
154 postindex_ =
"_"+this->
GetProblem()->GetName();
160 "Reduced_IpoptAlgorithm::Reduced_IpoptAlgorithm");
166 template <
typename PROBLEM,
typename VECTOR>
175 template <
typename PROBLEM,
typename VECTOR>
179 throw DOpEException(
"To use this algorithm you need to have IPOPT installed! To use this set the WITH_IPOPT CompilerFlag.",
"Reduced_IpoptAlgorithm::Solve");
185 this->GetReducedProblem()->GetControlBoxConstraints(q_min,q_max);
187 ConstraintVector<VECTOR> constraints(this->GetReducedProblem()->GetProblem()->GetSpaceTimeHandler(),vector_behavior_);
191 double cost_start=0.;
192 std::stringstream out;
193 this->GetOutputHandler()->InitNewtonOut(out);
194 global_tol = std::max(tol_,global_tol);
196 out <<
"**************************************************\n";
197 out <<
"* Starting Solution using IPOPT *\n";
198 out <<
"* Solving : "<<this->GetProblem()->GetName()<<
"\t*\n";
202 this->GetReducedProblem()->StateSizeInfo(out);
203 out <<
"* Constraints : ";
205 out <<
"**************************************************";
206 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
208 this->GetOutputHandler()->SetIterationNumber(iter,
"Opt_Ipopt"+postindex_);
210 this->GetOutputHandler()->Write(q,
"Control"+postindex_,
"control");
214 cost_start = cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
218 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
221 this->GetOutputHandler()->InitOut(out);
222 out<<
"CostFunctional: " << cost;
223 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
224 this->GetOutputHandler()->InitNewtonOut(out);
227 out<<
"************************************************\n";
228 out<<
"* Calling IPOPT *\n";
230 out<<
"* output will be written to logfile only! *\n";
232 out<<
"* output will not be written to logfile! *\n";
233 out<<
"************************************************\n\n";
234 this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
236 this->GetOutputHandler()->DisallowAllOutput();
238 this->GetOutputHandler()->StartSaveCTypeOutputToLog();
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);
251 Ipopt::SmartPtr<Ipopt::IpoptApplication> app = IpoptApplicationFactory();
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");
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());
270 status = app->OptimizeTNLP(mynlp);
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;
280 out<<
" (success) *\n";
282 out<<
" (unknown error: "<<ret_val<<
") *\n";
283 out<<
"************************************************\n";
285 this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
288 this->GetOutputHandler()->SetIterationNumber(iter,
"Opt_Ipopt"+postindex_);
290 this->GetOutputHandler()->Write(q,
"Control"+postindex_,
"control");
293 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
297 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
300 this->GetOutputHandler()->InitOut(out);
301 out<<
"CostFunctional: " << cost;
302 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
303 this->GetOutputHandler()->InitNewtonOut(out);
306 this->GetReducedProblem()->ComputeReducedFunctionals(q);
310 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_IpoptAlgorithm::Solve");
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";
317 out <<
"* Final value: "<<cost<<
" *\n";
318 out <<
"**************************************************";
319 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
321 #endif //Endof ifdef WITH_IPOPT
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 ¶m_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 ¶m_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 ¶m_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