GlobiPack Package Browser (Single Doxygen Collection)  Version of the Day
GlobiPack_Brents1DMinimization_def.hpp
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 #ifndef GLOBIPACK_BRENTS_1D_MINIMIZATION_DEF_HPP
45 #define GLOBIPACK_BRENTS_1D_MINIMIZATION_DEF_HPP
46 
47 
50 
51 
52 namespace GlobiPack {
53 
54 
55 // Constructor/Initializers/Accessors
56 
57 
58 template<typename Scalar>
60  :rel_tol_(Brents1DMinimizationUtils::rel_tol_default),
61  bracket_tol_(Brents1DMinimizationUtils::bracket_tol_default),
62  max_iters_(Brents1DMinimizationUtils::max_iters_default)
63 {}
64 
65 
66 // Overridden from ParameterListAcceptor (simple forwarding functions)
67 
68 
69 template<typename Scalar>
71 {
72  typedef ScalarTraits<Scalar> ST;
73  namespace BMU = Brents1DMinimizationUtils;
74  using Teuchos::getParameter;
75  paramList->validateParametersAndSetDefaults(*this->getValidParameters());
76  rel_tol_ = getParameter<double>(*paramList, BMU::rel_tol_name);
77  bracket_tol_ = getParameter<double>(*paramList, BMU::bracket_tol_name);
78  max_iters_ = getParameter<int>(*paramList, BMU::max_iters_name);
79  TEUCHOS_ASSERT_INEQUALITY( rel_tol_, >, ST::zero() );
80  TEUCHOS_ASSERT_INEQUALITY( bracket_tol_, >, ST::zero() );
81  TEUCHOS_ASSERT_INEQUALITY( max_iters_, >=, 0 );
82  setMyParamList(paramList);
83 }
84 
85 
86 template<typename Scalar>
88 {
89  namespace BMU = Brents1DMinimizationUtils;
90  static RCP<const ParameterList> validPL;
91  if (is_null(validPL)) {
97  validPL = pl;
98  }
99  return validPL;
100 }
101 
102 
103 // Bracket
104 
105 
106 template<typename Scalar>
108  const MeritFunc1DBase<Scalar> &phi,
109  const PointEval1D<Scalar> &pointLower,
110  const Ptr<PointEval1D<Scalar> > &pointMiddle,
111  const PointEval1D<Scalar> &pointUpper,
112  const Ptr<int> &numIters
113  ) const
114 {
115 
116  using Teuchos::as;
118  typedef Teuchos::TabularOutputter TO;
119  typedef ScalarTraits<Scalar> ST;
120  using Teuchos::OSTab;
121  typedef PointEval1D<Scalar> PE1D;
122  using std::min;
123  using std::max;
124 
125 #ifdef TEUCHOS_DEBUG
126  TEUCHOS_TEST_FOR_EXCEPT(is_null(pointMiddle));
127  TEUCHOS_ASSERT_INEQUALITY(pointLower.alpha, <, pointMiddle->alpha);
128  TEUCHOS_ASSERT_INEQUALITY(pointMiddle->alpha, <, pointUpper.alpha);
129  TEUCHOS_ASSERT_INEQUALITY(pointLower.phi, !=, PE1D::valNotGiven());
130  TEUCHOS_ASSERT_INEQUALITY(pointMiddle->phi, !=, PE1D::valNotGiven());
131  TEUCHOS_ASSERT_INEQUALITY(pointUpper.phi, !=, PE1D::valNotGiven());
132 #endif
133 
134  const RCP<Teuchos::FancyOStream> out = this->getOStream();
135 
136  *out << "\nStarting Brent's 1D minimization algorithm ...\n\n";
137 
138  TabularOutputter tblout(out);
139 
140  tblout.pushFieldSpec("itr", TO::INT);
141  tblout.pushFieldSpec("alpha_a", TO::DOUBLE);
142  tblout.pushFieldSpec("alpha_min", TO::DOUBLE);
143  tblout.pushFieldSpec("alpha_b", TO::DOUBLE);
144  tblout.pushFieldSpec("phi(alpha_min)", TO::DOUBLE);
145  tblout.pushFieldSpec("alpha_b - alpha_a", TO::DOUBLE);
146  tblout.pushFieldSpec("alpha_min - alpha_avg", TO::DOUBLE);
147  tblout.pushFieldSpec("tol", TO::DOUBLE);
148 
149  tblout.outputHeader();
150 
151  const Scalar INV_GOLD2=0.3819660112501051518; // (1/golden-ratio)^2
152  const Scalar TINY = ST::squareroot(ST::eps());
153 
154  const Scalar alpha_l = pointLower.alpha, phi_l = pointLower.phi;
155  Scalar &alpha_m = pointMiddle->alpha, &phi_m = pointMiddle->phi;
156  const Scalar alpha_u = pointUpper.alpha, phi_u = pointUpper.phi;
157 
158  Scalar d = ST::nan();
159  Scalar e = ST::nan();
160  Scalar u = ST::nan();
161 
162  Scalar phi_w = min(phi_l, phi_u);
163 
164  Scalar alpha_v = ST::nan();
165  Scalar alpha_w = ST::nan();
166  Scalar phi_v = ST::nan();
167 
168  if (phi_w == phi_l){
169  alpha_w = alpha_l;
170  alpha_v = alpha_u;
171  phi_v = phi_u;
172  }
173  else {
174  alpha_w = alpha_u;
175  alpha_v = alpha_l;
176  phi_v = phi_l;
177  }
178 
179  Scalar alpha_min = alpha_m;
180  Scalar phi_min = phi_m;
181  Scalar alpha_a = alpha_l;
182  Scalar alpha_b = alpha_u;
183 
184  bool foundMin = false;
185 
186  int iteration = 0;
187 
188  for ( ; iteration <= max_iters_; ++iteration) {
189 
190  if (iteration < 2)
191  e = 2.0 * (alpha_b - alpha_a);
192 
193  const Scalar alpha_avg = 0.5 *(alpha_a + alpha_b);
194  const Scalar tol1 = rel_tol_ * ST::magnitude(alpha_min) + TINY;
195  const Scalar tol2 = 2.0 * tol1;
196 
197  const Scalar step_diff = alpha_min - alpha_avg;
198  const Scalar step_diff_tol = (tol2 + bracket_tol_ * (alpha_b - alpha_a));
199 
200  // 2009/02/11: rabartl: Above, I changed from (tol2-0.5*(alpha_b-alpha_a)) which is
201  // actually in Brent's netlib code! This gives a negative tolerence when
202  // the solution alpha_min is near a minimum so you will max out the iters because
203  // a possitive number can never be smaller than a negative number. The
204  // above convergence criteria makes sense to me.
205 
206  tblout.outputField(iteration);
207  tblout.outputField(alpha_a);
208  tblout.outputField(alpha_min);
209  tblout.outputField(alpha_b);
210  tblout.outputField(phi_min);
211  tblout.outputField(alpha_b - alpha_a);
212  tblout.outputField(step_diff);
213  tblout.outputField(step_diff_tol);
214  tblout.nextRow();
215 
216  // If the difference between current point and the middle of the shrinking
217  // interval [alpha_a, alpha_b] is relatively small, then terminate the
218  // algorithm. Also, terminate the algorithm if this difference is small
219  // relative to the size of alpha. Does this make sense? However, don't
220  // terminate on the very first iteration because we have to take at least
221  // one step.
222  if (
223  ST::magnitude(step_diff) <= step_diff_tol
224  && iteration > 0
225  )
226  {
227  foundMin = true;
228  break;
229  }
230  // 2009/02/11: rabartl: Above, I added the iteration > 0 condition because
231  // the original version that I was given would terminate on the first
232  // first iteration if the initial guess for alpha happened to be too close
233  // to the midpoint of the bracketing interval! Is that crazy or what!
234 
235  if (ST::magnitude(e) > tol1 || iteration < 2) {
236 
237  const Scalar r = (alpha_min - alpha_w) * (phi_min - phi_v);
238  Scalar q = (alpha_min - alpha_v) * (phi_min - phi_w);
239  Scalar p = (alpha_min - alpha_v) * q - (alpha_min - alpha_w) * r;
240  q = 2.0 * (q - r);
241  if (q > ST::zero())
242  p = -p;
243  q = ST::magnitude(q);
244  const Scalar etemp = e;
245  e = d;
246 
247  if ( ST::magnitude(p) >= ST::magnitude(0.5 * q * etemp)
248  || p <= q * (alpha_a - alpha_min)
249  || p >= q * (alpha_b - alpha_min)
250  )
251  {
252  e = (alpha_min >= alpha_avg ? alpha_a - alpha_min : alpha_b - alpha_min);
253  d = INV_GOLD2 * e;
254  }
255  else {
256  d = p/q;
257  u = alpha_min + d;
258  if (u - alpha_a < tol2 || alpha_b - u < tol2)
259  // sign(tol1,alpha_avg-alpha_min)
260  d = ( alpha_avg - alpha_min > ST::zero()
261  ? ST::magnitude(tol1)
262  : -ST::magnitude(tol1) );
263  }
264 
265  }
266  else {
267 
268  e = (alpha_min >= alpha_avg ? alpha_a - alpha_min : alpha_b - alpha_min);
269  d = INV_GOLD2 * e;
270 
271  }
272 
273  u = ( ST::magnitude(d) >= tol1
274  ? alpha_min + d
275  : alpha_min + (d >= 0 ? ST::magnitude(tol1) : -ST::magnitude(tol1))
276  );
277 
278  const Scalar phi_eval_u = computeValue<Scalar>(phi, u);
279 
280  if (phi_eval_u <= phi_min) {
281 
282  if (u >= alpha_min)
283  alpha_a = alpha_min;
284  else
285  alpha_b = alpha_min;
286 
287  alpha_v = alpha_w;
288  phi_v = phi_w;
289  alpha_w = alpha_min;
290  phi_w = phi_min;
291  alpha_min = u;
292  phi_min = phi_eval_u;
293 
294  }
295  else {
296 
297  if (u < alpha_min)
298  alpha_a = u;
299  else
300  alpha_b = u;
301 
302  if (phi_eval_u <= phi_w || alpha_w == alpha_min) {
303  alpha_v = alpha_w;
304  phi_v = phi_w;
305  alpha_w = u;
306  phi_w = phi_eval_u;
307  }
308  else if (phi_eval_u <= phi_v || alpha_v == alpha_min || alpha_v == alpha_w) {
309  alpha_v = u;
310  phi_v = phi_eval_u;
311  }
312 
313  }
314  }
315 
316  alpha_m = alpha_min;
317  phi_m = phi_min;
318  if (!is_null(numIters))
319  *numIters = iteration;
320 
321  if (foundMin) {
322  *out <<"\nFound the minimum alpha="<<alpha_m<<", phi(alpha)="<<phi_m<<"\n";
323  }
324  else {
325  *out <<"\nExceeded maximum number of iterations!\n";
326  }
327 
328  *out << "\n";
329 
330  return foundMin;
331 
332 }
333 
334 
335 } // namespace GlobiPack
336 
337 
338 #endif // GLOBIPACK_BRENTS_1D_MINIMIZATION_DEF_HPP
Scalar phi
The value of the merit function phi(alpha).
bool is_null(const boost::shared_ptr< T > &p)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_ASSERT_INEQUALITY(val1, comp, val2)
bool approxMinimize(const MeritFunc1DBase< Scalar > &phi, const PointEval1D< Scalar > &pointLower, const Ptr< PointEval1D< Scalar > > &pointMiddle, const PointEval1D< Scalar > &pointUpper, const Ptr< int > &numIters=Teuchos::null) const
Approximatly mimimize a 1D function.
Brents1DMinimization()
Construct with default parameters.
basic_OSTab< char > OSTab
RCP< const ParameterList > getValidParameters() const
Scalar alpha
The value of the unknown alpha.
Represents the evaluation point of the merit function phi(alpha) and/or is derivative Dphi(alpha)...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setParameterList(RCP< ParameterList > const &paramList)
void setMyParamList(const RCP< ParameterList > &paramList)
TypeTo as(const TypeFrom &t)
Base class for 1D merit fucntions used in globalization methods.
virtual RCP< FancyOStream > getOStream() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)