Feat C++ API
A feature engineering automation tool
offspring.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 "offspring.h"
7 
8 namespace FT{
9 
10  namespace Sel{
11 
12  Offspring::Offspring(bool surv){ name = "offspring"; survival = surv; elitism = true;};
13 
15 
16  vector<size_t> Offspring::survive(Population& pop,
17  const Parameters& params, const Data& d)
18  {
19  /* Selects the offspring for survival.
20  *
21  * @param pop: population of programs, parents + offspring.
22  * @param params: parameters.
23  *
24  * @return selected: vector of indices corresponding to offspring that are selected.
25  *
26  */
27 
28  int P = pop.individuals.size()/2; // index P is where the offspring begin, and also the size of the pop
29 
30  vector<size_t> selected(P);
31  // select popsize/2 to popsize individuals
32  std::iota(selected.begin(),selected.end(),P);
33 
34  if (selected.at(selected.size()-1) > pop.size())
35  THROW_LENGTH_ERROR("error: selected includes " +
36  to_string(selected.at(selected.size()-1)) +
37  ", pop size is " + to_string(pop.size()) + "\n");
38 
39  if (elitism)
40  { // find best and worst inds and if best is not in selected, replace worst with it
41  size_t best_idx, worst_idx;
42  float min_fit, max_fit;
43 
44  for (unsigned i = 0; i < pop.individuals.size(); ++i)
45  {
46  if (pop.individuals.at(i).fitness < min_fit || i == 0)
47  {
48  min_fit = pop.individuals.at(i).fitness;
49  best_idx = i;
50  }
51  if (i >= P) // finds worst among offspring
52  {
53  if (pop.individuals.at(i).fitness > max_fit || i == P)
54  {
55  max_fit = pop.individuals.at(i).fitness;
56  worst_idx = i;
57  }
58  }
59  }
60  if (best_idx < P) // then best individual is in parents, so replace worst_idx
61  {
62  selected.at(worst_idx - P) = best_idx;
63  }
64  }
65  return selected;
66  }
67  }
68 }
data holding X, y, and Z data
Definition: data.h:42
#define THROW_LENGTH_ERROR(err)
Definition: error.h:32
T pop(vector< T > *v)
Definition: auto_backprop.h:49
std::string to_string(const T &value)
template function to convert objects to string for logging
Definition: utils.h:422
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
Offspring(bool surv)
Definition: offspring.cc:12
vector< size_t > survive(Population &pop, const Parameters &params, const Data &d)
Definition: offspring.cc:16