dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
BinaryTournamentSelection.h
Go to the documentation of this file.
1 
11 #ifndef DIGNEA_BINARYTOURNAMENTSELECTION_H
12 #define DIGNEA_BINARYTOURNAMENTSELECTION_H
13 
14 #include <dignea/core/Selection.h>
15 #include <dignea/utilities/random/PseudoRandom.h>
16 
22 template <class S>
24  public:
25  BinaryTournamentSelection() = default;
26 
27  virtual ~BinaryTournamentSelection() = default;
28 
29  void select(const vector<S> &, int &) override;
30 
31  S select(const vector<S> &population) override;
32 
33  std::string getName() const override {
34  return "Binary Tournament Selection";
35  };
36 };
37 
46 template <class S>
47 S BinaryTournamentSelection<S>::select(const vector<S> &population) {
48  int popSize = population.size() - 1;
49  int firstOption = PseudoRandom::randInt(0, popSize);
50  int secondOption = PseudoRandom::randInt(0, popSize);
51  int parent1 = min(firstOption, secondOption);
52  S result = population[parent1];
53  return result;
54 }
55 
65 template <class S>
66 void BinaryTournamentSelection<S>::select(const vector<S> &population,
67  int &parent) {
68  int popSize = population.size() - 1;
69  int firstOption = PseudoRandom::randInt(0, popSize);
70  int secondOption = PseudoRandom::randInt(0, popSize);
71  parent = min(firstOption, secondOption);
72 }
73 
74 #endif // DIGNEA_BINARYTOURNAMENTSELECTION_H
Binary Tournament Selection Operator.
Definition: BinaryTournamentSelection.h:23
void select(const vector< S > &, int &) override
Performs the Binary Tournament Selection operator over the population and sets the index of the paren...
Definition: BinaryTournamentSelection.h:66
static int randInt(int minBound, int maxBound)
Returns a random integer int the range [minBound, maxBound].
Definition: PseudoRandom.cpp:38
Abstract Selection interface.
Definition: Selection.h:21