00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _moSA_h
00037 #define _moSA_h
00038
00039 #include <math.h>
00040
00041 #include <eoEvalFunc.h>
00042 #include <moAlgo.h>
00043 #include <moRandMove.h>
00044 #include <moMoveIncrEval.h>
00045 #include <moCoolingSchedule.h>
00046 #include <moSolContinue.h>
00047
00049
00052 template < class M >
00053 class moSA:public moAlgo < typename M::EOType >
00054 {
00056 typedef typename M::EOType EOT;
00057
00059 typedef typename EOT::Fitness Fitness;
00060
00061 public:
00062
00064
00074 moSA (moRandMove < M > & _random_move_generator, moMoveIncrEval < M > & _incremental_evaluation,
00075 moSolContinue < EOT > & _continue, double _initial_temperature, moCoolingSchedule & _cooling_schedule,
00076 eoEvalFunc < EOT > & _full_evaluation):
00077 random_move_generator(_random_move_generator), incremental_evaluation(_incremental_evaluation),
00078 continu(_continue), initial_temperature(_initial_temperature),
00079 cooling_schedule(_cooling_schedule), full_evaluation(_full_evaluation)
00080 {}
00081
00083
00089 bool operator ()(EOT & _solution)
00090 {
00091 Fitness incremental_fitness, delta_fit;
00092 EOT best_solution;
00093 double temperature;
00094 M move;
00095
00096 if (_solution.invalid())
00097 {
00098 full_evaluation (_solution);
00099 }
00100
00101 temperature = initial_temperature;
00102
00103 best_solution = _solution;
00104
00105 do
00106 {
00107 continu.init ();
00108
00109 do
00110 {
00111 random_move_generator(move);
00112
00113 incremental_fitness = incremental_evaluation (move, _solution);
00114
00115 delta_fit = incremental_fitness - _solution.fitness ();
00116
00117 if( (_solution.fitness() > incremental_fitness ) && (exp (delta_fit / temperature) > 1.0) )
00118 {
00119 delta_fit = -delta_fit;
00120 }
00121
00122 if ( (incremental_fitness > _solution.fitness()) || (rng.uniform () < exp (delta_fit / temperature)) )
00123 {
00124 move(_solution);
00125 _solution.fitness(incremental_fitness);
00126
00127
00128 if ( _solution.fitness() > best_solution.fitness() )
00129 {
00130 best_solution = _solution;
00131 }
00132 }
00133
00134 }
00135 while ( continu (_solution) );
00136 }
00137 while ( cooling_schedule (temperature) );
00138
00139 _solution = best_solution;
00140
00141 return true;
00142 }
00143
00144 private:
00145
00147 moRandMove < M > & random_move_generator;
00148
00150 moMoveIncrEval < M > & incremental_evaluation;
00151
00153 moSolContinue < EOT > & continu;
00154
00156 double initial_temperature;
00157
00159 moCoolingSchedule & cooling_schedule;
00160
00162 eoEvalFunc < EOT > & full_evaluation;
00163 };
00164
00165 #endif