1 #ifndef FAIF_SEARCH_EA_H 2 #define FAIF_SEARCH_EA_H 13 #include <boost/bind.hpp> 14 #include <boost/concept_check.hpp> 16 #include "../utils/Random.hpp" 25 typedef std::vector<Ind> Population;
30 template<
typename Space>
32 typedef typename Space::Individual Individual;
33 typedef typename Space::Population Population;
34 typedef typename Space::Fitness Fitness;
46 template<
typename Space>
48 typedef typename Space::Individual Individual;
49 typedef typename Space::Population Population;
50 typedef typename Space::Fitness Fitness;
62 template<
typename Space>
64 typedef typename Space::Individual Individual;
65 typedef typename Space::Population Population;
66 typedef typename Space::Fitness Fitness;
71 Space::crossover(ind, population);
74 Population population;
91 static typename Space::Individual&
mutation(
typename Space::Individual& ind) {
107 static typename Space::Individual&
mutation(
typename Space::Individual& ind) {
108 return Space::mutation(ind);
119 static typename Space::Individual&
crossover(
typename Space::Individual& ind,
typename Space::Population& ) {
136 static typename Space::Individual&
crossover(
typename Space::Individual& ind,
typename Space::Population& pop) {
137 return Space::crossover(ind, pop);
148 static typename Space::Population&
selection(
typename Space::Population& population,
int size) {
150 typename Space::Population::iterator middle = population.begin();
151 std::advance(middle, size);
154 std::nth_element(population.begin(), middle, population.end(),
155 boost::bind(Space::fitness, _1) > boost::bind(Space::fitness, _2) );
157 population.erase( middle, population.end() );
164 typedef double Fitness;
165 typedef double Probability;
166 typedef std::vector<Fitness> Fitnesses;
167 typedef std::vector<Probability> Probabilities;
168 typedef std::vector<int> Indexes;
171 VoseAlg(
const Fitnesses& fitnesses)
172 : N_(static_cast<int>(fitnesses.size())),
173 gen_(0.0, static_cast<double>(N_) ),
177 Fitness min_fitness = std::numeric_limits<Fitness>::min();
178 Fitnesses::const_iterator ind_min = std::min_element(fitnesses.begin(), fitnesses.end() );
179 if( ind_min != fitnesses.end() )
180 min_fitness = *ind_min;
182 Fitness sum = std::accumulate( fitnesses.begin(), fitnesses.end(), 0.0 );
183 sum -= N_* min_fitness;
185 Probabilities norm_fitnesses;
186 norm_fitnesses.reserve(N_);
187 if( sum < std::numeric_limits<Fitness>::epsilon() ) {
188 fill_n( std::back_inserter(norm_fitnesses), N_, 0.0 );
190 std::transform( fitnesses.begin(), fitnesses.end(), std::back_inserter(norm_fitnesses),
191 boost::bind( std::divides<Fitness>(), boost::bind( std::minus<Fitness>(), _1, min_fitness ), sum ) );
199 Indexes small, large;
200 const Probability one_div_n = 1.0/
static_cast<double>(N_);
201 for(
int index = 0; index < N_; ++index) {
202 if(norm_fitnesses[index] < one_div_n)
203 small.push_back(index);
205 large.push_back(index);
207 while(!small.empty() && !large.empty()) {
208 int j = small.back(); small.pop_back();
209 int k = large.back(); large.pop_back();
210 prob_[j] = N_ * norm_fitnesses[j];
212 norm_fitnesses[k] += norm_fitnesses[j] - one_div_n;
213 if(norm_fitnesses[k] > one_div_n)
219 while(!small.empty()) {
220 prob_[small.back()] = 1.0;
223 while(!large.empty()) {
224 prob_[large.back()] = 1.0;
229 int getIndexForRandom() {
231 int index =
static_cast<int>(x);
232 if(( x - static_cast<Probability>(index)) > prob_[index])
233 index = alias_[index];
249 static typename Space::Population&
selection(
typename Space::Population& population,
int size) {
251 typedef typename Space::Population Population;
253 std::vector<typename Space::Fitness> fitnesses;
254 std::transform( population.begin(), population.end(),
255 std::back_inserter(fitnesses), boost::bind( &Space::fitness, _1 ) );
257 Population old_population(population);
260 std::generate_n( std::back_inserter(population), size,
266 static const typename Space::Individual& generateIndividualFun(
const typename Space::Population* population,
268 int index = vose->getIndexForRandom();
269 return population->at(index);
279 template<
typename Space,
287 typedef typename Space::Individual Individual;
288 typedef typename Space::Population Population;
289 typedef typename Space::Fitness Fitness;
299 Individual&
solve(Population& init_population, StopCondition stop = StopCondition() ) {
301 int pop_size =
static_cast<int>(init_population.size());
302 Population& current = init_population;
310 Population old_population(current);
312 doMutation( current,
typename Mutation<Space>::TransformationCategory() );
319 doCrossover(current,
typename Crossover<Space>::TransformationCategory() );
327 std::copy(old_population.begin(), old_population.end(), back_inserter(current) );
334 Selection<Space>::selection(current, pop_size);
340 stop.update(current);
342 while(! stop.isFinished() );
344 typename Population::iterator best =
345 std::max_element(current.begin(), current.end(),
346 boost::bind(Space::fitness, _1) < boost::bind(Space::fitness, _2) );
354 std::transform( population.begin(), population.end(), population.begin(),
355 boost::bind( &Mutation<Space>::mutation, _1 ) );
361 std::transform(population.begin(), population.end(), population.begin(),
362 boost::bind( &Crossover<Space>::crossover, _1, population));
370 #endif // FAIF_SEARCH_EA_H succession and selection policy - roulette wheel (probability of selection of an idividual is equal t...
Definition: EvolutionaryAlgorithm.hpp:246
the typedef-s for space for evolutionary algorithm, where the population is a vector of individuals...
Definition: EvolutionaryAlgorithm.hpp:24
the concept for evolutionary algorithm space
Definition: EvolutionaryAlgorithm.hpp:47
TransformationCustomTag TransformationCategory
tranformation policy (custom)
Definition: EvolutionaryAlgorithm.hpp:130
static Space::Population & selection(typename Space::Population &population, int size)
sort (partial) the population and removes the worst elements. Population is returned having n element...
Definition: EvolutionaryAlgorithm.hpp:148
the evolutionary algorithm
Definition: EvolutionaryAlgorithm.hpp:285
succession and selection policy - the n-th best individuals survive
Definition: EvolutionaryAlgorithm.hpp:143
crossover policy - crossover from Space
Definition: EvolutionaryAlgorithm.hpp:127
mutation policy - no mutation
Definition: EvolutionaryAlgorithm.hpp:85
static Space::Individual & mutation(typename Space::Individual &ind)
mutation is empty operation
Definition: EvolutionaryAlgorithm.hpp:91
mutation policy - mutation from Space
Definition: EvolutionaryAlgorithm.hpp:98
crossover policy - no crossover
Definition: EvolutionaryAlgorithm.hpp:113
the uniform distribution for double, in given range, e.g. <0,1), uses RandomSingleton ...
Definition: Random.hpp:74
the typedef-s for space, where the fitness is defined as double
Definition: Space.hpp:25
TransformationNoneTag TransformationCategory
tranformation policy (none)
Definition: EvolutionaryAlgorithm.hpp:88
Stop condition, finish the algorithm after STEPS_NUM iterations.
Definition: Space.hpp:46
TransformationNoneTag TransformationCategory
tranformation policy (none)
Definition: EvolutionaryAlgorithm.hpp:116
helping class implemented the M.D.Vose (1991) algorithm
Definition: EvolutionaryAlgorithm.hpp:163
TransformationCustomTag TransformationCategory
tranformation policy (custom)
Definition: EvolutionaryAlgorithm.hpp:101
Individual & solve(Population &init_population, StopCondition stop=StopCondition())
the evolutionary algorithm - until stop repeat mutation, cross-over, selection, succession. Modifies the initial population.
Definition: EvolutionaryAlgorithm.hpp:299
static Space::Individual & crossover(typename Space::Individual &ind, typename Space::Population &pop)
calls the mutation function from Space
Definition: EvolutionaryAlgorithm.hpp:136
static Space::Population & selection(typename Space::Population &population, int size)
select individuals with probability proportional to their fitness
Definition: EvolutionaryAlgorithm.hpp:249
static Space::Individual & mutation(typename Space::Individual &ind)
calls the mutation function from Space
Definition: EvolutionaryAlgorithm.hpp:107
static Space::Individual & crossover(typename Space::Individual &ind, typename Space::Population &)
crossover is empty operation
Definition: EvolutionaryAlgorithm.hpp:119
the concept for evolutionary algorithm space
Definition: EvolutionaryAlgorithm.hpp:31
the concept for evolutionary algorithm space
Definition: EvolutionaryAlgorithm.hpp:63