Feat C++ API
A feature engineering automation tool
individual.h
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 #ifndef INDIVIDUAL_H
6 #define INDIVIDUAL_H
7 
8 #include "../dat/state.h"
9 #ifdef USE_CUDA
10  #include "cuda-op/cuda_utils.h"
11 #endif
12 #include "../dat/data.h"
13 #include "../params.h"
14 #include "../model/ml.h"
15 #include "../util/utils.h"
16 #include "../util/serialization.h"
17 #include "nodevector.h"
18 
19 namespace FT{
20 
21 using namespace Model;
22 
23 namespace Pop{
24 
26 
31 class Individual{
32 public:
34  MatrixXf Phi;
35  VectorXf yhat;
36  VectorXf error;
37  shared_ptr<ML> ml;
38  float fitness;
39  float fitness_v;
40  float fairness;
41  float fairness_v;
42  vector<float> w;
43  vector<float> p;
44  unsigned int dim;
45  vector<float> obj;
46  unsigned int dcounter;
47  vector<unsigned int> dominated;
48  unsigned int rank;
49  float crowd_dist;
50  unsigned int complexity;
51  vector<char> dtypes;
52  // program output
53  unsigned id;
54  vector<int> parent_id;
55  string eqn;
56 
57  Individual();
58 
60  /* Individual(const Individual& other); */
61 
62  /* Individual(Individual && other); */
63 
64  /* Individual& operator=(Individual const& other); */
65 
66  /* Individual& operator=(Individual && other); */
67  void initialize(const Parameters& params, bool random, int id=0);
68 
70  MatrixXf out(const Data& d, bool predict=false);
71 
73  MatrixXf out_trace(const Data& d, vector<Trace>& stack_trace);
74 
76  MatrixXf state_to_phi(State& state);
77 
79  shared_ptr<CLabels> fit(const Data& d, const Parameters& params,
80  bool& pass);
82  shared_ptr<CLabels> fit(const Data& d, const Parameters& params);
83 
85  shared_ptr<CLabels> fit_tune(const Data& d,
86  const Parameters& params, bool set_default=false);
87 
89  void tune(const Data& d, const Parameters& params);
94  shared_ptr<CLabels> predict(const Data& d);
95  VectorXf predict_vector(const Data& d);
96  ArrayXXf predict_proba(const Data& d);
98  string get_eqn() ;
99 
101  vector<string> get_features();
102 
104  string program_str() const;
105 
107  /* const std::unique_ptr<Node> operator [](int i) const {return program.at(i);} */
108  /* const std::unique_ptr<Node> & operator [](int i) {return program.at(i);} */
109 
111  void set_rank(unsigned r);
112 
114  int size() const;
115 
117  int get_n_params();
118 
120  /* size_t subtree(size_t i, char otype) const; */
121 
122  // // get program depth.
123  // unsigned int depth();
124 
126  unsigned int get_dim();
127 
129  int check_dominance(const Individual& b) const;
130 
132  void set_obj(const vector<string>&);
133 
135  unsigned int set_complexity();
136 
138  unsigned int get_complexity() const;
139 
141  void clone(Individual& cpy, bool sameid=true) const;
142  Individual clone();
143 
144  void set_id(unsigned i);
145 
147  void set_parents(const vector<Individual>& parents);
148 
150  void set_parents(const vector<int>& parents){ parent_id = parents; }
151 
153  vector<float> get_p() const;
154 
156  float get_p(const size_t i, bool normalize=true) const;
157 
159  vector<float> get_p(const vector<size_t>& locs, bool normalize=false) const;
160 
162  void set_p(const vector<float>& weights, const float& fb,
163  const bool softmax_norm=false);
164 
166  std::map<char,size_t> get_max_state_size();
167 
169  void save(string filename);
171  void load(string filename);
172 
173  typedef Array<bool, Dynamic, Dynamic, RowMajor> ArrayXXb;
174  /* typedef Array<float, Dynamic, Dynamic, RowMajor> ArrayXXf; */
175 
176 };
177 
178 // serialization
180  program,
181  /* Phi, */
182  /* yhat, */
183  /* error, */
184  eqn,
185  ml,
186  fitness,
187  fitness_v,
188  fairness,
189  fairness_v,
190  w,
191  p,
192  dim,
193  obj,
194  dcounter,
195  dominated,
196  rank,
197  crowd_dist,
198  complexity,
199  dtypes,
200  id,
201  parent_id
202  )
203 }
204 }
205 
206 #endif
data holding X, y, and Z data
Definition: data.h:42
individual programs in the population
Definition: individual.h:31
vector< float > w
weights from ML training on program output
Definition: individual.h:42
void set_parents(const vector< int > &parents)
set parent ids using id values
Definition: individual.h:150
float fairness
aggregate fairness score
Definition: individual.h:40
unsigned int dim
dimensionality of individual
Definition: individual.h:44
vector< float > p
probability of variation of subprograms
Definition: individual.h:43
VectorXf yhat
current output
Definition: individual.h:35
vector< char > dtypes
the data types of each column of the
Definition: individual.h:51
unsigned id
tracking id
Definition: individual.h:53
MatrixXf Phi
transformation output of program
Definition: individual.h:34
float fitness
aggregate fitness score
Definition: individual.h:38
NodeVector program
executable data structure
Definition: individual.h:33
vector< float > obj
objectives for use with Pareto selection
Definition: individual.h:45
shared_ptr< ML > ml
ML model, trained on Phi.
Definition: individual.h:37
void tune(const Data &d, const Parameters &params)
tunes an ML model to the data after transformation
vector< unsigned int > dominated
individual indices this dominates
Definition: individual.h:47
Array< bool, Dynamic, Dynamic, RowMajor > ArrayXXb
Definition: individual.h:173
unsigned int dcounter
number of individuals this dominates
Definition: individual.h:46
VectorXf error
training error
Definition: individual.h:36
vector< int > parent_id
ids of parents
Definition: individual.h:54
float crowd_dist
crowding distance on the Pareto front
Definition: individual.h:49
unsigned int rank
pareto front rank
Definition: individual.h:48
float fairness_v
aggregate validation fairness score
Definition: individual.h:41
string eqn
equation form of the program
Definition: individual.h:55
float fitness_v
aggregate validation fitness score
Definition: individual.h:39
unsigned int complexity
the complexity of the program.
Definition: individual.h:50
static Rnd & r
Definition: rnd.h:135
main Feat namespace
Definition: data.cc:13
int i
Definition: params.cc:552
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Parameters, pop_size, gens, current_gen, ml, classification, max_stall, otypes, ttypes, otype, verbosity, term_weights, op_weights, fn_str, terminals, longitudinalMap, max_depth, max_size, max_dim, erc, num_features, objectives, shuffle, split, dtypes, feedback, n_classes, cross_rate, classes, class_weights, sample_weights, scorer, scorer_, feature_names, backprop, hillclimb, max_time, use_batch, residual_xo, stagewise_xo, stagewise_xo_tol, corr_delete_mutate, root_xo_rate, softmax_norm, normalize, protected_groups, tune_initial, tune_final)
contains various types of State actually used by feat
Definition: state.h:102
holds the hyperparameters for Feat.
Definition: params.h:25
an extension of a vector of unique pointers to nodes
Definition: nodevector.h:23