sim_anneal.h

00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, 2008, 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   template<class param_t, class func_t, class vec_t=ovector_view,
00057     class rng_t=gsl_rnga> class sim_anneal : 
00058   public multi_min<param_t,func_t,func_t,vec_t> {
00059     
00060     public:
00061   
00062     sim_anneal() {
00063       tp=&def_schedule;
00064       
00065       this->ntrial=100;
00066     }
00067 
00068     virtual ~sim_anneal() {}
00069 
00070     /** \brief Calculate the minimum \c fmin of \c func w.r.t the 
00071         array \c x of size \c nvar.
00072     */
00073     virtual int mmin(size_t nvar, vec_t &x, double &fmin, param_t &pa,
00074                      func_t &func) 
00075     {
00076       set_err_ret("Empty base function in sim_anneal::mmin()",
00077                   gsl_nobase);
00078     }
00079       
00080     /** \brief Specify the temperature schedule */
00081     int set_tptr_schedule(tptr_schedule<vec_t> &tr) {
00082       tp=&tr;
00083       return 0;
00084     }
00085     
00086     /** 
00087         \brief Print out iteration information.
00088 
00089         Depending on the value of the variable verbose, this prints out
00090         the iteration information. If verbose=0, then no information is
00091         printed, while if verbose>1, then after each iteration, the
00092         present values of x and y are output to std::cout along with the
00093         iteration number. If verbose>=2 then each iteration waits for a
00094         character.  
00095     */
00096     virtual int print_iter(size_t nv, vec_t &x, double y, int iter,
00097                            double tptr, std::string comment) 
00098     {
00099       if (this->verbose<=0) return 0;
00100         
00101       size_t i;
00102       char ch;
00103         
00104       std::cout << comment << " Iteration: " << iter << std::endl;
00105       text_out_file outs(&std::cout,79);
00106       outs.word_out("x:");
00107       for(i=0;i<nv;i++) outs.double_out(x[i]);
00108       outs.end_line();
00109       std::cout << "y: " << y << " Tptr: " << tptr << std::endl;
00110       if (this->verbose>1) {
00111         std::cout << "Press a key and type enter to continue. ";
00112         std::cin >> ch;
00113       }
00114         
00115       return 0;
00116     }
00117       
00118     /// The default random number generator
00119     rng_t def_rng;
00120 
00121     /// Return string denoting type, \c "sim_anneal".
00122     virtual const char *type() { return "sim_anneal"; }
00123 
00124     /// The default temperature schedule
00125     tptr_geoseries<vec_t> def_schedule;
00126 
00127 #ifndef DOXYGEN_INTERNAL
00128 
00129     protected:
00130     
00131     /// Pointer to the temperature annealing schedule
00132     tptr_schedule<vec_t> *tp;
00133 
00134 #endif
00135 
00136   };
00137 
00138 #ifndef DOXYGENP
00139 }
00140 #endif
00141 
00142 #endif
00143 

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

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page