//----------------------------------------------------------------------------- // t-eobreeder.cpp //----------------------------------------------------------------------------- // to avoid long name warnings #pragma warning(disable:4786) #include // eoBin, eoPop, eoBreeder #include #include #include #include //----------------------------------------------------------------------------- typedef eoBin Chrom; //----------------------------------------------------------------------------- void binary_value(Chrom& chrom) { float sum = 0; for (unsigned i = 0; i < chrom.size(); i++) if (chrom[i]) sum += pow(2, chrom.size() - i - 1); chrom.fitness(sum); } //----------------------------------------------------------------------------- main() { const unsigned POP_SIZE = 8, CHROM_SIZE = 4; 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 << pop[i] << " " << pop[i].fitness() << endl; eoBinBitFlip bitflip; eoBinCrossover xover; eoProportionalOpSel propSel; eoBreeder breeder( propSel ); propSel.addOp(bitflip, 0.25); propSel.addOp(xover, 0.75); breeder(pop); cout << "new population:" << endl; for (i = 0; i < pop.size(); i++) cout << pop[i] << " " << pop[i].fitness() << endl; return 0; } //-----------------------------------------------------------------------------