24 #ifndef _REDUCED_SNOPT__ALGORITHM_H_
25 #define _REDUCED_SNOPT__ALGORITHM_H_
52 template<
typename PROBLEM,
typename VECTOR>
90 int rsa_func_(DOpEWrapper::SNOPT_FUNC_DATA& data);
92 std::string _postindex;
93 std::string _vector_behavior;
95 double _func_prec, _feas_tol, _opt_tol;
96 int _max_inner_iter, _max_outer_iter;
103 using namespace dealii;
107 template<
typename PROBLEM,
typename VECTOR>
112 param_reader.
SetSubsection(
"reduced_snoptalgorithm parameters");
114 Patterns::Double(0, 1),
115 "Declares how many digits we assume to have computed correctly, this should correspond to the tolerance used for the PDE solve");
117 Patterns::Double(0, 1),
118 "Tolerance with respect to the feasibility of the constraints.");
120 Patterns::Double(0, 1),
121 "Tolerance with respect to the optimality condition.");
123 Patterns::Integer(0),
124 "Maximal allowed number of inner iterations over all outer iterations");
125 param_reader.
declare_entry(
"max iterations",
"1000", Patterns::Integer(0),
126 "Maximal allowed number of outer iterations over all outer iterations");
129 "Select if the snopt output should be stored in log file");
135 template<
typename PROBLEM,
typename VECTOR>
145 param_reader.
SetSubsection(
"reduced_snoptalgorithm parameters");
147 _func_prec = param_reader.
get_double(
"function precision");
148 _feas_tol = param_reader.
get_double(
"feasibility tol");
149 _opt_tol = param_reader.
get_double(
"optimality tol");
150 _max_inner_iter = param_reader.
get_integer(
"max inner iterations");
151 _max_outer_iter = param_reader.
get_integer(
"max iterations");
152 _capture_out = param_reader.
get_bool(
"capture snopt output");
154 _vector_behavior = vector_behavior;
155 _postindex =
"_" + this->
GetProblem()->GetName();
161 template<
typename PROBLEM,
typename VECTOR>
169 template<
typename PROBLEM,
typename VECTOR>
175 throw DOpEException(
"To use this algorithm you need to have SNOPT installed! To use this set the WITH_SNOPT CompilerFlag.",
"Reduced_SnoptAlgorithm::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(_opt_tol,global_tol);
192 out <<
"**************************************************\n";
193 out <<
"* Starting Solution using SNOPT *\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_Snopt"+_postindex);
206 this->GetOutputHandler()->Write(q,
"Control"+_postindex,
"control");
210 cost_start = cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
214 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_SnoptAlgorithm::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 SNOPT *\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();
238 DOpEWrapper::SNOPT_Problem RSAProb;
244 integer *iAfun = NULL;
245 integer *jAvar = NULL;
246 doublereal *A = NULL;
248 integer lenG = n*(neF);
251 integer *iGfun =
new integer[lenG];
252 integer *jGvar =
new integer[lenG];
254 doublereal *x =
new doublereal[n];
255 doublereal *xlow =
new doublereal[n];
256 doublereal *xupp =
new doublereal[n];
257 doublereal *xmul =
new doublereal[n];
258 integer *xstate =
new integer[n];
260 doublereal *F =
new doublereal[neF];
261 doublereal *Flow =
new doublereal[neF];
262 doublereal *Fupp =
new doublereal[neF];
263 doublereal *Fmul =
new doublereal[neF];
264 integer *Fstate =
new integer[neF];
268 char *xnames =
new char[nxnames*8];
269 char *Fnames =
new char[nFnames*8];
272 doublereal ObjAdd = 0;
276 const VECTOR& gv_qmin = q_min.GetSpacialVector();
279 for(
unsigned int i=0; i < n; i++)
282 xlow[i] = gv_qmin(i);
283 xupp[i] = gv_qmax(i);
291 for(
unsigned int j = 1; j<neF; j++)
298 for(
unsigned int i=0; i < n; i++)
307 RSAProb.setProblemSize( n, neF );
308 RSAProb.setObjective ( ObjRow, ObjAdd );
309 RSAProb.setA ( lenA, iAfun, jAvar, A );
310 RSAProb.setG ( lenG, iGfun, jGvar );
311 RSAProb.setX ( x, xlow, xupp, xmul, xstate );
312 RSAProb.setF ( F, Flow, Fupp, Fmul, Fstate );
313 RSAProb.setXNames ( xnames, nxnames );
314 RSAProb.setFNames ( Fnames, nFnames );
315 RSAProb.setProbName (
"RSA" );
316 RSAProb.setNeA ( neA );
317 RSAProb.setNeG ( neG );
320 RSAProb.setUserFun ( DOpEWrapper::SNOPT_A_userfunc_ );
322 RSAProb.setIntParameter(
"Derivative option", 1 );
323 RSAProb.setRealParameter(
"Function precison", _func_prec);
324 RSAProb.setRealParameter(
"Major optimality tolerance", global_tol);
325 RSAProb.setRealParameter(
"Major feasibility tolerance", _feas_tol);
326 RSAProb.setRealParameter(
"Minor feasibility tolerance", _feas_tol);
327 RSAProb.setIntParameter(
"Minor iterations limit", _max_inner_iter);
328 RSAProb.setIntParameter(
"Major iterations limit", _max_outer_iter);
330 ret_val = RSAProb.GetReturnStatus();
333 for(
unsigned int i=0; i < gv_q.size(); i++)
342 delete []iGfun;
delete []jGvar;
344 delete []x;
delete []xlow;
delete []xupp;
345 delete []xmul;
delete []xstate;
347 delete []F;
delete []Flow;
delete []Fupp;
348 delete []Fmul;
delete []Fstate;
350 delete []xnames;
delete []Fnames;
355 this->GetOutputHandler()->StopSaveCTypeOutputToLog();
356 this->GetOutputHandler()->ResumeOutput();
357 out<<
"\n************************************************\n";
358 out<<
"* SNOPT Finished *\n";
359 out<<
"* with Exit Code: "<<std::setw(3)<<ret_val;
361 out<<
" (success) *\n";
363 out<<
" (unknown error) *\n";
364 out<<
"************************************************\n";
366 this->GetOutputHandler()->Write(out,1+this->GetBasePriority());
369 this->GetOutputHandler()->SetIterationNumber(iter,
"Opt_Snopt"+_postindex);
371 this->GetOutputHandler()->Write(q,
"Control"+_postindex,
"control");
374 cost = this->GetReducedProblem()->ComputeReducedCostFunctional(q);
378 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_SnoptAlgorithm::Solve");
381 this->GetOutputHandler()->InitOut(out);
382 out<<
"CostFunctional: " << cost;
383 this->GetOutputHandler()->Write(out,2+this->GetBasePriority());
384 this->GetOutputHandler()->InitNewtonOut(out);
387 this->GetReducedProblem()->ComputeReducedFunctionals(q);
391 this->GetExceptionHandler()->HandleCriticalException(e,
"Reduced_SnoptAlgorithm::Solve");
394 out <<
"**************************************************\n";
395 out <<
"* Stopping Solution Using SNOPT *\n";
396 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";
398 out <<
"* Final value: "<<cost<<
" *\n";
399 out <<
"**************************************************";
400 this->GetOutputHandler()->Write(out,1+this->GetBasePriority(),1,1);
402 #endif //Endof ifdef WITHSNOPT
408 template <
typename PROBLEM,
typename VECTOR>
415 ControlVector<VECTOR> tmp(this->GetReducedProblem()->GetProblem()->GetSpaceTimeHandler(),_vector_behavior);
416 VECTOR& ref_x = tmp.GetSpacialVector();
417 assert(ref_x.size() == *(data.n));
418 for(
unsigned int i=0; i < ref_x.size(); i++)
419 ref_x(i) = (data.x)[i];
421 assert(*(data.needF) > 0);
424 (data.F)[0] = this->GetReducedProblem()->ComputeReducedCostFunctional(tmp);
426 catch(DOpEException& e)
429 this->GetExceptionHandler()->HandleException(e,
"Reduced_SnoptAlgorithm::rsa_func_");
435 constraints =
new ConstraintVector<VECTOR>(this->GetReducedProblem()->GetProblem()->GetSpaceTimeHandler(),_vector_behavior);
438 this->GetReducedProblem()->ComputeReducedConstraints(tmp,*constraints);
440 catch(DOpEException& e)
443 this->GetExceptionHandler()->HandleException(e,
"Reduced_SnoptAlgorithm::rsa_func_");
445 if(constraints != NULL)
450 assert(*(data.neF) == gc.size()+1);
451 for(
unsigned int i=0; i < gc.size(); i++)
453 (data.F)[i+1] = gc(i);
457 if(*(data.needG) > 0)
459 ControlVector<VECTOR> gradient(tmp);
460 ControlVector<VECTOR> gradient_transposed(tmp);
464 this->GetReducedProblem()->ComputeReducedGradient(tmp,gradient,gradient_transposed);
466 catch(DOpEException& e)
469 this->GetExceptionHandler()->HandleException(e,
"Reduced_SnoptAlgorithm::rsa_func_");
471 if(constraints != NULL)
475 assert(*(data.neG) == *(data.n)**(data.neF));
476 const VECTOR& ref_g = gradient_transposed.GetSpacialVector();
477 for(
unsigned int i=0; i < *(data.n); i++)
479 (data.G)[i] = ref_g(i);
481 if(constraints != NULL)
485 for(
unsigned int j=0; j < gc.size(); j++)
489 this->GetReducedProblem()->ComputeReducedGradientOfGlobalConstraints(j,tmp,*constraints,gradient,gradient_transposed);
491 catch(DOpEException& e)
494 this->GetExceptionHandler()->HandleException(e,
"Reduced_SnoptAlgorithm::rsa_func_");
496 if(constraints != NULL)
501 for(
unsigned int i=0; i < *(data.n); i++)
503 (data.G)[*(data.n)*(j+1)+i] = ref_g(i);
509 if(constraints != NULL)
514 #endif //Endof ifdef WITHSNOPT
void PrintInfos(std::stringstream &out)
Definition: controlvector.cc:542
Definition: constraintvector.h:47
Reduced_SnoptAlgorithm(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_snopt_algorithm.h:136
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
~Reduced_SnoptAlgorithm()
Definition: reduced_snopt_algorithm.h:162
const dealii::Vector< double > & GetGlobalConstraints() const
Definition: constraintvector.cc:174
void PrintInfos(std::stringstream &out)
Definition: constraintvector.cc:450
static void declare_params(ParameterReader ¶m_reader)
Definition: reduced_snopt_algorithm.h:109
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
PROBLEM * GetProblem()
Definition: reducedalgorithm.h:187
Definition: reducedalgorithm.h:54
void ReInit()
Definition: controlvector.cc:60
virtual int Solve(ControlVector< VECTOR > &q, double global_tol=-1.)
Definition: reduced_snopt_algorithm.h:171
VECTOR & GetSpacialVector()
Definition: controlvector.cc:118
Definition: reduced_snopt_algorithm.h:53
static void declare_params(ParameterReader ¶m_reader)
Definition: reducedalgorithm.h:241
int get_integer(const std::string &entry_name)
Definition: parameterreader.h:126
void SetSubsection(const std::string subsection)
Definition: parameterreader.h:93
Definition: reducedprobleminterface.h:338
VECTOR & GetSpacialVector(std::string name)
Definition: constraintvector.cc:130
Definition: dopeexception.h:35
Definition: optproblemcontainer.h:72