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:
parent
796b0cbb8e
commit
e0b82a27d1
6 changed files with 29 additions and 24 deletions
|
|
@ -56,16 +56,10 @@ template < class M > class moSA:public moAlgo < typename M::EOType >
|
||||||
{
|
{
|
||||||
|
|
||||||
//! Alias for the type
|
//! Alias for the type
|
||||||
typedef
|
typedef typename M::EOType EOT;
|
||||||
typename
|
|
||||||
M::EOType
|
|
||||||
EOT;
|
|
||||||
|
|
||||||
//! Alias for the fitness
|
//! Alias for the fitness
|
||||||
typedef
|
typedef typename EOT::Fitness Fitness;
|
||||||
typename
|
|
||||||
EOT::Fitness
|
|
||||||
Fitness;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
@ -123,18 +117,26 @@ template < class M > class moSA:public moAlgo < typename M::EOType >
|
||||||
|
|
||||||
move_rand (move);
|
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 (incremental_fitness);
|
||||||
__sol.fitness (incr_eval (move, __sol));
|
|
||||||
move (__sol);
|
move (__sol);
|
||||||
|
|
||||||
/* Updating the best solution found
|
/* Updating the best solution found
|
||||||
until now ? */
|
until now ? */
|
||||||
if (__sol.fitness () > best_sol.fitness ())
|
if (__sol.fitness () > best_sol.fitness ())
|
||||||
best_sol = __sol;
|
{
|
||||||
|
best_sol = __sol;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 ++)
|
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) ;
|
__route.fitness (len) ;
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,11 @@
|
||||||
#define route_h
|
#define route_h
|
||||||
|
|
||||||
#include <eoVector.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
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -40,11 +40,11 @@
|
||||||
void RouteEval :: operator () (Route & __route)
|
void RouteEval :: operator () (Route & __route)
|
||||||
{
|
{
|
||||||
|
|
||||||
float len = 0 ;
|
float len = 0.0 ;
|
||||||
|
|
||||||
for (unsigned int i = 0 ; i < Graph :: size () ; i ++)
|
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) ;
|
__route.fitness (len) ;
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@
|
||||||
#include "two_opt_incr_eval.h"
|
#include "two_opt_incr_eval.h"
|
||||||
#include "graph.h"
|
#include "graph.h"
|
||||||
|
|
||||||
float TwoOptIncrEval :: operator () (const TwoOpt & __move, const Route & __route)
|
tspFitness TwoOptIncrEval :: operator () (const TwoOpt & __move, const Route & __route)
|
||||||
{
|
{
|
||||||
// From
|
// From
|
||||||
unsigned int v1 = __route [__move.first], v1_next = __route [__move.first + 1] ;
|
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] ;
|
unsigned int v2 = __route [__move.second], v2_next = __route [__move.second + 1] ;
|
||||||
|
|
||||||
return __route.fitness ()
|
return __route.fitness ()
|
||||||
- Graph :: distance (v1, v2)
|
+ Graph :: distance (v1, v2)
|
||||||
- Graph :: distance (v1_next, v2_next)
|
+ Graph :: distance (v1_next, v2_next)
|
||||||
+ Graph :: distance (v1, v1_next)
|
- Graph :: distance (v1, v1_next)
|
||||||
+ Graph :: distance (v2, v2_next) ;
|
- Graph :: distance (v2, v2_next) ;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,10 +42,9 @@
|
||||||
|
|
||||||
class TwoOptIncrEval : public moMoveIncrEval <TwoOpt>
|
class TwoOptIncrEval : public moMoveIncrEval <TwoOpt>
|
||||||
{
|
{
|
||||||
|
|
||||||
public :
|
public :
|
||||||
|
|
||||||
float operator () (const TwoOpt & __move, const Route & __route) ;
|
tspFitness operator () (const TwoOpt & __move, const Route & __route) ;
|
||||||
|
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue