コード例 #1
0
ファイル: CGenAlg.cpp プロジェクト: craigaloewen/FlappyBirdAI
//-----------------------------------Epoch()-----------------------------
//
//	takes a population of chromosones and runs the algorithm through one
//	 cycle.
//	Returns a new population of chromosones.
//
//-----------------------------------------------------------------------
vector<SGenome> CGenAlg::Epoch(vector<SGenome> &old_pop)
{
	//assign the given population to the classes population
  m_vecPop = old_pop;

  //reset the appropriate variables
  Reset();

  //sort the population (for scaling and elitism)
  sort(m_vecPop.begin(), m_vecPop.end());

  //calculate best, worst, average and total fitness
	CalculateBestWorstAvTot();
  
  //create a temporary vector to store new chromosones
	vector <SGenome> vecNewPop;

	//Now to add a little elitism we shall add in some copies of the
	//fittest genomes. Make sure we add an EVEN number or the roulette
  //wheel sampling will crash
	if (!(CParams::iNumCopiesElite * CParams::iNumElite % 2))
	{
		GrabNBest(CParams::iNumElite, CParams::iNumCopiesElite, vecNewPop);
	}
	

	//now we enter the GA loop
	
	//repeat until a new population is generated
	while (vecNewPop.size() < m_iPopSize)
	{
		//grab two chromosones
		SGenome mum = GetChromoRoulette();
		SGenome dad = GetChromoRoulette();

		//create some offspring via crossover
		vector<double>		baby1, baby2;

		Crossover(mum.vecWeights, dad.vecWeights, baby1, baby2);

		//now we mutate
		Mutate(baby1);
		Mutate(baby2);

		//now copy into vecNewPop population
		vecNewPop.push_back(SGenome(baby1, 0));
		vecNewPop.push_back(SGenome(baby2, 0));
	}

	//finished so assign new pop back into m_vecPop
	m_vecPop = vecNewPop;

	return m_vecPop;
}
コード例 #2
0
ファイル: GeneticAlg.cpp プロジェクト: mrG7/GeneticNeuralNet
TArray<FGenome> GeneticAlg::Epoch(TArray<FGenome> &OldPop)
{
	PopArray = OldPop;
	Reset();
	PopArray.Sort(); //sorts Fgenomes by fitness
	CalculateBestWorstAvTot();
	TArray<FGenome> NewPop;
	if (!(NumCopiesElite * NumElites % 2))
	{
		GrabNBest(NumElites, NumCopiesElite, NewPop);
	}
	while (NewPop.Num() < PopulationSize)
	{
		//grab two chromosones
		FGenome mum = GetChromoRoulette();
		FGenome dad = GetChromoRoulette();

		//create some offspring via crossover
		TArray<float>		baby1, baby2;

		CrossOver(mum.WeightsArray, dad.WeightsArray, baby1, baby2);

		//now we mutate
		Mutate(baby1);
		Mutate(baby2);

		//now copy into vecNewPop population
		NewPop.Add(FGenome(baby1, 0));
		NewPop.Add(FGenome(baby2, 0));
	}

	//finished so assign new pop back into m_vecPop
	PopArray = NewPop;

	return PopArray;
	
}