PlayaSimpleBacktracking.cpp
00001 #include "PlayaSimpleBacktracking.hpp"
00002 #include "PlayaOut.hpp"
00003 #include "PlayaTabs.hpp"
00004 #include "PlayaLinearCombinationImpl.hpp"
00005
00006
00007 namespace Playa
00008 {
00009 using std::endl;
00010
00011 SimpleBacktracking::SimpleBacktracking(const ParameterList& params)
00012 : LineSearchBase(params)
00013 {;}
00014
00015 std::string SimpleBacktracking::description() const
00016 {
00017 std::ostringstream oss;
00018 oss << "SimpleBacktracking(maxSteps=" << maxSteps()
00019 << ", minStepSize=" << minStepSize() << ")";
00020 return oss.str();
00021 }
00022
00023 LineSearchStatus SimpleBacktracking::search(const RCP<ObjectiveBase>& obj,
00024 const Vector<double>& x0,
00025 const double& f0,
00026 const Vector<double>& direction,
00027 const double& alphaMax,
00028 Vector<double>& xn,
00029 Vector<double>& gradF,
00030 double& fVal) const
00031 {
00032 Tabs tab(0);
00033 try
00034 {
00035 if (verb() > 0)
00036 {
00037 Out::root() << tab << "backtracking line search" << endl;
00038 }
00039 double alpha = alphaMax;
00040 if (verb() > 3)
00041 {
00042 Out::root() << tab << "line search initial vector " << endl;
00043 Out::os() << x0 << endl;
00044 Out::root() << tab << "line search direction " << endl;
00045 Out::os() << direction << endl;
00046 Out::root() << tab << "line search initial value " << f0 << endl;
00047 }
00048
00049 for (int i = 0; i < maxSteps(); i++)
00050 {
00051 Tabs tab1;
00052 if (verb() > 1)
00053 {
00054 Out::root() << tab1 << "line search step "
00055 << i << " alpha=" << alpha << endl;
00056 }
00057
00058 xn = x0 + alpha * direction;
00059
00060 if (verb() > 3)
00061 {
00062 Out::root() << tab1 << "line search trial vector " << endl;
00063 Out::os() << xn << endl;
00064 }
00065
00066 obj->evalGrad(xn, fVal, gradF);
00067
00068 if (verb() > 1)
00069 {
00070 Out::root() << tab1 << "f=" << fVal << " f0=" << f0 << endl;
00071 }
00072
00073 if (alpha < minStepSize()) return LS_StepTooSmall;
00074
00075 if (fVal < f0)
00076 {
00077 if (verb() > 0)
00078 {
00079 Out::root() << tab1 << "Line search successful: steps = " << i << endl;
00080 }
00081 return LS_Success;
00082 }
00083 alpha = alpha/2.0;
00084 }
00085 return LS_ExceededMaxiters;
00086 }
00087
00088 catch(std::exception& e)
00089 {
00090 Out::root() << "Exception detected in SimpleBacktracking::search(): "
00091 << e.what() << endl;
00092 return LS_Crashed;
00093 }
00094 }
00095
00096
00097
00098 }