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


C++ GAPopulation::userData方法代码示例

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


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

示例1: while

//   This population evaluator is the administrator for the parallelization.
// It looks around to see when slaves are available to evaluate a genome.  As
// soon as a slave is available and a genome needs to be evaluated, this 
// routine sends it off.  When a slave is finished, it posts a message to 
// say so and this routine gets the message and grabs the results from the 
// slave that posted the message.
//   An index of -1 means that the slave has no assignment.  The first int in 
// the stream of stuff is always the ID of the slave (0-nslaves) that is 
// sending the information.  After that it is either nothing (the slave just 
// reported that it is ready for another genome) or it is a float (the score 
// of the genome that was assigned to the slave).
void 
PopulationEvaluator(GAPopulation& pop) {
  PVMDataPtr data = (PVMDataPtr)pop.userData();
  int* index = new int [data->nreq];
  int done = 0, outstanding = 0, next = 0;
  int bufid, status, bytes, msgtag, tid, who;

  while(!done) {
// If we have a genome that needs to be evaluated and one of the slaves is
// ready to evaluate it, send the genome to the slave.
    if(next < pop.size() && (bufid=pvm_nrecv(-1, MSG_READY)) != 0) {
      if(bufid > 0) {
	pvm_bufinfo(bufid, &bytes, &msgtag, &tid);
	status = SendGenomeData(pop.individual(next), tid);
	if(status >= 0) {
	  if((who = id2idx(tid, *data)) >= 0) {
	    index[who] = next; next++;
	    outstanding++;
	  }
	  else {
	    cerr << "PopEval: bogus tid mapping: " << tid << "\n";
	  }
	}
	else {
	  cerr << "PopEval: error sending data to: " << tid;
	  cerr << "  error code is: " << status << "\n";
	}
      }
      else {
	cerr << "PopEval: error from pvm_nrecv: " << bufid << "\n";
      }
    }

// If we have any genomes waiting for their evaluation and any slaves have 
// posted a message stating that they have a finished score ready for us, get
// the score from the slave and stuff it into the appropriate genome.
    if(outstanding > 0 && (bufid=pvm_nrecv(-1, MSG_GENOME_SCORE)) != 0) {
      if(bufid > 0) {
	pvm_bufinfo(bufid, &bytes, &msgtag, &tid);
	if((who = id2idx(tid, *data)) >= 0) {
	  if(index[who] >= 0) {
	    status = RecvGenomeScore(pop.individual(index[who]));
	    if(status >= 0) {
	      index[who] = -1;
	      outstanding--;
	    }
	    else {
	      cerr << "PopEval: error receiving score from: " << tid;
	      cerr << "  error code is: " << status << "\n";
	    }
	  }
	  else {
	    cerr << "PopEval: index conflict from tid " << tid << "\n";
	  }
	}
	else {
	  cerr << "PopEval: bogus tid mapping: " << tid << "\n";
	}
      }
      else {
	cerr << "PopEval: error from pvm_nrecv: " << bufid << "\n";
      }
    }

    if(next == pop.size() && outstanding == 0) done = 1;
    if(next > pop.size()) {
      cerr << "bogus value for next: " << next;
      cerr << "  popsize is: " << pop.size() << "\n";
    }
  }

  delete [] index;
}
开发者ID:distanceModling,项目名称:GAlib,代码行数:84,代码来源:genome.C


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