Tsp fitness has been changed and moSA.h has been updated

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@813 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jboisson 2007-11-26 08:46:41 +00:00
commit e0b82a27d1
6 changed files with 29 additions and 24 deletions

View file

@ -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;
}
}
}

View file

@ -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) ;

View file

@ -38,7 +38,11 @@
#define route_h
#include <eoVector.h>
#include <eoScalarFitness.h>
typedef eoVector <float, unsigned int> Route ; // [Fitness (- length), Gene (city)]
// A float that has to be minimized.
typedef eoScalarFitness< float, std::greater<float> > tspFitness ;
typedef eoVector <tspFitness, unsigned int> Route ; // [Fitness (length), Gene (city)]
#endif

View file

@ -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) ;

View file

@ -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) ;
}

View file

@ -42,10 +42,9 @@
class TwoOptIncrEval : public moMoveIncrEval <TwoOpt>
{
public :
float operator () (const TwoOpt & __move, const Route & __route) ;
tspFitness operator () (const TwoOpt & __move, const Route & __route) ;
} ;