GlobiPack Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BrentsLineSearch_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_TestLagrPolyMeritFunc1D.hpp"
46 #include "GlobiPack_BrentsLineSearch.hpp"
47 #include "Teuchos_Tuple.hpp"
48 
49 #include "meritFuncsHelpers.hpp"
50 
52 
53 
54 namespace {
55 
56 
57 //
58 // Helper code and declarations
59 //
60 
61 
63 using GlobiPack::brentsLineSearch;
64 using GlobiPack::computeValue;
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 double g_tol_scale = 100.0;
78 
79 
81 {
83  "tol", &g_tol_scale, "Floating point tolerance scaling of eps." );
84 }
85 
86 
87 //
88 // Unit tests for BrentsLineSearch
89 //
90 
91 
92 //
93 // Check that object can exactly interplate a quadratic merit function. This
94 // takes more than one iteration because of the golden search stuff.
95 //
96 
97 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( BrentsLineSearch, quadExact, Scalar )
98 {
99 
101  typedef typename ST::magnitudeType ScalarMag;
102 
103  const RCP<TestLagrPolyMeritFunc1D<Scalar> > phi = quadPhi<Scalar>();
104 
105  RCP<BrentsLineSearch<Scalar> > linesearch = brentsLineSearch<Scalar>();
106 
107  linesearch->setOStream(rcpFromRef(out));
108 
109  const PointEval1D<Scalar> point_k = computePoint(*phi, ST::zero());
110  PointEval1D<Scalar> point_kp1 = computePoint(*phi, as<Scalar>(8.0));
111 
112  int numIters = -1;
113  const bool linesearchResult = linesearch->doLineSearch(
114  *phi, point_k, inOutArg(point_kp1), outArg(numIters) );
115 
116  TEST_ASSERT(linesearchResult);
117  TEST_FLOATING_EQUALITY(point_kp1.alpha, as<Scalar>(2.0),
118  as<Scalar>(g_tol_scale*ST::squareroot(ST::eps())));
119  TEST_FLOATING_EQUALITY(point_kp1.phi, as<ScalarMag>(3.0),
120  as<Scalar>(g_tol_scale)*ST::eps());
121 
122 }
123 
124 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( BrentsLineSearch, quadExact )
125 
126 
127 //
128 // Check that object can approximately mimimize a cubic function.
129 //
130 
131 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( BrentsLineSearch, cubicApprox, Scalar )
132 {
133 
135  typedef typename ST::magnitudeType ScalarMag;
136 
137  const RCP<TestLagrPolyMeritFunc1D<Scalar> > phi = quadPhi<Scalar>();
138 
139  RCP<BrentsLineSearch<Scalar> > linesearch = brentsLineSearch<Scalar>();
140 
141  const RCP<ParameterList> pl = parameterList();
142  pl->sublist("Minimize").set("Relative Tol", as<double>(g_tol_scale*ST::eps()));
143  pl->sublist("Minimize").set("Bracket Tol", as<double>(ST::eps()));
144  linesearch->setParameterList(pl);
145 
146  linesearch->setOStream(rcpFromRef(out));
147 
148  const PointEval1D<Scalar> point_k = computePoint(*phi, ST::zero());
149  PointEval1D<Scalar> point_kp1 = computePoint(*phi, as<Scalar>(8.0));
150 
151  int numIters = -1;
152  const bool linesearchResult = linesearch->doLineSearch(
153  *phi, point_k, inOutArg(point_kp1), outArg(numIters) );
154 
155  TEST_ASSERT(linesearchResult);
156  TEST_FLOATING_EQUALITY(point_kp1.alpha, as<Scalar>(2.0),
157  as<Scalar>(g_tol_scale*ST::squareroot(ST::eps())));
158  TEST_FLOATING_EQUALITY(point_kp1.phi, as<ScalarMag>(3.0),
159  as<Scalar>(g_tol_scale)*ST::eps());
160 
161 }
162 
163 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES( BrentsLineSearch, cubicApprox )
164 
165 
166 } // namespace
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(Teuchos_Conditions, NumberConditionSerialization, T)
#define TEST_ASSERT(v1)
static CommandLineProcessor & getCLP()
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.
Linesearch subclass implementing a function-value-only approximate minimization algorithm using brack...
TypeTo as(const TypeFrom &t)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT_REAL_SCALAR_TYPES(TEST_GROUP, TEST_NAME)
TEUCHOS_STATIC_SETUP()