Merge
This commit is contained in:
commit
f8d2d1cfa5
21 changed files with 452 additions and 406 deletions
|
|
@ -11,7 +11,6 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
|||
######################################################################################
|
||||
|
||||
set (TEST_LIST
|
||||
t-smpThread
|
||||
t-smpScheduler
|
||||
t-smpMW_eoEasyEA
|
||||
t-smpMW_eoEasyPSO
|
||||
|
|
@ -19,6 +18,7 @@ set (TEST_LIST
|
|||
t-smpIsland
|
||||
t-smpTopo
|
||||
t-smpMI_Homogeneous
|
||||
t-smpMI_Heterogeneous
|
||||
)
|
||||
|
||||
######################################################################################
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
222
smp/test/t-smpMI_Heterogeneous.cpp
Normal file
222
smp/test/t-smpMI_Heterogeneous.cpp
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
// TODO : Un vrai test, propre, qui veut dire quelque chose :)
|
||||
|
||||
#include <smp>
|
||||
#include <eo>
|
||||
#include <ga.h>
|
||||
|
||||
#include "smpTestClass.h"
|
||||
|
||||
using namespace paradiseo::smp;
|
||||
using namespace std;
|
||||
|
||||
typedef eoBit<double> Indi2; // A bitstring with fitness double
|
||||
|
||||
// Conversion functions
|
||||
Indi2 fromBase(Indi& i, unsigned size)
|
||||
{
|
||||
// Dummy conversion. We just create a new Indi2
|
||||
Indi2 v;
|
||||
for (unsigned ivar=0; ivar<size; ivar++)
|
||||
{
|
||||
bool r = rng.flip(); // new value, random in {0,1}
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
std::cout << "Convert from base : " << v << std::endl;
|
||||
return v;
|
||||
}
|
||||
|
||||
Indi toBase(Indi2& i)
|
||||
{
|
||||
// Dummy conversion. We just create a new Indi
|
||||
Indi v;
|
||||
std::cout << "Convert to base : " << v << std::endl;
|
||||
return v;
|
||||
}
|
||||
|
||||
// EVAL
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the number of ones of a bitstring
|
||||
// @param _Indi2 A biststring Indi2vidual
|
||||
|
||||
double binary_value(const Indi2 & _Indi2)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _Indi2.size(); i++)
|
||||
sum += _Indi2[i];
|
||||
return sum;
|
||||
}
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// PARAMETRES
|
||||
// all parameters are hard-coded!
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 16; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 10; // Size of population
|
||||
const unsigned int MAX_GEN = 10; // Maximum number of generation before STOP
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
|
||||
const float MUT_RATE = 1.0; // mutation rate
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Indi2> eval( binary_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// declare the population
|
||||
eoPop<Indi2> pop;
|
||||
// fill it!
|
||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
{
|
||||
Indi2 v; // void Indi2vidual, to be filled
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
{
|
||||
bool r = rng.flip(); // new value, random in {0,1}
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
eval(v); // evaluate it
|
||||
pop.push_back(v); // and put it in the population
|
||||
}
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Indi2> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
|
||||
// REPLACE
|
||||
// The simple GA evolution engine uses generational replacement
|
||||
// so no replacement procedure is needed
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eo1PtBitXover<Indi2> xover;
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBitMutation<Indi2> mutation(P_MUT_PER_BIT);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination condition
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi2> continuator(MAX_GEN);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
// standard Generational GA requires as parameters
|
||||
// selection, evaluation, crossover and mutation, stopping criterion
|
||||
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi2> criteria(1);
|
||||
eoDetTournamentSelect<Indi2> selectOne(2);
|
||||
eoSelectNumber<Indi2> who(selectOne, 1);
|
||||
|
||||
MigPolicy<Indi2> migPolicy;
|
||||
migPolicy.push_back(PolicyElement<Indi2>(who, criteria));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi2> intPolicy;
|
||||
|
||||
// We bind conversion functions
|
||||
auto frombase = std::bind(fromBase, std::placeholders::_1, VEC_SIZE);
|
||||
auto tobase = std::bind(toBase, std::placeholders::_1);
|
||||
|
||||
Island<eoSGA,Indi2, Indi> gga(frombase, tobase, pop, intPolicy, migPolicy, select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
eval, continuator);
|
||||
//////////////////////////////////////////////////////////////////
|
||||
typedef struct {
|
||||
unsigned popSize = 10;
|
||||
unsigned tSize = 2;
|
||||
double pCross = 0.8;
|
||||
double pMut = 0.7;
|
||||
unsigned maxGen = 10;
|
||||
} Param;
|
||||
|
||||
Param param;
|
||||
|
||||
loadInstances("t-data.dat", n, bkv, a, b);
|
||||
|
||||
// Evaluation function
|
||||
IndiEvalFunc plainEval;
|
||||
|
||||
// Init a solution
|
||||
IndiInit chromInit;
|
||||
|
||||
// Define selection
|
||||
eoDetTournamentSelect<Indi> selectOne2(param.tSize);
|
||||
eoSelectPerc<Indi> select2(selectOne2);// by default rate==1
|
||||
|
||||
// Define operators for crossover and mutation
|
||||
IndiXover Xover; // CROSSOVER
|
||||
IndiSwapMutation mutationSwap; // MUTATION
|
||||
|
||||
// Encapsule in a tranform operator
|
||||
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
|
||||
|
||||
// Define replace operator
|
||||
eoPlusReplacement<Indi> replace;
|
||||
|
||||
eoGenContinue<Indi> genCont(param.maxGen); // generation continuation
|
||||
|
||||
// Define population
|
||||
eoPop<Indi> pop2(param.popSize, chromInit);
|
||||
|
||||
// Island 1
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria2(1);
|
||||
eoDetTournamentSelect<Indi> selectOne3(5);
|
||||
eoSelectNumber<Indi> who2(selectOne3, 2);
|
||||
|
||||
MigPolicy<Indi> migPolicy2;
|
||||
migPolicy2.push_back(PolicyElement<Indi>(who2, criteria2));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi> intPolicy2;
|
||||
|
||||
Island<eoEasyEA,Indi> test(pop2, intPolicy2, migPolicy2, genCont, plainEval, select2, transform, replace);
|
||||
|
||||
// Topology
|
||||
Topology<Complete> topo;
|
||||
|
||||
IslandModel<Indi> model(topo);
|
||||
model.add(test);
|
||||
model.add(gga);
|
||||
|
||||
model();
|
||||
|
||||
cout << test.getPop() << endl;
|
||||
cout << gga.getPop() << endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -6,104 +6,117 @@
|
|||
using namespace paradiseo::smp;
|
||||
using namespace std;
|
||||
|
||||
void changeTopo(IslandModel<Indi>& _model, AbstractTopology& _topo)
|
||||
{
|
||||
static bool first = false;
|
||||
// Change topology after 1s of computation
|
||||
std::chrono::milliseconds dura(1000);
|
||||
std::this_thread::sleep_for( dura );
|
||||
if(!first)
|
||||
{
|
||||
_model.setTopology(_topo);
|
||||
first = !first;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Defining parameters
|
||||
typedef struct {
|
||||
unsigned popSize = 100;
|
||||
unsigned popSize = 1000;
|
||||
unsigned tSize = 2;
|
||||
double pCross = 0.8;
|
||||
double pMut = 0.7;
|
||||
unsigned maxGen = 100;
|
||||
unsigned maxGen = 1000;
|
||||
} Param;
|
||||
|
||||
Param param;
|
||||
|
||||
// Fixing the seed
|
||||
rng.reseed(42);
|
||||
|
||||
// Load instance
|
||||
loadInstances("t-data.dat", n, bkv, a, b);
|
||||
|
||||
// Evaluation function
|
||||
//Common part to all islands
|
||||
IndiEvalFunc plainEval;
|
||||
|
||||
// Init a solution
|
||||
IndiInit chromInit;
|
||||
|
||||
// Define selection
|
||||
eoDetTournamentSelect<Indi> selectOne(param.tSize);
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// Define operators for crossover and mutation
|
||||
IndiXover Xover; // CROSSOVER
|
||||
IndiSwapMutation mutationSwap; // MUTATION
|
||||
|
||||
// Encapsule in a tranform operator
|
||||
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
|
||||
|
||||
// Define replace operator
|
||||
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
|
||||
// MODEL
|
||||
// Topologies
|
||||
Topology<Complete> topo;
|
||||
IslandModel<Indi> model(topo);
|
||||
|
||||
// ISLAND 1
|
||||
// // Algorithm part
|
||||
eoGenContinue<Indi> genCont(param.maxGen+100);
|
||||
eoPop<Indi> pop(param.popSize, chromInit);
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria(5);
|
||||
eoDetTournamentSelect<Indi> selectOne1(20);
|
||||
eoSelectNumber<Indi> who(selectOne1, 3);
|
||||
|
||||
Topology<Ring> topo2;
|
||||
//std::function<void(void)> task = std::bind(changeTopo, model, topo2);
|
||||
//Notifier topoChanger(task);
|
||||
|
||||
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
|
||||
{
|
||||
// Island 1
|
||||
// // 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;
|
||||
|
||||
IslandModel<Indi> model(topo);
|
||||
model.add(test);
|
||||
model.add(test2);
|
||||
model.add(test3);
|
||||
|
|
|
|||
|
|
@ -1,60 +0,0 @@
|
|||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
|
||||
#include <smp>
|
||||
|
||||
#include "smpTestClass.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace paradiseo::smp;
|
||||
|
||||
void f(std::atomic<int> &x)
|
||||
{
|
||||
for(int i = 0; i < 100; i++) {
|
||||
cout << x << endl;
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
void g(std::atomic<int> &x)
|
||||
{
|
||||
for(int i = 0; i < 100; i++)
|
||||
x--;
|
||||
}
|
||||
|
||||
void foo()
|
||||
{
|
||||
std::cout << "Foo" << std::endl;
|
||||
//std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
//---------------------------------------------------
|
||||
std::atomic<int> nb(0);
|
||||
|
||||
Thread t1(&f,std::ref(nb));
|
||||
Thread t2(&g,std::ref(nb));
|
||||
|
||||
t1.join();
|
||||
t2.join();
|
||||
|
||||
assert(nb == 0); // Test atomic_barrier
|
||||
|
||||
//--------------------------------------------------
|
||||
try
|
||||
{
|
||||
t1.start(foo);
|
||||
|
||||
t1.join();
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue