Exemple #1
0
main()
{
  Pop pop;
  EORandom<float> random;
  Chrom chrom(NUM_GENES, BITSxGEN);
  for (unsigned i = 0; i < POP_SIZE; i++)
    {
      random(chrom);
      chrom.fitness(i);
      pop.push_back(chrom);
    }

  Bin fitness;
  EOEvalAll<Chrom> eval(fitness);
  EOStat<Chrom> graphEval( eval, true );  // True is for verbose
  
  EORank<Chrom> transform(1);
  //  EOMutation<Chrom> mutation(0.05);
  //  transform.addOp((EOOp<Chrom>*)&mutation);
  NxOver<Chrom> nxover(2);
  transform.addOp(&nxover);
  
  EOGenTerm<Chrom> term(100);
  
  EOAgeGA<Chrom> agega(graphEval, transform, term);
  
  try
    {
      agega(pop); 
    }
  catch(UException& e)
    {
      cout << e.what() << endl;
    }
 
  cout << "the best: " << pop.front() << endl;

  return 0;
}
Exemple #2
0
main( int argc, char** argv) {

  unsigned numGenes = 2,  // Number of genes in the chromosome
    numGenerations,       // Number of generations
    popSize;              // Population size
  float xOverPr,      // Crossover rate, from 0 to 1
    mutPr;            // Mutation rate, from 0 to 1
  bool verbose;       // true if you want many things printed
  
  // Obtain command line parameters or default values
  getParams( argc, argv, verbose, popSize, numGenerations,
	     xOverPr, mutPr );  

  // Create an initial population and the population-level operators
  Pop pop;
  unsigned i, j;
  Uniform<float> u( 0, 1);
  for ( j = 0; j < popSize; j ++ ) {
    Chrom* aChrom = new Chrom;	
    for ( i = 0; i < numGenes; i ++ ) {
      aChrom->push_back( u() );
    }
    pop.push_back( *aChrom );	  
    if ( verbose ) {
      copy( aChrom->begin(), aChrom->end(), ostream_iterator<int>( cout, " ") );
      cout << endl;
    }
    delete aChrom;    
  }
  // Create an instance of the evaluation function object 
  deJongF2float thisEvalFunc;
  // From that, create an object that evaluates all the population
  EOEvalAll<Chrom>  eval( thisEvalFunc) ;
  // Then, an object that select who's going to reproduce
  EOLottery<Chrom> select;
  // Another object that mates them
  EORandomBreed<Chrom> breed;
  // And another that weeds out the worst
  EOElimAll<Chrom> replace;
  // This one check if the algorithm has finishd or not
  EOGenTerm<Chrom> term( numGenerations );
 
  // Then the EO-level operators
  // This is a mutation-like operator
  EOFloatArrayRndMutate<Chrom> rndMut( 0.1, mutPr );
  // And this is the classical 2-point crossover
  EOXOver2<Chrom> xOver(xOverPr); 

  // Add operators to breeder
  breed.addOp( &rndMut );
  breed.addOp( &xOver );

  // Then, the algorithm itself is instantiated
  EOEasyGA<Chrom> ga( eval, select, breed, term, replace, verbose );  
  // Now, apply the algorithm to the population
  eval( pop );
  ga( pop );
  // Sort Population. First is the best
  sort( pop.begin(), pop.end(), SortEO<Chrom>() ); 
  // And print results!
  cout << "Best fitness ....... " << pop[0].fitness() << endl
       << "Value...............  " << pop[0] << endl;
  return 0;
}
    void operator()()
    {
	communicator world;
	ostringstream ss;
	ss << "trace." << world.rank() << ".algo.txt";
	ofstream of(ss.str().c_str());

	for (int g = 0; g >= 0/* < 10*/; ++g)
	    {
		// of << "."; of.flush();

		// of << _pop.size() << " "; of.flush();

		// Selection
		for (int i = 0; i < world.size(); ++i)
		    {
			if (i == world.rank()) continue;

			// int chunk = 2;
			// int size = _pop.size() > chunk ? chunk : _pop.size();
			int size = _pop.size();
			if (size)
			    {
				Pop<EOT> newpop;
				// copy_n(_pop.begin(), size, back_inserter(newpop));

				for (auto& ind : _pop)
				    {
					newpop.push_back(ind);
				    }

				of << _pop.size() << " "; of.flush();
				// _pop.erase(_pop.begin(), _pop.begin() + size);
				_pop.clear();
				of << _pop.size() << " "; of.flush();

				m_em.lock();
				vec_em[i].push( newpop );
				m_em.unlock();
			    }
		    }
		// !Selection

		for (int i = 0; i < world.size(); ++i)
		    {
			if (i == world.rank()) continue;

			m_imm.lock();
			while (!vec_imm[i].empty())
			    {
				of << "="; of.flush();
				auto& newpop = vec_imm[i].front();
				// copy(newpop.begin(), newpop.end(), back_inserter(_pop));
				for (auto& ind : newpop)
				    {
					_pop.push_back(ind);
				    }
				vec_imm[i].pop();
			    }
			m_imm.unlock();
		    }

		// std::this_thread::sleep_for(std::chrono::microseconds(100000));
		pt_cv->notify_one();
	    }
    }