diff --git a/trunk/paradiseo-mo/src/moSA.h b/trunk/paradiseo-mo/src/moSA.h index f49bf6041..f320ca9f5 100755 --- a/trunk/paradiseo-mo/src/moSA.h +++ b/trunk/paradiseo-mo/src/moSA.h @@ -56,16 +56,10 @@ template < class M > class moSA:public moAlgo < typename M::EOType > { //! Alias for the type - typedef - typename - M::EOType - EOT; + typedef typename M::EOType EOT; //! Alias for the fitness - typedef - typename - EOT::Fitness - Fitness; + typedef typename EOT::Fitness Fitness; public: @@ -123,18 +117,26 @@ template < class M > class moSA:public moAlgo < typename M::EOType > move_rand (move); - Fitness delta_fit = incr_eval (move, __sol) - __sol.fitness (); + Fitness incremental_fitness = incr_eval (move, __sol); - if (delta_fit > 0 || rng.uniform () < exp (delta_fit / temp)) + Fitness delta_fit = incremental_fitness - __sol.fitness (); + + if((__sol.fitness() > incremental_fitness ) && (exp (delta_fit / temp) > 1.0)) + { + delta_fit = -delta_fit; + } + + if (incremental_fitness > __sol.fitness() || rng.uniform () < exp (delta_fit / temp)) { - - __sol.fitness (incr_eval (move, __sol)); + __sol.fitness (incremental_fitness); move (__sol); /* Updating the best solution found until now ? */ if (__sol.fitness () > best_sol.fitness ()) - best_sol = __sol; + { + best_sol = __sol; + } } } diff --git a/trunk/paradiseo-mo/tutorial/examples/tsp/part_route_eval.cpp b/trunk/paradiseo-mo/tutorial/examples/tsp/part_route_eval.cpp index bc5c3a718..1639e002a 100644 --- a/trunk/paradiseo-mo/tutorial/examples/tsp/part_route_eval.cpp +++ b/trunk/paradiseo-mo/tutorial/examples/tsp/part_route_eval.cpp @@ -46,7 +46,7 @@ void PartRouteEval :: operator () (Route & __route) for (unsigned int i = (unsigned int) (__route.size () * from) ; i < (unsigned int ) (__route.size () * to) ; i ++) { - len -= Graph :: distance (__route [i], __route [(i + 1) % Graph :: size ()]) ; + len += Graph :: distance (__route [i], __route [(i + 1) % Graph :: size ()]) ; } __route.fitness (len) ; diff --git a/trunk/paradiseo-mo/tutorial/examples/tsp/route.h b/trunk/paradiseo-mo/tutorial/examples/tsp/route.h index 09b993bc1..ac9629cbb 100644 --- a/trunk/paradiseo-mo/tutorial/examples/tsp/route.h +++ b/trunk/paradiseo-mo/tutorial/examples/tsp/route.h @@ -38,7 +38,11 @@ #define route_h #include +#include -typedef eoVector Route ; // [Fitness (- length), Gene (city)] +// A float that has to be minimized. +typedef eoScalarFitness< float, std::greater > tspFitness ; + +typedef eoVector Route ; // [Fitness (length), Gene (city)] #endif diff --git a/trunk/paradiseo-mo/tutorial/examples/tsp/route_eval.cpp b/trunk/paradiseo-mo/tutorial/examples/tsp/route_eval.cpp index 693f43c6c..abde85b99 100644 --- a/trunk/paradiseo-mo/tutorial/examples/tsp/route_eval.cpp +++ b/trunk/paradiseo-mo/tutorial/examples/tsp/route_eval.cpp @@ -40,11 +40,11 @@ void RouteEval :: operator () (Route & __route) { - float len = 0 ; + float len = 0.0 ; for (unsigned int i = 0 ; i < Graph :: size () ; i ++) { - len -= Graph :: distance (__route [i], __route [(i + 1) % Graph :: size ()]) ; + len += Graph :: distance (__route [i], __route [(i + 1) % Graph :: size ()]) ; } __route.fitness (len) ; diff --git a/trunk/paradiseo-mo/tutorial/examples/tsp/two_opt_incr_eval.cpp b/trunk/paradiseo-mo/tutorial/examples/tsp/two_opt_incr_eval.cpp index 06a3d594e..2d4657b73 100644 --- a/trunk/paradiseo-mo/tutorial/examples/tsp/two_opt_incr_eval.cpp +++ b/trunk/paradiseo-mo/tutorial/examples/tsp/two_opt_incr_eval.cpp @@ -37,7 +37,7 @@ #include "two_opt_incr_eval.h" #include "graph.h" -float TwoOptIncrEval :: operator () (const TwoOpt & __move, const Route & __route) +tspFitness TwoOptIncrEval :: operator () (const TwoOpt & __move, const Route & __route) { // From unsigned int v1 = __route [__move.first], v1_next = __route [__move.first + 1] ; @@ -46,8 +46,8 @@ float TwoOptIncrEval :: operator () (const TwoOpt & __move, const Route & __rout unsigned int v2 = __route [__move.second], v2_next = __route [__move.second + 1] ; return __route.fitness () - - Graph :: distance (v1, v2) - - Graph :: distance (v1_next, v2_next) - + Graph :: distance (v1, v1_next) - + Graph :: distance (v2, v2_next) ; + + Graph :: distance (v1, v2) + + Graph :: distance (v1_next, v2_next) + - Graph :: distance (v1, v1_next) + - Graph :: distance (v2, v2_next) ; } diff --git a/trunk/paradiseo-mo/tutorial/examples/tsp/two_opt_incr_eval.h b/trunk/paradiseo-mo/tutorial/examples/tsp/two_opt_incr_eval.h index 856b28201..91ffca098 100644 --- a/trunk/paradiseo-mo/tutorial/examples/tsp/two_opt_incr_eval.h +++ b/trunk/paradiseo-mo/tutorial/examples/tsp/two_opt_incr_eval.h @@ -42,10 +42,9 @@ class TwoOptIncrEval : public moMoveIncrEval { - public : - float operator () (const TwoOpt & __move, const Route & __route) ; + tspFitness operator () (const TwoOpt & __move, const Route & __route) ; } ;