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 typename M::EOType EOT;
00035
00037 typedef typename EOT::Fitness Fitness;
00038
00039 public:
00040
00042
00052 moSA (moRandMove < M > &__move_rand,
00053 moMoveIncrEval < M > &__incr_eval,
00054 moSolContinue < EOT > &__cont,
00055 double __init_temp,
00056 moCoolingSchedule & __cool_sched, eoEvalFunc < EOT > &__full_eval):
00057 move_rand (__move_rand),
00058 incr_eval (__incr_eval),
00059 cont (__cont),
00060 init_temp (__init_temp),
00061 cool_sched (__cool_sched),
00062 full_eval (__full_eval)
00063 {
00064
00065 }
00066
00068
00074 bool operator ()(EOT & __sol)
00075 {
00076
00077 if (__sol.invalid ())
00078 {
00079 full_eval (__sol);
00080 }
00081
00082 double temp = init_temp;
00083
00084 M move;
00085
00086 EOT best_sol = __sol;
00087
00088 Fitness current_fitness, delta;
00089 double exp1, exp2;
00090
00091 do
00092 {
00093 cont.init ();
00094 do
00095 {
00096 move_rand (move);
00097
00098 current_fitness= incr_eval (move, __sol);
00099
00100 delta = current_fitness - __sol.fitness();
00101
00102 if(((long double)delta) < 0.0)
00103 {
00104 delta=-delta;
00105 }
00106
00107 if ((current_fitness > __sol.fitness()) || ((rng.uniform ()) < (exp (-delta/ temp))))
00108 {
00109 __sol.fitness (current_fitness);
00110 move (__sol);
00111
00112
00113 if (__sol.fitness () > best_sol.fitness ())
00114 {
00115 best_sol = __sol;
00116 }
00117 }
00118 }
00119 while (cont (__sol));
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