Add IslandModel as islands container and algorithm launcher.

This commit is contained in:
quemy 2012-11-19 19:51:35 +01:00
commit 8d6d32e8cc
10 changed files with 283 additions and 18 deletions

View file

@ -44,6 +44,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "-Wunknown-pragmas -O2" CACHE STRING "" FORCE)
if(SMP)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11 -pthread" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -pthread" CACHE STRING "" FORCE)
add_definitions(-D_GLIBCXX_USE_NANOSLEEP)
endif(SMP)
######################################################################################

View file

@ -30,6 +30,8 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#ifndef ABS_ISLAND_H_
#define ABS_ISLAND_H_
#include <atomic>
#include <eo>
#include <migPolicy.h>
@ -38,6 +40,10 @@ namespace paradiseo
namespace smp
{
// Forward declaration
template<class EOT>
class IslandModel;
/** AbstractIsland: An abstract island.
@see smp::Island
@ -47,6 +53,14 @@ template<class EOT>
class AIsland
{
public:
virtual void operator()() = 0;
/**
* Check if there is population to receive
*/
virtual void setModel(IslandModel<EOT>* _model) = 0;
/**
* Send population to mediator
* @param _select Method to select EOT to send
@ -62,6 +76,9 @@ public:
* Check if there is population to receive or to emigrate
*/
virtual void check(void) = 0;
virtual bool isStopped(void) = 0;
};
}

85
smp/src/bimap.h Normal file
View file

@ -0,0 +1,85 @@
/*
<bimap.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
Alexandre Quemy, Thibault Lasnier - INSA Rouen
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef BIMAP_MODEL_H_
#define BIMAP_H_
#include <set>
#include <map>
namespace paradiseo
{
namespace smp
{
template<class A, class B>
class Bimap
{
public:
void add(A a, B b)
{
ASet.insert(a);
BSet.insert(b);
rightAssociation[&a] = &b;
leftAssociation[&b] = &a;
}
B& getRight(A const a)
{
return *rightAssociation[&a];
}
A& getLeft(B const b)
{
return *leftAssociation[&b];
}
unsigned size()
{
return ASet.size();
}
bool empty()
{
return ASet.empty();
}
protected:
std::set<A> ASet;
std::set<B> BSet;
std::map<A*,B*> rightAssociation;
std::map<B*,A*> leftAssociation;
};
}
}
#endif

View file

@ -39,7 +39,8 @@ paradiseo::smp::Island<EOAlgo,EOT>::Island(unsigned _popSize, eoInit<EOT>& _chro
algo(EOAlgo<EOT>(wrap_pp<eoContinue<EOT>>(this->ck,args)...)),
pop(_popSize, _chromInit),
intPolicy(_intPolicy),
migPolicy(_migPolicy)
migPolicy(_migPolicy),
stopped(false)
{
// 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>");
@ -48,7 +49,15 @@ paradiseo::smp::Island<EOAlgo,EOT>::Island(unsigned _popSize, eoInit<EOT>& _chro
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::operator()()
{
stopped = false;
algo(pop);
stopped = true;
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::setModel(IslandModel<EOT>* _model)
{
model = _model;
}
template<template <class> class EOAlgo, class EOT>
@ -66,25 +75,31 @@ eoPop<EOT>& paradiseo::smp::Island<EOAlgo,EOT>::getPop()
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::check()
{
std::cout << "On check" << std::endl;
// std::cout << "On check" << std::endl;
for(PolicyElement<EOT>& elem : migPolicy)
{
if(!elem(pop))
{
std::cout << "On lance l'emmigration" << std::endl;
// std::cout << "On lance l'emmigration" << std::endl;
send(elem.getSelect());
}
}
receive();
}
template<template <class> class EOAlgo, class EOT>
bool paradiseo::smp::Island<EOAlgo,EOT>::isStopped(void)
{
return (bool)stopped;
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::send(eoSelect<EOT>& _select)
{
std::cout << "Ile lance la migration" << std::endl;
//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;
//std::cout << " La population migrante est :" << std::endl << migPop << std::endl;
}
template<template <class> class EOAlgo, class EOT>

View file

@ -33,9 +33,11 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#include <queue>
#include <vector>
#include <utility>
#include <atomic>
#include <eo>
#include <abstractIsland.h>
#include <islandModel.h>
#include <migPolicy.h>
#include <intPolicy.h>
#include <PPExpander.h>
@ -65,7 +67,13 @@ public:
/**
* Start the island.
*/
void operator()();
void operator()(void);
/**
* Set model
* @param _model Pointer to the Island Model corresponding
*/
virtual void setModel(IslandModel<EOT>* _model);
/**
* Update the list of imigrants.
@ -84,6 +92,8 @@ public:
*/
virtual void check(void);
virtual bool isStopped(void);
protected:
/**
@ -97,11 +107,13 @@ protected:
*/
virtual void receive(void);
IslandModel<EOT>* model;
eoPop<EOT> pop;
EOAlgo<EOT> algo;
std::queue<eoPop<EOT>*> listImigrants;
IntPolicy<EOT>& intPolicy;
MigPolicy<EOT>& migPolicy;
std::atomic<bool> stopped;
};
#include <island.cpp>

103
smp/src/islandModel.h Normal file
View file

@ -0,0 +1,103 @@
/*
<IslandModel.h>
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
Alexandre Quemy, Thibault Lasnier - INSA Rouen
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can ue,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef ISLAND_MODEL_H_
#define ISLAND_MODEL_H_
#include <queue>
#include <algorithm>
#include <utility>
#include <future>
#include <thread>
#include <bimap.h>
#include <abstractIsland.h>
namespace paradiseo
{
namespace smp
{
/** IslandModel
*/
template<class EOT>
class IslandModel
{
public:
void add(AIsland<EOT>& _island)
{
static unsigned i = 0;
islands[i] = &_island;
islands[i]->setModel(this);
i++;
}
void operator()()
{
std::vector<Thread> threads(islands.size());
unsigned i = 0;
for(auto it : islands)
{
threads[i].start(&AIsland<EOT>::operator(), it.second);
i++;
}
unsigned workingThread = islands.size();
while(workingThread > 0)
{
workingThread = islands.size();
for(auto& it : islands)
if(it.second->isStopped())
workingThread--;
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
}
for(auto& thread : threads)
thread.join();
}
void update(eoPop<EOT>* _data, AIsland<EOT>* _island)
{
listEmigrants.insert(std::pair<eoPop<EOT>*,AIsland<EOT>*>(_data, _island));
}
protected:
std::queue<std::pair<eoPop<EOT>*,AIsland<EOT>*>> listEmigrants;
Bimap<unsigned, unsigned> table;
std::map<unsigned, AIsland<EOT>*> islands;
};
}
}
#endif

View file

@ -27,6 +27,7 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <thread>
#include <eo>
template <class EOT>
@ -39,9 +40,9 @@ template <class EOT>
bool paradiseo::smp::PolicyElement<EOT>::operator()(const eoPop<EOT>& _pop)
{
// DEBUG
static int i = 0;
std::cout << i << std::endl;
i++;
//static int i = 0;
//std::cout << " " << std::this_thread::get_id() << std::endl;
//i++;
// END DEBUG
return criteria(_pop);

View file

@ -114,9 +114,8 @@ void paradiseo::smp::Scheduler<EOT,Policy>::operator()(eoUF<EOT&, void>& func, e
planning[i] *= 2;
}
}
/* A nanosleep can increase performances by 10% but
as it is not supported by Fedora atm, I deactivate it. */
//std::this_thread::sleep_for(std::chrono::nanoseconds(10));
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
}
done = true;

View file

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

View file

@ -13,7 +13,7 @@ int main(void)
unsigned tSize = 2;
double pCross = 0.8;
double pMut = 0.7;
unsigned maxGen = 100;
unsigned maxGen = 10;
} Param;
Param param;
@ -43,24 +43,55 @@ int main(void)
eoPlusReplacement<Indi> replace;
eoGenContinue<Indi> genCont(param.maxGen); // generation continuation
eoGenContinue<Indi> genCont_2(param.maxGen); // generation continuation
// Define population
eoPop<Indi> pop(param.popSize, chromInit);
try
{
// Emigration policy
eoPeriodicContinue<Indi> criteria(25); // We mig each gen
// Island 1
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria(5);
eoDetTournamentSelect<Indi> selectOne(2);
eoSelectNumber<Indi> who(selectOne, 5);
eoSelectNumber<Indi> who(selectOne, 1);
MigPolicy<Indi> migPolicy;
migPolicy.push_back(PolicyElement<Indi>(who, criteria));
IntPolicy<Indi> intPolicy(plainEval,replace);
// // Integration policy
eoPlusReplacement<Indi> replace_1;
IntPolicy<Indi> intPolicy(plainEval,replace_1);
Island<eoEasyEA,Indi> test(param.popSize, chromInit, intPolicy, migPolicy, genCont, plainEval, select, transform, replace);
test();
// Island 2
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria_2(5);
eoDetTournamentSelect<Indi> selectOne_2(2);
eoSelectNumber<Indi> who_2(selectOne_2, 1);
MigPolicy<Indi> migPolicy_2;
migPolicy_2.push_back(PolicyElement<Indi>(who_2, criteria_2));
// // Integration policy
eoPlusReplacement<Indi> replace_2;
IntPolicy<Indi> intPolicy_2(plainEval,replace_2);
Island<eoEasyEA,Indi> test2(param.popSize, chromInit, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
//test();
IslandModel<Indi> model;
model.add(test);
model.add(test2);
model();
cout << test.getPop() << endl;
cout << test2.getPop() << endl;
}
catch(exception& e)
{