dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
UniformCrossover.h
Go to the documentation of this file.
1 
11 #ifndef DIGNEA_UNIFORMCROSSOVER_H
12 #define DIGNEA_UNIFORMCROSSOVER_H
13 
14 #include <dignea/core/Crossover.h>
15 #include <dignea/utilities/random/PseudoRandom.h>
16 
17 #include <random>
18 
24 template <class S>
25 class UniformCrossover : public Crossover<S> {
26  public:
27  UniformCrossover() = default;
28 
29  virtual ~UniformCrossover() = default;
30 
31  void run(S &firstInd, S &secondInd) override;
32 
33  std::string getName() const override { return "Uniform One Crossover"; };
34 };
35 
43 template <class S>
44 void UniformCrossover<S>::run(S &firstInd, S &secondInd) {
45  // Usamos C++17 para no declarar el tipo del vector que obtenemos
46  vector secondIndVars = secondInd.getVariables();
47  vector firstIndVars = firstInd.getVariables();
48 
49  for (int i = 0; i < firstInd.getNumberOfVars(); i++) {
50  if (PseudoRandom::randDouble() < 0.5) {
51  auto tmpVariable = secondIndVars[i];
52  auto copyVar = firstIndVars[i];
53  secondIndVars[i] = copyVar;
54  firstIndVars[i] = tmpVariable;
55  }
56  }
57  firstInd.setVariables(firstIndVars);
58  secondInd.setVariables(secondIndVars);
59 }
60 
61 #endif // DIGNEA_UNIFORMCROSSOVER_H
Abstract Crossover interface.
Definition: Crossover.h:19
static double randDouble()
Generates a random double value between 0.0 and 1.0.
Definition: PseudoRandom.cpp:31
Uniform Crossover Operator.
Definition: UniformCrossover.h:25
void run(S &firstInd, S &secondInd) override
Performs the Uniform Crossover between the two individuals.
Definition: UniformCrossover.h:44