Feat C++ API
A feature engineering automation tool
archive.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 "archive.h"
7 
8 namespace FT{
9 
10  namespace Pop{
11 
12  Archive::Archive(): selector(true) {};
13 
14  void Archive::set_objectives(vector<string> objectives)
15  {
16 
17  this->sort_complexity = in(objectives,std::string("complexity"));
18  }
19 
20 
21 
23  const Individual& rhs)
24  {
25  return lhs.complexity < rhs.complexity;
26  }
27 
28  bool Archive::sortObj1(const Individual& lhs,
29  const Individual& rhs)
30  {
31  return lhs.obj.at(0) < rhs.obj.at(0);
32  }
33 
35  const Individual& rhs)
36  {
37  return (lhs.fitness == rhs.fitness &&
38  lhs.get_complexity() == rhs.get_complexity());
39  }
40 
42  const Individual& rhs)
43  {
44  for (const auto& o_lhs : lhs.obj)
45  {
46  for (const auto& o_rhs : rhs.obj)
47  {
48  if (o_lhs != o_rhs)
49  return false;
50 
51  }
52  }
53  return true;
54  }
55 
57  {
58  auto tmp = pop.individuals;
59  selector.fast_nds(tmp);
60  /* vector<size_t> front = this->sorted_front(); */
61  for (const auto& t : tmp )
62  {
63  if (t.rank ==1){
64  individuals.push_back(t);
65  individuals.at(individuals.size()-1).set_complexity();
66  }
67  }
68  cout << "intializing archive with " << individuals.size() << " inds\n";
69  if (this->sort_complexity)
70  std::sort(individuals.begin(),individuals.end(), &sortComplexity);
71  else
72  std::sort(individuals.begin(),individuals.end(), &sortObj1);
73 
74  }
75 
76  void Archive::update(const Population& pop, const Parameters& params)
77  {
78 
79  vector<Individual> tmp = pop.individuals;
80 
81  for (const auto& p : individuals)
82  tmp.push_back(p);
83 
84  selector.fast_nds(tmp);
85 
86  vector<int> pf = selector.front.at(0);
87 
88  individuals.resize(0); // clear archive
89  for (const auto& i : pf) // refill archive with new pareto front
90  {
91  individuals.push_back(tmp.at(i));
92  individuals.at(individuals.size()-1).set_complexity();
93  }
94  if (this->sort_complexity)
95  std::sort(individuals.begin(),individuals.end(),&sortComplexity);
96  else
97  std::sort(individuals.begin(),individuals.end(), &sortObj1);
98  /* auto it = std::unique(individuals.begin(),individuals.end(), &sameFitComplexity); */
99  auto it = std::unique(individuals.begin(),individuals.end(),
100  &sameObjectives);
101  individuals.resize(std::distance(individuals.begin(),it));
102  }
103  }
104 }
individual programs in the population
Definition: individual.h:31
float fitness
aggregate fitness score
Definition: individual.h:38
vector< float > obj
objectives for use with Pareto selection
Definition: individual.h:45
unsigned int get_complexity() const
get the program complexity without updating it.
Definition: individual.cc:109
unsigned int complexity
the complexity of the program.
Definition: individual.h:50
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
vector< T > unique(vector< T > w)
returns unique elements in vector
Definition: utils.h:336
main Feat namespace
Definition: data.cc:13
int i
Definition: params.cc:552
holds the hyperparameters for Feat.
Definition: params.h:25
static bool sameObjectives(const Individual &lhs, const Individual &rhs)
Definition: archive.cc:41
static bool sortObj1(const Individual &lhs, const Individual &rhs)
Sort population by first objective.
Definition: archive.cc:28
void init(Population &pop)
Definition: archive.cc:56
static bool sameFitComplexity(const Individual &lhs, const Individual &rhs)
check for repeats
Definition: archive.cc:34
static bool sortComplexity(const Individual &lhs, const Individual &rhs)
Sort population in increasing complexity.
Definition: archive.cc:22
void update(const Population &pop, const Parameters &params)
Definition: archive.cc:76
bool sort_complexity
whether to sort archive by complexity
Definition: archive.h:29
NSGA2 selector
nsga2 selection operator for getting the front
Definition: archive.h:31
vector< Individual > individuals
individual programs in the archive
Definition: archive.h:28
void set_objectives(vector< string > objectives)
Definition: archive.cc:14
Defines a population of programs and functions for constructing them.
Definition: population.h:28
void fast_nds(vector< Individual > &)
Definition: nsga2.cc:121
vector< vector< int > > front
Definition: nsga2.h:34