Feat C++ API
A feature engineering automation tool
random.cc
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 
6 #include "random.h"
7 
8 namespace FT{
9 
10  namespace Sel{
11 
14  Random::Random(bool surv){ name = "random"; survival = surv; elitism = true;};
15 
17 
18  vector<size_t> Random::select(Population& pop,
19  const Parameters& params, const Data& d)
20  {
21  /* Selects parents for making offspring.
22  *
23  * @param pop: population of programs, all potential parents.
24  * @param params: parameters.
25  *
26  * @return selected: vector of indices corresponding to offspring that are selected.
27  *
28  */
29 
30  int P = pop.size(); // index P is where the offspring begin, and also the size of the pop
31 
32  vector<size_t> all_idx(pop.size());
33  std::iota(all_idx.begin(), all_idx.end(), 0);
34 
35  cout << "selecting randoms\n";
36  vector<size_t> selected;
37  for (unsigned i = 0; i < P; ++i)
38  selected.push_back(r.random_choice(all_idx)); // select randomly
39 
40  cout << "getting elite\n";
41  if (elitism)
42  enforce_elite(pop, selected);
43 
44  return selected;
45  }
46 
47  vector<size_t> Random::survive(Population& pop,
48  const Parameters& params, const Data& d)
49  {
50  /* Selects the offspring for survival.
51  *
52  * @param pop: population of programs, parents + offspring.
53  * @param params: parameters.
54  *
55  * @return selected: vector of indices corresponding to offspring that are selected.
56  *
57  */
58 
59  int P = pop.individuals.size()/2; // index P is where the offspring begin, and also the size of the pop
60 
61  vector<size_t> all_idx(pop.size());
62  std::iota(all_idx.begin(), all_idx.end(), 0);
63 
64  cout << "selecting randoms\n";
65  vector<size_t> selected;
66  for (unsigned i = 0; i < P; ++i)
67  selected.push_back(r.random_choice(all_idx)); // select randomly
68  cout << "getting elite\n";
69  if (elitism)
70  enforce_elite(pop, selected);
71 
72  return selected;
73  }
74 
75  void Random::enforce_elite(Population& pop, vector<size_t>& selected)
76  {
77  // find best and worst inds and if best is not in selected, replace worst with it
78  size_t best_idx, worst_idx;
79  float min_fit, max_fit;
80 
81  for (unsigned i = 0; i < pop.individuals.size(); ++i)
82  {
83  if (pop.individuals.at(i).fitness < min_fit || i == 0)
84  {
85  min_fit = pop.individuals.at(i).fitness;
86  best_idx = i;
87  }
88  }
89 
90  if (!in(selected, best_idx) ) // add best individual to selected
91  {
92  for (unsigned i = 0; i < selected.size(); ++i)
93  {
94  if (pop.individuals.at(selected.at(i)).fitness > max_fit || i == 0)
95  {
96  max_fit = pop.individuals.at(selected.at(i)).fitness;
97  worst_idx = i;
98  }
99 
100  }
101  selected.at(worst_idx) = best_idx;
102  }
103 
104  }
105  }
106 
107 }
data holding X, y, and Z data
Definition: data.h:42
T random_choice(const vector< T > &v)
Definition: rnd.h:73
T pop(vector< T > *v)
Definition: auto_backprop.h:49
bool in(const vector< T > v, const T &i)
check if element is in vector.
Definition: utils.h:47
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
Defines a population of programs and functions for constructing them.
Definition: population.h:28
Random(bool surv)
Definition: random.cc:14
bool elitism
Definition: random.h:31
vector< size_t > survive(Population &pop, const Parameters &params, const Data &d)
Definition: random.cc:47
vector< size_t > select(Population &pop, const Parameters &params, const Data &d)
Definition: random.cc:18
void enforce_elite(Population &pop, vector< size_t > &selected)
replaces worst individual in selected with best individual in Pop.
Definition: random.cc:75