sim_anneal.h

00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_SIM_ANNEAL_H
00024 #define O2SCL_SIM_ANNEAL_H
00025 
00026 #include <iostream>
00027 #include <o2scl/collection.h>
00028 #include <o2scl/multi_min.h>
00029 #include <o2scl/tptr_schedule.h>
00030 #include <o2scl/rnga.h>
00031 #include <o2scl/gsl_rnga.h>
00032 #include <o2scl/tptr_geoseries.h>
00033 
00034 #ifndef DOXYGENP
00035 namespace o2scl {
00036 #endif
00037 
00038   /** 
00039       \brief Simulated annealing base
00040 
00041       The seed of the generator is not fixed initially by calls to
00042       mmin(), so if successive calls should reproduce the same
00043       results, then the random seed should be set by the user before
00044       each call.
00045       
00046       For the algorithms here, it is important that all of the inputs
00047       x[i] to the function are scaled similarly relative 
00048       to the temperature. For example, if the inputs x[i] are all
00049       of order 1, one might consider a temperature schedule which begins
00050       with \f$ T=1 \f$ . 
00051 
00052       The number of iterations at each temperature is controlled by
00053       minimize::ntrial which defaults to 100.
00054 
00055   */
00056 #ifdef DOXYGENP
00057   template<class param_t, class func_t, class vec_t=ovector_view,
00058     class rng_t=gsl_rnga> class sim_anneal : 
00059     public multi_min<param_t,func_t,dfunc_t,vec_t> 
00060 #else
00061   template<class param_t, class func_t, class vec_t=ovector_view,
00062     class rng_t=gsl_rnga> class sim_anneal : 
00063     public multi_min<param_t,func_t,func_t,vec_t> 
00064 #endif
00065 {
00066 
00067       public:
00068   
00069       sim_anneal() {
00070         tp=&def_schedule;
00071       
00072         this->ntrial=100;
00073       }
00074 
00075       virtual ~sim_anneal() {}
00076 
00077       /** \brief Calculate the minimum \c fmin of \c func w.r.t the 
00078           array \c x of size \c nvar.
00079       */
00080       virtual int mmin(size_t nvar, vec_t &x, double &fmin, param_t &pa,
00081                        func_t &func) 
00082       {
00083         set_err_ret("Empty base function in sim_anneal::mmin()",
00084                     gsl_nobase);
00085       }
00086       
00087       /** \brief Specify the temperature schedule */
00088       int set_tptr_schedule(tptr_schedule<vec_t> &tr) {
00089         tp=&tr;
00090         return 0;
00091       }
00092     
00093       /** 
00094           \brief Print out iteration information.
00095 
00096           Depending on the value of the variable verbose, this prints out
00097           the iteration information. If verbose=0, then no information is
00098           printed, while if verbose>1, then after each iteration, the
00099           present values of x and y are output to std::cout along with the
00100           iteration number. If verbose>=2 then each iteration waits for a
00101           character.  
00102       */
00103       virtual int print_iter(size_t nv, vec_t &x, double y, int iter,
00104                      double tptr, std::string comment) 
00105       {
00106         if (this->verbose<=0) return 0;
00107         
00108         size_t i;
00109         char ch;
00110         
00111         std::cout << comment << " Iteration: " << iter << std::endl;
00112         text_out_file outs(&std::cout,79);
00113         outs.word_out("x:");
00114         for(i=0;i<nv;i++) outs.double_out(x[i]);
00115         outs.end_line();
00116         std::cout << "y: " << y << " Tptr: " << tptr << std::endl;
00117         if (this->verbose>1) {
00118           std::cout << "Press a key and type enter to continue. ";
00119           std::cin >> ch;
00120         }
00121         
00122         return 0;
00123       }
00124       
00125       /// The default random number generator
00126       rng_t def_rng;
00127 
00128       /// Return string denoting type, \c "sim_anneal".
00129       virtual const char *type() { return "sim_anneal"; }
00130 
00131       /// The default temperature schedule
00132       tptr_geoseries<vec_t> def_schedule;
00133 
00134 #ifndef DOXYGEN_INTERNAL
00135 
00136       protected:
00137     
00138       /// Pointer to the temperature annealing schedule
00139       tptr_schedule<vec_t> *tp;
00140 
00141 #endif
00142 
00143     };
00144 
00145 #ifndef DOXYGENP
00146 }
00147 #endif
00148 
00149 #endif
00150 

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.