Ejemplo n.º 1
0
//--------------------------------------------------------------
void ofApp::draw()
{
	if (!foundSolution)
	{
		for (int i = 0; i < ArrayCount(population); i++)
		{
			population[i].Fitness();
		}

		vector<DNA> matingPool = vector<DNA>();
		for (int i = 0; i < ArrayCount(population); i++)
		{
			int n = int(population[i].fitness * ArrayCount(population));
			for (int k = 0; k < n; k++)
			{
				matingPool.push_back(population[i]);
			}
		}

		for (int i = 0; i < ArrayCount(population); i++)
		{
			int a = int(ofRandom(0, matingPool.size()));
			int b = int(ofRandom(0, matingPool.size()));

			DNA parentA = matingPool[a];
			DNA parentB = matingPool[b];

			DNA child = parentA.crossover(parentB);
			child.mutate();

			population[i] = child;
		}
	}

	ofClear(ofColor::white);

	ofSetColor(ofColor::black);
	for (int i = 0; i < ArrayCount(population); i++)
	{
		//string str = population[i].genes;
		population[i].genes[ArrayCount(population[i].genes)] = '\0';

		if (population[i].genes == target)
		{
			foundSolution = true;
			ofNoFill();
			ofSetLineWidth(6);
			ofSetCurveResolution(200);
			ofCircle(ofPoint((int(i / POPULATION_PER_COLUMN) * Y_SPACING) + 100, 
								((i % POPULATION_PER_COLUMN) * X_SPACING) + BUFFER_SPACING), 120);
		}
		

		myFont.drawString(population[i].genes, 
							(int(i / POPULATION_PER_COLUMN) * Y_SPACING) + BUFFER_SPACING, 
							((i % POPULATION_PER_COLUMN) * X_SPACING) + BUFFER_SPACING);

	}

}
// 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
		RocketRef mom = mMatingPool[m];
		RocketRef 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 );
		
		mPopulation[i].reset(); // get rid of the old rocket
		mPopulation[i] = std::make_shared<Rocket>( location, child, mTarget );
    }
    mGenerations++;
}
Ejemplo n.º 3
0
void Population::reproduce() {
  for (int i = 0; i < popSize; i++) {
    int a = ofRandom(matingPool.size());
    int b = ofRandom(matingPool.size());
    DNA partnerA = matingPool[a];
    DNA partnerB = matingPool[b];
    //Step 3a: Crossover
    DNA child = partnerA.crossover(partnerB);
    //Step 3b: Mutation
    child.mutate(mutationRate);
    //Note that we are overwriting the population with the new children. When draw() loops, we will perform all the same steps with the new population of children.
    population[i] = child;
  }
}
void Population::reproduction() {

  for (int i = 0; i < (int)elements.size(); i++) {
    elements.at(i)->computeFitness(target);
  }

  vector<DNA *> pool = matingPool();


  for (int i = 0; i < (int)elements.size(); i++) {
    DNA * partnerA = pool.at(randInt(pool.size()));
    DNA * partnerB = pool.at(randInt(pool.size()));
    
    DNA * child = partnerA->crossover(partnerB);
    child->mutate(mutationRate);

    elements.at(i) = child;
  }

}