GlobiPack Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GoldenQuadInterpBracket_UnitTests.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // GlobiPack: Collection of Scalar 1D globalizaton utilities
6 // Copyright (2009) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 
45 #include "GlobiPack_GoldenQuadInterpBracket.hpp"
46 #include "GlobiPack_TestLagrPolyMeritFunc1D.hpp"
47 #include "Teuchos_Tuple.hpp"
49 
50 
51 namespace {
52 
53 
54 //
55 // Helper code and declarations
56 //
57 
58 
62 using GlobiPack::goldenQuadInterpBracket;
64 using GlobiPack::computePoint;
65 using Teuchos::as;
66 using Teuchos::inOutArg;
67 using Teuchos::outArg;
68 using Teuchos::null;
69 using Teuchos::RCP;
70 using Teuchos::rcpFromRef;
71 using Teuchos::Array;
72 using Teuchos::tuple;
74 using Teuchos::parameterList;
75 
76 
77 template<class Scalar>
78 inline Scalar sqr(const Scalar &x) { return x*x; }
79 
80 
81 // Set up a quadratic merit function with minimizer at alpha=2.0, phi=3.0;
82 template<class Scalar>
83 const RCP<TestLagrPolyMeritFunc1D<Scalar> > quadPhi()
84 {
86  typedef typename ST::magnitudeType ScalarMag;
87  Array<Scalar> alphaPoints = tuple<Scalar>(0.0, 2.0, 4.0);
88  Array<ScalarMag> phiPoints = tuple<ScalarMag>(6.0, 3.0, 6.0);
89  return testLagrPolyMeritFunc1D<Scalar>(alphaPoints, phiPoints);
90 }
91 
92 
93 double g_tol = Teuchos::ScalarTraits<double>::eps()*100.0;
94 
95 
97 {
99  "tol", &g_tol, "Floating point tolerance" );
100 }
101 
102 
103 //
104 // Unit tests for GoldenQuadInterpBracket
105 //
106 
107 
108 //
109 // Check that object can exactly interplate a quadratic merit function at the
110 // very first iteration
111 //
112 
113 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( GoldenQuadInterpBracket, bracket, Scalar )
114 {
115 
117  using std::min;
118 
119  const RCP<TestLagrPolyMeritFunc1D<Scalar> > phi = quadPhi<Scalar>();
120 
121  RCP<GoldenQuadInterpBracket<Scalar> > bracket = goldenQuadInterpBracket<Scalar>();
122 
123  bracket->setOStream(rcpFromRef(out));
124 
125  const Array<Scalar> alpha =
126  tuple<Scalar>(
127  1e-14, 1e-10, 1e-7, 1e-4, 0.1, 1.0, 1.1, 1.5,
128  1.9, 2.0, 2.1, 4.0, 8.0, 30.0);
129 
130  for (int i = 0; i < as<int>(alpha.size()); ++i ) {
131 
132  PointEval1D<Scalar> p_l = computePoint(*phi, ST::zero());
133  PointEval1D<Scalar> p_m = computePoint(*phi, alpha[i]);
134  PointEval1D<Scalar> p_u;
135  int numIters = -1;
136 
137  if (alpha[i] > ST::eps()) {
138 
139  const bool bracketResult = bracket->bracketMinimum(
140  *phi, inOutArg(p_l), inOutArg(p_m), outArg(p_u), outArg(numIters) );
141 
142  TEST_ASSERT(bracketResult);
143  TEST_COMPARE(p_l.alpha, <, p_m.alpha);
144  TEST_COMPARE(p_m.alpha, <, p_u.alpha);
145  TEST_COMPARE(p_l.phi, >, p_m.phi);
146  TEST_COMPARE(p_m.phi, <, p_u.phi);
147 
148  }
149  else {
150 
151  // If alpha[i] < eps, then there will not be enough accuracy in the
152  // merit function to allow for a successfull line search.
153 
154  }
155 
156  }
157 
158 }
159 
160 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( GoldenQuadInterpBracket, bracket )
161 
162 
163 } // namespace
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Teuchos_Conditions, NumberConditionSerialization, T)
const RCP< TestLagrPolyMeritFunc1D< Scalar > > testLagrPolyMeritFunc1D(const ArrayView< const Scalar > &alpha, const ArrayView< const Scalar > &phi)
static magnitudeType eps()
Simple concrete class that implements a 1D algorithm to bracket the minimum of a 1D merit function...
static CommandLineProcessor & getCLP()
Represents the evaluation point of the merit function phi(alpha) and/or is derivative Dphi(alpha)...
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
PointEval1D< Scalar > computePoint(const MeritFunc1DBase< Scalar > &phi, const Scalar &alpha, const bool compute_phi=true, const bool compute_Dphi=false)
Compute a point as an object.
TypeTo as(const TypeFrom &t)
TEST_ASSERT(castedDep1->getValuesAndValidators().size()==2)
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES(TEST_GROUP, TEST_NAME)
Lagrange Polynomial Merit Function used in testing.
TEUCHOS_STATIC_SETUP()
#define TEST_COMPARE(v1, comp, v2)