Clean test.
This commit is contained in:
parent
5a4596862b
commit
23a0b8f77b
6 changed files with 131 additions and 92 deletions
|
|
@ -78,6 +78,8 @@ public:
|
||||||
* @return true if stopped
|
* @return true if stopped
|
||||||
*/
|
*/
|
||||||
virtual bool isStopped(void) const = 0;
|
virtual bool isStopped(void) const = 0;
|
||||||
|
|
||||||
|
virtual void receive(void) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::mutex m;
|
std::mutex m;
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,11 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual bool isStopped(void) const;
|
virtual bool isStopped(void) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if there is population to receive
|
||||||
|
*/
|
||||||
|
virtual void receive(void);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -129,11 +134,6 @@ protected:
|
||||||
*/
|
*/
|
||||||
virtual void send(eoSelect<EOT>& _select);
|
virtual void send(eoSelect<EOT>& _select);
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if there is population to receive
|
|
||||||
*/
|
|
||||||
virtual void receive(void);
|
|
||||||
|
|
||||||
eoEvalFunc<EOT>& eval;
|
eoEvalFunc<EOT>& eval;
|
||||||
eoPop<EOT>& pop;
|
eoPop<EOT>& pop;
|
||||||
EOAlgo<EOT> algo;
|
EOAlgo<EOT> algo;
|
||||||
|
|
|
||||||
|
|
@ -48,18 +48,12 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
|
||||||
{
|
{
|
||||||
running = true;
|
running = true;
|
||||||
|
|
||||||
|
// INIT PART
|
||||||
|
// Create topology, table and initialize islands
|
||||||
|
initModel();
|
||||||
|
|
||||||
std::vector<std::thread> threads(islands.size());
|
std::vector<std::thread> threads(islands.size());
|
||||||
|
|
||||||
// Preparing islands
|
|
||||||
for(auto& it : islands)
|
|
||||||
it.second = true; // Indicate islands are running
|
|
||||||
|
|
||||||
// Construct topology according to the number of islands
|
|
||||||
topo.construct(islands.size());
|
|
||||||
|
|
||||||
// Create table
|
|
||||||
table = createTable();
|
|
||||||
|
|
||||||
// Launching threads
|
// Launching threads
|
||||||
unsigned i = 0;
|
unsigned i = 0;
|
||||||
for(auto it : islands)
|
for(auto it : islands)
|
||||||
|
|
@ -68,14 +62,15 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
// Lambda function in order to know the number of working islands
|
||||||
std::function<int()> workingIslands = [this]() -> int
|
std::function<int()> workingIslands = [this]() -> int
|
||||||
{
|
{
|
||||||
return (int)std::count_if(std::begin(islands), std::end(islands),
|
return (int)std::count_if(std::begin(islands), std::end(islands),
|
||||||
[](std::pair<AIsland<EOT>*, bool>& i) -> bool
|
[](std::pair<AIsland<EOT>*, bool>& i) -> bool
|
||||||
{ return i.second; } );
|
{ return i.second; } );
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// SCHEDULING PART
|
||||||
while(workingIslands() > 0)
|
while(workingIslands() > 0)
|
||||||
{
|
{
|
||||||
// Count working islands
|
// Count working islands
|
||||||
|
|
@ -93,13 +88,32 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
|
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ENDING PART
|
||||||
|
// Wait the end of algorithms
|
||||||
for(auto& thread : threads)
|
for(auto& thread : threads)
|
||||||
thread.join();
|
thread.join();
|
||||||
|
|
||||||
|
// Send last population
|
||||||
|
while(!listEmigrants.empty())
|
||||||
|
send();
|
||||||
|
|
||||||
|
// Wait the end of messages sending
|
||||||
for(auto& message : sentMessages)
|
for(auto& message : sentMessages)
|
||||||
message.join();
|
message.join();
|
||||||
|
|
||||||
|
// Force last integration
|
||||||
|
i = 0;
|
||||||
|
for(auto it : islands)
|
||||||
|
{
|
||||||
|
threads[i] = std::thread(&AIsland<EOT>::receive, it.first);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait the end of the last integration
|
||||||
|
for(auto& thread : threads)
|
||||||
|
thread.join();
|
||||||
|
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -120,6 +134,7 @@ void paradiseo::smp::IslandModel<EOT>::setTopology(AbstractTopology& _topo)
|
||||||
// If we change when the algorithm is running, we need to recontruct the topo
|
// If we change when the algorithm is running, we need to recontruct the topo
|
||||||
if(running)
|
if(running)
|
||||||
{
|
{
|
||||||
|
std::cout << "Changing topology" << std::endl;
|
||||||
topo.construct(islands.size());
|
topo.construct(islands.size());
|
||||||
// If we change the topology during the algorithm, we need to isolate stopped islands
|
// If we change the topology during the algorithm, we need to isolate stopped islands
|
||||||
for(auto it : islands)
|
for(auto it : islands)
|
||||||
|
|
@ -132,7 +147,7 @@ template<class EOT>
|
||||||
void paradiseo::smp::IslandModel<EOT>::send(void)
|
void paradiseo::smp::IslandModel<EOT>::send(void)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(m);
|
std::lock_guard<std::mutex> lock(m);
|
||||||
while (!listEmigrants.empty())
|
if (!listEmigrants.empty())
|
||||||
{
|
{
|
||||||
//std::cout << "Mediator envoie ! " << listEmigrants.size() << std::endl;
|
//std::cout << "Mediator envoie ! " << listEmigrants.size() << std::endl;
|
||||||
// Get the neighbors
|
// Get the neighbors
|
||||||
|
|
@ -148,6 +163,26 @@ void paradiseo::smp::IslandModel<EOT>::send(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class EOT>
|
||||||
|
bool paradiseo::smp::IslandModel<EOT>::isRunning() const
|
||||||
|
{
|
||||||
|
return (bool)running;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class EOT>
|
||||||
|
void paradiseo::smp::IslandModel<EOT>::initModel(void)
|
||||||
|
{
|
||||||
|
// Preparing islands
|
||||||
|
for(auto& it : islands)
|
||||||
|
it.second = true; // Indicate islands are active
|
||||||
|
|
||||||
|
// Construct topology according to the number of islands
|
||||||
|
topo.construct(islands.size());
|
||||||
|
|
||||||
|
// Create table
|
||||||
|
table = createTable();
|
||||||
|
}
|
||||||
|
|
||||||
template<class EOT>
|
template<class EOT>
|
||||||
Bimap<unsigned, AIsland<EOT>*> paradiseo::smp::IslandModel<EOT>::createTable()
|
Bimap<unsigned, AIsland<EOT>*> paradiseo::smp::IslandModel<EOT>::createTable()
|
||||||
{
|
{
|
||||||
|
|
@ -161,9 +196,3 @@ Bimap<unsigned, AIsland<EOT>*> paradiseo::smp::IslandModel<EOT>::createTable()
|
||||||
|
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class EOT>
|
|
||||||
bool paradiseo::smp::IslandModel<EOT>::isRunning() const
|
|
||||||
{
|
|
||||||
return (bool)running;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,11 @@ protected:
|
||||||
*/
|
*/
|
||||||
void send(void);
|
void send(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize islands, topology and table before starting the model
|
||||||
|
*/
|
||||||
|
void initModel(void);
|
||||||
|
|
||||||
Bimap<unsigned, AIsland<EOT>*> createTable();
|
Bimap<unsigned, AIsland<EOT>*> createTable();
|
||||||
|
|
||||||
std::queue<std::pair<eoPop<EOT>,AIsland<EOT>*>> listEmigrants;
|
std::queue<std::pair<eoPop<EOT>,AIsland<EOT>*>> listEmigrants;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
// TODO : Un vrai test, propre, qui veut dire quelque chose :)
|
||||||
|
|
||||||
#include <smp>
|
#include <smp>
|
||||||
#include <eo>
|
#include <eo>
|
||||||
#include <ga.h>
|
#include <ga.h>
|
||||||
|
|
|
||||||
|
|
@ -8,107 +8,108 @@ using namespace std;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
// Defining parameters
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned popSize = 10;
|
unsigned popSize = 1000;
|
||||||
unsigned tSize = 2;
|
unsigned tSize = 2;
|
||||||
double pCross = 0.8;
|
double pCross = 0.8;
|
||||||
double pMut = 0.7;
|
double pMut = 0.7;
|
||||||
unsigned maxGen = 10;
|
unsigned maxGen = 1000;
|
||||||
} Param;
|
} Param;
|
||||||
|
|
||||||
Param param;
|
Param param;
|
||||||
|
|
||||||
|
// Fixing the seed
|
||||||
rng.reseed(42);
|
rng.reseed(42);
|
||||||
|
|
||||||
|
// Load instance
|
||||||
loadInstances("t-data.dat", n, bkv, a, b);
|
loadInstances("t-data.dat", n, bkv, a, b);
|
||||||
|
|
||||||
// Evaluation function
|
//Common part to all islands
|
||||||
IndiEvalFunc plainEval;
|
IndiEvalFunc plainEval;
|
||||||
|
|
||||||
// Init a solution
|
|
||||||
IndiInit chromInit;
|
IndiInit chromInit;
|
||||||
|
|
||||||
// Define selection
|
|
||||||
eoDetTournamentSelect<Indi> selectOne(param.tSize);
|
eoDetTournamentSelect<Indi> selectOne(param.tSize);
|
||||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||||
|
|
||||||
// Define operators for crossover and mutation
|
|
||||||
IndiXover Xover; // CROSSOVER
|
IndiXover Xover; // CROSSOVER
|
||||||
IndiSwapMutation mutationSwap; // MUTATION
|
IndiSwapMutation mutationSwap; // MUTATION
|
||||||
|
|
||||||
// Encapsule in a tranform operator
|
|
||||||
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
|
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
|
||||||
|
|
||||||
// Define replace operator
|
|
||||||
eoPlusReplacement<Indi> replace;
|
eoPlusReplacement<Indi> replace;
|
||||||
|
|
||||||
eoGenContinue<Indi> genCont(param.maxGen+100); // generation continuation
|
|
||||||
eoGenContinue<Indi> genCont_2(param.maxGen); // generation continuation
|
|
||||||
eoGenContinue<Indi> genCont_3(param.maxGen); // generation continuation
|
|
||||||
|
|
||||||
// Define population
|
// ISLAND 1
|
||||||
|
// // Algorithm part
|
||||||
|
eoGenContinue<Indi> genCont(param.maxGen+100);
|
||||||
eoPop<Indi> pop(param.popSize, chromInit);
|
eoPop<Indi> pop(param.popSize, chromInit);
|
||||||
|
// // Emigration policy
|
||||||
|
// // // Element 1
|
||||||
|
eoPeriodicContinue<Indi> criteria(5);
|
||||||
|
eoDetTournamentSelect<Indi> selectOne1(20);
|
||||||
|
eoSelectNumber<Indi> who(selectOne1, 3);
|
||||||
|
|
||||||
|
MigPolicy<Indi> migPolicy;
|
||||||
|
migPolicy.push_back(PolicyElement<Indi>(who, criteria));
|
||||||
|
|
||||||
|
// // Integration policy
|
||||||
|
eoPlusReplacement<Indi> intPolicy;
|
||||||
|
|
||||||
|
Island<eoEasyEA,Indi> test(pop, intPolicy, migPolicy, genCont, plainEval, select, transform, replace);
|
||||||
|
|
||||||
|
// ISLAND 1
|
||||||
|
// // Algorithm part
|
||||||
|
eoGenContinue<Indi> genCont_2(param.maxGen); // generation continuation
|
||||||
|
eoPop<Indi> pop2(30, chromInit);
|
||||||
|
// // Emigration policy
|
||||||
|
// // // Element 1
|
||||||
|
eoPeriodicContinue<Indi> criteria_2(5);
|
||||||
|
eoDetTournamentSelect<Indi> selectOne_2(25);
|
||||||
|
eoSelectNumber<Indi> who_2(selectOne_2, 5);
|
||||||
|
|
||||||
|
MigPolicy<Indi> migPolicy_2;
|
||||||
|
migPolicy_2.push_back(PolicyElement<Indi>(who_2, criteria_2));
|
||||||
|
|
||||||
|
// // Integration policy
|
||||||
|
eoPlusReplacement<Indi> intPolicy_2;
|
||||||
|
|
||||||
|
Island<eoEasyEA,Indi> test2(pop2, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
|
||||||
|
|
||||||
|
// Island 3
|
||||||
|
// // Algorithm part
|
||||||
|
eoGenContinue<Indi> genCont_3(param.maxGen);
|
||||||
|
eoPop<Indi> pop3(30, chromInit);
|
||||||
|
// // Emigration policy
|
||||||
|
// // // Element 1
|
||||||
|
eoPeriodicContinue<Indi> criteria_3(10);
|
||||||
|
eoDetTournamentSelect<Indi> selectOne_3(15);
|
||||||
|
eoSelectNumber<Indi> who_3(selectOne_3, 1);
|
||||||
|
|
||||||
|
MigPolicy<Indi> migPolicy_3;
|
||||||
|
migPolicy.push_back(PolicyElement<Indi>(who_3, criteria_3));
|
||||||
|
|
||||||
|
// // Integration policy
|
||||||
|
eoPlusReplacement<Indi> intPolicy_3;
|
||||||
|
|
||||||
|
Island<eoEasyEA,Indi> test3(pop3, intPolicy_3, migPolicy_3, genCont_3, plainEval, select, transform, replace);
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Island 1
|
// Topologies
|
||||||
// // Emigration policy
|
|
||||||
// // // Element 1
|
|
||||||
eoPeriodicContinue<Indi> criteria(5);
|
|
||||||
eoDetTournamentSelect<Indi> selectOne(20);
|
|
||||||
eoSelectNumber<Indi> who(selectOne, 3);
|
|
||||||
|
|
||||||
MigPolicy<Indi> migPolicy;
|
|
||||||
migPolicy.push_back(PolicyElement<Indi>(who, criteria));
|
|
||||||
|
|
||||||
// // Integration policy
|
|
||||||
eoPlusReplacement<Indi> intPolicy;
|
|
||||||
|
|
||||||
eoPop<Indi> pop(param.popSize, chromInit);
|
|
||||||
|
|
||||||
Island<eoEasyEA,Indi> test(pop, intPolicy, migPolicy, genCont, plainEval, select, transform, replace);
|
|
||||||
|
|
||||||
// Island 2
|
|
||||||
// // Emigration policy
|
|
||||||
// // // Element 1
|
|
||||||
eoPeriodicContinue<Indi> criteria_2(5);
|
|
||||||
eoDetTournamentSelect<Indi> selectOne_2(25);
|
|
||||||
eoSelectNumber<Indi> who_2(selectOne_2, 5);
|
|
||||||
|
|
||||||
MigPolicy<Indi> migPolicy_2;
|
|
||||||
migPolicy_2.push_back(PolicyElement<Indi>(who_2, criteria_2));
|
|
||||||
|
|
||||||
// // Integration policy
|
|
||||||
eoPlusReplacement<Indi> intPolicy_2;
|
|
||||||
|
|
||||||
eoPop<Indi> pop2(30, chromInit);
|
|
||||||
Island<eoEasyEA,Indi> test2(pop2, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
|
|
||||||
|
|
||||||
// Island 3
|
|
||||||
// // Emigration policy
|
|
||||||
// // // Element 1
|
|
||||||
eoPeriodicContinue<Indi> criteria_3(10);
|
|
||||||
eoDetTournamentSelect<Indi> selectOne_3(15);
|
|
||||||
eoSelectNumber<Indi> who_3(selectOne_3, 1);
|
|
||||||
|
|
||||||
MigPolicy<Indi> migPolicy_3;
|
|
||||||
migPolicy.push_back(PolicyElement<Indi>(who_3, criteria_3));
|
|
||||||
|
|
||||||
// // Integration policy
|
|
||||||
eoPlusReplacement<Indi> intPolicy_3;
|
|
||||||
|
|
||||||
eoPop<Indi> pop3(30, chromInit);
|
|
||||||
Island<eoEasyEA,Indi> test3(pop3, intPolicy_3, migPolicy_3, genCont_3, plainEval, select, transform, replace);
|
|
||||||
|
|
||||||
// Topology
|
|
||||||
Topology<Complete> topo;
|
Topology<Complete> topo;
|
||||||
|
Topology<Ring> topo2;
|
||||||
|
|
||||||
IslandModel<Indi> model(topo);
|
IslandModel<Indi> model(topo);
|
||||||
model.add(test);
|
model.add(test);
|
||||||
model.add(test2);
|
model.add(test2);
|
||||||
model.add(test3);
|
model.add(test3);
|
||||||
|
|
||||||
model();
|
std::thread t = std::thread(&IslandModel<Indi>::operator(), &model);
|
||||||
|
|
||||||
|
// Change topology after 1s of computation
|
||||||
|
std::chrono::milliseconds dura(1000);
|
||||||
|
std::this_thread::sleep_for( dura );
|
||||||
|
|
||||||
|
model.setTopology(topo2);
|
||||||
|
|
||||||
|
t.join();
|
||||||
|
|
||||||
cout << test.getPop() << endl;
|
cout << test.getPop() << endl;
|
||||||
cout << test2.getPop() << endl;
|
cout << test2.getPop() << endl;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue