예제 #1
0
파일: ga.cpp 프로젝트: franaln/evolve
void GA::evolve()
{
  // create initial population (generation 0)
  std::cout << "-- Generation 0 of " <<  m_generation_max << " ..." << std::endl;
  m_generation = 0;
  m_population.resize(m_population_size);
  m_last_population.resize(m_population_size);

  for (unsigned int i=0; i<m_population_size; i++){
    m_population[i] = new Individual();

    for (auto &var : m_variables) {
      m_population[i]->add_cut(get_random_cut(var));
    }
  }
  evaluate_fitness();
  show_best();

  // loop step until condition is satisfied
  while (check_end_condition()) {
    m_generation++;
    std::cout << "-- Generation " << m_generation << " of " <<  m_generation_max << " ..." << std::endl;
    step();
    show_best();
  }

  std::cout << std::endl;

  long total_calc = 1;
  for (auto &var : m_variables) {
    total_calc *= var.bins;
  }
  std::cout << "-- Number of calculations = " << hist_z->GetNbins() << " of " << total_calc << std::endl;

  output.close();

  // make plots
  std::cout << "-- Doing some plots..." << std::endl;
  plots();


}
예제 #2
0
파일: ga.cpp 프로젝트: franaln/evolve
void GA::step()
{
  pop_vector children;
  children.clear();
  children.reserve(m_population_size);

  // elitism
  unsigned int elite_size = m_population_size * m_elitism_rate;

  for (unsigned int i=0; i<elite_size; i++) {
    children.push_back(m_population[i]->copy());
  }

  // crossover
  while (children.size() < (m_population_size - elite_size)) {

    // select parents
    int p1 = roulette();
    int p2 = roulette();

    // crossover parents
    crossover(m_population[p1], m_population[p2], children);
  }

  // mutation
  mutate(children);

  // update population and save last population
  update(children);

  // evaluate fitness and sort
  evaluate_fitness();

  // log, save generation
  log();

  save_histograms();
}
예제 #3
0
stResult<TCity> * main_genetic(mySlimTree* SlimTree, TCity * queryObject, stDistance range)
{
	time_t begin, end;

	begin = time(NULL);
	
	srand(time(NULL));
	
	Population * pop = new_population();
	Population * aux_pop = new_empty_population();
	
	infeasible(SlimTree, pop);
	
	set_genotype(pop);
	
	evaluate_fitness(pop, SlimTree, queryObject, range);

	#if DEBUG
	print_population(pop ,"Pop", number_of_gerations);
	#endif

	while( (number_of_gerations < MAX_NUMBER_OF_GERATIONS) && (delta_fitness > 0.0001) )
	{
		number_of_gerations++;
		#if DEBUG
		cout << "Number of Gerations: "<< number_of_gerations << endl;
		#endif
		selection_by_tournament(pop, aux_pop);
		#if DEBUG
		cout << "Torneio" << endl;
		#endif
		crossover_uniform(aux_pop);
		#if DEBUG
		cout << "Crossover Uniforme" << endl;
		#endif
		mutation(aux_pop);
		#if DEBUG
		cout << "Mutação" << endl;
		#endif
		set_phenotype(aux_pop);
		#if DEBUG
		cout << "Fenotipo" << endl;
		#endif
		infeasible(SlimTree, aux_pop);
		#if DEBUG
		cout << "Infactibilidade" << endl;
		#endif
		set_genotype(aux_pop);
		#if DEBUG
		cout << "Fenotipo" << endl;
		#endif
		copy_population(aux_pop, pop);
		#if DEBUG
		cout << "Copiar aux pop" << endl;
		#endif
		evaluate_fitness(pop, SlimTree, queryObject, range);
		#if DEBUG
		cout << "Fitness" << endl;
		#endif
		
		old_media_of_fitness = media_of_fitness;
		media_of_fitness = get_media_of_population(pop);
		delta_fitness = fabs(media_of_fitness - old_media_of_fitness);
	}

	end = time(NULL);
	
	#if DEBUG
	print_population(pop, "PopulatioN", number_of_gerations);
	
	print_statistic(pop, begin, end);
	#endif
}