Feat C++ API
A feature engineering automation tool
tournament.cc
Go to the documentation of this file.
1 /* FEWTWO
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 
6 #include "tournament.h"
7 
8 namespace FT{
9 
10  namespace Sel{
12 
16  Tournament::Tournament(bool surv){ name = "tournament"; survival = surv; };
17 
19 
20  size_t Tournament::tournament(vector<Individual>& pop, size_t i, size_t j) const
21  {
22  Individual& ind1 = pop.at(i);
23  Individual& ind2 = pop.at(j);
24 
25  if (ind1.fitness < ind2.fitness)
26  return i;
27  else if (ind1.fitness == ind2.fitness)
28  return r() < 0.5 ? i : j;
29  else
30  return j;
31 
32  }
33 
34  vector<size_t> Tournament::select(Population& pop,
35  const Parameters& params, const Data& d)
36  {
37  /* Selection using tournament selection.
38  *
39  * Input:
40  *
41  * pop: population of programs.
42  * params: parameters.
43  * r: random number generator
44  *
45  * Output:
46  *
47  * selected: vector of indices corresponding to pop that are selected.
48  * modifies individual ranks, objectives and dominations.
49  */
50  vector<size_t> pool(pop.size());
51  std::iota(pool.begin(), pool.end(), 0);
52  // if this is first generation, just return indices to pop
53  if (params.current_gen==0)
54  return pool;
55 
56  vector<size_t> selected(pop.size());
57 
58  for (int i = 0; i < pop.size(); ++i)
59  {
60  size_t winner = tournament(pop.individuals, r.random_choice(pool),
61  r.random_choice(pool));
62  selected.push_back(winner);
63  }
64  return selected;
65  }
66 
68  const Parameters& params, const Data& d)
69  {
70  /* Selection using the survival scheme of NSGA-II.
71  *
72  * Input:
73  *
74  * pop: population of programs.
75  * params: parameters.
76  * r: random number generator
77  *
78  * Output:
79  *
80  * selected: vector of indices corresponding to pop that are selected.
81  * modifies individual ranks, objectives and dominations.
82  */
83 
84  THROW_RUNTIME_ERROR("Not implemented");
85  return vector<size_t>();
86  }
87 
88 
89  }
90 
91 }
92 
data holding X, y, and Z data
Definition: data.h:42
individual programs in the population
Definition: individual.h:31
float fitness
aggregate fitness score
Definition: individual.h:38
T random_choice(const vector< T > &v)
Definition: rnd.h:73
#define THROW_RUNTIME_ERROR(err)
Definition: error.h:30
T pop(vector< T > *v)
Definition: auto_backprop.h:49
static Rnd & r
Definition: rnd.h:135
main Feat namespace
Definition: data.cc:13
int i
Definition: params.cc:552
holds the hyperparameters for Feat.
Definition: params.h:25
int current_gen
holds current generation
Definition: params.h:30
Defines a population of programs and functions for constructing them.
Definition: population.h:28
size_t tournament(vector< Individual > &pop, size_t i, size_t j) const
Definition: tournament.cc:20
vector< size_t > survive(Population &pop, const Parameters &p, const Data &d)
survival according to the survival scheme of Tournament
Definition: tournament.cc:67
vector< size_t > select(Population &pop, const Parameters &p, const Data &d)
selection according to the survival scheme of Tournament
Definition: tournament.cc:34
Tournament(bool surv)
Definition: tournament.cc:16