Add notion of base EOT for heterogeneous model. Island can be templated on a EOT regardless the EOT of its model. Island must have a conversion function from and to base EOT. Base EOT is optionnal and would be considered as EOT. In that case, conversion functions would be identity function.

This commit is contained in:
quemy 2012-12-02 15:36:30 +01:00
commit f67ee442c7
11 changed files with 86 additions and 58 deletions

View file

@ -63,16 +63,7 @@ public:
*/
virtual void setModel(IslandModel<EOT>* _model) = 0;
/**
* Send population to mediator
* @param _select Method to select EOT to send
*/
virtual void send(eoSelect<EOT>& _select) = 0;
/**
* Check if there is population to receive
*/
virtual void receive(void) = 0;
/**
* Check if there is population to receive or to emigrate

View file

@ -27,10 +27,10 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
template<class EOT>
paradiseo::smp::ContWrapper<EOT>::ContWrapper(eoContinue<EOT>& _cont, AIsland<EOT>* island) :
template<class EOT, class bEOT>
paradiseo::smp::ContWrapper<EOT, bEOT>::ContWrapper(eoContinue<EOT>& _cont, AIsland<bEOT>* island) :
ck(_cont),
islandNotifier(island, &AIsland<EOT>::check)
islandNotifier(island, &AIsland<bEOT>::check)
{
ck.add(islandNotifier);
}

View file

@ -50,7 +50,7 @@ By using the wrapper, we do not have to modify original continuators inside the
it avoids some side effects.
*/
template<class EOT>
template<class EOT, class bEOT>
class ContWrapper
{
public:
@ -59,11 +59,11 @@ public:
* @param _cont Original continuators
* @param _policy Policy to wrap with continuators
*/
ContWrapper(eoContinue<EOT>& _cont, AIsland<EOT>* island);
ContWrapper(eoContinue<EOT>& _cont, AIsland<bEOT>* island);
protected:
eoCheckPoint<EOT> ck;
IslandNotifier<EOT> islandNotifier;
IslandNotifier<bEOT> islandNotifier;
};
#include <contWrapper.cpp>

View file

@ -27,12 +27,12 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
template<template <class> class EOAlgo, class EOT>
template<template <class> class EOAlgo, class EOT, class bEOT>
template<class... Args>
paradiseo::smp::Island<EOAlgo,EOT>::Island(eoPop<EOT>& _pop, IntPolicy<EOT>& _intPolicy, MigPolicy<EOT>& _migPolicy, Args&... args) :
paradiseo::smp::Island<EOAlgo,EOT,bEOT>::Island(std::function<EOT(bEOT&)> _convertFromBase, std::function<bEOT(EOT&)> _convertToBase, eoPop<EOT>& _pop, IntPolicy<EOT>& _intPolicy, MigPolicy<EOT>& _migPolicy, Args&... args) :
// The PPExpander looks for the continuator in the parameters pack.
// The private inheritance of ContWrapper wraps the continuator and add islandNotifier.
ContWrapper<EOT>(Loop<Args...>().template findValue<eoContinue<EOT>>(args...), this),
ContWrapper<EOT, bEOT>(Loop<Args...>().template findValue<eoContinue<EOT>>(args...), this),
// We inject the wrapped continuator by tag dispatching method during the algorithm construction.
algo(EOAlgo<EOT>(wrap_pp<eoContinue<EOT>>(this->ck,args)...)),
// With the PPE we look for the eval function in order to evaluate EOT to integrate
@ -41,14 +41,26 @@ paradiseo::smp::Island<EOAlgo,EOT>::Island(eoPop<EOT>& _pop, IntPolicy<EOT>& _in
intPolicy(_intPolicy),
migPolicy(_migPolicy),
stopped(false),
model(nullptr)
model(nullptr),
convertFromBase(_convertFromBase),
convertToBase(_convertToBase)
{
// Check in compile time the inheritance thanks to type_trait.
static_assert(std::is_base_of<eoAlgo<EOT>,EOAlgo<EOT>>::value, "Algorithm must inherit from eoAlgo<EOT>");
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::operator()()
template<template <class> class EOAlgo, class EOT, class bEOT>
template<class... Args>
paradiseo::smp::Island<EOAlgo,EOT,bEOT>::Island(eoPop<EOT>& _pop, IntPolicy<EOT>& _intPolicy, MigPolicy<EOT>& _migPolicy, Args&... args) :
Island(
// Default conversion functions for homogeneous islands
[](bEOT& i) -> EOT { return (EOT)i; },
[](EOT& i) -> bEOT { return (bEOT)i; },
_pop, _intPolicy, _migPolicy, args...)
{ }
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::operator()()
{
stopped = false;
algo(pop);
@ -58,20 +70,20 @@ void paradiseo::smp::Island<EOAlgo,EOT>::operator()()
message.join();
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::setModel(IslandModel<EOT>* _model)
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::setModel(IslandModel<bEOT>* _model)
{
model = _model;
}
template<template <class> class EOAlgo, class EOT>
eoPop<EOT>& paradiseo::smp::Island<EOAlgo,EOT>::getPop()
template<template <class> class EOAlgo, class EOT, class bEOT>
eoPop<EOT>& paradiseo::smp::Island<EOAlgo,EOT,bEOT>::getPop()
{
return pop;
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::check()
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::check()
{
// Sending
for(PolicyElement<EOT>& elem : migPolicy)
@ -82,37 +94,50 @@ void paradiseo::smp::Island<EOAlgo,EOT>::check()
receive();
}
template<template <class> class EOAlgo, class EOT>
bool paradiseo::smp::Island<EOAlgo,EOT>::isStopped(void) const
template<template <class> class EOAlgo, class EOT, class bEOT>
bool paradiseo::smp::Island<EOAlgo,EOT,bEOT>::isStopped(void) const
{
return (bool)stopped;
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::send(eoSelect<EOT>& _select)
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::send(eoSelect<EOT>& _select)
{
// Allow island to work alone
if(model != nullptr)
{
eoPop<EOT> migPop;
_select(pop, migPop);
// Convert pop to base pop
eoPop<bEOT> baseMigPop;
for(auto& indi : migPop)
baseMigPop.push_back(convertToBase(indi));
std::cout << "On envoie de l'île : " << migPop << std::endl;
// Delete delivered messages
for(auto it = sentMessages.begin(); it != sentMessages.end(); it++)
if(!it->joinable())
sentMessages.erase(it);
sentMessages.push_back(std::thread(&IslandModel<EOT>::update, model, migPop, this));
sentMessages.push_back(std::thread(&IslandModel<bEOT>::update, model, baseMigPop, this));
}
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::receive(void)
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::receive(void)
{
std::lock_guard<std::mutex> lock(this->m);
while (!listImigrants.empty())
{
eoPop<EOT> offspring = listImigrants.front();
std::cout << "On reçoit dans l'île : " << listImigrants.size() << std::endl;
eoPop<bEOT> base_offspring = listImigrants.front();
// Convert objects from base to our objects type
eoPop<EOT> offspring;
for(auto& indi : base_offspring)
offspring.push_back(convertFromBase(indi));
// Evaluate objects to integrate
for(auto& indi : offspring)
@ -124,9 +149,10 @@ void paradiseo::smp::Island<EOAlgo,EOT>::receive(void)
}
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::update(eoPop<EOT> _data)
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::update(eoPop<bEOT> _data)
{
std::cout << "On update dans l'île" << std::endl;
std::lock_guard<std::mutex> lock(this->m);
listImigrants.push(_data);
}

View file

@ -60,10 +60,22 @@ The island wraps an algorithm and provide mecanism for emigration and integratio
@see smp::AbstractIsland, smp::MigPolicy
*/
template<template <class> class EOAlgo, class EOT>
class Island : private ContWrapper<EOT>, public AIsland<EOT>
template<template <class> class EOAlgo, class EOT, class bEOT = EOT>
class Island : private ContWrapper<EOT, bEOT>, public AIsland<bEOT>
{
public:
/**
* Constructor
* @param _convertFromBase Function to convert EOT from base EOT
* @param _convertToBase Function to convert base EOT to EOT
* @param _popSize Size of the algorithm population.
* @param _chromInit Population initializer.
* @param _intPolicy Integration policy
* @param _migPolicy Migration policy
* @param args Parameters to construct the algorithm.
*/
template<class... Args>
Island(std::function<EOT(bEOT&)> _convertFromBase, std::function<bEOT(EOT&)> _convertToBase, eoPop<EOT>& pop, IntPolicy<EOT>& _intPolicy, MigPolicy<EOT>& _migPolicy, Args&... args);
/**
* Constructor
* @param _popSize Size of the algorithm population.
@ -84,7 +96,7 @@ public:
* Set model
* @param _model Pointer to the Island Model corresponding
*/
virtual void setModel(IslandModel<EOT>* _model);
virtual void setModel(IslandModel<bEOT>* _model);
/**
* Return a reference to the island population.
@ -101,7 +113,7 @@ public:
* Update the list of imigrants.
* @param _data Elements to integrate in the main population.
*/
void update(eoPop<EOT> _data);
void update(eoPop<bEOT> _data);
/**
* Check if the algorithm is stopped.
@ -125,12 +137,14 @@ protected:
eoEvalFunc<EOT>& eval;
eoPop<EOT>& pop;
EOAlgo<EOT> algo;
std::queue<eoPop<EOT>> listImigrants;
std::queue<eoPop<bEOT>> listImigrants;
IntPolicy<EOT>& intPolicy;
MigPolicy<EOT>& migPolicy;
std::atomic<bool> stopped;
std::vector<std::thread> sentMessages;
IslandModel<EOT>* model;
IslandModel<bEOT>* model;
std::function<EOT(bEOT&)> convertFromBase;
std::function<bEOT(EOT&)> convertToBase;
};
#include <island.cpp>

View file

@ -51,8 +51,8 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
std::vector<std::thread> threads(islands.size());
// Preparing islands
for(auto it : islands)
it.second = true;
for(auto& it : islands)
it.second = true; // Indicate islands are running
// Construct topology according to the number of islands
topo.construct(islands.size());
@ -75,7 +75,7 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
[](std::pair<AIsland<EOT>*, bool>& i) -> bool
{ return i.second; } );
};
while(workingIslands() > 0)
{
// Count working islands
@ -107,6 +107,7 @@ template<class EOT>
void paradiseo::smp::IslandModel<EOT>::update(eoPop<EOT> _data, AIsland<EOT>* _island)
{
std::lock_guard<std::mutex> lock(m);
std::cout << "Mediateur reçoit ! " << _data << std::endl;
listEmigrants.push(std::pair<eoPop<EOT>,AIsland<EOT>*>(_data, _island));
}
@ -133,6 +134,7 @@ void paradiseo::smp::IslandModel<EOT>::send(void)
std::lock_guard<std::mutex> lock(m);
while (!listEmigrants.empty())
{
std::cout << "Mediator envoie ! " << listEmigrants.size() << std::endl;
// Get the neighbors
unsigned idFrom = table.getLeft()[listEmigrants.front().second];
std::vector<unsigned> neighbors = topo.getIdNeighbors(idFrom);

View file

@ -52,6 +52,8 @@ island model pattern according to a topology.
@see smp::Island, smp::MigPolicy
*/
template<class EOT>
class IslandModel
{

View file

@ -35,6 +35,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#include <scheduler.h>
#include <islandModel.h>
#include <island.h>
#include <abstractIsland.h>
#include <migPolicy.h>
#include <intPolicy.h>
#include <policyElement.h>

View file

@ -19,6 +19,7 @@ set (TEST_LIST
t-smpIsland
t-smpTopo
t-smpMI_Homogeneous
#t-smpMI_Heterogeneous
)
######################################################################################

View file

@ -82,15 +82,6 @@ int main(void)
eoPop<Indi> pop2(param.popSize, chromInit);
Island<eoEasyEA,Indi> test2(pop2, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
// Topology
Topology<Complete> topo;
IslandModel<Indi> model(topo);
model.add(test);
model.add(test2);
//model();
test();
test2();

View file

@ -9,11 +9,11 @@ using namespace std;
int main(void)
{
typedef struct {
unsigned popSize = 100;
unsigned popSize = 10;
unsigned tSize = 2;
double pCross = 0.8;
double pMut = 0.7;
unsigned maxGen = 100;
unsigned maxGen = 10;
} Param;
Param param;