Example #1
0
void Genome::crossover(const Genome &with,Genome &baby1,Genome &baby2)
{
	baby1.setFitness(0.0);
	baby2.setFitness(0.0);

	int crossoverPoint = NeuralNetRandom::RandomInt(0,numberOfGenes()-1);

	std::vector<double> newChromosome1;
	std::vector<double> newChromosome2;

	std::vector<double>::iterator myGenes = _chromosome.begin();
	std::vector<double> otherChromosome = with.chromosome();
	std::vector<double>::iterator otherGenes = otherChromosome.begin();

	if (crossoverPoint > 0)
	{
		newChromosome1.assign(myGenes,myGenes+crossoverPoint+1);
		newChromosome2.assign(otherGenes,otherGenes+crossoverPoint+1);
	}
	newChromosome1.insert(newChromosome1.end(),otherGenes+crossoverPoint,otherChromosome.end());
	newChromosome2.insert(newChromosome2.end(),myGenes+crossoverPoint,_chromosome.end());

	baby1.setChromosome(newChromosome1);
	baby2.setChromosome(newChromosome2);
}
Example #2
0
void Genome::mutate(Genome &theGenome)
{
	std::vector<double> chromosome = theGenome.chromosome();
	for (int i=0;i<(int)chromosome.size();++i)
	{
		if (NeuralNetRandom::RandomFloat() < MutationRate)
			chromosome[i] += NeuralNetRandom::RandomClamped()*MaxMutationPerturbation;
	}
	theGenome.setChromosome(chromosome);
}