diff --git a/smp/src/topology/customBooleanTopology.cpp b/smp/src/topology/customBooleanTopology.cpp index c6ae61213..91bbf053c 100644 --- a/smp/src/topology/customBooleanTopology.cpp +++ b/smp/src/topology/customBooleanTopology.cpp @@ -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; diff --git a/smp/src/topology/customStochasticTopology.cpp b/smp/src/topology/customStochasticTopology.cpp index 87fa6d33e..43a8b3cc4 100644 --- a/smp/src/topology/customStochasticTopology.cpp +++ b/smp/src/topology/customStochasticTopology.cpp @@ -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 paradiseo::smp::CustomStochasticTopology::getIdNeighbors(u std::uniform_real_distribution<> dis(0,1); std::vector 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 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) diff --git a/smp/src/topology/hypercubic.cpp b/smp/src/topology/hypercubic.cpp index 972d4f428..67001cf79 100644 --- a/smp/src/topology/hypercubic.cpp +++ b/smp/src/topology/hypercubic.cpp @@ -49,7 +49,7 @@ void paradiseo::smp::Hypercubic::operator()(unsigned nbNode, std::vector 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; diff --git a/smp/test/t-smpMI_Heterogeneous.cpp b/smp/test/t-smpMI_Heterogeneous.cpp index 64f4937f6..f3a0e6265 100644 --- a/smp/test/t-smpMI_Heterogeneous.cpp +++ b/smp/test/t-smpMI_Heterogeneous.cpp @@ -1,5 +1,3 @@ -// TODO : Un vrai test, propre, qui veut dire quelque chose :) - #include #include #include @@ -9,7 +7,7 @@ using namespace paradiseo::smp; using namespace std; -typedef eoBit Indi2; // A bitstring with fitness double +typedef eoBit 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 eval( binary_value ); + eoEvalFuncPtr eval(binary_value); -// INIT - //////////////////////////////// - // Initilisation of population - //////////////////////////////// + // PSO population initialization + eoPop pop; - // declare the population - eoPop pop; - // fill it! - for (unsigned int igeno=0; igeno select(T_SIZE); // T_SIZE in [2,POP_SIZE] + // ISLAND 1 : PSO + // // Algorithm part + eoDetTournamentSelect select(T_SIZE); // T_SIZE in [2,POP_SIZE] + eo1PtBitXover xover; + eoBitMutation mutation(P_MUT_PER_BIT); + eoGenContinue 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 xover; -// MUTATION - // standard bit-flip mutation for bitstring - eoBitMutation mutation(P_MUT_PER_BIT); - -// STOP -// CHECKPOINT - ////////////////////////////////////// - // termination condition - ///////////////////////////////////// - // stop after MAX_GEN generations - eoGenContinue continuator(MAX_GEN); - -// GENERATION - ///////////////////////////////////////// - // the algorithm - //////////////////////////////////////// - // standard Generational GA requires as parameters - // selection, evaluation, crossover and mutation, stopping criterion - -// // Emigration policy - // // // Element 1 - eoPeriodicContinue criteria(1); - eoDetTournamentSelect selectOne(2); - eoSelectNumber who(selectOne, 1); + // // Emigration policy + // // // Element 1 + eoPeriodicContinue criteria(1); + eoDetTournamentSelect selectOne(2); + eoSelectNumber who(selectOne, 1); - MigPolicy migPolicy; - migPolicy.push_back(PolicyElement(who, criteria)); + MigPolicy migPolicy; + migPolicy.push_back(PolicyElement(who, criteria)); - // // Integration policy - eoPlusReplacement intPolicy; + // // Integration policy + eoPlusReplacement 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 gga(frombase, tobase, pop, intPolicy, migPolicy, select, xover, CROSS_RATE, mutation, MUT_RATE, - eval, continuator); + Island 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 replace; - eoGenContinue genCont(param.maxGen); // generation continuation - - // Define population eoPop pop2(param.popSize, chromInit); - // Island 1 + // ISLAND 2 : EasyEA // // Emigration policy // // // Element 1 eoPeriodicContinue criteria2(1); @@ -207,18 +157,26 @@ int main(void) Island test(pop2, intPolicy2, migPolicy2, genCont, plainEval, select2, transform, replace); - // Topology - Topology topo; - - IslandModel model(topo); + // MODEL CREATION + Topology topo; + IslandModel 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; }