Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "PlayaLineSearchBasedOptBase.hpp"
00044 #include "PlayaOut.hpp"
00045 #include "PlayaTabs.hpp"
00046 #include "PlayaLinearCombinationImpl.hpp"
00047 #include "PlayaLineSearchBuilder.hpp"
00048 #include "PlayaOptConvergenceTestBuilder.hpp"
00049
00050
00051 namespace Playa
00052 {
00053 using std::endl;
00054 using std::setw;
00055
00056 LineSearchBasedOptBase::LineSearchBasedOptBase(
00057 const ParameterList& params
00058 )
00059 : lineSearch_(),
00060 convTest_()
00061 {
00062 const ParameterList& lsParams = params.sublist("Line Search");
00063 lineSearch_ = LineSearchBuilder::createLineSearch(lsParams);
00064
00065 const ParameterList& ctParams = params.sublist("Convergence Test");
00066 convTest_ = OptConvergenceTestBuilder::createConvTest(ctParams);
00067 }
00068
00069
00070 OptState LineSearchBasedOptBase::run(const RCP<ObjectiveBase>& obj,
00071 const Vector<double>& xInit,
00072 const RCP<ConvergenceMonitor>& convMonitor) const
00073 {
00074 Tabs tab0(0);
00075 PLAYA_MSG1(verb(), tab0 << "in " << description() << "::run()");
00076
00077 RCP<DirectionGeneratorBase> dirGen = makeDirectionGenerator();
00078
00079 VectorSpace<double> vs = xInit.space();
00080
00081 Vector<double> xCur = xInit.copy();
00082 Vector<double> gradCur =vs.createMember();
00083 double fCur;
00084
00085 obj->evalGrad(xCur, fCur, gradCur);
00086
00087 OptState state(xCur, fCur, gradCur);
00088
00089 Vector<double> xNew = vs.createMember();
00090 Vector<double> gradNew = vs.createMember();
00091 Vector<double> p = vs.createMember();
00092 double fNew;
00093
00094
00095 while (state.status()==Opt_Continue)
00096 {
00097 Tabs tab1;
00098 PLAYA_MSG2(verb(),
00099 tab1 << "--------------------------------------------------------------");
00100 PLAYA_MSG2(verb(), tab1 << description() << " iter "
00101 << setw(5) << state.iter()
00102 << " f=" << setw(14) << state.fCur() << " |grad|=" << setw(14)
00103 << state.gradCur().norm2());
00104
00105
00106 Tabs tab2;
00107 Tabs tab3;
00108 PLAYA_MSG4(verb(), tab2 << "current x " << endl << tab3 << state.xCur());
00109
00110 obj->iterationCallback(state.xCur(), state.iter());
00111
00112 if (convMonitor.ptr()!=null)
00113 {
00114 convMonitor->addRecord(state.iter(), tuple(state.fCur()));
00115 }
00116
00117
00118
00119 bool dirOK = dirGen->generateDirection(obj, state.xCur(),
00120 state.gradCur(), state.fCur(), p);
00121
00122 if (!dirOK)
00123 {
00124 state.setStatus(Opt_DirectionFailure);
00125 return state;
00126 }
00127
00128
00129 LineSearchStatus info = lineSearch_->search(obj,
00130 state.xCur(), state.fCur(), p,
00131 1.0, xNew, gradNew, fNew);
00132
00133 if (info != LS_Success)
00134 {
00135 state.setStatus(Opt_LineSearchFailed);
00136 return state;
00137 }
00138
00139 PLAYA_MSG4(verb(), tab2 << "new x " << endl << tab3 << xNew);
00140
00141
00142 state.update(xNew, gradNew, fNew);
00143
00144
00145 OptStatus iterStatus = convTest_->test(state);
00146 state.setStatus(iterStatus);
00147 }
00148
00149
00150 if (convMonitor.ptr()!=null)
00151 {
00152 convMonitor->addRecord(state.iter(), tuple(state.fCur()));
00153 }
00154
00155 return state;
00156 }
00157
00158 void LineSearchBasedOptBase::print(std::ostream& os) const
00159 {
00160 Tabs tab(0);
00161 os << tab << description() << "[" << endl;
00162 Tabs tab1;
00163 os << tab1 << "Line search=" << endl;
00164 lineSearch_->print(os);
00165 os << tab1 << "Convergence test=" << endl;
00166 convTest_->print(os);
00167 os << tab << "]";
00168
00169 }
00170
00171
00172 }