Esempio n. 1
0
void es::evaluate_gen(unsigned int gen_id)
{
    try
    {
        run_simulation(gen_id);
        boost::format nec_output(eap::run_directory + "gen%04d/ind%09da%02d.out");
        for (unsigned int i_pop=0; i_pop<m_pop.size(); ++i_pop)
        {
            for (unsigned int i_ant=0; i_ant<m_ant_configs.size(); ++i_ant)
            {
                evaluation_ptr p_eval(new evaluation);
                m_pop[i_pop]->m_evals.push_back(p_eval);
                unsigned int read = read_radiation(str(nec_output % gen_id % i_pop % i_ant), p_eval);
                if (read != (num_polar() * m_step_freq))
                    throw eap::InvalidStateException("Problem with output in " + str(nec_output % gen_id % i_pop % i_ant));
                m_pop[i_pop]->m_one_ant_on_fitness.push_back(compare(m_free_inds[i_ant]->m_evals[0], m_pop[i_pop]->m_evals[i_ant]));
                m_pop[i_pop]->m_gain_fitness += m_pop[i_pop]->m_one_ant_on_fitness[i_ant];
            }
            //normalize gain fitness
            m_pop[i_pop]->m_gain_fitness /= m_max_gain;
            m_pop[i_pop]->m_coupling_fitness = read_coupling(str(nec_output % gen_id % i_pop % m_ant_configs.size()), m_ant_configs.size());

            //normalize coupling fitness
            m_pop[i_pop]->m_coupling_fitness += std::abs(m_min_coup);
            m_pop[i_pop]->m_coupling_fitness /= m_max_coup;

            m_pop[i_pop]->m_fitness = cal_fitness(m_pop[i_pop]);
        }
    }
    catch (...)
    {
        throw;
    }
}
Esempio n. 2
0
/**********************************************************************
 * 迭代
 **********************************************************************/
void tran()
{
    int i, j, pos;
    int k;
    //找当前种群最优个体 
    min = cur[0];
    for ( i=1; i<SIZE-1; i++ ) {
        if ( cur[i].fitness<min.fitness ) {
            min = cur[i];
        }
    }
    for ( k=0; k<SIZE; k+=2 ) {
        // 选择交叉个体 
        i = sel();
        j = sel();
        
        // 选择交叉位置 
        pos = randi(LEN-1);
        
        //交叉
        if ( randd()<P_CORSS ) {
            memcpy(next[k].x, cur[i].x, pos);
            memcpy(next[k].x+pos, cur[j].x+pos, LEN-pos);
            memcpy(next[k+1].x,cur[j].x,pos);
            memcpy(next[k+1].x+pos,cur[i].x+pos,LEN-pos);
        } else {
            memcpy(next[k].x,cur[i].x,LEN);
            memcpy(next[k+1].x,cur[j].x,LEN);
        }
        //变异
        if ( randd()<P_MUTATION ) {
            pos = randi(LEN-1);
            next[k].x[pos] ^= next[k].x[pos];
         
            pos = randi(LEN-1);
            next[k+1].x[pos] ^= next[k+1].x[pos];
        }
    }
    //找下一代的最差个体 
    max = next[0];
    j = 0;
    
    for ( i=1; i<SIZE-1; i++ ) {
        if ( next[i].fitness<min.fitness ) {
            max = next[i];
            j = i;
        }
    }
    //用上一代的最优个体替换下一代的最差个体
    next[j] = min;
    
    memcpy(cur, next, sizeof(cur));
    
    cal_fitness();
}
/**
 * main program
 */
int
main (void)
{

	float cpu1,cpu2;	
	cpu1 = ((float) clock())/CLOCKS_PER_SEC;
  srand (time (NULL));

  ga_struct *population = malloc (sizeof (ga_struct) * POPSIZE);
  ga_struct *beta_population = malloc (sizeof (ga_struct) * POPSIZE);

  init_population (population, beta_population);


  char *gen_str = population[0].gen;
char element[5] = "\0";
	  strncpy (element, gen_str, 4);
	  //if (strcmp ("0000", element) == 0)
	  
	  
  int index = 0;
  for (; index < POPSIZE; index++)
    {
      
      cal_fitness (population);
      
      sort_by_fitness (population);
      

      // print current best individual
      printf ("binary string: %s - fitness: %d\n", population[0].gen,
	      population[0].fitness);

      if (population[0].fitness == 0)
	{
	  //~ print equation
	  decode_gen (&population[0]);
	  break;
	}
	  
      mate (population, beta_population);
      swap (&population, &beta_population);
      
    }

  free_population (population);
  free_population (beta_population);
cpu2 = ((float) clock())/CLOCKS_PER_SEC;
  

  printf("Execution time (s) = %le\n",cpu2-cpu1);

  return 0;
}
Esempio n. 4
0
/*****************************************************************************
 * Population initial
 *****************************************************************************/
void population_init()
{
    int tmp;
    int i;
    int j;
  
    for ( i=0; i<SIZE; i++ ) {
        tmp = randi(N);
        for ( j=0; j<LEN; j++ ) {
            cur[i].x[j] = tmp%2;
            tmp = tmp>>1;
        }
    }
    cal_fitness();
}