00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __moSA_h
00013 #define __moSA_h
00014
00015 #include <unistd.h>
00016
00017 #include <eoOp.h>
00018 #include <eoEvalFunc.h>
00019
00020 #include "moAlgo.h"
00021 #include "moRandMove.h"
00022 #include "moMoveIncrEval.h"
00023 #include "moCoolingSchedule.h"
00024 #include "moSolContinue.h"
00025
00026 #include <math.h>
00027
00029
00032 template < class M > class moSA:public moAlgo < typename M::EOType >
00033 {
00034
00036 typedef
00037 typename
00038 M::EOType
00039 EOT;
00040
00042 typedef
00043 typename
00044 EOT::Fitness
00045 Fitness;
00046
00047 public:
00048
00050
00060 moSA (moRandMove < M > &__move_rand,
00061 moMoveIncrEval < M > &__incr_eval,
00062 moSolContinue < EOT > &__cont,
00063 double __init_temp,
00064 moCoolingSchedule & __cool_sched, eoEvalFunc < EOT > &__full_eval):
00065 move_rand (__move_rand),
00066 incr_eval (__incr_eval),
00067 cont (__cont),
00068 init_temp (__init_temp),
00069 cool_sched (__cool_sched),
00070 full_eval (__full_eval)
00071 {
00072
00073 }
00074
00076
00082 bool operator ()(EOT & __sol)
00083 {
00084
00085 if (__sol.invalid ())
00086 {
00087 full_eval (__sol);
00088 }
00089
00090 double temp = init_temp;
00091
00092 M move;
00093
00094 EOT best_sol = __sol;
00095
00096 do
00097 {
00098
00099 cont.init ();
00100 do
00101 {
00102
00103 move_rand (move);
00104
00105 Fitness delta_fit = incr_eval (move, __sol) - __sol.fitness ();
00106
00107 if (delta_fit > 0 || rng.uniform () < exp (delta_fit / temp))
00108 {
00109
00110 __sol.fitness (incr_eval (move, __sol));
00111 move (__sol);
00112
00113
00114
00115 if (__sol.fitness () > best_sol.fitness ())
00116 best_sol = __sol;
00117 }
00118
00119 }
00120 while (cont (__sol));
00121
00122 }
00123 while (cool_sched (temp));
00124
00125 __sol = best_sol;
00126
00127 return true;
00128 }
00129
00130 private:
00131
00133 moRandMove < M > &move_rand;
00134
00136 moMoveIncrEval < M > &incr_eval;
00137
00139 moSolContinue < EOT > &cont;
00140
00142 double init_temp;
00143
00145 moCoolingSchedule & cool_sched;
00146
00148 eoEvalFunc < EOT > &full_eval;
00149 };
00150
00151 #endif