Feat C++ API
A feature engineering automation tool
population.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 "population.h"
7 
8 namespace FT{
9 namespace Pop{
10 
11 int last;
12 
14 {
15  individuals.resize(p);
16  for (unsigned i = 0; i < individuals.size(); ++i)
17  {
18  individuals.at(i).set_parents(vector<int>(1,-1));
19  }
20 }
21 
23 
25 void Population::resize(int pop_size)
26 {
27  individuals.resize(pop_size);
28 }
29 
31 int Population::size(){ return individuals.size(); }
32 
33 const Individual Population::operator [](size_t i) const {return individuals.at(i);}
34 
35 const Individual & Population::operator [](size_t i) {return individuals.at(i);}
36 
37 
38 void Population::init(const Individual& starting_model,
39  const Parameters& params,
40  bool random,
41  string filename
42  )
43 {
48  int starting_size = 1;
49 
50  if (!filename.empty())
51  {
52  this->load(filename);
53 
54  starting_size = this->individuals.size();
55  cout << "individuals.size(): " << this->individuals.size() << endl;
56  cout << "params.pop_size: " << params.pop_size << endl;
57 
58  if (starting_size < params.pop_size)
59  {
60  individuals.resize(params.pop_size);
61  individuals.at(starting_size) = starting_model;
62  ++starting_size;
63  }
64  else if (starting_size == params.pop_size)
65  {
66  WARN("The first model provided in the startup file is going "
67  "to get overwritten by the initial (linear) model. "
68  "To avoid this, increase pop_size by 1."
69  );
70  individuals.at(0) = starting_model;
71  }
72  else
73  {
74  WARN("Too many models provided in the startup file ("
75  + to_string(starting_size) + "); pop_size is set to "
76  + to_string(params.pop_size) + ". Also, the first model is going "
77  "to get overwritten by the initial (linear) model.");
78  individuals.at(0) = starting_model;
79  }
80  }
81  else
82  {
83  individuals.at(0) = starting_model;
84  }
85 
86 
87  /* if the starting pop is smaller than the
88  * set size of the population, initialize
89  * additional individuals.*/
90  #pragma omp parallel for
91  for (int i = starting_size; i< individuals.size(); ++i)
92  {
93  individuals.at(i).initialize(params, random, i);
94  }
95 }
96 
97 void Population::update(vector<size_t> survivors)
98 {
99 
103  vector<size_t> pop_idx(individuals.size());
104  std::iota(pop_idx.begin(),pop_idx.end(),0);
105  std::reverse(pop_idx.begin(),pop_idx.end());
106  for (const auto& i : pop_idx)
107  if (!in(survivors,i))
108  individuals.erase(individuals.begin()+i);
109 
110 }
111 
113 {
118  individuals.push_back(ind);
119 }
120 
121 string Population::print_eqns(bool just_offspring, string sep)
122 {
123  string output = "";
124  int start = 0;
125 
126  if (just_offspring)
127  start = individuals.size()/2;
128 
129  for (unsigned int i=start; i< individuals.size(); ++i)
130  output += individuals.at(i).get_eqn() + sep;
131 
132  return output;
133 }
134 
135 vector<size_t> Population::sorted_front(unsigned rank=1)
136 {
137  /* Returns individuals on the Pareto front, sorted by increasign complexity. */
138  vector<size_t> pf;
139  for (unsigned int i =0; i<individuals.size(); ++i)
140  {
141  if (individuals.at(i).rank == rank)
142  pf.push_back(i);
143  }
144  std::sort(pf.begin(),pf.end(),SortComplexity(*this));
145  auto it = std::unique(pf.begin(),pf.end(),SameFitComplexity(*this));
146  pf.resize(std::distance(pf.begin(),it));
147  return pf;
148 }
149 
150 void Population::save(string filename)
151 {
152  std::ofstream out;
153  if (!filename.empty())
154  out.open(filename);
155  else
156  out.open("pop.json");
157 
158  json j;
159  to_json(j, *this);
160  out << j ;
161  out.close();
162  logger.log("Saved population to file " + filename, 1);
163 }
164 
165 void Population::load(string filename)
166 {
167  //TODO: replace with from_json(j, this) call
168  std::ifstream indata;
169  indata.open(filename);
170  if (!indata.good())
171  THROW_INVALID_ARGUMENT("Invalid input file " + filename + "\n");
172 
173  std::string line;
174  indata >> line;
175 
176  json j = json::parse(line);
177  from_json(j, *this);
178 
179  logger.log("Loaded population from " + filename + " of size = "
180  + to_string(this->size()),1);
181 
182  indata.close();
183 }
184 
185 
186 } // Pop
187 } // FT
individual programs in the population
Definition: individual.h:31
string log(string m, int v, string sep="\n") const
print message with verbosity control.
Definition: logger.cc:54
#define WARN(err)
Definition: error.h:33
#define THROW_INVALID_ARGUMENT(err)
Definition: error.h:31
int last
Definition: population.cc:11
void from_json(const json &j, NodeVector &nv)
Definition: nodevector.cc:442
void to_json(json &j, const NodeVector &nv)
Definition: nodevector.cc:392
static Logger & logger
Definition: logger.h:46
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
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
int pop_size
population size
Definition: params.h:28
check for same fitness and complexity to filter uniqueness.
Definition: population.h:78
Sort population in increasing complexity.
Definition: population.h:67
void load(string filename)
Definition: population.cc:165
void add(Individual &)
adds a program to the population.
Definition: population.cc:112
const Individual operator[](size_t i) const
setting and getting from individuals vector
Definition: population.cc:33
void update(vector< size_t > survivors)
reduce programs to the indices in survivors.
Definition: population.cc:97
string print_eqns(bool just_offspring=false, string sep="\n")
return population equations.
Definition: population.cc:121
int size()
returns population size
Definition: population.cc:31
vector< size_t > sorted_front(unsigned)
return complexity-sorted Pareto front indices.
Definition: population.cc:135
void resize(int pop_size)
update individual vector size
Definition: population.cc:25
void init(const Individual &starting_model, const Parameters &params, bool random=false, string filename="")
initialize population of programs with a starting model and/or from file
Definition: population.cc:38
vector< Individual > individuals
individual programs
Definition: population.h:29
void save(string filename)
Definition: population.cc:150