Clean heterogeneous test
This commit is contained in:
parent
49190367af
commit
e8c188b688
5 changed files with 94 additions and 136 deletions
|
|
@ -40,7 +40,8 @@ Contact: paradiseo-help@lists.gforge.inria.fr
|
||||||
paradiseo::smp::CustomBooleanTopology::CustomBooleanTopology(std::string filename)
|
paradiseo::smp::CustomBooleanTopology::CustomBooleanTopology(std::string filename)
|
||||||
{
|
{
|
||||||
std::ifstream f(filename);
|
std::ifstream f(filename);
|
||||||
if (f)
|
|
||||||
|
if(f)
|
||||||
{
|
{
|
||||||
int temp;
|
int temp;
|
||||||
unsigned size;
|
unsigned size;
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,8 @@ Contact: paradiseo-help@lists.gforge.inria.fr
|
||||||
|
|
||||||
paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string filename)
|
paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string filename)
|
||||||
{
|
{
|
||||||
std :: ifstream f(filename);
|
std::ifstream f(filename);
|
||||||
if (f)
|
if(f)
|
||||||
{
|
{
|
||||||
double temp;
|
double temp;
|
||||||
unsigned size;
|
unsigned size;
|
||||||
|
|
@ -53,14 +53,13 @@ paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string f
|
||||||
{
|
{
|
||||||
lineVector.clear();
|
lineVector.clear();
|
||||||
|
|
||||||
//line contains a line of text from the file
|
// Line contains a line of text from the file
|
||||||
std::istringstream tokenizer(line);
|
std::istringstream tokenizer(line);
|
||||||
std::string token;
|
std::string token;
|
||||||
|
|
||||||
while(tokenizer>> temp >> std::skipws)
|
while(tokenizer>> temp >> std::skipws)
|
||||||
{
|
{
|
||||||
//white spaces are skipped, and the integer is converted to boolean, to be stored
|
// White spaces are skipped, and the integer is converted to boolean, to be stored
|
||||||
|
|
||||||
if(temp<0)
|
if(temp<0)
|
||||||
isNeighbor = 0;
|
isNeighbor = 0;
|
||||||
else if(temp>1)
|
else if(temp>1)
|
||||||
|
|
@ -70,14 +69,14 @@ paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string f
|
||||||
lineVector.push_back(isNeighbor);
|
lineVector.push_back(isNeighbor);
|
||||||
}
|
}
|
||||||
|
|
||||||
//if this is the first line, we must initiate the variable size
|
// If this is the first line, we must initiate the variable size
|
||||||
if(isFirst)
|
if(isFirst)
|
||||||
{
|
{
|
||||||
size = lineVector.size();
|
size = lineVector.size();
|
||||||
isFirst=false;
|
isFirst=false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//for each vector non empty, if the size is not equal to the others, error
|
// For each vector non empty, if the size is not equal to the others, error
|
||||||
if(lineVector.size() != size && !lineVector.empty())
|
if(lineVector.size() != size && !lineVector.empty())
|
||||||
throw std::runtime_error("Mistake in the topology, line "+ std::to_string(_matrix.size()+1) );
|
throw std::runtime_error("Mistake in the topology, line "+ std::to_string(_matrix.size()+1) );
|
||||||
|
|
||||||
|
|
@ -85,7 +84,7 @@ paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string f
|
||||||
_matrix.push_back(lineVector);
|
_matrix.push_back(lineVector);
|
||||||
}
|
}
|
||||||
|
|
||||||
//for each vector, verify if the size is equal to the size of the final matrix
|
// For each vector, verify if the size is equal to the size of the final matrix
|
||||||
for(auto& line : _matrix)
|
for(auto& line : _matrix)
|
||||||
if(line.size() != _matrix.size())
|
if(line.size() != _matrix.size())
|
||||||
throw std::runtime_error("Mistake in the topology, matrix is not square" );
|
throw std::runtime_error("Mistake in the topology, matrix is not square" );
|
||||||
|
|
@ -105,7 +104,7 @@ std::vector<unsigned> paradiseo::smp::CustomStochasticTopology::getIdNeighbors(u
|
||||||
std::uniform_real_distribution<> dis(0,1);
|
std::uniform_real_distribution<> dis(0,1);
|
||||||
|
|
||||||
std::vector<unsigned> neighbors;
|
std::vector<unsigned> neighbors;
|
||||||
for(unsigned j=0; j<_matrix.size();j++)
|
for(unsigned j = 0; j < _matrix.size(); j++)
|
||||||
if(_matrix[idNode][j] > dis(gen))
|
if(_matrix[idNode][j] > dis(gen))
|
||||||
neighbors.push_back(j);
|
neighbors.push_back(j);
|
||||||
return neighbors;
|
return neighbors;
|
||||||
|
|
@ -113,7 +112,7 @@ std::vector<unsigned> paradiseo::smp::CustomStochasticTopology::getIdNeighbors(u
|
||||||
|
|
||||||
void paradiseo::smp::CustomStochasticTopology::construct(unsigned nbNode)
|
void paradiseo::smp::CustomStochasticTopology::construct(unsigned nbNode)
|
||||||
{
|
{
|
||||||
assert(nbNode==_matrix.size());
|
assert(nbNode == _matrix.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void paradiseo::smp::CustomStochasticTopology::isolateNode(unsigned idNode)
|
void paradiseo::smp::CustomStochasticTopology::isolateNode(unsigned idNode)
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ void paradiseo::smp::Hypercubic::operator()(unsigned nbNode, std::vector<std::ve
|
||||||
for(auto& line : matrix)
|
for(auto& line : matrix)
|
||||||
line.resize(nbNode);
|
line.resize(nbNode);
|
||||||
|
|
||||||
//Construction
|
// Construction
|
||||||
matrix[0][0] = false;
|
matrix[0][0] = false;
|
||||||
for(unsigned dim = 1; dim <= power; dim ++)
|
for(unsigned dim = 1; dim <= power; dim ++)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -36,12 +36,12 @@ void paradiseo::smp::Mesh::operator()(unsigned nbNode, std::vector<std::vector<b
|
||||||
unsigned i = 0, j, height, width;
|
unsigned i = 0, j, height, width;
|
||||||
std::vector<unsigned> listFact = paradiseo::smp::Mesh::factorization(nbNode);
|
std::vector<unsigned> listFact = paradiseo::smp::Mesh::factorization(nbNode);
|
||||||
|
|
||||||
//Compute width and height
|
// Compute width and height
|
||||||
//find the ratio height/width of the grid that matches best the variable _ratio
|
// Find the ratio height/width of the grid that matches best the variable _ratio
|
||||||
while (i < listFact.size() - 1 && (double)listFact[i]*listFact[i] / nbNode < _ratio)
|
while (i < listFact.size() - 1 && (double)listFact[i]*listFact[i] / nbNode < _ratio)
|
||||||
i++;
|
i++;
|
||||||
|
|
||||||
// listFact[i] contains first factor which produces a ratio above the variable _ratio,
|
// ListFact[i] contains first factor which produces a ratio above the variable _ratio,
|
||||||
// or the last element if there is no ratio that can go over the variable _ratio.
|
// or the last element if there is no ratio that can go over the variable _ratio.
|
||||||
double r1 = (double)listFact[i] * listFact[i] / nbNode;
|
double r1 = (double)listFact[i] * listFact[i] / nbNode;
|
||||||
double r2 = (double)listFact[i-1] * listFact[i-1] / nbNode;
|
double r2 = (double)listFact[i-1] * listFact[i-1] / nbNode;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
// TODO : Un vrai test, propre, qui veut dire quelque chose :)
|
|
||||||
|
|
||||||
#include <smp>
|
#include <smp>
|
||||||
#include <eo>
|
#include <eo>
|
||||||
#include <ga.h>
|
#include <ga.h>
|
||||||
|
|
@ -9,7 +7,7 @@
|
||||||
using namespace paradiseo::smp;
|
using namespace paradiseo::smp;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
typedef eoBit<double> Indi2; // A bitstring with fitness double
|
typedef eoBit<double> Indi2; // A bitstring with fitness double
|
||||||
|
|
||||||
// Conversion functions
|
// Conversion functions
|
||||||
Indi2 fromBase(Indi& i, unsigned size)
|
Indi2 fromBase(Indi& i, unsigned size)
|
||||||
|
|
@ -35,127 +33,83 @@ Indi toBase(Indi2& i)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
// EVAL
|
// Eval function for the PSO
|
||||||
//-----------------------------------------------------------------------------
|
// A simple fitness function that computes the number of ones of a bitstring
|
||||||
// a simple fitness function that computes the number of ones of a bitstring
|
// @param _Indi2 A biststring Indi2vidual
|
||||||
// @param _Indi2 A biststring Indi2vidual
|
|
||||||
|
|
||||||
double binary_value(const Indi2 & _Indi2)
|
double binary_value(const Indi2 & _Indi2)
|
||||||
{
|
{
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
for (unsigned i = 0; i < _Indi2.size(); i++)
|
for (unsigned i = 0; i < _Indi2.size(); i++)
|
||||||
sum += _Indi2[i];
|
sum += _Indi2[i];
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
// GENERAL
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
// PARAMETRES
|
//////////////////////////////////////////////////////////////////
|
||||||
// all parameters are hard-coded!
|
// PSO PART
|
||||||
const unsigned int SEED = 42; // seed for random number generator
|
//////////////////////////////////////////////////////////////////
|
||||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
// PSO general parameters
|
||||||
const unsigned int VEC_SIZE = 16; // Number of bits in genotypes
|
const unsigned int SEED = 42; // seed for random number generator
|
||||||
const unsigned int POP_SIZE = 10; // Size of population
|
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||||
const unsigned int MAX_GEN = 10; // Maximum number of generation before STOP
|
const unsigned int VEC_SIZE = 16; // Number of bits in genotypes
|
||||||
const float CROSS_RATE = 0.8; // Crossover rate
|
const unsigned int POP_SIZE = 10; // Size of population
|
||||||
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
|
const unsigned int MAX_GEN = 10; // Maximum number of generation before STOP
|
||||||
const float MUT_RATE = 1.0; // mutation rate
|
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
|
rng.reseed(SEED);
|
||||||
//////////////////////////
|
|
||||||
// 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
|
eoEvalFuncPtr<Indi2> eval(binary_value);
|
||||||
/////////////////////////////
|
|
||||||
// Fitness function
|
|
||||||
////////////////////////////
|
|
||||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
|
||||||
eoEvalFuncPtr<Indi2> eval( binary_value );
|
|
||||||
|
|
||||||
// INIT
|
// PSO population initialization
|
||||||
////////////////////////////////
|
eoPop<Indi2> pop;
|
||||||
// Initilisation of population
|
|
||||||
////////////////////////////////
|
|
||||||
|
|
||||||
// declare the population
|
for(unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||||
eoPop<Indi2> pop;
|
|
||||||
// fill it!
|
|
||||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
|
||||||
{
|
{
|
||||||
Indi2 v; // void Indi2vidual, to be filled
|
Indi2 v; // void Indi2vidual, to be filled
|
||||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||||
{
|
{
|
||||||
bool r = rng.flip(); // new value, random in {0,1}
|
bool r = rng.flip(); // new value, random in {0,1}
|
||||||
v.push_back(r); // append that random value to v
|
v.push_back(r); // append that random value to v
|
||||||
}
|
}
|
||||||
eval(v); // evaluate it
|
eval(v); // evaluate it
|
||||||
pop.push_back(v); // and put it in the population
|
pop.push_back(v); // and put it in the population
|
||||||
}
|
}
|
||||||
|
|
||||||
// ENGINE
|
// ISLAND 1 : PSO
|
||||||
/////////////////////////////////////
|
// // Algorithm part
|
||||||
// selection and replacement
|
eoDetTournamentSelect<Indi2> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||||
////////////////////////////////////
|
eo1PtBitXover<Indi2> xover;
|
||||||
// SELECT
|
eoBitMutation<Indi2> mutation(P_MUT_PER_BIT);
|
||||||
// The robust tournament selection
|
eoGenContinue<Indi2> continuator(MAX_GEN);
|
||||||
eoDetTournamentSelect<Indi2> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
|
||||||
|
|
||||||
// REPLACE
|
// // Emigration policy
|
||||||
// The simple GA evolution engine uses generational replacement
|
// // // Element 1
|
||||||
// so no replacement procedure is needed
|
eoPeriodicContinue<Indi2> criteria(1);
|
||||||
|
eoDetTournamentSelect<Indi2> selectOne(2);
|
||||||
// OPERATORS
|
eoSelectNumber<Indi2> who(selectOne, 1);
|
||||||
//////////////////////////////////////
|
|
||||||
// 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<Indi2> migPolicy;
|
||||||
migPolicy.push_back(PolicyElement<Indi2>(who, criteria));
|
migPolicy.push_back(PolicyElement<Indi2>(who, criteria));
|
||||||
|
|
||||||
// // Integration policy
|
// // Integration policy
|
||||||
eoPlusReplacement<Indi2> intPolicy;
|
eoPlusReplacement<Indi2> intPolicy;
|
||||||
|
|
||||||
// We bind conversion functions
|
// We bind conversion functions
|
||||||
auto frombase = std::bind(fromBase, std::placeholders::_1, VEC_SIZE);
|
auto frombase = std::bind(fromBase, std::placeholders::_1, VEC_SIZE);
|
||||||
auto tobase = std::bind(toBase, std::placeholders::_1);
|
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,
|
Island<eoSGA,Indi2, Indi> gga(frombase, tobase, pop, intPolicy, migPolicy, select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator);
|
||||||
eval, continuator);
|
|
||||||
//////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////
|
||||||
typedef struct {
|
|
||||||
|
//////////////////////////////////////////////////////////////////
|
||||||
|
// EasyEA PART
|
||||||
|
//////////////////////////////////////////////////////////////////
|
||||||
|
// EA general parameters
|
||||||
|
typedef struct {
|
||||||
unsigned popSize = 10;
|
unsigned popSize = 10;
|
||||||
unsigned tSize = 2;
|
unsigned tSize = 2;
|
||||||
double pCross = 0.8;
|
double pCross = 0.8;
|
||||||
|
|
@ -164,7 +118,6 @@ int main(void)
|
||||||
} Param;
|
} Param;
|
||||||
|
|
||||||
Param param;
|
Param param;
|
||||||
|
|
||||||
loadInstances("t-data.dat", n, bkv, a, b);
|
loadInstances("t-data.dat", n, bkv, a, b);
|
||||||
|
|
||||||
// Evaluation function
|
// Evaluation function
|
||||||
|
|
@ -186,13 +139,10 @@ int main(void)
|
||||||
|
|
||||||
// Define replace operator
|
// Define replace operator
|
||||||
eoPlusReplacement<Indi> replace;
|
eoPlusReplacement<Indi> replace;
|
||||||
|
|
||||||
eoGenContinue<Indi> genCont(param.maxGen); // generation continuation
|
eoGenContinue<Indi> genCont(param.maxGen); // generation continuation
|
||||||
|
|
||||||
// Define population
|
|
||||||
eoPop<Indi> pop2(param.popSize, chromInit);
|
eoPop<Indi> pop2(param.popSize, chromInit);
|
||||||
|
|
||||||
// Island 1
|
// ISLAND 2 : EasyEA
|
||||||
// // Emigration policy
|
// // Emigration policy
|
||||||
// // // Element 1
|
// // // Element 1
|
||||||
eoPeriodicContinue<Indi> criteria2(1);
|
eoPeriodicContinue<Indi> criteria2(1);
|
||||||
|
|
@ -207,18 +157,26 @@ int main(void)
|
||||||
|
|
||||||
Island<eoEasyEA,Indi> test(pop2, intPolicy2, migPolicy2, genCont, plainEval, select2, transform, replace);
|
Island<eoEasyEA,Indi> test(pop2, intPolicy2, migPolicy2, genCont, plainEval, select2, transform, replace);
|
||||||
|
|
||||||
// Topology
|
// MODEL CREATION
|
||||||
Topology<Complete> topo;
|
Topology<Complete> topo;
|
||||||
|
IslandModel<Indi> model(topo);
|
||||||
IslandModel<Indi> model(topo);
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
model.add(test);
|
model.add(test);
|
||||||
model.add(gga);
|
model.add(gga);
|
||||||
|
|
||||||
model();
|
model();
|
||||||
|
|
||||||
cout << test.getPop() << endl;
|
cout << test.getPop() << endl;
|
||||||
cout << gga.getPop() << endl;
|
cout << gga.getPop() << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
catch(exception& e)
|
||||||
|
{
|
||||||
|
cout << "Exception: " << e.what() << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue