mmin.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_MULTI_MIN_H
24 #define O2SCL_MULTI_MIN_H
25 
26 /** \file mmin.h
27  \brief File defining \ref o2scl::mmin_base, \ref o2scl::grad_funct11,
28  \ref o2scl::gradient, and \ref o2scl::gradient_gsl
29 */
30 
31 #include <o2scl/multi_funct.h>
32 #include <o2scl/mm_funct.h>
33 #include <o2scl/string_conv.h>
34 
35 #ifndef DOXYGEN_NO_O2NS
36 namespace o2scl {
37 #endif
38 
39  /// Array of multi-dimensional functions typedef
40  typedef std::function<int(size_t,boost::numeric::ublas::vector<double> &,
43 
44  /** \brief Class for automatically computing gradients [abstract base]
45 
46  Default template arguments
47  - \c func_t - (no default)
48  - \c vec_t - \ref boost::numeric::ublas::vector < double >
49 
50  \future Consider making an exact_grad class for computing exact
51  gradients.
52  */
53  template<class func_t, class vec_t=boost::numeric::ublas::vector<double> >
54  class gradient {
55 
56  public:
57 
58  // (Need to have empty default constructor since we
59  // have private copy constructor)
60  gradient() {}
61 
62  virtual ~gradient() {}
63 
64  /// Set the function to compute the gradient of
65  virtual int set_function(func_t &f) {
66  func=&f;
67  return 0;
68  }
69 
70  /** \brief Compute the gradient \c g at the point \c x
71  */
72  virtual int operator()(size_t nv, vec_t &x, vec_t &g)=0;
73 
74 #ifndef DOXYGEN_INTERNAL
75 
76  protected:
77 
78  /// A pointer to the user-specified function
79  func_t *func;
80 
81  private:
82 
83  gradient(const gradient &);
84  gradient& operator=(const gradient&);
85 
86 #endif
87 
88  };
89 
90  /** \brief Simple automatic computation of gradient by finite
91  differencing
92 
93  \comment
94  \endcomment
95  */
96  template<class func_t, class vec_t> class gradient_gsl :
97  public gradient<func_t,vec_t> {
98 
99  public:
100 
101  gradient_gsl() {
102  epsrel=1.0e-6;
103  epsmin=1.0e-15;
104  }
105 
106  virtual ~gradient_gsl() {}
107 
108  /** \brief The relative stepsize for finite-differencing
109  (default \f$ 10^{-6} \f$ )
110  */
111  double epsrel;
112 
113  /// The minimum stepsize (default \f$ 10^{-15} \f$)
114  double epsmin;
115 
116  /** \brief Compute the gradient \c g at the point \c x
117  */
118  virtual int operator()(size_t nv, vec_t &x, vec_t &g) {
119  double fv1, fv2, h;
120 
121  fv1=(*this->func)(nv,x);
122 
123  for(size_t i=0;i<nv;i++) {
124 
125  h=epsrel*fabs(x[i]);
126  if (fabs(h)<=epsmin) h=epsrel;
127 
128  x[i]+=h;
129  fv2=(*this->func)(nv,x);
130  x[i]-=h;
131  g[i]=(fv2-fv1)/h;
132 
133  }
134 
135  return 0;
136  }
137 
138  };
139 
140  /** \brief Multidimensional minimization [abstract base]
141 
142  <b>The template parameters:</b>
143  The template parameter \c func_t specifies the function to
144  min and should be a class containing a definition
145  \code
146  func_t::operator()(size_t nv, const vec_t &x, double &f);
147  \endcode
148  where \c f is the value of the function at \c x ,
149  where \c x is a array-like class defining \c operator[] of size \c nv.
150  The parameter \c dfunc_t (if used) should provide the gradient with
151  \code
152  func_t::operator()(size_t nv, vec_t &x, vec_t &g);
153  \endcode
154  where \c g is the gradient of the function at \c x.
155 
156  Verbose I/O is sent through \c std::cout and \c std::cin by
157  default, but this can be modified using \ref
158  set_verbose_stream(). Note that this function
159  stores pointers to the user-specified output streams,
160  and these pointers are not copied in child copy
161  constructors.
162  */
163  template<class func_t=multi_funct11, class dfunc_t=func_t,
165 
166 #ifndef DOXYGEN_INTERNAL
167 
168  protected:
169 
170  /// Stream for verbose output
171  std::ostream *outs;
172 
173  /// Stream for verbose input
174  std::istream *ins;
175 
176 #endif
177 
178  public:
179 
180  mmin_base() {
181  verbose=0;
182  ntrial=100;
183  tol_rel=1.0e-4;
184  tol_abs=1.0e-4;
185  last_ntrial=0;
186  err_nonconv=true;
187  outs=&std::cout;
188  ins=&std::cin;
189  }
190 
191  virtual ~mmin_base() {}
192 
193  /// Output control
194  int verbose;
195 
196  /// Maximum number of iterations
197  int ntrial;
198 
199  /// Function value tolerance
200  double tol_rel;
201 
202  /// The independent variable tolerance
203  double tol_abs;
204 
205  /// The number of iterations for in the most recent minimization
207 
208  /// If true, call the error handler if the routine does not "converge"
210 
211  /** \brief Set streams for verbose I/O
212 
213  Note that this function stores pointers to the user-specified
214  output streams, and these pointers are not copied in child copy
215  constructors.
216  */
217  int set_verbose_stream(std::ostream &out, std::istream &in) {
218  outs=&out;
219  ins=&in;
220  return 0;
221  }
222 
223  /** \brief Calculate the minimum \c min of \c func w.r.t. the
224  array \c x of size \c nvar.
225  */
226  virtual int mmin(size_t nvar, vec_t &x, double &fmin,
227  func_t &func)=0;
228 
229  /** \brief Calculate the minimum \c min of \c func
230  w.r.t. the array \c x of size \c nvar with gradient
231  \c dfunc
232  */
233  virtual int mmin_de(size_t nvar, vec_t &x, double &fmin,
234  func_t &func, dfunc_t &dfunc)
235  {
236  return mmin(nvar,x,fmin,func);
237  }
238 
239  /** \brief Print out iteration information.
240 
241  Depending on the value of the variable verbose, this prints out
242  the iteration information. If verbose=0, then no information is
243  printed, while if verbose>1, then after each iteration, the
244  present values of x and y are output to std::cout along with the
245  iteration number. If verbose>=2 then each iteration waits for a
246  character.
247  */
248  template<class vec2_t>
249  int print_iter(size_t nv, vec2_t &x, double y, int iter,
250  double value, double limit, std::string comment)
251  {
252 
253  if (verbose<=0) return 0;
254 
255  int i;
256  char ch;
257 
258  (*outs) << comment << " Iteration: " << iter << std::endl;
259  {
260  (*outs) << "x: " << std::endl;
261  for(i=0;i<((int)nv);i++) (*outs) << x[i] << " ";
262  (*outs) << std::endl;
263  }
264  (*outs) << "y: " << y << " Val: " << value << " Lim: "
265  << limit << std::endl;
266  if (verbose>1) {
267  (*outs) << "Press a key and type enter to continue. ";
268  (*ins) >> ch;
269  }
270 
271  return 0;
272  }
273 
274  /// Return string denoting type ("mmin_base")
275  const char *type() { return "mmin_base"; }
276 
277  /** \brief Copy constructor
278  */
281  this->verbose=mb.verbose;
282  this->ntrial=mb.ntrial;
283  this->tol_rel=mb.tol_rel;
284  this->tol_abs=mb.tol_abs;
285  this->last_ntrial=mb.last_ntrial;
286  this->err_nonconv=mb.err_nonconv;
287  }
288 
289  /** \brief Copy constructor from operator=
290  */
293 
294  if (this != &mb) {
295  this->verbose=mb.verbose;
296  this->ntrial=mb.ntrial;
297  this->tol_rel=mb.tol_rel;
298  this->tol_abs=mb.tol_abs;
299  this->last_ntrial=mb.last_ntrial;
300  this->err_nonconv=mb.err_nonconv;
301  }
302 
303  return *this;
304  }
305 
306  };
307 
308 #ifndef DOXYGEN_NO_O2NS
309 }
310 #endif
311 
312 #endif
313 
Class for automatically computing gradients [abstract base].
Definition: mmin.h:54
double epsmin
The minimum stepsize (default )
Definition: mmin.h:114
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
int set_verbose_stream(std::ostream &out, std::istream &in)
Set streams for verbose I/O.
Definition: mmin.h:217
std::ostream * outs
Stream for verbose output.
Definition: mmin.h:171
const char * type()
Return string denoting type ("mmin_base")
Definition: mmin.h:275
int verbose
Output control.
Definition: mmin.h:194
virtual int mmin_de(size_t nvar, vec_t &x, double &fmin, func_t &func, dfunc_t &dfunc)
Calculate the minimum min of func w.r.t. the array x of size nvar with gradient dfunc.
Definition: mmin.h:233
virtual int set_function(func_t &f)
Set the function to compute the gradient of.
Definition: mmin.h:65
bool err_nonconv
If true, call the error handler if the routine does not "converge".
Definition: mmin.h:209
Multidimensional minimization [abstract base].
Definition: mmin.h:164
int print_iter(size_t nv, vec2_t &x, double y, int iter, double value, double limit, std::string comment)
Print out iteration information.
Definition: mmin.h:249
double epsrel
The relative stepsize for finite-differencing (default )
Definition: mmin.h:111
Simple automatic computation of gradient by finite differencing.
Definition: mmin.h:96
int last_ntrial
The number of iterations for in the most recent minimization.
Definition: mmin.h:206
virtual int operator()(size_t nv, vec_t &x, vec_t &g)
Compute the gradient g at the point x.
Definition: mmin.h:118
func_t * func
A pointer to the user-specified function.
Definition: mmin.h:79
std::function< int(size_t, boost::numeric::ublas::vector< double > &, boost::numeric::ublas::vector< double > &)> grad_funct11
Array of multi-dimensional functions typedef.
Definition: mmin.h:42
double tol_abs
The independent variable tolerance.
Definition: mmin.h:203
double tol_rel
Function value tolerance.
Definition: mmin.h:200
virtual int operator()(size_t nv, vec_t &x, vec_t &g)=0
Compute the gradient g at the point x.
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).