//--------------------------------------------------------------
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++;
}
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;
  }

}
Exemple #5
0
//void GenePool::updatePool(double crossover, double mutationchance, double severity, int tourneysize, int gen)
void GenePool::doPool(int maxsize, int deltype, int tourneysize, double crossover, double mutationchance, double severity, int numgens, double mutdev, double jostlechance, int simtype, int runs, int smult, int lmult)
{
   int top = 1;
   DNA thedna; //set by mutate/breed
   
   for (int fill = 1;fill < maxsize;fill++)
   {
      dnavect.push_back(thedna);
      fitness.push_back(-1);
   }
   
   
   //printf("fitness[%d] = %d ; cycle = %d\n", num, fitness[num], gs.cycle);
   //if (fitness[num] < 0) {dnavect[num].save("bad.dna");}
   
   //delete if needed
   
   
   int tourney[tourneysize];
   Random ran;
   
   
   for (int vvi = 0;vvi < numgens;vvi++)
   {
   
   //int selected[5]; //the index of tourney selected dudes
   //printf("ms1\n");
   int sa = 0;int sb = 0; //the two selected to mate 

   //pick best 'tourneysize' from the tourney
      //printf("ms2\n");
         //select sample for tourney
      for (int ii = 0;ii < tourneysize; ii++)
      {
         tourney[ii] = ran.nextInt(top);
      }
      int highestfit = -1;int highestfitindex = 0;
      //printf("ms3\n");
      for (int b = 0;b < tourneysize;b++)
      {
         if (fitness[tourney[b]] > highestfit) {highestfit = fitness[tourney[b]];highestfitindex = tourney[b];}
      }
      sa = highestfitindex;
      //printf("ms4\n");
      for (int ii = 0;ii < tourneysize; ii++)
      {
         tourney[ii] = ran.nextInt(top);
      }
      highestfit = -1;highestfitindex = 0;
      //printf("ms5\n");
      for (int b = 0;b < tourneysize;b++)
      {
         if (fitness[tourney[b]] > highestfit) {highestfit = fitness[tourney[b]];highestfitindex = tourney[b];}
      }
      sb = highestfitindex;

      //printf("ms6\n");
   //breed and mutate x1
      int inf1 = vvi;int inf2 = sa;int inf3 = fitness[sa];int inf4 = sb;int inf5 = fitness[sb];
      thedna = this->dnavect[sa].mate(dnavect[sb], crossover, reactionlock);
      //printf("msvv\n");
      double fmut = ran.nextGaussian()*mutdev+mutationchance;
      thedna.mutate(fmut, severity, reactionlock, jostlechance);
      //printf("ms7\n");
      int putwhere = 0;
      if (top == maxsize)
      {
         if (deltype == 0)
         { 
            int lowest = -1;
            int lowestindex = 0;
            for(int i = 0;i < top;i++)
            {
               if ((fitness[i] < lowest) || lowest == -1) {lowest = fitness[i]; lowestindex = i;}
            }
         
         //printf("ms8\n");
         putwhere = lowestindex;
         dnavect[putwhere] = thedna;
         }
         
         if (deltype == 1) //random del
         {
            putwhere = ran.nextInt(top);
            dnavect[putwhere] = thedna;
         }
         
         if (deltype == 2)
         {
            for (int ii = 0;ii < tourneysize; ii++)
            {
               tourney[ii] = ran.nextInt(top);
            }
            int lowfit = -1;int lowfitindex = 0;

            for (int b = 0;b < tourneysize;b++)
            {
               if (fitness[tourney[b]] < lowfit || lowfit == -1) {lowfit = fitness[tourney[b]];lowfitindex = tourney[b];}
            }
            
            putwhere = lowfitindex;
            dnavect[putwhere] = thedna;
         }
            
      }
      else
      {
         putwhere = top;top++;
         dnavect[putwhere] = thedna;
         //dnavect.push_back(thedna);
      }

      //printf("ms9\n");
      
      int inf6 = putwhere;int inf7=fitness[putwhere];
      this->simulate(putwhere, simtype, runs, smult, lmult);
      printf("Creature: %d\n", inf1);
      printf("mate1: %d, mate1fit: %d,  mate2: %d,  mate2fit: %d\n",inf2, inf3, inf4, inf5);
      printf("Final Mutation Chance: %lf\n", fmut);
      printf("Fitness of this creature: %d\n", fitness[putwhere]);
      printf("Overwrite Number: %d,  Overwrite Fitness: %d\n", inf6, inf7);
      printf("POOL INFO: Best: %d, Worst: %d, Mean: %g, StdDev: %g\n\n\n", this->bestFitness(top), this->worstFitness(top), this->meanFitness(top), this->standardDeviation(top));

      //do milestone save here
      
}


}