All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
anneal.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, 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 
40 #ifndef DOXYGEN_NO_O2NS
41 namespace o2scl {
42 #endif
43 
44  /** \brief Simulated annealing base
45 
46  The seed of the generator is not fixed initially by calls to
47  mmin(), so if successive calls should reproduce the same
48  results, then the random seed should be set by the user before
49  each call.
50 
51  For the algorithms here, it is important that all of the inputs
52  x[i] to the function are scaled similarly relative
53  to the temperature. For example, if the inputs x[i] are all
54  of order 1, one might consider a temperature schedule which begins
55  with \f$ T=1 \f$ .
56 
57  The number of iterations at each temperature is controlled by
58  multi_min::ntrial which defaults to 100.
59 
60  */
61  template<class func_t=multi_funct11,
63  class rng_t=int, class rng_dist_t=rng_gsl >
64  class anneal_base : public mmin_base<func_t,func_t,vec_t> {
65 
66 #ifdef O2SCL_NEVER_DEFINED
67  }
68  {
69 #endif
70 
71  public:
72 
73  anneal_base() {
74  this->ntrial=100;
75  }
76 
77  virtual ~anneal_base() {}
78 
79  /** \brief Calculate the minimum \c fmin of \c func w.r.t the
80  array \c x of size \c nvar.
81  */
82  virtual int mmin(size_t nvar, vec_t &x, double &fmin,
83  func_t &func)=0;
84 
85  /** \brief Print out iteration information.
86 
87  Depending on the value of the variable verbose, this prints out
88  the iteration information. If verbose=0, then no information is
89  printed, while if verbose>1, then after each iteration, the
90  present values of x and y are output to std::cout along with the
91  iteration number. If verbose>=2 then each iteration waits for a
92  character.
93  */
94  virtual int print_iter(size_t nv, vec_t &x, double y, int iter,
95  double tptr, std::string comment)
96  {
97  if (this->verbose<=0) return 0;
98 
99  size_t i;
100  char ch;
101 
102  (*this->outs) << comment << " Iteration: " << iter << std::endl;
103  std::cout << "x: ";
104  for(i=0;i<nv;i++) std::cout << x[i] << " ";
105  std::cout << std::endl;
106  (*this->outs) << "y: " << y << " Tptr: " << tptr << std::endl;
107  if (this->verbose>1) {
108  (*this->outs) << "Press a key and type enter to continue. ";
109  (*this->ins) >> ch;
110  }
111 
112  return 0;
113  }
114 
115  /// The default random number distribution
116  rng_dist_t rng_dist;
117 
118  /// The default random number generator
119  rng_t rng;
120 
121  /// Return string denoting type, \c "anneal_base".
122  virtual const char *type() { return "anneal_base"; }
123 
124  /** \brief Copy constructor
125  */
129 
130  this->rng=ab.rng;
131  this->rng_dist=ab.rng_dist;
132 
133  }
134 
135  /** \brief Copy constructor from operator=
136  */
139 
140  if (this != &ab) {
142  this->rng=ab.rng;
143  this->rng_dist=ab.rng_dist;
144  }
145  return *this;
146  }
147 
148  };
149 
150 #ifndef DOXYGEN_NO_O2NS
151 }
152 #endif
153 
154 #endif
155 
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:291
virtual const char * type()
Return string denoting type, "anneal_base".
Definition: anneal.h:122
rng_dist_t rng_dist
The default random number distribution.
Definition: anneal.h:116
Multidimensional minimization [abstract base].
Definition: mmin.h:163
Simulated annealing base.
Definition: anneal.h:64
rng_t rng
The default random number generator.
Definition: anneal.h:119
Random number generator (GSL)
Definition: rng_gsl.h:51
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct11
Multi-dimensional function typedef.
Definition: multi_funct.h:45
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:94

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..