MPI Multistart: removed second template parameter Fitness, as it can be deduced from Individual.
This commit is contained in:
parent
0c1fc2ce99
commit
e58b3e5cb2
1 changed files with 43 additions and 44 deletions
|
|
@ -61,7 +61,6 @@ public:
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// define your individuals
|
// define your individuals
|
||||||
typedef SerializableEOReal Indi;
|
typedef SerializableEOReal Indi;
|
||||||
typedef double IndiFitness;
|
|
||||||
|
|
||||||
// EVAL
|
// EVAL
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
@ -113,7 +112,7 @@ struct SerializableBasicType : public eoserial::Persistent
|
||||||
T _value;
|
T _value;
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class EOT, class FitT >
|
template< class EOT >
|
||||||
struct MultiStartData
|
struct MultiStartData
|
||||||
{
|
{
|
||||||
typedef eoF<void> ResetAlgo;
|
typedef eoF<void> ResetAlgo;
|
||||||
|
|
@ -129,7 +128,7 @@ struct MultiStartData
|
||||||
// dynamic parameters
|
// dynamic parameters
|
||||||
int runs;
|
int runs;
|
||||||
bool firstIndividual;
|
bool firstIndividual;
|
||||||
FitT bestFitness;
|
typename EOT::Fitness bestFitness;
|
||||||
EOT bestIndividual;
|
EOT bestIndividual;
|
||||||
eoPop< EOT > pop;
|
eoPop< EOT > pop;
|
||||||
|
|
||||||
|
|
@ -140,11 +139,11 @@ struct MultiStartData
|
||||||
int masterRank;
|
int masterRank;
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class EOT, class FitT >
|
template< class EOT >
|
||||||
class SendTaskMultiStart : public SendTaskFunction< MultiStartData< EOT, FitT > >
|
class SendTaskMultiStart : public SendTaskFunction< MultiStartData< EOT > >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using SendTaskFunction< MultiStartData< EOT, FitT > >::_data;
|
using SendTaskFunction< MultiStartData< EOT > >::_data;
|
||||||
|
|
||||||
void operator()( int wrkRank )
|
void operator()( int wrkRank )
|
||||||
{
|
{
|
||||||
|
|
@ -152,18 +151,18 @@ class SendTaskMultiStart : public SendTaskFunction< MultiStartData< EOT, FitT >
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class EOT, class FitT >
|
template< class EOT >
|
||||||
class HandleResponseMultiStart : public HandleResponseFunction< MultiStartData< EOT, FitT > >
|
class HandleResponseMultiStart : public HandleResponseFunction< MultiStartData< EOT > >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using HandleResponseFunction< MultiStartData< EOT, FitT > >::_data;
|
using HandleResponseFunction< MultiStartData< EOT > >::_data;
|
||||||
|
|
||||||
void operator()( int wrkRank )
|
void operator()( int wrkRank )
|
||||||
{
|
{
|
||||||
std::cout << "Response received." << std::endl;
|
std::cout << "Response received." << std::endl;
|
||||||
|
|
||||||
EOT individual;
|
EOT individual;
|
||||||
MultiStartData< EOT, FitT >& d = *_data;
|
MultiStartData< EOT >& d = *_data;
|
||||||
d.comm.recv( wrkRank, 1, individual );
|
d.comm.recv( wrkRank, 1, individual );
|
||||||
|
|
||||||
std::cout << "Fitness of individual: " << individual.fitness() << std::endl;
|
std::cout << "Fitness of individual: " << individual.fitness() << std::endl;
|
||||||
|
|
@ -180,11 +179,11 @@ class HandleResponseMultiStart : public HandleResponseFunction< MultiStartData<
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class EOT, class FitT >
|
template< class EOT >
|
||||||
class ProcessTaskMultiStart : public ProcessTaskFunction< MultiStartData< EOT, FitT > >
|
class ProcessTaskMultiStart : public ProcessTaskFunction< MultiStartData< EOT > >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using ProcessTaskFunction< MultiStartData<EOT, FitT > >::_data;
|
using ProcessTaskFunction< MultiStartData<EOT > >::_data;
|
||||||
|
|
||||||
void operator()()
|
void operator()()
|
||||||
{
|
{
|
||||||
|
|
@ -200,11 +199,11 @@ class ProcessTaskMultiStart : public ProcessTaskFunction< MultiStartData< EOT, F
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class EOT, class FitT >
|
template< class EOT >
|
||||||
class IsFinishedMultiStart : public IsFinishedFunction< MultiStartData< EOT, FitT > >
|
class IsFinishedMultiStart : public IsFinishedFunction< MultiStartData< EOT > >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using IsFinishedFunction< MultiStartData< EOT, FitT > >::_data;
|
using IsFinishedFunction< MultiStartData< EOT > >::_data;
|
||||||
|
|
||||||
bool operator()()
|
bool operator()()
|
||||||
{
|
{
|
||||||
|
|
@ -212,12 +211,12 @@ class IsFinishedMultiStart : public IsFinishedFunction< MultiStartData< EOT, Fit
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class EOT, class FitT >
|
template< class EOT >
|
||||||
class MultiStartStore : public JobStore< MultiStartData< EOT, FitT > >
|
class MultiStartStore : public JobStore< MultiStartData< EOT > >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef typename MultiStartData<EOT,FitT>::ResetAlgo ResetAlgo;
|
typedef typename MultiStartData<EOT>::ResetAlgo ResetAlgo;
|
||||||
typedef eoUF< eoPop<EOT>&, void > ReinitJob;
|
typedef eoUF< eoPop<EOT>&, void > ReinitJob;
|
||||||
typedef eoUF< int, std::vector<int> > GetSeeds;
|
typedef eoUF< int, std::vector<int> > GetSeeds;
|
||||||
|
|
||||||
|
|
@ -234,13 +233,13 @@ class MultiStartStore : public JobStore< MultiStartData< EOT, FitT > >
|
||||||
_getSeeds( getSeeds ),
|
_getSeeds( getSeeds ),
|
||||||
_reinitJob( reinitJob )
|
_reinitJob( reinitJob )
|
||||||
{
|
{
|
||||||
this->_iff = new IsFinishedMultiStart< EOT, FitT >;
|
this->_iff = new IsFinishedMultiStart< EOT >;
|
||||||
this->_iff->needDelete(true);
|
this->_iff->needDelete(true);
|
||||||
this->_stf = new SendTaskMultiStart< EOT, FitT >;
|
this->_stf = new SendTaskMultiStart< EOT >;
|
||||||
this->_stf->needDelete(true);
|
this->_stf->needDelete(true);
|
||||||
this->_hrf = new HandleResponseMultiStart< EOT, FitT >;
|
this->_hrf = new HandleResponseMultiStart< EOT >;
|
||||||
this->_hrf->needDelete(true);
|
this->_hrf->needDelete(true);
|
||||||
this->_ptf = new ProcessTaskMultiStart< EOT, FitT >;
|
this->_ptf = new ProcessTaskMultiStart< EOT >;
|
||||||
this->_ptf->needDelete(true);
|
this->_ptf->needDelete(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -279,31 +278,31 @@ class MultiStartStore : public JobStore< MultiStartData< EOT, FitT > >
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiStartData<EOT, FitT>* data()
|
MultiStartData<EOT>* data()
|
||||||
{
|
{
|
||||||
return &_data;
|
return &_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MultiStartData< EOT, FitT > _data;
|
MultiStartData< EOT > _data;
|
||||||
|
|
||||||
GetSeeds & _getSeeds;
|
GetSeeds & _getSeeds;
|
||||||
ReinitJob & _reinitJob;
|
ReinitJob & _reinitJob;
|
||||||
int _masterRank;
|
int _masterRank;
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class EOT, class FitT >
|
template< class EOT >
|
||||||
class MultiStart : public OneShotJob< MultiStartData< EOT, FitT > >
|
class MultiStart : public OneShotJob< MultiStartData< EOT > >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
MultiStart( AssignmentAlgorithm & algo,
|
MultiStart( AssignmentAlgorithm & algo,
|
||||||
int masterRank,
|
int masterRank,
|
||||||
MultiStartStore< EOT, FitT > & store,
|
MultiStartStore< EOT > & store,
|
||||||
// dynamic parameters
|
// dynamic parameters
|
||||||
int runs,
|
int runs,
|
||||||
const std::vector<int>& seeds = std::vector<int>() ) :
|
const std::vector<int>& seeds = std::vector<int>() ) :
|
||||||
OneShotJob< MultiStartData< EOT, FitT > >( algo, masterRank, store )
|
OneShotJob< MultiStartData< EOT > >( algo, masterRank, store )
|
||||||
{
|
{
|
||||||
store.init( algo.idles(), runs );
|
store.init( algo.idles(), runs );
|
||||||
}
|
}
|
||||||
|
|
@ -313,14 +312,14 @@ class MultiStart : public OneShotJob< MultiStartData< EOT, FitT > >
|
||||||
return this->store.data()->bestIndividual;
|
return this->store.data()->bestIndividual;
|
||||||
}
|
}
|
||||||
|
|
||||||
FitT best_fitness()
|
typename EOT::Fitness best_fitness()
|
||||||
{
|
{
|
||||||
return this->store.data()->bestFitness;
|
return this->store.data()->bestFitness;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class EOT, class FitT>
|
template<class EOT>
|
||||||
struct DummyGetSeeds : public MultiStartStore<EOT,FitT>::GetSeeds
|
struct DummyGetSeeds : public MultiStartStore<EOT>::GetSeeds
|
||||||
{
|
{
|
||||||
std::vector<int> operator()( int n )
|
std::vector<int> operator()( int n )
|
||||||
{
|
{
|
||||||
|
|
@ -328,8 +327,8 @@ struct DummyGetSeeds : public MultiStartStore<EOT,FitT>::GetSeeds
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class EOT, class FitT>
|
template<class EOT>
|
||||||
struct GetRandomSeeds : public MultiStartStore<EOT,FitT>::GetSeeds
|
struct GetRandomSeeds : public MultiStartStore<EOT>::GetSeeds
|
||||||
{
|
{
|
||||||
std::vector<int> operator()( int n )
|
std::vector<int> operator()( int n )
|
||||||
{
|
{
|
||||||
|
|
@ -341,8 +340,8 @@ struct GetRandomSeeds : public MultiStartStore<EOT,FitT>::GetSeeds
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class EOT, class FitT>
|
template<class EOT>
|
||||||
struct ReinitMultiEA : public MultiStartStore<EOT,FitT>::ReinitJob
|
struct ReinitMultiEA : public MultiStartStore<EOT>::ReinitJob
|
||||||
{
|
{
|
||||||
ReinitMultiEA( const eoPop<EOT>& pop, eoEvalFunc<EOT>& eval ) : _originalPop( pop ), _eval( eval )
|
ReinitMultiEA( const eoPop<EOT>& pop, eoEvalFunc<EOT>& eval ) : _originalPop( pop ), _eval( eval )
|
||||||
{
|
{
|
||||||
|
|
@ -363,8 +362,8 @@ struct ReinitMultiEA : public MultiStartStore<EOT,FitT>::ReinitJob
|
||||||
eoEvalFunc<EOT>& _eval;
|
eoEvalFunc<EOT>& _eval;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class EOT, class FitT>
|
template<class EOT>
|
||||||
struct ResetAlgoEA : public MultiStartStore<EOT,FitT>::ResetAlgo
|
struct ResetAlgoEA : public MultiStartStore<EOT>::ResetAlgo
|
||||||
{
|
{
|
||||||
ResetAlgoEA( eoGenContinue<EOT> & continuator ) :
|
ResetAlgoEA( eoGenContinue<EOT> & continuator ) :
|
||||||
_continuator( continuator ),
|
_continuator( continuator ),
|
||||||
|
|
@ -501,14 +500,14 @@ int main(int argc, char **argv)
|
||||||
eval, continuator);
|
eval, continuator);
|
||||||
|
|
||||||
DynamicAssignmentAlgorithm assignmentAlgo;
|
DynamicAssignmentAlgorithm assignmentAlgo;
|
||||||
MultiStartStore< Indi, IndiFitness > store(
|
MultiStartStore< Indi > store(
|
||||||
gga,
|
gga,
|
||||||
DEFAULT_MASTER,
|
DEFAULT_MASTER,
|
||||||
*new ReinitMultiEA< Indi, IndiFitness >( pop, eval ),
|
*new ReinitMultiEA< Indi >( pop, eval ),
|
||||||
*new ResetAlgoEA< Indi, IndiFitness >( continuator ),
|
*new ResetAlgoEA< Indi >( continuator ),
|
||||||
*new DummyGetSeeds< Indi, IndiFitness >());
|
*new DummyGetSeeds< Indi >());
|
||||||
|
|
||||||
MultiStart< Indi, IndiFitness > msjob( assignmentAlgo, DEFAULT_MASTER, store, 5 );
|
MultiStart< Indi > msjob( assignmentAlgo, DEFAULT_MASTER, store, 5 );
|
||||||
msjob.run();
|
msjob.run();
|
||||||
|
|
||||||
if( msjob.isMaster() )
|
if( msjob.isMaster() )
|
||||||
|
|
@ -516,7 +515,7 @@ int main(int argc, char **argv)
|
||||||
std::cout << "Global best individual has fitness " << msjob.best_fitness() << std::endl;
|
std::cout << "Global best individual has fitness " << msjob.best_fitness() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiStart< Indi, IndiFitness > msjob10( assignmentAlgo, DEFAULT_MASTER, store, 10 );
|
MultiStart< Indi > msjob10( assignmentAlgo, DEFAULT_MASTER, store, 10 );
|
||||||
msjob10.run();
|
msjob10.run();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Reference in a new issue