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
00037 #ifndef __moSA_h
00038 #define __moSA_h
00039
00040 #include <eoOp.h>
00041 #include <eoEvalFunc.h>
00042
00043 #include "moAlgo.h"
00044 #include "moRandMove.h"
00045 #include "moMoveIncrEval.h"
00046 #include "moCoolingSchedule.h"
00047 #include "moSolContinue.h"
00048
00049 #include <math.h>
00050
00052
00055 template < class M > class moSA:public moAlgo < typename M::EOType >
00056 {
00057
00059 typedef
00060 typename
00061 M::EOType
00062 EOT;
00063
00065 typedef
00066 typename
00067 EOT::Fitness
00068 Fitness;
00069
00070 public:
00071
00073
00083 moSA (moRandMove < M > &__move_rand,
00084 moMoveIncrEval < M > &__incr_eval,
00085 moSolContinue < EOT > &__cont,
00086 double __init_temp,
00087 moCoolingSchedule & __cool_sched, eoEvalFunc < EOT > &__full_eval):
00088 move_rand (__move_rand),
00089 incr_eval (__incr_eval),
00090 cont (__cont),
00091 init_temp (__init_temp),
00092 cool_sched (__cool_sched),
00093 full_eval (__full_eval)
00094 {
00095
00096 }
00097
00099
00105 bool operator ()(EOT & __sol)
00106 {
00107
00108 if (__sol.invalid ())
00109 {
00110 full_eval (__sol);
00111 }
00112
00113 double temp = init_temp;
00114
00115 M move;
00116
00117 EOT best_sol = __sol;
00118
00119 do
00120 {
00121
00122 cont.init ();
00123 do
00124 {
00125
00126 move_rand (move);
00127
00128 Fitness delta_fit = incr_eval (move, __sol) - __sol.fitness ();
00129
00130 if (delta_fit > 0 || rng.uniform () < exp (delta_fit / temp))
00131 {
00132
00133 __sol.fitness (incr_eval (move, __sol));
00134 move (__sol);
00135
00136
00137
00138 if (__sol.fitness () > best_sol.fitness ())
00139 best_sol = __sol;
00140 }
00141
00142 }
00143 while (cont (__sol));
00144
00145 }
00146 while (cool_sched (temp));
00147
00148 __sol = best_sol;
00149
00150 return true;
00151 }
00152
00153 private:
00154
00156 moRandMove < M > &move_rand;
00157
00159 moMoveIncrEval < M > &incr_eval;
00160
00162 moSolContinue < EOT > &cont;
00163
00165 double init_temp;
00166
00168 moCoolingSchedule & cool_sched;
00169
00171 eoEvalFunc < EOT > &full_eval;
00172 };
00173
00174 #endif