Addition of the function PSO : Parallel evaluation
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@650 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
2cef623c31
commit
43fbde1096
9 changed files with 161 additions and 23 deletions
|
|
@ -57,7 +57,6 @@ void Communicable :: unlock () {
|
|||
}
|
||||
|
||||
void Communicable :: stop () {
|
||||
|
||||
sem_wait (& sem_stop);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
// "eoVector_comm.h"
|
||||
|
||||
// (c) OPAC Team, LIFL, August 2005
|
||||
|
||||
/*
|
||||
|
|
@ -35,4 +34,36 @@ template <class F, class T> void unpack (eoVector <F, T> & __v) {
|
|||
unpack (__v [i]);
|
||||
}
|
||||
|
||||
template <class F, class T, class V> void pack (const eoVectorParticle <F, T, V> & __v) {
|
||||
|
||||
pack (__v.fitness ()) ;
|
||||
pack (__v.best());
|
||||
unsigned len = __v.size ();
|
||||
pack (len);
|
||||
for (unsigned i = 0 ; i < len; i ++)
|
||||
pack (__v [i]);
|
||||
for (unsigned i = 0 ; i < len; i ++)
|
||||
pack (__v.bestPositions[i]);
|
||||
for (unsigned i = 0 ; i < len; i ++)
|
||||
pack (__v.velocities[i]);
|
||||
}
|
||||
|
||||
template <class F, class T, class V> void unpack (eoVectorParticle <F, T, V> & __v) {
|
||||
|
||||
F fit;
|
||||
unpack(fit);
|
||||
__v.fitness (fit);
|
||||
unpack(fit);
|
||||
__v.best(fit);
|
||||
unsigned len;
|
||||
unpack (len);
|
||||
__v.resize (len);
|
||||
for (unsigned i = 0 ; i < len; i ++)
|
||||
unpack (__v [i]);
|
||||
for (unsigned i = 0 ; i < len; i ++)
|
||||
unpack (__v.bestPositions[i]);
|
||||
for (unsigned i = 0 ; i < len; i ++)
|
||||
unpack (__v.velocities[i]);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -54,9 +54,10 @@ template <class U, class V> void pack (const std :: pair <U, V> & __pair) {
|
|||
pack (__pair.second);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
|
||||
/* Float */
|
||||
/* Char */
|
||||
extern void unpack (char & __c);
|
||||
|
||||
/* Float */
|
||||
|
|
@ -101,4 +102,5 @@ template <class U, class V> void unpack (std :: pair <U, V> & __pair) {
|
|||
unpack (__pair.second);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -131,6 +131,7 @@ template< class EOT > void peoEA< EOT > :: run() {
|
|||
trans( off );
|
||||
|
||||
printDebugMessage( "performing the evaluation of the population." );
|
||||
|
||||
pop_eval( off );
|
||||
|
||||
printDebugMessage( "performing the replacement of the population." );
|
||||
|
|
|
|||
34
trunk/paradiseo-peo/src/peoEvalFuncPSO.h
Normal file
34
trunk/paradiseo-peo/src/peoEvalFuncPSO.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
// "peoPSO.h"
|
||||
|
||||
// (c) OPAC Team, October 2007
|
||||
|
||||
/*
|
||||
Contact: clive.canape@inria.fr
|
||||
*/
|
||||
|
||||
#ifndef PEOEVALFUNCPSO_H
|
||||
#define PEOEVALFUNCPSO_H
|
||||
|
||||
#include <eoEvalFunc.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
template< class POT, class FitT = POT::Fitness, class FunctionArg = const POT& >
|
||||
#else
|
||||
template< class POT, class FitT = typename POT::Fitness, class FunctionArg = const POT& >
|
||||
#endif
|
||||
struct peoEvalFuncPSO: public eoEvalFunc<POT> {
|
||||
|
||||
peoEvalFuncPSO( FitT (* _eval)( FunctionArg ) )
|
||||
: eoEvalFunc<POT>(), evalFunc( _eval ) {};
|
||||
|
||||
virtual void operator() ( POT & _peo )
|
||||
{
|
||||
_peo.fitness((*evalFunc)( _peo ));
|
||||
};
|
||||
|
||||
private:
|
||||
FitT (* evalFunc )( FunctionArg );
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
82
trunk/paradiseo-peo/src/peoPSO.h
Normal file
82
trunk/paradiseo-peo/src/peoPSO.h
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
// "peoPSO.h"
|
||||
|
||||
// (c) OPAC Team, October 2007
|
||||
|
||||
/*
|
||||
Contact: clive.canape@inria.fr
|
||||
*/
|
||||
|
||||
#ifndef __peoPSO_h
|
||||
#define __peoPSO_h
|
||||
|
||||
#include <eoContinue.h>
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoPopEvalFunc.h>
|
||||
#include <eoPSO.h>
|
||||
#include <eoVelocity.h>
|
||||
#include <eoFlight.h>
|
||||
#include "peoPopEval.h"
|
||||
#include "core/runner.h"
|
||||
#include "core/peo_debug.h"
|
||||
|
||||
|
||||
template < class POT > class peoPSO : public Runner {
|
||||
|
||||
public:
|
||||
|
||||
peoPSO(
|
||||
eoContinue< POT >& __cont,
|
||||
peoPopEval< POT >& __pop_eval,
|
||||
eoVelocity < POT > &_velocity,
|
||||
eoFlight < POT > &_flight);
|
||||
|
||||
|
||||
void run();
|
||||
|
||||
|
||||
void operator()( eoPop< POT >& __pop );
|
||||
|
||||
private:
|
||||
|
||||
eoContinue< POT >& cont;
|
||||
peoPopEval< POT >& pop_eval;
|
||||
eoPop< POT >* pop;
|
||||
eoVelocity < POT > &velocity;
|
||||
eoFlight < POT > &flight;
|
||||
};
|
||||
|
||||
|
||||
template < class POT > peoPSO< POT > :: peoPSO(
|
||||
|
||||
eoContinue< POT >& __cont,
|
||||
peoPopEval< POT >& __pop_eval,
|
||||
eoVelocity < POT > &__velocity,
|
||||
eoFlight < POT > &__flight
|
||||
) : cont( __cont ), pop_eval(__pop_eval ),velocity( __velocity),flight( __flight)
|
||||
{
|
||||
pop_eval.setOwner( *this );
|
||||
}
|
||||
|
||||
|
||||
template< class POT > void peoPSO< POT > :: operator ()( eoPop< POT >& __pop ) {
|
||||
|
||||
pop = &__pop;
|
||||
}
|
||||
|
||||
|
||||
template< class POT > void peoPSO< POT > :: run() {
|
||||
|
||||
printDebugMessage( "Performing the first evaluation of the population." );
|
||||
do {
|
||||
printDebugMessage( "Performing the velocity evaluation." );
|
||||
velocity.apply ( *pop );
|
||||
printDebugMessage( "Performing the flight." );
|
||||
flight.apply ( *pop );
|
||||
printDebugMessage( "Performing the evaluation." );
|
||||
pop_eval(*pop);
|
||||
velocity.updateNeighborhood( *pop );
|
||||
} while ( cont( *pop ) );
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -120,20 +120,15 @@ template< class EOT > peoParaPopEval< EOT > :: peoParaPopEval(
|
|||
|
||||
|
||||
template< class EOT > void peoParaPopEval< EOT >::operator()( eoPop< EOT >& __pop ) {
|
||||
|
||||
for ( unsigned i = 0; i < __pop.size(); i++ ) {
|
||||
|
||||
__pop[ i ].fitness( typename EOT :: Fitness() );
|
||||
|
||||
for ( unsigned i = 0; i < __pop.size(); i++ ) {
|
||||
__pop[ i ].fitness(typename EOT :: Fitness() );
|
||||
progression[ &__pop[ i ] ].first = funcs.size() - 1;
|
||||
progression[ &__pop[ i ] ].second = funcs.size();
|
||||
|
||||
for ( unsigned j = 0; j < funcs.size(); j++ ) {
|
||||
/* Queuing the 'invalid' solution and its associated owner */
|
||||
tasks.push( &__pop[ i ] );
|
||||
}
|
||||
}
|
||||
|
||||
total = funcs.size() * __pop.size();
|
||||
requestResourceRequest( funcs.size() * __pop.size() );
|
||||
stop();
|
||||
|
|
@ -141,7 +136,6 @@ template< class EOT > void peoParaPopEval< EOT >::operator()( eoPop< EOT >& __po
|
|||
|
||||
|
||||
template< class EOT > void peoParaPopEval< EOT > :: packData() {
|
||||
|
||||
// printDebugMessage ("debut pakc data");
|
||||
pack( progression[ tasks.front() ].first-- );
|
||||
|
||||
|
|
@ -155,7 +149,6 @@ template< class EOT > void peoParaPopEval< EOT > :: packData() {
|
|||
|
||||
|
||||
template< class EOT > void peoParaPopEval< EOT > :: unpackData() {
|
||||
|
||||
unpack( num_func );
|
||||
/* Unpacking the solution */
|
||||
unpack( sol );
|
||||
|
|
@ -165,15 +158,13 @@ template< class EOT > void peoParaPopEval< EOT > :: unpackData() {
|
|||
|
||||
|
||||
template< class EOT > void peoParaPopEval< EOT > :: execute() {
|
||||
|
||||
/* Computing the fitness of the solution */
|
||||
funcs[ num_func ]->operator()( sol );
|
||||
funcs[ num_func ]->operator()( sol );
|
||||
}
|
||||
|
||||
|
||||
template< class EOT > void peoParaPopEval< EOT > :: packResult() {
|
||||
|
||||
/* Packing the fitness of the solution */
|
||||
/* Packing the fitness of the solution */
|
||||
pack( sol.fitness() );
|
||||
/* Packing the @ of the individual */
|
||||
pack( ad_sol );
|
||||
|
|
@ -181,7 +172,6 @@ template< class EOT > void peoParaPopEval< EOT > :: packResult() {
|
|||
|
||||
|
||||
template< class EOT > void peoParaPopEval< EOT > :: unpackResult() {
|
||||
|
||||
typename EOT :: Fitness fit;
|
||||
|
||||
/* Unpacking the computed fitness */
|
||||
|
|
@ -212,12 +202,10 @@ template< class EOT > void peoParaPopEval< EOT > :: unpackResult() {
|
|||
|
||||
|
||||
template< class EOT > void peoParaPopEval< EOT > :: notifySendingData() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
template< class EOT > void peoParaPopEval< EOT > :: notifySendingAllResourceRequests() {
|
||||
|
||||
getOwner()->setPassive();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@
|
|||
#include <peo>
|
||||
|
||||
|
||||
#define POP_SIZE 10
|
||||
#define NUM_GEN 100
|
||||
#define POP_SIZE 2
|
||||
#define NUM_GEN 2
|
||||
#define CROSS_RATE 1.0
|
||||
#define MUT_RATE 0.01
|
||||
|
||||
|
|
@ -33,7 +33,7 @@
|
|||
// by default, parallel evaluation of the population is performed;
|
||||
// for parallel fitness evaluation, uncomment the following line
|
||||
|
||||
// #define PARALLEL_FIT_EVALUATION
|
||||
#define PARALLEL_FIT_EVALUATION
|
||||
|
||||
|
||||
int main( int __argc, char** __argv ) {
|
||||
|
|
@ -105,6 +105,7 @@ int main( int __argc, char** __argv ) {
|
|||
|
||||
peo :: run( );
|
||||
peo :: finalize( );
|
||||
|
||||
// shutting down the ParadisEO-PEO environment
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,6 @@
|
|||
#include "route_eval.h"
|
||||
|
||||
void RouteEval :: operator () (Route & __route) {
|
||||
|
||||
std::cout<<"\nICI";
|
||||
__route.fitness (- (int) length (__route));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue