dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
EGenerational.h
Go to the documentation of this file.
1 
11 #ifndef DIGNEA_EGENERATIONAL_H
12 #define DIGNEA_EGENERATIONAL_H
13 
14 #include <dignea/core/Replacement.h>
16 #include <dignea/utilities/exceptions/NotImplemented.h>
17 
18 #include <string>
19 
20 using namespace std;
21 
27 template <class S>
28 class EGenerational : public Replacement<S> {
29  public:
30  EGenerational() = default;
31 
32  virtual ~EGenerational() = default;
33 
34  public:
35  vector<S> replace(vector<S> &, vector<S> &) override;
36 
37  vector<S> replace(vector<S> &, const S &, const int &position) override;
38 
39  string getName() const override { return "EGenerational"; }
40 };
41 
50 template <class S>
51 vector<S> EGenerational<S>::replace(vector<S> &population,
52  vector<S> &offspring) {
53  if (population.empty() || offspring.empty()) {
54  std::string what = "vector of individuals empty";
55  throw(runtime_error("EGenerational Replacement" + what));
56  }
57  if (population.size() != offspring.size()) {
58  std::string what =
59  "population.siz() = " + to_string(population.size()) +
60  " != offspring.size = " + to_string(offspring.size());
61  throw(runtime_error("EGenerational Replacement" + what));
62  }
63  // Nos quedamos con el mejor de todos --> elitismo
64  sortByFitness(population);
65  for (unsigned int i = 1; i < population.size(); i++) {
66  population[i] = offspring[i];
67  }
68  return population;
69 }
79 template <class S>
80 vector<S> EGenerational<S>::replace(vector<S> &population, const S &offspring,
81  const int &position) {
82  string where =
83  "Replace with position not allowed in EGenerational replacement";
84  throw NotImplemented(where);
85 }
86 
87 #endif // DIGNEA_EGENERATIONAL_H
void sortByFitness(vector< S > &population)
Sorts the population by fitness in descending order.
Definition: Sorter.h:69
Elitist Generational Replacement Operator Performs the generational replacement operator with elitism...
Definition: EGenerational.h:28
vector< S > replace(vector< S > &, vector< S > &) override
Performs the generational replacement operator between the two vectors of individuals....
Definition: EGenerational.h:51
Replacement skeleton operator.
Definition: Replacement.h:16