Add integration method, observer pattern from policy to island

This commit is contained in:
quemy 2012-11-10 19:22:59 +01:00
commit 477dbe49a9
5 changed files with 67 additions and 13 deletions

View file

@ -38,6 +38,7 @@ paradiseo::smp::Island<EOAlgo,EOT>::Island(unsigned _popSize, eoInit<EOT>& _chro
intPolicy(_intPolicy) intPolicy(_intPolicy)
{ {
static_assert(std::is_base_of<eoAlgo<EOT>,EOAlgo<EOT>>::value, "Algorithm must inherit from eoAlgo<EOT>"); static_assert(std::is_base_of<eoAlgo<EOT>,EOAlgo<EOT>>::value, "Algorithm must inherit from eoAlgo<EOT>");
_migPolicy.addObserver(this);
} }
template<template <class> class EOAlgo, class EOT> template<template <class> class EOAlgo, class EOT>
@ -49,7 +50,7 @@ void paradiseo::smp::Island<EOAlgo,EOT>::operator()()
template<template <class> class EOAlgo, class EOT> template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::update(eoPop<EOT>& _data) void paradiseo::smp::Island<EOAlgo,EOT>::update(eoPop<EOT>& _data)
{ {
algo(pop); listImigrants.push_back(&_data);
} }
template<template <class> class EOAlgo, class EOT> template<template <class> class EOAlgo, class EOT>
@ -59,8 +60,21 @@ eoPop<EOT>& paradiseo::smp::Island<EOAlgo,EOT>::getPop()
} }
template<template <class> class EOAlgo, class EOT> template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::send() void paradiseo::smp::Island<EOAlgo,EOT>::send(eoSelect<EOT>& _select)
{ {
algo(pop); std::cout << "Ile lance la migration" << std::endl;
eoPop<EOT> migPop;
_select(pop, migPop);
std::cout << " La population migrante est :" << std::endl << migPop << std::endl;
} }
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::receive(void)
{
while (!listImigrants.empty())
{
intPolicy(pop, *(listImigrants.front()));
listImigrants.pop();
}
}

View file

@ -35,6 +35,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#include <utility> #include <utility>
#include <eo> #include <eo>
#include <abstractIsland.h>
#include <migPolicy.h> #include <migPolicy.h>
#include <PPExpander.h> #include <PPExpander.h>
#include <contWrapper.h> #include <contWrapper.h>
@ -46,7 +47,7 @@ namespace smp
{ {
template<template <class> class EOAlgo, class EOT> template<template <class> class EOAlgo, class EOT>
class Island : private ContWrapper<EOT> class Island : private ContWrapper<EOT>, public AIsland<EOT>
{ {
public: public:
template<class... Args> template<class... Args>
@ -58,12 +59,15 @@ public:
eoPop<EOT>& getPop(); eoPop<EOT>& getPop();
//friend void Policy<EOT>::notifyIsland(eoSelect<EOT>& _select) const;
protected: protected:
void send(); void send(eoSelect<EOT>& _select);
void receive(void);
eoPop<EOT> pop; eoPop<EOT> pop;
EOAlgo<EOT> algo; EOAlgo<EOT> algo;
std::queue<std::pair<unsigned, eoPop<EOT>>> listImigrants; std::queue<eoPop<EOT>*> listImigrants;
eoReplacement<EOT>& intPolicy; eoReplacement<EOT>& intPolicy;
}; };

View file

@ -30,14 +30,20 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#ifndef MIG_POLICY_H_ #ifndef MIG_POLICY_H_
#define MIG_POLICY_H_ #define MIG_POLICY_H_
#include <eo> #include <set>
#include <eo>
#include <migPolicyElement.h> #include <migPolicyElement.h>
#include <island.h>
#include <abstractIsland.h>
namespace paradiseo namespace paradiseo
{ {
namespace smp namespace smp
{ {
// Forward declaration
template <class EOT>
class AIsland;
template <class EOT> template <class EOT>
class Policy : public eoContinue<EOT>, public std::vector<PolicyElement<EOT>> class Policy : public eoContinue<EOT>, public std::vector<PolicyElement<EOT>>
@ -50,12 +56,34 @@ public:
{ {
std::cout << "."; std::cout << ".";
if(!elem(_pop)) if(!elem(_pop))
{
std::cout << "On lance l'emmigration" << std::endl; std::cout << "On lance l'emmigration" << std::endl;
notifyIsland(elem.getSelect());
}
} }
return true; // Always return true because it never stops the algorithm return true; // Always return true because it never stops the algorithm
} }
void addObserver(AIsland<EOT>* _observer)
{
observers.insert(_observer);
}
void removeObserver(AIsland<EOT>* _observer)
{
observers.erase(_observer);
}
protected:
void notifyIsland(eoSelect<EOT>& _select) const
{
std::cout << "On notifie les iles" << std::endl;
for (AIsland<EOT>* it : observers)
it->send(_select);
}
std::set<AIsland<EOT>*> observers;
}; };
} }

View file

@ -64,6 +64,11 @@ public :
criteria.add(_criteria); criteria.add(_criteria);
} }
eoSelect<EOT>& getSelect()
{
return selection;
}
protected : protected :
eoSelect<EOT>& selection; eoSelect<EOT>& selection;
eoContinue<EOT>& criteria; eoContinue<EOT>& criteria;

View file

@ -51,10 +51,13 @@ int main(void)
{ {
// Emigration policy // Emigration policy
eoPeriodicContinue<Indi> criteria(25); // We mig each gen eoPeriodicContinue<Indi> criteria(25); // We mig each gen
eoDetTournamentSelect<Indi> selectOne(2);
eoSelectNumber<Indi> who(selectOne, 5);
Policy<Indi> pol; Policy<Indi> pol;
pol.push_back(PolicyElement<Indi>(select, criteria)); pol.push_back(PolicyElement<Indi>(who, criteria));
Island<eoEasyEA,Indi> test(param.popSize, chromInit, replace, pol, genCont, plainEval, select, transform, replace); Island<eoEasyEA,Indi> test(param.popSize, chromInit, replace, pol, genCont, plainEval, select, transform, replace);
test(); test();
cout << test.getPop() << endl; cout << test.getPop() << endl;
} }