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)=0; 00075 00076 /** \brief Specify the temperature schedule */ 00077 int set_tptr_schedule(tptr_schedule<vec_t> &tr) { 00078 tp=&tr; 00079 return 0; 00080 } 00081 00082 /** 00083 \brief Print out iteration information. 00084 00085 Depending on the value of the variable verbose, this prints out 00086 the iteration information. If verbose=0, then no information is 00087 printed, while if verbose>1, then after each iteration, the 00088 present values of x and y are output to std::cout along with the 00089 iteration number. If verbose>=2 then each iteration waits for a 00090 character. 00091 */ 00092 virtual int print_iter(size_t nv, vec_t &x, double y, int iter, 00093 double tptr, std::string comment) 00094 { 00095 if (this->verbose<=0) return 0; 00096 00097 size_t i; 00098 char ch; 00099 00100 std::cout << comment << " Iteration: " << iter << std::endl; 00101 text_out_file outs(&std::cout,79); 00102 outs.word_out("x:"); 00103 for(i=0;i<nv;i++) outs.double_out(x[i]); 00104 outs.end_line(); 00105 std::cout << "y: " << y << " Tptr: " << tptr << std::endl; 00106 if (this->verbose>1) { 00107 std::cout << "Press a key and type enter to continue. "; 00108 std::cin >> ch; 00109 } 00110 00111 return 0; 00112 } 00113 00114 /// The default random number generator 00115 rng_t def_rng; 00116 00117 /// Return string denoting type, \c "sim_anneal". 00118 virtual const char *type() { return "sim_anneal"; } 00119 00120 /// The default temperature schedule 00121 tptr_geoseries<vec_t> def_schedule; 00122 00123 #ifndef DOXYGEN_INTERNAL 00124 00125 protected: 00126 00127 /// Pointer to the temperature annealing schedule 00128 tptr_schedule<vec_t> *tp; 00129 00130 #endif 00131 00132 }; 00133 00134 #ifndef DOXYGENP 00135 } 00136 #endif 00137 00138 #endif 00139
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