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