Genetic

Genetic - /* * Implementation of Class Genetic */ #include...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/**************************************************************** * Implementation of Class Genetic ***************************************************************/ #include "Genetic.h" #include "Simulation.h" #include "Troll.h" #include <stdlib.h> #include <iostream> #include <iomanip> #include <fstream> #include <assert.h> # using namespace std; u /***************************************************************** * Populating the world with trolls. .. ****************************************************************/ void Genetic::MakeRandomIndividual( int which ) { Population[ which ].DNA.Priorities[ EATING_PRIORITY ] = (double) ( rand() % 10000 ) / 10000.0; Population[ which ].DNA.Priorities[ KILLING_PRIORITY ] = (double) ( rand() % 10000 ) / 10000.0; Population[ which ].DNA.Priorities[ FLEEING_PRIORITY ] = (double) ( rand() % 10000 ) / 10000.0; Population[ which ].DNA.Priorities[ EXPLORING_PRIORITY ] = (double) ( rand() % 10000 ) / 10000.0; Population[ which ].DNA.Priorities[ HEALING_PRIORITY ] = (double) ( rand() % 10000 ) / 10000.0; } void Genetic::CreateInitialPopulation() { for( int i = 0; i < GA_POPULATION_SIZE; i++ ) { MakeRandomIndividual( i ); } } /***************************************************************** * The evolutionary process ****************************************************************/ // The mothership method for the whole process Individual Genetic::RunEvolution() { // Set up the gene pool CreateInitialPopulation(); theSim.BuildTestCases(); Individual bestTroll, currentBestTroll; I // And iterate the testing process over N generations for( int i = 0; i < GA_GENERATIONS; i++ ) { RunGeneration(); R currentBestTroll = ReportGenerationResults(i);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
if( i == 0 || (i > 0 && currentBestTroll.Performance > bestTroll.Performance)) bestTroll = currentBestTroll; if( i < GA_GENERATIONS - 1 ) BegetNextGeneration(); cout << "Generation " << setw(3) << i + 1 << ": Performance of best
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Genetic - /* * Implementation of Class Genetic */ #include...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online