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 "moCoolSched.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 moCoolSched & __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
00091 temp = init_temp;
00092
00093 M move;
00094
00095 EOT best_sol = __sol;
00096
00097 do
00098 {
00099
00100 cont.init ();
00101 do
00102 {
00103
00104 move_rand (move);
00105
00106 Fitness delta_fit = incr_eval (move, __sol) - __sol.fitness ();
00107
00108 if (delta_fit > 0 || rng.uniform () < exp (delta_fit / temp))
00109 {
00110
00111 __sol.fitness (incr_eval (move, __sol));
00112 move (__sol);
00113
00114
00115
00116 if (__sol.fitness () > best_sol.fitness ())
00117 best_sol = __sol;
00118 }
00119
00120 }
00121 while (cont (__sol));
00122
00123 }
00124 while (cool_sched (temp));
00125
00126 __sol = best_sol;
00127
00128 return true;
00129 }
00130
00131 private:
00132
00134 moRandMove < M > &move_rand;
00135
00137 moMoveIncrEval < M > &incr_eval;
00138
00140 moSolContinue < EOT > &cont;
00141
00143 double
00144 init_temp;
00145
00147 moCoolSched & cool_sched;
00148
00150 eoEvalFunc < EOT > &full_eval;
00151 };
00152
00153 #endif