当前位置: 首页>>代码示例>>C++>>正文


C++ Population::GetChildren方法代码示例

本文整理汇总了C++中Population::GetChildren方法的典型用法代码示例。如果您正苦于以下问题:C++ Population::GetChildren方法的具体用法?C++ Population::GetChildren怎么用?C++ Population::GetChildren使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Population的用法示例。


在下文中一共展示了Population::GetChildren方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1:

void
Strategies::FullGenerationalReplacement( Population &pop ) {
   /* All adults die, all children become adults */
   pop.SetAdults(pop.GetChildren());

   /* Clear children */
   pop.GetChildren().clear();
}
开发者ID:khskrede,项目名称:AI,代码行数:8,代码来源:strategies.cpp

示例2: while

void
Strategies::OverProduction( Population &pop ) {
   
   /* All adults die, children fight to become adults */
   pop.GetAdults().clear();
   
   int num;
   while ( pop.GetAdults().size() < pop.GetAdultsSize() ) {
      num = rand() % pop.GetChildren().size();
      pop.GetAdults().push_back( pop.GetChildren().at( num ) );
      pop.GetChildren().erase( pop.GetChildren().begin()+num );
   }

   /* Clear children */
   pop.GetChildren().clear();
}
开发者ID:khskrede,项目名称:AI,代码行数:16,代码来源:strategies.cpp

示例3: sort

void
Strategies::Elitism( Population &pop ) {
   unsigned int elitism = pop.GetElitism();
   if (elitism > 0) {
      sort(pop.GetAdults().begin(), pop.GetAdults().end(), IndividualSort);
      for (unsigned int i = 0; i<elitism; i++) {
         pop.GetChildren().push_back(pop.GetAdults().at(i));
      }
   }
}
开发者ID:khskrede,项目名称:AI,代码行数:10,代码来源:strategies.cpp

示例4: Elitism

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();
      
   }
}
开发者ID:khskrede,项目名称:AI,代码行数:89,代码来源:strategies.cpp


注:本文中的Population::GetChildren方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。