Feat C++ API
A feature engineering automation tool
simulated_annealing.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 "simulated_annealing.h"
7 
8 namespace FT{
9 
10  namespace Sel{
11 
12  SimAnneal::SimAnneal(bool surv){ name = "simanneal"; survival = surv; t0 = 10;};
13 
15 
16  vector<size_t> SimAnneal::select(Population& pop,
17  const Parameters& params, const Data& d)
18  {
19  /* Selects parents for making offspring.
20  *
21  * @param pop: population of programs, all potential parents
22  * @param params: parameters.
23  *
24  * @return selected: vector of indices corresponding to offspring that are selected.
25  *
26  */
27 
28  int P = pop.size(); // index P is where the offspring begin, and also the size of the pop
29 
30  vector<size_t> all_idx(pop.size());
31  std::iota(all_idx.begin(), all_idx.end(), 0);
32  return all_idx;
33  }
34 
35  vector<size_t> SimAnneal::survive(Population& pop,
36  const Parameters& params, const Data& d)
37  {
38  /* Selects the offspring for survival using simulated annealing.
39  *
40  * Offspring are compared to their parents. The probability of an offspring, R, replacing
41  * its parent, S, is given by
42  *
43  * P(t, R, S ) = exp( ( fitness(S) - fitness(R) ) / t)
44  *
45  * where t is the temperature.
46  *
47  * @param pop: population of programs, parents + offspring.
48  * @param params: parameters.
49  *
50  * @return selected: vector of indices corresponding to offspring that are selected.
51  *
52  */
53 
54  // decrease temperature linearly based on current generation
55  // cooling schedule: Tg = (0.9)^g * t0, g = current generation
56  this->t = pow(0.9, float(params.current_gen))*this->t0;
57  /* cout << "t: " << this->t << "\n"; */
58 
59  int P = pop.individuals.size()/2; // index P is where the offspring begin, and also the size of the pop
60  vector<size_t> selected(P);
61  #pragma omp parallel for
62  for (unsigned i = P; i < pop.individuals.size(); ++i)
63  {
64  Individual& offspring = pop.individuals.at(i);
65  /* cout << "offspring: " << offspring.get_eqn() << "\n"; */
66  int pid = offspring.parent_id.at(0);
67  bool found = false;
68  int j = 0;
69  while (!found && j < P)
70  {
71  if ( pop.individuals.at(j).id == pid)
72  found=true;
73  else
74  ++j;
75  }
76  Individual& parent = pop.individuals.at(j);
77  /* cout << "parent: " << parent.get_eqn() << "\n"; */
78  /* cout << "offspring fitness: " << offspring.fitness << "\n"; */
79  /* cout << "parent fitness: " << parent.fitness << "\n"; */
80  float probability = exp ( (parent.fitness - offspring.fitness)/this->t );
81 
82  /* cout << "probability: " << probability << "\n"; */
83  if (r() < probability)
84  selected.at(i-P) = i;
85  else
86  selected.at(i-P) = j;
87  }
88  return selected;
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
vector< int > parent_id
ids of parents
Definition: individual.h:54
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
float t
annealing temperature
float t0
initial temperature
vector< size_t > select(Population &pop, const Parameters &params, const Data &d)
vector< size_t > survive(Population &pop, const Parameters &params, const Data &d)