//----------------------------------------------------------------------------- // t-eogeneration.cpp //----------------------------------------------------------------------------- // to avoid long name warnings #pragma warning(disable:4786) #include #include "binary_value.h" //----------------------------------------------------------------------------- typedef eoBin Chrom; //----------------------------------------------------------------------------- main() { const unsigned POP_SIZE = 8, CHROM_SIZE = 16; unsigned i; eoUniform uniform(false, true); eoBinRandom random; eoPop pop; for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); random(chrom); binary_value(chrom); pop.push_back(chrom); } cout << "population:" << endl; for (i = 0; i < pop.size(); ++i) cout << "\t" << pop[i] << " " << pop[i].fitness() << endl; // selection eoLottery lottery; // breeder eoBinBitFlip bitflip; eoBinCrossover xover; eoProportionalOpSel propSel; eoBreeder breeder( propSel ); propSel.addOp(bitflip, 0.25); propSel.addOp(xover, 0.75); // replacement eoInclusion inclusion; // Evaluation eoEvalFuncPtr eval( binary_value ); // GA generation eoGeneration generation(lottery, breeder, inclusion, eval); // evolution unsigned g = 0; do { try { generation(pop); } catch (exception& e) { cout << "exception: " << e.what() << endl;; exit(EXIT_FAILURE); } cout << "pop[" << ++g << "]" << endl; for (i = 0; i < pop.size(); ++i) cout << "\t" << pop[i] << " " << pop[i].fitness() << endl; } while (pop[0].fitness() < pow(2.0, CHROM_SIZE) - 1); return 0; } //-----------------------------------------------------------------------------