| //-----------------------------------------------------------------------------
// FirstRealGA.cpp //----------------------------------------------------------------------------- //* // An instance of a VERY simple Real-coded Genetic Algorithm // //----------------------------------------------------------------------------- // standard includes #include <stdexcept> // runtime_error #include <iostream> // cout #include <strstream> // ostrstream, istrstream // the general include for eo #include <eo> |
| //-----------------------------------------------------------------------------
// Include the corresponding file #include <es.h> // real-representation & operators // define your individuals typedef eoReal<double> Indi; |
| //-----------------------------------------------------------------------------
void main_function(int argc, char **argv) { |
| // all parameters are hard-coded!
const unsigned int SEED = 42; // seed for random number generator const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes const unsigned int POP_SIZE = 20; // Size of population const unsigned int T_SIZE = 3; // size for tournament selection const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP const float CROSS_RATE = 0.8; // Crossover rate const double EPSILON = 0.01; // range for real uniform mutation const float MUT_RATE = 0.5; // mutation rate |
| /////////////////////////////
// Fitness function //////////////////////////// // Evaluation: from a plain C++ fn to an EvalFunc Object eoEvalFuncPtr<Indi> eval( real_value ); |
| ////////////////////////////////
// Initilisation of population //////////////////////////////// // declare the population eoPop<Indi> pop; // fill it! for (unsigned int igeno=0; igeno<POP_SIZE; igeno++) { Indi v; // void individual, to be filled for (unsigned ivar=0; ivar<VEC_SIZE; ivar++) { double r = 2*rng.uniform() - 1; // new value, random in [-1,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 } |
| // sort pop before printing
it!
pop.sort(); // Print (sorted) intial population (raw printout) cout << "Initial Population" << endl; cout << pop; |
| /////////////////////////////////////
// selection and replacement //////////////////////////////////// |
| // The robust tournament selection
eoDetTournamentSelect<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE] |
| // eoSGA uses generational replacement
by default
// so no replacement procedure has to be given |
| //////////////////////////////////////
// termination condition ///////////////////////////////////// // stop after MAX_GEN generations eoGenContinue<Indi> continuator(MAX_GEN); |
| //////////////////////////////////////
// The variation operators ////////////////////////////////////// |
| // offspring(i) uniformly chosen
in [parent(i)-epsilon, parent(i)+epsilon]
eoUniformMutation<Indi> mutation(EPSILON); |
| // offspring(i) is a linear
combination of parent(i)
eoArithmeticCrossover<Indi> xover; |
| /////////////////////////////////////////
// the algorithm //////////////////////////////////////// // standard Generational GA requires // selection, evaluation, crossover and mutation, stopping criterion eoSGA<Indi> gga(select, xover,
CROSS_RATE, mutation, MUT_RATE,
|
| // Print (sorted) intial population
pop.sort(); cout << "FINAL Population\n" << pop << endl; |
| }
// A main that catches the exceptions int main(int argc, char **argv) { #ifdef _MSC_VER // rng.reseed(42); 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; } |