示例#1
0
void
Strategies::GenerationalMixing( Population &pop ) {
   /* Put all adults in child pool */
   while ( pop.GetAdults().size() > 0 ) {
      pop.GetChildren().push_back( pop.GetAdults().back() );
      pop.GetAdults().pop_back();
   }
   
   /* Clear adults */
   pop.GetAdults().clear();
   
   /* Select adults with equal probability */
   while ( pop.GetAdults().size() < pop.GetChildrenSize() ) {
      pop.GetAdults().push_back( pop.GetChildren().at( rand() % pop.GetChildrenSize() ) );
   }
   
   /* Clear children */
   pop.GetChildren().clear();
}
示例#2
0
void
Strategies::Select( Population &pop, float roulette[], int size, float max ) {
   
   /* Spin wheel untill we have enough children */
   /* Make sure an individual does not mate with itself */
   float spin1=0, spin2=0;
   int chosen1=0, chosen2=0;
   
   float crossover_rate = pop.GetCrossoverRate();
   
   while (pop.GetChildren().size() < pop.GetChildrenSize()) {
      /* Crossover */
      if( rand()/RAND_MAX <= crossover_rate ) {
         while (chosen1 == chosen2) {
            spin1 = (rand() * max) / RAND_MAX;
            spin2 = (rand() * max) / RAND_MAX;
            for ( unsigned int i = 0; i < pop.GetAdults().size(); i++) {
               if(roulette[i]<spin1) chosen1 = i;
               if(roulette[i]<spin2) chosen2 = i;
            } 
         }
         pop.GetChildren().push_back(pop.GetAdults().at(chosen1).Reproduce(pop.GetAdults().at(chosen2)));
         pop.GetChildren().push_back(pop.GetAdults().at(chosen2).Reproduce(pop.GetAdults().at(chosen1)));
      }
      /* Clone */
      else {
         spin1 = (rand() * max) / RAND_MAX;
         for ( unsigned int i = 0; i < pop.GetAdults().size(); i++) {
            if(roulette[i]<spin1) { 
               chosen1 = i;
            }
            else {
               break;
            }
         } 
         pop.GetChildren().push_back(pop.GetAdults().at(chosen2));
      }
      chosen1 = chosen2;
   }
}
示例#3
0
void
Strategies::TournamentSelection( Population &pop ) {

   /* Take care of elitism first */
   Elitism( pop );
      
   float crossover_rate = pop.GetCrossoverRate();

   std::vector<Individual> group_a;
   std::vector<Individual> group_b;
   while ( pop.GetChildren().size() < pop.GetChildrenSize() ) {

      unsigned int selector;
         
      /* Reproduce or clone best individual */
      if( rand()/RAND_MAX <= crossover_rate ) {


         /* select individuals for group a */
         for (unsigned int i = 0; i<pop.GetTournamentSize(); i++) {
            while( group_a.size() < pop.GetTournamentSize() ) {
               selector = rand() % pop.GetAdults().size();
               group_a.push_back( pop.GetAdults().at( selector ) );
               pop.GetAdults().erase( pop.GetAdults().begin() + selector );
            }
         }
         float temp_fit=0;
         /* Find best individual in group a */
         unsigned int best_in_a=0;
         for ( unsigned int i = 0; i < group_a.size(); i++ ) {
            if ( temp_fit < group_a.at(i).GetFitness() ) {
               best_in_a = i;
               temp_fit = group_a.at(i).GetFitness();
            }
         }
         
         /* select individuals for group b */
         unsigned int selector;
         for (unsigned int i = 0; i<pop.GetTournamentSize(); i++) {
            while( group_b.size() < pop.GetTournamentSize() ) {
               selector = rand() % pop.GetAdults().size();
               group_b.push_back( pop.GetAdults().at( selector ) );
               pop.GetAdults().erase( pop.GetAdults().begin() + selector );
            }
         }
         /* Find best individual in group b */
         unsigned int best_in_b=0;
         for ( unsigned int i = 0; i < group_b.size(); i++ ) {
            if ( temp_fit < group_b.at(i).GetFitness() ) {
               best_in_b = i;
               temp_fit = group_b.at(i).GetFitness();
            }
         }

         pop.GetChildren().push_back( group_a.at( best_in_a ).Reproduce( group_b.at( best_in_b ) ) );
         pop.GetChildren().push_back( group_b.at( best_in_b ).Reproduce( group_a.at( best_in_a ) ) );
      }
      else{
         /* select individuals for group 1 */
         for (unsigned int i = 0; i<pop.GetTournamentSize(); i++) {
            while( group_a.size() < pop.GetTournamentSize() ) {
               selector = rand() % pop.GetAdults().size();
               group_a.push_back( pop.GetAdults().at( selector ) );
               pop.GetAdults().erase( pop.GetAdults().begin() + selector );
            }
         }
         
         /* Find best individual */
         unsigned int best_in_a=0;
         float temp_fit=0;
         for ( unsigned int i = 0; i < group_a.size(); i++ ) {
            if ( temp_fit < group_a.at(i).GetFitness() ) {
               best_in_a = i;
               temp_fit = group_a.at(i).GetFitness();
            }
         }
         
         pop.GetChildren().push_back( group_a.at( best_in_a ) );
      }
      
      for ( unsigned int i = 0; i<group_a.size(); i++)
         pop.GetAdults().push_back(group_a.at(i));
      group_a.clear();
      for ( unsigned int i = 0; i<group_b.size(); i++)
         pop.GetAdults().push_back(group_b.at(i));
      group_b.clear();
      
   }
}