For this particular program, as all new lines are concerned with program parameter input and information output, the font color for program-parameter-related sections will refer to the section where the parameters are used whereas the background color will remain blue.
//-----------------------------------------------------------------------------
// SecondGA.cpp //----------------------------------------------------------------------------- //* // Same code than FirstBitEA as far as Evolutionary Computation is concerned // but now you learn to enter the parameters in a more flexible way // and to twidle the output to your preferences! //----------------------------------------------------------------------------- // standard includes #include <stdexcept> // runtime_error #include <iostream> // cout #include <strstream> // ostrstream, istrstream #include <fstream> // the general include for eo #include <eo> |
// a simple fitness function that computes
the number of ones of a bitstring
#include "binary_value.h" |
//-----------------------------------------------------------------------------
// define your genotype and fitness types typedef eoBit<double> Indi; |
// the main_function: nothing changed(!),
except variable initialization
void main_function(int argc, char **argv) { |
// sort pop for pretty printout
pop.sort(); // Print (sorted) intial population (raw printout) cout << "Initial Population" << endl << pop << endl; |
/////////////////////////////////////
// selection and replacement //////////////////////////////////// |
// The robust tournament selection
eoDetTournamentSelect<Indi> selectOne(tSize); // tSize in [2,POPSIZE] // is now encapsulated in a eoSelectPerc (entage) eoSelectPerc<Indi> select(selectOne); // or eoSelectPerc<Indi> select(selectOne, rate); // but by default rate==1 |
// And we now have the full
slection/replacement - though with
// the same generational replacement at the moment :-) eoGenerationalReplacement<Indi> replace; |
//////////////////////////////////////
// The variation operators ////////////////////////////////////// |
// 1-point crossover for bitstring
eo1PtBitXover<Indi> xover1; // uniform crossover for bitstring eoUBitXover<Indi> xoverU; // 2-pots xover eoNPtsBitXover<Indi> xover2(2); // Combine them with relative rates eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate); xover.add(xoverU, URate); xover.add(xover2, twoPointsRate, true); |
// standard bit-flip mutation
for bitstring
eoBitMutation<Indi> mutationBitFlip(pMutPerBit); // mutate exactly 1 bit per individual eoDetBitFlip<Indi> mutationOneBit; // Combine them with relative rates eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate); mutation.add(mutationOneBit, oneBitRate, true); // The operators are encapsulated into an eoTRansform object eoSGATransform<Indi> transform(xover, pCross, mutation, pMut); |
//////////////////////////////////////
// termination condition see FirstBitEA.cpp ///////////////////////////////////// eoGenContinue<Indi> genCont(maxGen); eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen); eoFitContinue<Indi> fitCont(vecSize); eoCombinedContinue<Indi> continuator(genCont); continuator.add(steadyCont); continuator.add(fitCont); |
/////////////////////////////////////////
// the algorithm //////////////////////////////////////// // Easy EA requires // stopping criterion, eval, selection, transformation, replacement eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace); // Apply algo to pop - that's it! gga(pop); |
// Print (sorted) final population
pop.sort(); cout << "FINAL Population\n" << pop << endl; |
}
// A main that catches the exceptions int main(int argc, char **argv) { #ifdef _MSC_VER int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); flag |= _CRTDBG_LEAK_CHECK_DF; _CrtSetDbgFlag(flag); // _CrtSetBreakAlloc(100); #endif try { main_function(argc, argv); } catch(exception& e) { cout << "Exception: " << e.what() << '\n'; } return 1; } |