示例#1
0
void
Strategies::RankSelection( Population &pop ) {
   /* Take care of elitism first */
   Elitism( pop );
   
   /* Create roullette wheel */
   float max = 0;
   float min = 999999999999999999;
   float sum = 0;
   float roulette[pop.GetAdults().size()];

   /* Find min and max */

   for ( unsigned int i = 0 ; i < pop.GetAdults().size() ; i++ ) {
      if ( max < pop.GetAdults().at(i).GetFitness() ) max = pop.GetAdults().at(i).GetFitness();
      if ( min > pop.GetAdults().at(i).GetFitness() ) min = pop.GetAdults().at(i).GetFitness();
   }
   
   max = pop.GetAdults().back().GetFitness();
   min = pop.GetAdults().at(0).GetFitness();
   
   roulette[0]=0;
   for ( unsigned int i = 1 ; i < pop.GetAdults().size() ; i++ ) {
      roulette[i] = roulette[i-1] + min+(max-min)*(i - 1) / (pop.GetAdults().size() - 1);
   }
   sum = roulette[pop.GetAdults().size()-1] + max;
   
   Select( pop, roulette, pop.GetAdults().size(), sum );
}
示例#2
0
void
Strategies::SigmaScaling( Population &pop ) {
   /* Take care of elitism first */
   Elitism( pop );
   
   /* Create roullette wheel */
   float averagefitness = pop.GetAverageFit();
   float stddeviation = pop.GetStdDeviation();

   float max;
   float roulette[pop.GetAdults().size()];
   roulette[0] = 0;
      
   if ( stddeviation != 0 ) {
      float fitness;
      for ( unsigned int i = 1 ; i < pop.GetAdults().size() ; i++ ) {
         fitness = pop.GetAdults().at(i-1).GetFitness();
         roulette[i] = roulette[i-1] + 1 
                       + (fitness-averagefitness)/(2*stddeviation);
      }
      fitness = pop.GetAdults().back().GetFitness();
      max = roulette[pop.GetAdults().size()-1] + 1 + (fitness-averagefitness)/(2*stddeviation);
   }
   else {
      for ( unsigned int i = 1 ; i < pop.GetAdults().size() ; i++ ) {
         roulette[i]=i;
      }
      max = pop.GetAdults().size();
   }
   
   Select( pop, roulette, pop.GetAdults().size(), max );
}
void Population::Evolve(size_t elitism_count) {
	std::vector<Individual> evolved_pop(pop_.size());
	std::vector<size_t> elites = Elitism(elitism_count);
	
	/* Choosing elite individuals uses raw fitness */
	for (size_t j = 0; j < elitism_count; ++j) {
		evolved_pop[j] = pop_[elites[j]];
	}

	for (size_t j = elitism_count; j < pop_.size(); ++j) {
		size_t p1 = SelectIndividual();
		size_t p2;
		do {
			p2 = SelectIndividual();
		} while (p2 == p1);

		Individual parent1(pop_[p1]);
		Individual parent2(pop_[p2]);
		Crossover(&parent1, &parent2);

		evolved_pop[j] = parent1;
		evolved_pop[j].Mutate(mutation_rate_);
	}
	this->pop_ = evolved_pop;
	CalculateFitness();
}
示例#4
0
void
Strategies::FitnessProportionate( Population &pop ) {
   /* Take care of elitism first */
   Elitism( pop );

   /* Create roullette wheel, normalize fitness values */
   float sum = pop.GetFitnessSum(); 
   float roulette[pop.GetAdults().size()];
   roulette[0]=0;
   
   for ( unsigned int i = 1 ; i < pop.GetAdults().size() ; i++ ) {
      roulette[i]=roulette[i-1] + pop.GetAdults().at(i-1).GetFitness() / sum;
   }
   
   Select( pop, roulette, pop.GetAdults().size(), 1 );
}
示例#5
0
void
Strategies::BoltzmannSelection( Population &pop ) {
   /* Take care of elitism first */
   Elitism( pop );
   
   float temperature = pop.GetTemperature();

   /* Create roullette wheel */
   float max = 0;
   float roulette[pop.GetAdults().size()];

   /* e ^ ( f(i) / T ) / <e ^ ( f(i) / T )>g */
   roulette[0]=0;
   for ( unsigned int i = 1 ; i < pop.GetAdults().size() ; i++ ) {
      roulette[i] = roulette[i-1] + exp( pop.GetAdults().at(i).GetFitness() / temperature );
   }
   max = roulette[pop.GetAdults().size()-1] + exp( pop.GetAdults().back().GetFitness() / temperature ) ;

   Select( pop, roulette, pop.GetAdults().size(), max );
}
示例#6
0
void
Strategies::TournamentSelection( Population &pop ) {

   /* Take care of elitism first */
   Elitism( pop );
      
   float crossover_rate = pop.GetCrossoverRate();

   std::vector<Individual> group_a;
   std::vector<Individual> group_b;
   while ( pop.GetChildren().size() < pop.GetChildrenSize() ) {

      unsigned int selector;
         
      /* Reproduce or clone best individual */
      if( rand()/RAND_MAX <= crossover_rate ) {


         /* select individuals for group a */
         for (unsigned int i = 0; i<pop.GetTournamentSize(); i++) {
            while( group_a.size() < pop.GetTournamentSize() ) {
               selector = rand() % pop.GetAdults().size();
               group_a.push_back( pop.GetAdults().at( selector ) );
               pop.GetAdults().erase( pop.GetAdults().begin() + selector );
            }
         }
         float temp_fit=0;
         /* Find best individual in group a */
         unsigned int best_in_a=0;
         for ( unsigned int i = 0; i < group_a.size(); i++ ) {
            if ( temp_fit < group_a.at(i).GetFitness() ) {
               best_in_a = i;
               temp_fit = group_a.at(i).GetFitness();
            }
         }
         
         /* select individuals for group b */
         unsigned int selector;
         for (unsigned int i = 0; i<pop.GetTournamentSize(); i++) {
            while( group_b.size() < pop.GetTournamentSize() ) {
               selector = rand() % pop.GetAdults().size();
               group_b.push_back( pop.GetAdults().at( selector ) );
               pop.GetAdults().erase( pop.GetAdults().begin() + selector );
            }
         }
         /* Find best individual in group b */
         unsigned int best_in_b=0;
         for ( unsigned int i = 0; i < group_b.size(); i++ ) {
            if ( temp_fit < group_b.at(i).GetFitness() ) {
               best_in_b = i;
               temp_fit = group_b.at(i).GetFitness();
            }
         }

         pop.GetChildren().push_back( group_a.at( best_in_a ).Reproduce( group_b.at( best_in_b ) ) );
         pop.GetChildren().push_back( group_b.at( best_in_b ).Reproduce( group_a.at( best_in_a ) ) );
      }
      else{
         /* select individuals for group 1 */
         for (unsigned int i = 0; i<pop.GetTournamentSize(); i++) {
            while( group_a.size() < pop.GetTournamentSize() ) {
               selector = rand() % pop.GetAdults().size();
               group_a.push_back( pop.GetAdults().at( selector ) );
               pop.GetAdults().erase( pop.GetAdults().begin() + selector );
            }
         }
         
         /* Find best individual */
         unsigned int best_in_a=0;
         float temp_fit=0;
         for ( unsigned int i = 0; i < group_a.size(); i++ ) {
            if ( temp_fit < group_a.at(i).GetFitness() ) {
               best_in_a = i;
               temp_fit = group_a.at(i).GetFitness();
            }
         }
         
         pop.GetChildren().push_back( group_a.at( best_in_a ) );
      }
      
      for ( unsigned int i = 0; i<group_a.size(); i++)
         pop.GetAdults().push_back(group_a.at(i));
      group_a.clear();
      for ( unsigned int i = 0; i<group_b.size(); i++)
         pop.GetAdults().push_back(group_b.at(i));
      group_b.clear();
      
   }
}
示例#7
0
void TSP_GA::Mate()
{
    int esize = m_iPopulationSize * m_fElitRate;
    Elitism(esize);
    std::vector<ga_struct> omp_Buffer = m_Buffer;

    srand (time(NULL));

    // Mate the rest
#pragma omp parallel for shared(omp_Buffer)
    for (int i = esize; i < m_iPopulationSize; i++)
    {
        int pos1 = -1, pos2 = -1, i1, i2;

        int elit = (m_iPopulationSize * m_fElitRate);
        i1 = rand() % elit;
        i2 = (rand() % (m_iPopulationSize - elit)) + elit;

        pos1 = rand() % m_iSize;
        pos2 = rand() % m_iSize;
        if (pos1 > pos2)
        {
            std::swap(pos1, pos2);
        }

        omp_Buffer[i].way.clear();
        std::vector<int>num;
        for (int j = 0; j < m_iSize; j++)
        {
            num.push_back(j);
        }

        for (int j = 1; j < m_iSize - 1; j++)
        {
            for (int k = j + 1; k < m_iSize - 1; k++)
            {
                if (m_Population[i1].way[j] == m_Population[i2].way[j]
                 && m_Population[i1].way[k] == m_Population[i2].way[k])
                {
                    pos1 = j, pos2 = k;
                }
            }
        }

        if (WayLength(m_Population[i1].way, pos1, pos2) >=
            WayLength(m_Population[i2].way, pos1, pos2))
        {
            std::swap(i1, i2);
        }

        for (int j = pos1; j < pos2; j++)
        {
            num[m_Population[i1].way[j]] = -1;
        }
        for (int j = 0; j < pos1; j++)
        {
            if (num[m_Population[i2].way[j]] != -1)
            {
                omp_Buffer[i].way.push_back(m_Population[i2].way[j]);
                num[m_Population[i2].way[j]] = -1;
            }
        }
        for (int j = pos1; j < pos2; j++)
        {
            omp_Buffer[i].way.push_back(m_Population[i1].way[j]);
        }
        for (int j = pos1; j < m_iSize; j++)
        {
            if (num[m_Population[i2].way[j]] != -1)
            {
                omp_Buffer[i].way.push_back(m_Population[i2].way[j]);
                num[m_Population[i2].way[j]] = -1;
            }
        }
        omp_Buffer[i].way.push_back(0);

        float rnd = rand() / (float)RAND_MAX;

        if (rnd < m_fMutationRate)
        {
            Mutate(omp_Buffer[i]);
        }
        else if (rnd < m_fMutationMoveRate)
        {
            Mutate_move(omp_Buffer[i]);
        }
        else if (rnd < m_fMutationSupRate)
        {
            SupMutate(omp_Buffer[i]);
        }

    }
    m_Buffer = omp_Buffer;
}