PlayaLineSearchBasedOptBase.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
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   /* -------- Main optimization loop ------------- */
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     /* Find a direction for the line search */
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     /* Perform the line search */
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     /* Update the state to know about the new approximation to the min */
00142     state.update(xNew, gradNew, fNew);
00143 
00144     /* Check for convergence */
00145     OptStatus iterStatus = convTest_->test(state);
00146     state.setStatus(iterStatus);
00147   }
00148 
00149   /* -------- End main optimization loop ------------- */
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 }

Site Contact