anneal.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_ANNEAL_H
24 #define O2SCL_ANNEAL_H
25 
26 /** \file anneal.h
27  \brief File defining \ref o2scl::anneal_base
28 */
29 
30 #include <iostream>
31 
32 #include <boost/config.hpp>
33 #include <boost/numeric/ublas/vector.hpp>
34 #include <boost/numeric/ublas/matrix.hpp>
35 
36 #include <o2scl/multi_funct.h>
37 #include <o2scl/mmin.h>
38 #include <o2scl/rng_gsl.h>
39 #include <o2scl/prob_dens_func.h>
40 
41 #ifndef DOXYGEN_NO_O2NS
42 namespace o2scl {
43 #endif
44 
45  /** \brief Simulated annealing base
46 
47  The seed of the generator is not fixed initially by calls to
48  mmin(), so if successive calls should reproduce the same
49  results, then the random seed should be set by the user before
50  each call.
51 
52  For the algorithms here, it is important that all of the inputs
53  <tt>x[i]</tt> to the function are scaled similarly relative to
54  the temperature. For example, if the inputs <tt>x[i]</tt> are
55  all of order 1, one might consider a temperature schedule which
56  begins with \f$ T=1 \f$ .
57 
58  The number of iterations at each temperature is controlled by
59  \ref o2scl::mmin_base::ntrial which defaults to 100.
60 
61  \todo I'm having trouble with std::uniform_real_distribution
62  on clang at the moment, so this class uses
63  \ref o2scl::prob_dens_uniform for the moment.
64  */
65  template<class func_t=multi_funct11,
67  class rng_t=rng_gsl> class anneal_base :
68  public mmin_base<func_t,func_t,vec_t> {
69 
70 #ifdef O2SCL_NEVER_DEFINED
71  }
72  {
73 #endif
74 
75  public:
76 
77  anneal_base() : dist(0.0,1.0) {
78  this->ntrial=100;
79  }
80 
81  virtual ~anneal_base() {}
82 
83  /** \brief Calculate the minimum \c fmin of \c func w.r.t the
84  array \c x of size \c nvar.
85  */
86  virtual int mmin(size_t nvar, vec_t &x, double &fmin,
87  func_t &func)=0;
88 
89  /** \brief Print out iteration information.
90 
91  Depending on the value of the variable verbose, this prints out
92  the iteration information. If verbose=0, then no information is
93  printed, while if verbose>1, then after each iteration, the
94  present values of x and y are output to std::cout along with the
95  iteration number. If verbose>=2 then each iteration waits for a
96  character.
97  */
98  virtual int print_iter(size_t nv, vec_t &x, double y, int iter,
99  double tptr, std::string comment)
100  {
101  if (this->verbose<=0) return 0;
102 
103  size_t i;
104  char ch;
105 
106  (*this->outs) << comment << " Iteration: " << iter << std::endl;
107  std::cout << "x: ";
108  for(i=0;i<nv;i++) std::cout << x[i] << " ";
109  std::cout << std::endl;
110  (*this->outs) << "y: " << y << " Tptr: " << tptr << std::endl;
111  if (this->verbose>1) {
112  (*this->outs) << "Press a key and type enter to continue. ";
113  (*this->ins) >> ch;
114  }
115 
116  return 0;
117  }
118 
119  /// The default random number generator
120  rng_t rng;
121 
122  /// The random distribution object
124  //std::uniform_real_distribution<> dist;
125 
126  /// Return string denoting type, \c "anneal_base".
127  virtual const char *type() { return "anneal_base"; }
128 
129  /** \brief Copy constructor
130  */
134 
135  this->rng=ab.rng;
136 
137  }
138 
139  /** \brief Copy constructor from operator=
140  */
143 
144  if (this != &ab) {
146  this->rng=ab.rng;
147  }
148  return *this;
149  }
150 
151  };
152 
153 #ifndef DOXYGEN_NO_O2NS
154 }
155 #endif
156 
157 #endif
158 
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
std::istream * ins
Stream for verbose input.
Definition: mmin.h:174
std::ostream * outs
Stream for verbose output.
Definition: mmin.h:171
mmin_base< func_t, dfunc_t, vec_t > & operator=(const mmin_base< func_t, dfunc_t, vec_t > &mb)
Copy constructor from operator=.
Definition: mmin.h:292
o2scl::prob_dens_uniform dist
The random distribution object.
Definition: anneal.h:123
virtual const char * type()
Return string denoting type, "anneal_base".
Definition: anneal.h:127
virtual int mmin(size_t nvar, vec_t &x, double &fmin, func_t &func)=0
Calculate the minimum fmin of func w.r.t the array x of size nvar.
A uniform one-dimensional probability density over a finite range.
Multidimensional minimization [abstract base].
Definition: mmin.h:164
Simulated annealing base.
Definition: anneal.h:67
rng_t rng
The default random number generator.
Definition: anneal.h:120
Random number generator (GSL)
Definition: rng_gsl.h:55
virtual int print_iter(size_t nv, vec_t &x, double y, int iter, double tptr, std::string comment)
Print out iteration information.
Definition: anneal.h:98
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct11
Multi-dimensional function typedef.
Definition: multi_funct.h:45
int ntrial
Maximum number of iterations.
Definition: mmin.h:197

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).