// Making the next generation
void Population::reproduction()
{
    // Refill the population with children from the mating pool
    for( int i = 0; i < mPopulation.size(); i++ )
	{
		// Sping the wheel of fortune to pick two parents
		int m = randInt( mMatingPool.size() );
		int d = randInt( mMatingPool.size() );
		// Pick two parents
		Rocket *mom = mMatingPool[m];
		Rocket *dad = mMatingPool[d];
		// Get their genes
		DNA *momgenes = mom->getDNA();
		DNA *dadgenes = dad->getDNA();
		// Mate their genes
		DNA *child = momgenes->crossover( dadgenes );
		// Mutate their genes
		child->mutate( mMutationRate );
		// Fill the new population with the new child
		Vec2f location = Vec2f( getWindowWidth() / 2.0, getWindowHeight() + 20.0 );
		
		delete mPopulation[i]; // get rid of the old rocket
		mPopulation[i] = new Rocket( location, child, mTarget );
    }
    mGenerations++;
}
예제 #2
0
Rocket Rocket::mate(Rocket partner)
{
    Rocket child = Rocket();
    child.setDNA(dna.crossover(partner.getDNA()));
    child.setTarget(target);
    child.setObstacles(obstacles);
    return child;
}