GlobiPack Package Browser (Single Doxygen Collection) Version of the Day
ArmijoPolyInterpLineSearch_UnitTests.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 //    GlobiPack: Collection of Scalar 1D globalizaton utilities
00006 //                 Copyright (2009) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 
00045 #include "GlobiPack_TestLagrPolyMeritFunc1D.hpp"
00046 #include "GlobiPack_ArmijoPolyInterpLineSearch.hpp"
00047 #include "Teuchos_Tuple.hpp"
00048 
00049 #include "meritFuncsHelpers.hpp"
00050 
00051 #include "Teuchos_UnitTestHarness.hpp"
00052 
00053 
00054 namespace {
00055 
00056 
00057 //
00058 // Helper code and declarations
00059 //
00060 
00061 
00062 using GlobiPack::TestLagrPolyMeritFunc1D;
00063 using GlobiPack::testLagrPolyMeritFunc1D;
00064 using GlobiPack::ArmijoPolyInterpLineSearch;
00065 using GlobiPack::armijoQuadraticLineSearch;
00066 using GlobiPack::computeValue;
00067 using Teuchos::as;
00068 using Teuchos::inOutArg;
00069 using Teuchos::outArg;
00070 using Teuchos::null;
00071 using Teuchos::RCP;
00072 using Teuchos::rcpFromRef;
00073 using Teuchos::Array;
00074 using Teuchos::tuple;
00075 using Teuchos::ParameterList;
00076 using Teuchos::parameterList;
00077 
00078 
00079 double g_tol = Teuchos::ScalarTraits<double>::eps()*100.0;
00080 
00081 
00082 TEUCHOS_STATIC_SETUP()
00083 {
00084   Teuchos::UnitTestRepository::getCLP().setOption(
00085     "tol", &g_tol, "Floating point tolerance" );
00086 }
00087 
00088 
00089 //
00090 // Unit tests for ArmijoPolyInterpLineSearch
00091 //
00092 
00093 
00094 //
00095 // Check that internal default parameters are set correctly
00096 //
00097 
00098 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArmijoPolyInterpLineSearch, defaultParams, Scalar )
00099 {
00100   typedef Teuchos::ScalarTraits<Scalar> ST;
00101   typedef typename ST::magnitudeType ScalarMag;
00102   namespace AQLSU = GlobiPack::ArmijoPolyInterpLineSearchUtils;
00103   RCP<ArmijoPolyInterpLineSearch<Scalar> > linesearch =
00104     armijoQuadraticLineSearch<Scalar>();
00105   TEST_EQUALITY(linesearch->eta(), as<ScalarMag>(AQLSU::eta_default));
00106   TEST_EQUALITY(linesearch->minFrac(), as<ScalarMag>(AQLSU::minFrac_default));
00107   TEST_EQUALITY(linesearch->maxFrac(), as<ScalarMag>(AQLSU::maxFrac_default));
00108   TEST_EQUALITY(linesearch->doMaxIters(), as<ScalarMag>(AQLSU::doMaxIters_default));
00109   TEST_EQUALITY(linesearch->maxIters(), as<ScalarMag>(AQLSU::maxIters_default));
00110 }
00111 
00112 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( ArmijoPolyInterpLineSearch, defaultParams )
00113 
00114 
00115 //
00116 // Check that parameter list is parsed correctly
00117 //
00118 
00119 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArmijoPolyInterpLineSearch, parseParams, Scalar )
00120 {
00121   typedef Teuchos::ScalarTraits<Scalar> ST;
00122   namespace AQLSU = GlobiPack::ArmijoPolyInterpLineSearchUtils;
00123   ECHO(RCP<ArmijoPolyInterpLineSearch<Scalar> > linesearch =
00124     armijoQuadraticLineSearch<Scalar>());
00125   const double eta = 0.99999;
00126   const double minFrac = 4.0;
00127   const double maxFrac = 5.0;
00128   const int minIters = 5;
00129   const int maxIters = 100;
00130   const bool doMaxIters = true;
00131   ECHO(const RCP<ParameterList> pl = parameterList());
00132   ECHO(pl->set("Armijo Slope Fraction", eta));
00133   ECHO(pl->set("Min Backtrack Fraction", minFrac));
00134   ECHO(pl->set("Max Backtrack Fraction", maxFrac));
00135   ECHO(pl->set("Min Num Iterations", minIters));
00136   ECHO(pl->set("Max Num Iterations", maxIters));
00137   ECHO(pl->set("Do Max Iterations", doMaxIters));
00138   ECHO(linesearch->setParameterList(pl));
00139   const Scalar tol = ST::eps();
00140   TEST_FLOATING_EQUALITY(linesearch->eta(), as<Scalar>(eta), tol);
00141   TEST_FLOATING_EQUALITY(linesearch->minFrac(), as<Scalar>(minFrac), tol);
00142   TEST_FLOATING_EQUALITY(linesearch->maxFrac(), as<Scalar>(maxFrac), tol);
00143   TEST_EQUALITY(linesearch->minIters(), minIters);
00144   TEST_EQUALITY(linesearch->maxIters(), maxIters);
00145   TEST_EQUALITY(linesearch->doMaxIters(), doMaxIters);
00146 }
00147 
00148 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( ArmijoPolyInterpLineSearch, parseParams )
00149 
00150 
00151 //
00152 // Check that the ArmijoPolyInterpLineSearch object validates its parameters
00153 // and their values correctly.
00154 //
00155 
00156 /*
00157 
00158 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArmijoPolyInterpLineSearch, validateParams, Scalar )
00159 {
00160   TEUCHOS_TEST_FOR_EXCEPT(true);
00161 }
00162 
00163 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( ArmijoPolyInterpLineSearch, validateParams )
00164 
00165 */
00166 
00167 
00168 //
00169 // Check that object can exactly interplate a quadratic merit function at the
00170 // very first iteration
00171 //
00172 
00173 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArmijoPolyInterpLineSearch, quadExact, Scalar )
00174 {
00175 
00176   typedef Teuchos::ScalarTraits<Scalar> ST;
00177   typedef typename ST::magnitudeType ScalarMag;
00178 
00179   const RCP<TestLagrPolyMeritFunc1D<Scalar> > phi = quadPhi<Scalar>();
00180   
00181   RCP<ArmijoPolyInterpLineSearch<Scalar> > linesearch =
00182     armijoQuadraticLineSearch<Scalar>();
00183 
00184   linesearch->setOStream(rcpFromRef(out));
00185  
00186   const PointEval1D<Scalar> point_k = computePoint(*phi, ST::zero(), true, true);
00187   PointEval1D<Scalar> point_kp1 = computePoint(*phi, as<Scalar>(5.0));
00188 
00189   int numIters = -1;
00190   const bool linesearchResult = linesearch->doLineSearch(
00191     *phi, point_k, inOutArg(point_kp1), outArg(numIters) );
00192 
00193   TEST_ASSERT(linesearchResult);
00194   TEST_EQUALITY(numIters, 1);
00195   TEST_FLOATING_EQUALITY(point_kp1.alpha, as<Scalar>(2.0), g_tol);
00196   TEST_FLOATING_EQUALITY(point_kp1.phi, as<ScalarMag>(3.0), g_tol);
00197 
00198 }
00199 
00200 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( ArmijoPolyInterpLineSearch, quadExact )
00201 
00202 
00203 //
00204 // Check that object will accept the inital point passed in without doing any
00205 // evaluations.
00206 //
00207 
00208 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArmijoPolyInterpLineSearch, noEval, Scalar )
00209 {
00210 
00211   typedef Teuchos::ScalarTraits<Scalar> ST;
00212   typedef typename ST::magnitudeType ScalarMag;
00213 
00214   const RCP<TestLagrPolyMeritFunc1D<Scalar> > phi = quadPhi<Scalar>();
00215   
00216   RCP<ArmijoPolyInterpLineSearch<Scalar> > linesearch =
00217     armijoQuadraticLineSearch<Scalar>();
00218 
00219   linesearch->setOStream(rcpFromRef(out));
00220 
00221   const Scalar alpha_k_init = as<Scalar>(2.1);
00222   const PointEval1D<Scalar> point_k = computePoint(*phi, ST::zero(), true, true);
00223   PointEval1D<Scalar> point_kp1 = computePoint(*phi, alpha_k_init);
00224 
00225   int numIters = -1;
00226   const bool linesearchResult = linesearch->doLineSearch(
00227     *phi, point_k, inOutArg(point_kp1), outArg(numIters) );
00228 
00229   TEST_ASSERT(linesearchResult);
00230   TEST_EQUALITY(numIters, 0);
00231   TEST_FLOATING_EQUALITY(point_kp1.alpha, alpha_k_init, g_tol);
00232   TEST_FLOATING_EQUALITY(point_kp1.phi, computeValue(*phi, alpha_k_init), g_tol);
00233 
00234 }
00235 
00236 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( ArmijoPolyInterpLineSearch, noEval )
00237 
00238 
00239 //
00240 // Check that object will force a minimum number of iterations if asked.
00241 //
00242 
00243 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ArmijoPolyInterpLineSearch, minIters, Scalar )
00244 {
00245 
00246   typedef Teuchos::ScalarTraits<Scalar> ST;
00247   typedef typename ST::magnitudeType ScalarMag;
00248 
00249   const RCP<TestLagrPolyMeritFunc1D<Scalar> > phi = quadPhi<Scalar>();
00250   
00251   RCP<ArmijoPolyInterpLineSearch<Scalar> > linesearch =
00252     armijoQuadraticLineSearch<Scalar>();
00253 
00254   const RCP<ParameterList> pl = parameterList();
00255   pl->set("Max Backtrack Fraction", 1.0);
00256   pl->set("Min Num Iterations", 1);
00257   linesearch->setParameterList(pl);
00258 
00259   linesearch->setOStream(rcpFromRef(out));
00260 
00261   const Scalar alpha_k_init = as<Scalar>(2.1);
00262   const PointEval1D<Scalar> point_k = computePoint(*phi, ST::zero(), true, true);
00263   PointEval1D<Scalar> point_kp1 = computePoint(*phi, alpha_k_init);
00264 
00265   int numIters = -1;
00266   const bool linesearchResult = linesearch->doLineSearch(
00267     *phi, point_k, inOutArg(point_kp1), outArg(numIters) );
00268 
00269   TEST_ASSERT(linesearchResult);
00270   TEST_EQUALITY(numIters, 1);
00271   TEST_FLOATING_EQUALITY(point_kp1.alpha, as<Scalar>(2.0), g_tol);
00272   TEST_FLOATING_EQUALITY(point_kp1.phi, as<ScalarMag>(3.0), g_tol);
00273 
00274 }
00275 
00276 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( ArmijoPolyInterpLineSearch, minIters )
00277 
00278 
00279 //
00280 // ToDo:
00281 //
00282 // (*) Check that the object performs min bracketing correctly.
00283 //
00284 // (*) Check that the object performs max bracketing correctly.
00285 //
00286 // (*) Check that the ArmijoPolyInterpLineSearch object deals with NaN returns
00287 // currectly.
00288 //
00289 // (*) Check that the ArmijoPolyInterpLineSearch object will throw the right
00290 // exception if a positive initial derivative is presented.
00291 //
00292 // (*) Check that the ArmijoPolyInterpLineSearch object will deal with line search
00293 // failure correctly.
00294 //
00295 // (*) Check that the ArmijoPolyInterpLineSearch object deals with invalid input
00296 // correctly (i.e. check preconditions and throws).
00297 //
00298 
00299 
00300 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends