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)
|
||||
{
|
||||
std::ifstream f(filename);
|
||||
if (f)
|
||||
|
||||
if(f)
|
||||
{
|
||||
int temp;
|
||||
unsigned size;
|
||||
|
|
|
|||
|
|
@ -40,8 +40,8 @@ Contact: paradiseo-help@lists.gforge.inria.fr
|
|||
|
||||
paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string filename)
|
||||
{
|
||||
std :: ifstream f(filename);
|
||||
if (f)
|
||||
std::ifstream f(filename);
|
||||
if(f)
|
||||
{
|
||||
double temp;
|
||||
unsigned size;
|
||||
|
|
@ -53,14 +53,13 @@ paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string f
|
|||
{
|
||||
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::string token;
|
||||
|
||||
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)
|
||||
isNeighbor = 0;
|
||||
else if(temp>1)
|
||||
|
|
@ -70,14 +69,14 @@ paradiseo::smp::CustomStochasticTopology::CustomStochasticTopology(std::string f
|
|||
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)
|
||||
{
|
||||
size = lineVector.size();
|
||||
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())
|
||||
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);
|
||||
}
|
||||
|
||||
//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)
|
||||
if(line.size() != _matrix.size())
|
||||
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::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))
|
||||
neighbors.push_back(j);
|
||||
return neighbors;
|
||||
|
|
@ -113,7 +112,7 @@ std::vector<unsigned> paradiseo::smp::CustomStochasticTopology::getIdNeighbors(u
|
|||
|
||||
void paradiseo::smp::CustomStochasticTopology::construct(unsigned nbNode)
|
||||
{
|
||||
assert(nbNode==_matrix.size());
|
||||
assert(nbNode == _matrix.size());
|
||||
}
|
||||
|
||||
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)
|
||||
line.resize(nbNode);
|
||||
|
||||
//Construction
|
||||
// Construction
|
||||
matrix[0][0] = false;
|
||||
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;
|
||||
std::vector<unsigned> listFact = paradiseo::smp::Mesh::factorization(nbNode);
|
||||
|
||||
//Compute width and height
|
||||
//find the ratio height/width of the grid that matches best the variable _ratio
|
||||
// Compute width and height
|
||||
// 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)
|
||||
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.
|
||||
double r1 = (double)listFact[i] * listFact[i] / 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 <eo>
|
||||
#include <ga.h>
|
||||
|
|
@ -9,7 +7,7 @@
|
|||
using namespace paradiseo::smp;
|
||||
using namespace std;
|
||||
|
||||
typedef eoBit<double> Indi2; // A bitstring with fitness double
|
||||
typedef eoBit<double> Indi2; // A bitstring with fitness double
|
||||
|
||||
// Conversion functions
|
||||
Indi2 fromBase(Indi& i, unsigned size)
|
||||
|
|
@ -35,127 +33,83 @@ Indi toBase(Indi2& i)
|
|||
return v;
|
||||
}
|
||||
|
||||
// EVAL
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the number of ones of a bitstring
|
||||
// @param _Indi2 A biststring Indi2vidual
|
||||
// Eval function for the PSO
|
||||
// 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;
|
||||
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
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// PSO PART
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// PSO general parameters
|
||||
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);
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Indi2> eval( binary_value );
|
||||
eoEvalFuncPtr<Indi2> eval(binary_value);
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
// PSO population initialization
|
||||
eoPop<Indi2> pop;
|
||||
|
||||
// declare the population
|
||||
eoPop<Indi2> pop;
|
||||
// fill it!
|
||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
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
|
||||
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]
|
||||
// ISLAND 1 : PSO
|
||||
// // Algorithm part
|
||||
eoDetTournamentSelect<Indi2> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
eo1PtBitXover<Indi2> xover;
|
||||
eoBitMutation<Indi2> mutation(P_MUT_PER_BIT);
|
||||
eoGenContinue<Indi2> continuator(MAX_GEN);
|
||||
|
||||
// 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);
|
||||
// // 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));
|
||||
MigPolicy<Indi2> migPolicy;
|
||||
migPolicy.push_back(PolicyElement<Indi2>(who, criteria));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi2> intPolicy;
|
||||
// // 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);
|
||||
// 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);
|
||||
Island<eoSGA,Indi2, Indi> gga(frombase, tobase, pop, intPolicy, migPolicy, select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator);
|
||||
//////////////////////////////////////////////////////////////////
|
||||
typedef struct {
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// EasyEA PART
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// EA general parameters
|
||||
typedef struct {
|
||||
unsigned popSize = 10;
|
||||
unsigned tSize = 2;
|
||||
double pCross = 0.8;
|
||||
|
|
@ -164,7 +118,6 @@ int main(void)
|
|||
} Param;
|
||||
|
||||
Param param;
|
||||
|
||||
loadInstances("t-data.dat", n, bkv, a, b);
|
||||
|
||||
// Evaluation function
|
||||
|
|
@ -186,13 +139,10 @@ int main(void)
|
|||
|
||||
// Define replace operator
|
||||
eoPlusReplacement<Indi> replace;
|
||||
|
||||
eoGenContinue<Indi> genCont(param.maxGen); // generation continuation
|
||||
|
||||
// Define population
|
||||
eoPop<Indi> pop2(param.popSize, chromInit);
|
||||
|
||||
// Island 1
|
||||
// ISLAND 2 : EasyEA
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria2(1);
|
||||
|
|
@ -207,18 +157,26 @@ int main(void)
|
|||
|
||||
Island<eoEasyEA,Indi> test(pop2, intPolicy2, migPolicy2, genCont, plainEval, select2, transform, replace);
|
||||
|
||||
// Topology
|
||||
Topology<Complete> topo;
|
||||
|
||||
IslandModel<Indi> model(topo);
|
||||
// MODEL CREATION
|
||||
Topology<Complete> topo;
|
||||
IslandModel<Indi> model(topo);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
model.add(test);
|
||||
model.add(gga);
|
||||
|
||||
|
||||
model();
|
||||
|
||||
|
||||
cout << test.getPop() << endl;
|
||||
cout << gga.getPop() << endl;
|
||||
|
||||
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue