// Epoch Method
void GeneticAlgorithm::Epoch() {
	// Update the fitness scores
	UpdateFitnessScores();
	// Create a new population
	int iNewOffspring = 0;
	// Create a placeholder for the offspring
	vector<Genome> vOffspringGenomes;
	// Loop through the babies
	while (iNewOffspring < mPopulationSize) {
		// Select 2 parents
		Genome vMother = Selection();
		Genome vFather = Selection();
		// Offspring placeholders
		Genome vOffspringAlpha, vOffspringBeta;
		// Crossover
		Crossover(vMother.vBits, vFather.vBits, vOffspringAlpha.vBits, vOffspringBeta.vBits);
		// Mutate
		Mutate(vOffspringAlpha.vBits);
		Mutate(vOffspringBeta.vBits);
		// Add the offspring to the new population
		vOffspringGenomes.push_back(vOffspringAlpha);
		vOffspringGenomes.push_back(vOffspringBeta);
		// Increment the offspring
		iNewOffspring += 2;
	}
	// Copy the offspring back into the initial population
	mGenomes = vOffspringGenomes;
	// Increment the generations
	++mGeneration;
}
示例#2
0
//--------------------------------Epoch---------------------------------
//
//	This is the workhorse of the GA. It first updates the fitness
//	scores of the population then creates a new population of
//	genomes using the Selection, Croosover and Mutation operators
//	we have discussed
//----------------------------------------------------------------------
void CgaBob::Epoch()
{
	
	UpdateFitnessScores();
    
	//Now to create a new population
	int NewBabies = 0;
    
	//create some storage for the baby genomes 
	vector<SGenome> vecBabyGenomes;
    
	while (NewBabies < m_iPopSize)
	{
		//select 2 parents
		SGenome mum = RouletteWheelSelection();
		SGenome dad = RouletteWheelSelection();
        
		//operator - crossover
		SGenome baby1, baby2;
		Crossover(mum.vecBits, dad.vecBits, baby1.vecBits, baby2.vecBits);
        
		//operator - mutate
		Mutate(baby1.vecBits);
		Mutate(baby2.vecBits);
        
		//add to new population
		vecBabyGenomes.push_back(baby1);
		vecBabyGenomes.push_back(baby2);
        
		NewBabies += 2;
	}
    
	//copy babies back into starter population
	m_vecGenomes = vecBabyGenomes;
    
	//increment the generation counter
	++m_iGeneration;
}