Object-oriented Scientific Computing Library: Version 0.910
sim_anneal.h
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006-2012, 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/multi_min.h>
00028 #include <o2scl/rnga.h>
00029 #include <o2scl/gsl_rnga.h>
00030 #include <o2scl/multi_funct.h>
00031 
00032 #ifndef DOXYGENP
00033 namespace o2scl {
00034 #endif
00035 
00036   /** \brief Simulated annealing base
00037 
00038       The seed of the generator is not fixed initially by calls to
00039       mmin(), so if successive calls should reproduce the same
00040       results, then the random seed should be set by the user before
00041       each call.
00042       
00043       For the algorithms here, it is important that all of the inputs
00044       x[i] to the function are scaled similarly relative 
00045       to the temperature. For example, if the inputs x[i] are all
00046       of order 1, one might consider a temperature schedule which begins
00047       with \f$ T=1 \f$ . 
00048 
00049       The number of iterations at each temperature is controlled by
00050       multi_min::ntrial which defaults to 100.
00051 
00052   */
00053 #ifdef DOXYGENP
00054   template<class func_t=multi_funct, 
00055     class vec_t=ovector_base, class rng_t=gsl_rnga> class sim_anneal : 
00056   public multi_min
00057 #else
00058     template<class func_t=multi_funct<>, 
00059     class vec_t=ovector_base, class rng_t=gsl_rnga> class sim_anneal : 
00060   public multi_min<func_t,func_t,vec_t> 
00061 #endif
00062 {
00063     
00064     public:
00065   
00066     sim_anneal() {
00067       this->ntrial=100;
00068     }
00069 
00070     virtual ~sim_anneal() {}
00071 
00072     /** \brief Calculate the minimum \c fmin of \c func w.r.t the 
00073         array \c x of size \c nvar.
00074     */
00075     virtual int mmin(size_t nvar, vec_t &x, double &fmin, 
00076                      func_t &func)=0;
00077     
00078     /** \brief Print out iteration information.
00079 
00080         Depending on the value of the variable verbose, this prints out
00081         the iteration information. If verbose=0, then no information is
00082         printed, while if verbose>1, then after each iteration, the
00083         present values of x and y are output to std::cout along with the
00084         iteration number. If verbose>=2 then each iteration waits for a
00085         character.  
00086     */
00087     virtual int print_iter(size_t nv, vec_t &x, double y, int iter,
00088                            double tptr, std::string comment) 
00089     {
00090       if (this->verbose<=0) return 0;
00091         
00092       size_t i;
00093       char ch;
00094       
00095       (*this->outs) << comment << " Iteration: " << iter << std::endl;
00096       std::cout << "x: ";
00097       for(i=0;i<nv;i++) std::cout << x[i] << " ";
00098       std::cout << std::endl;
00099       (*this->outs) << "y: " << y << " Tptr: " << tptr << std::endl;
00100       if (this->verbose>1) {
00101         (*this->outs) << "Press a key and type enter to continue. ";
00102         (*this->ins) >> ch;
00103       }
00104         
00105       return 0;
00106     }
00107       
00108     /// The default random number generator
00109     rng_t def_rng;
00110 
00111     /// Return string denoting type, \c "sim_anneal".
00112     virtual const char *type() { return "sim_anneal"; }
00113 
00114   };
00115 
00116 #ifndef DOXYGENP
00117 }
00118 #endif
00119 
00120 #endif
00121 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.