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


C++ QArray::at方法代码示例

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


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

示例1: evolve

void SimpleGA::evolve(Team *T) {
   Team *oldTeam, *newTeam, *delTeam;
   QArray<floatbot> botFitness;
   unsigned int teamSize, breed;
   int i;
   Bot *curbot;
   Bot *newbot1, *newbot2;

   oldTeam = T;
   newTeam = new Team;
   delTeam = new Team;

   teamSize = oldTeam->size();
   botFitness.resize(teamSize);
   for (i=0; i< (int)teamSize; i++) {
      curbot = oldTeam->bot(i);
      botFitness[i].fit = curbot->fitnessFunction();
      botFitness[i].bot = i;
   }
   botFitness.sort();

   // reverse fitness array
   for (i=0; i<(int)(teamSize/2); i++) {
      floatbot temp;
      temp.fit = botFitness[i].fit;
      temp.bot = botFitness[i].bot;
      botFitness.at(i).fit = botFitness.at(teamSize-i-1).fit;
      botFitness.at(i).bot = botFitness.at(teamSize-i-1).bot;
      botFitness.at(teamSize-i-1).fit = temp.fit;
      botFitness.at(teamSize-i-1).bot = temp.bot;
   }
   
   // add the good bots to the new team
   breed = teamSize / 2;                 // number of bots to breed
   if ( (breed/2)*2 != breed ) breed--;  // make sure its even
   for (i=0; i<(int)(teamSize - breed); i++) {
      // number of bots to throw out is the number bred, so keep the number not thrown out
      curbot = oldTeam->bot(botFitness[i].bot);
      newTeam->insertBot(curbot);
   }
   for (i=(teamSize - breed); i<(int)teamSize; i++) {
      // put bots to be deleted on a separate team
      curbot = oldTeam->bot(botFitness[i].bot);
      delTeam->insertBot(curbot);
   }
   while (oldTeam->size() > 0) {
      // remove bots from oldTeam
      curbot = oldTeam->removeBot(0);
   }
   while (delTeam->size() > 0) {
      // actually delete bots
      curbot = delTeam->removeBot(0);
      delete curbot;
   }
   
   // do the crossover of the best bots
   for (i=0; i<(int)breed; i+=2) {
      newbot1 = new Bot(oldTeam);
      newbot2 = new Bot(oldTeam);
      crossover(newTeam->bot(i), newTeam->bot(i+1), newbot1, newbot2);
      newTeam->insertBot(newbot1);
      newTeam->insertBot(newbot2);
   }
   
   // mutate a bot given the mutation rate
   for (i=0; i<(int)teamSize; i++) {
      double rnd = Random::randd(0,1);
      if (rnd < MutationRate) {
         curbot = newTeam->bot(i);
         curbot->mutateBot();
      }
   }
   
   // copy bots back into old team
   while (newTeam->size() > 0) {
      curbot = newTeam->removeBot(0);
      oldTeam->insertBot(curbot);
   }
   oldTeam->generations(oldTeam->generations() + 1);
   delete delTeam;
   delete newTeam;
}
开发者ID:edlau,项目名称:GABot,代码行数:82,代码来源:simplega.cpp


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