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