61 WARN(
"Failed to build tree. trying again ...");
99 for (
unsigned int i =0;
i<
program.size(); ++
i)
103 n_params +=
program.at(
i)->arity.at(
'f');
117 for (
const auto&
p : parents)
131 cout<<
"Weights are\n";
132 for(
float weight : weights)
137 for(
auto root : root1)
140 cout<<
"Program is \n";
141 for (
const auto&
p :
program) std::cout <<
p->name <<
" ";
150 for (
unsigned i =0;
i<weights.size(); ++
i)
151 sum += fabs(weights.at(
i));
155 p.resize(weights.size());
156 for (
unsigned i=0;
i< weights.size(); ++
i)
157 p.at(
i) = 1 - fabs(weights.at(
i)/sum);
160 float u = 1.0/float(
p.size());
168 for (
unsigned i=0;
i<
p.size(); ++
i)
169 p.at(
i) = (1-fb)*u + fb*
p.at(
i);
186 float size = rts.at(0)+1;
192 while ( j < rts.size())
195 size = rts.at(j) - rts.at(j-1);
199 float tmp = normalize ?
p.at(j)/
size :
p.at(j) ;
201 return normalize ?
p.at(j)/
size :
p.at(j) ;
206 if (
i >= rts.size() || j == rts.size())
208 cout <<
"WARN: bad root index attempt in get_p()\n";
212 float tmp = normalize ?
p.at(j)/
size :
p.at(j) ;
218 bool normalize)
const
226 for (
const auto& el : locs)
229 ps.push_back(
get_p(el,normalize));
245 shared_ptr<CLabels> yh = this->
ml->fit(
Phi,d.
y,params,pass,
dtypes);
249 logger.
log(
"Setting individual's weights...", 3);
255 vector<float>
w(
Phi.rows(), 0);
259 this->
yhat =
ml->labels_to_vector(yh);
268 return this->
fit(d, params, pass);
276 MatrixXf Phi_pred =
out(d,
true);
279 if (Phi_pred.size()==0)
289 shared_ptr<CLabels>
yhat =
ml->predict(Phi_pred);
298 MatrixXf Phi_pred =
out(d,
true);
301 if (Phi_pred.size()==0)
303 "predict() is called\n");
307 ArrayXXf
yhat =
ml->predict_proba(Phi_pred);
313 return ml->labels_to_vector(this->
predict(d));
330 if (state.
f.
size()==0)
332 if (state.
c.
size() == 0)
334 if (state.
b.
size() == 0)
337 cols = state.
b.
top().size();
340 cols = state.
c.
top().size();
344 cols = state.
f.
top().size();
348 Matrix<float,Dynamic,Dynamic,RowMajor>
Phi (
352 std::map<char,int> rows;
358 for (
int i = 0;
i < this->
dtypes.size(); ++
i)
366 Row = ArrayXf::Map(state.
f.
at(rows.at(rt)).data(),cols);
371 state.
c.
at(rows.at(rt)).data(),cols).cast<
float>();
376 state.
b.
at(rows.at(rt)).data(),cols).cast<
float>();
409 if (n->isNodeTrain())
411 if(state.
check(n->arity))
412 n->evaluate(d, state);
444 state.allocate(state_size,d.
X.cols());
449 if (n->isNodeTrain())
451 if(state.
check(n->arity))
453 n->evaluate(d, state);
455 state.update_idx(n->otype, n->arity);
459 std::cout <<
"individual::out() error: node " << n->name <<
" in " +
program_str() +
461 std::cout <<
"float state size: " << state.
f.
size() <<
"\n";
462 std::cout <<
"bool state size: " << state.
b.
size() <<
"\n";
463 std::cout <<
"op arity: " << n->arity.at(
'f') <<
"f, " << n->arity.at(
'b') <<
"b\n";
468 state.copy_to_host();
488 if (state.
f.
size()==0)
490 if (state.
c.
size() == 0)
492 if (state.
b.
size() == 0)
495 cols = state.
b.cols();
498 cols = state.
c.cols();
501 cols = state.
f.cols();
503 int rows_f = state.
f.rows();
504 int rows_c = state.
c.rows();
505 int rows_b = state.
b.rows();
508 Matrix<float,Dynamic,Dynamic,RowMajor>
Phi (rows_f+rows_b+rows_c, cols);
511 Phi << state.
f.cast<
float>(),
512 state.
c.cast<
float>(),
513 state.
b.cast<
float>();
518 for (
unsigned int i=0;
i<rows_f; ++
i)
524 for (
unsigned int i=0;
i<rows_c; ++
i)
531 for (
unsigned int i=0;
i<rows_b; ++
i)
563 if (
program.at(roots.at(root))->isNodeDx())
567 state_trace.push_back(
Trace());
575 if (
i > roots.at(root))
578 if (root + 1 < roots.size())
582 if (
program.at(roots.at(root))->isNodeDx())
586 state_trace.push_back(
Trace());
593 state_trace.at(trace_idx).copy_to_trace(state,
630 state.allocate(state_size,d.
X.cols());
637 if (
program.at(roots.at(root))->isNodeDx())
640 state_trace.push_back(
Trace());
646 if (
i > roots.at(root)){
648 if (
program.at(roots.at(root))->isNodeDx())
651 state_trace.push_back(
Trace());
660 state_trace.at(trace_idx).copy_to_trace(state,
664 state.update_idx(
program.at(
i)->otype,
678 state.copy_to_host();
684 if (state.
f.
size()==0)
686 if (state.
c.
size() == 0)
688 if (state.
b.
size() == 0)
691 cols = state.
b.cols();
694 cols = state.
c.cols();
697 cols = state.
f.cols();
699 int rows_f = state.
f.rows();
700 int rows_c = state.
c.rows();
701 int rows_b = state.
b.rows();
705 Matrix<float,Dynamic,Dynamic,RowMajor>
Phi (rows_f+rows_c+rows_b,
708 ArrayXXf PhiF = ArrayXXf::Map(state.
f.data(),state.
f.rows(),
710 ArrayXXi PhiC = ArrayXXi::Map(state.
c.data(),state.
c.rows(),
712 ArrayXXb PhiB = ArrayXXb::Map(state.
b.data(),state.
b.rows(),
716 Phi << PhiF.cast<
float>(),
723 for (
unsigned int i=0;
i<rows_f; ++
i)
729 for (
unsigned int i=0;
i<rows_c; ++
i)
736 for (
unsigned int i=0;
i<rows_b; ++
i)
762 cout << n->name <<
" failed arity check" << endl;
763 cout <<
"state fs:\n";
764 for (
auto s : state.
fs)
766 cout <<
"state bs:\n";
767 for (
auto s : state.
bs)
769 cout <<
"state cs:\n";
770 for (
auto s : state.
cs)
775 +
" ] is invalid\n");
781 vector<char> root_types;
784 root_types.push_back(
program.at(
r)->otype);
786 std::map<char,int> rows;
791 for (
int i = 0;
i < root_types.size(); ++
i)
793 char rt = root_types.at(
i);
797 eqn +=
"[" + state.
fs.
at(rows[rt]) +
"]";
800 eqn +=
"[" + state.
cs.
at(rows[rt]) +
"]";
803 eqn +=
"[" + state.
bs.
at(rows[rt]) +
"]";
819 vector<string> features;
831 vector<char> root_types;
834 root_types.push_back(
program.at(
r)->otype);
836 std::map<char,int> rows;
841 for (
int i = 0;
i < root_types.size(); ++
i)
843 char rt = root_types.at(
i);
847 features.push_back(state.
fs.
at(rows[rt]));
850 features.push_back(state.
cs.
at(rows[rt]));
853 features.push_back(state.
bs.
at(rows[rt]));
888 for (
unsigned int i =
program.size();
i>0; --
i)
890 ca +=
program.at(
i-1)->total_arity();
915 for (
int i=0;
i<
obj.size(); ++
i) {
922 if (flag1==1 && flag2==0)
926 else if (flag1==0 && flag2==1)
943 for (
const auto& n : objectives)
945 if (n.compare(
"fitness")==0)
947 else if (n.compare(
"complexity")==0)
949 else if (n.compare(
"size")==0)
952 else if (n.compare(
"CN")==0)
957 else if (n.compare(
"corr")==0)
959 else if (n.compare(
"fairness")==0)
970 std::map<char, vector<unsigned int>> state_c;
973 n->eval_complexity(state_c);
975 for (
const auto& s : state_c)
976 for (
const auto& t : s.second)
998 std::map<char, size_t> stack_size;
999 std::map<char, size_t> max_stack_size;
1000 stack_size[
'f'] = 0;
1001 stack_size[
'c'] = 0;
1002 stack_size[
'b'] = 0;
1003 max_stack_size[
'f'] = 0;
1004 max_stack_size[
'c'] = 0;
1005 max_stack_size[
'b'] = 0;
1009 ++stack_size.at(n->otype);
1011 if ( max_stack_size.at(n->otype) < stack_size.at(n->otype))
1012 max_stack_size.at(n->otype) = stack_size.at(n->otype);
1014 for (
const auto& a : n->arity)
1015 stack_size.at(a.first) -= a.second;
1017 return max_stack_size;
1027 this->
ml = std::make_shared<ML>(params.
ml, params.
normalize,
1030 shared_ptr<CLabels> yh = this->
ml->fit_tune(
Phi, d.
y,
1031 params, pass,
dtypes, set_default);
1035 logger.
log(
"Setting individual's weights...", 3);
1041 vector<float>
w(
Phi.rows(), 0);
1045 this->
yhat =
ml->labels_to_vector(yh);
1053 if (!filename.empty())
1063 std::ifstream indata;
1064 indata.open(filename);
1071 json j = json::parse(line);
data holding X, y, and Z data
type & top()
returns element at particular location in stack
unsigned int size()
returns top element of stack
individual programs in the population
void set_parents(const vector< Individual > &parents)
set parent ids using parents
vector< string > get_features()
return vectorized representation of program
vector< float > w
weights from ML training on program output
float fairness
aggregate fairness score
int size() const
return size of program
MatrixXf out(const Data &d, bool predict=false)
calculate program output matrix Phi
unsigned int dim
dimensionality of individual
vector< float > p
probability of variation of subprograms
VectorXf yhat
current output
string get_eqn()
return symbolic representation of program
vector< char > dtypes
the data types of each column of the
ArrayXXf predict_proba(const Data &d)
void initialize(const Parameters ¶ms, bool random, int id=0)
copy assignment
int check_dominance(const Individual &b) const
check whether this dominates b.
MatrixXf Phi
transformation output of program
int get_n_params()
get number of params in program
float fitness
aggregate fitness score
MatrixXf state_to_phi(State &state)
converts program states to output matrices
NodeVector program
executable data structure
vector< float > obj
objectives for use with Pareto selection
shared_ptr< ML > ml
ML model, trained on Phi.
void set_obj(const vector< string > &)
set obj vector given a string of objective names
void save(string filename)
save individual as a json object.
string program_str() const
return program name list
shared_ptr< CLabels > predict(const Data &d)
Array< bool, Dynamic, Dynamic, RowMajor > ArrayXXb
vector< float > get_p() const
get probabilities of variation
unsigned int dcounter
number of individuals this dominates
shared_ptr< CLabels > fit(const Data &d, const Parameters ¶ms, bool &pass)
fits an ML model to the data after transformation
vector< int > parent_id
ids of parents
float crowd_dist
crowding distance on the Pareto front
unsigned int rank
pareto front rank
float fairness_v
aggregate validation fairness score
MatrixXf out_trace(const Data &d, vector< Trace > &stack_trace)
calculate program output while maintaining stack trace
string eqn
equation form of the program
unsigned int get_complexity() const
get the program complexity without updating it.
std::map< char, size_t > get_max_state_size()
get maximum stack size needed for evaluation.
void set_p(const vector< float > &weights, const float &fb, const bool softmax_norm=false)
set probabilities
unsigned int set_complexity()
calculate program complexity and return it.
VectorXf predict_vector(const Data &d)
float fitness_v
aggregate validation fitness score
void load(string filename)
load individual from a file.
unsigned int get_dim()
grab sub-tree locations given starting point.
shared_ptr< CLabels > fit_tune(const Data &d, const Parameters ¶ms, bool set_default=false)
fits and tunes an ML model to the data after transformation
void set_rank(unsigned r)
setting and getting from individuals vector
unsigned int complexity
the complexity of the program.
string log(string m, int v, string sep="\n") const
print message with verbosity control.
int rnd_int(int lowerLimit, int upperLimit)
T random_choice(const vector< T > &v)
#define THROW_LENGTH_ERROR(err)
#define THROW_RUNTIME_ERROR(err)
#define THROW_INVALID_ARGUMENT(err)
void from_json(const json &j, NodeVector &nv)
void to_json(json &j, const NodeVector &nv)
float condition_number(const MatrixXf &X)
returns the condition number of a matrix.
vector< T > softmax(const vector< T > &w)
return the softmax transformation of a vector.
float mean_square_corrcoef(const MatrixXf &X)
std::string to_string(const T &value)
template function to convert objects to string for logging
void clean(ArrayXf &x)
limits node output to be between MIN_FLT and MAX_FLT
contains various types of State actually used by feat
Stack< ArrayXb > b
boolean node stack
Stack< string > fs
floating node string stack
bool check_s(std::map< char, unsigned int > &arity)
Stack< string > bs
boolean node string stack
bool check(std::map< char, unsigned int > &arity)
checks if arity of node provided satisfies the node names in various string State
Stack< string > cs
categorical node string stack
Stack< ArrayXi > c
categorical stack
Stack< ArrayXf > f
floating node stack
used for tracing stack outputs for backprop algorithm.
holds the hyperparameters for Feat.
bool softmax_norm
use softmax norm on probabilities
unsigned int max_dim
maximum dimensionality of programs
bool classification
flag to conduct classification rather than
float feedback
strength of ml feedback on probabilities
unsigned int n_classes
number of classes for classification
NodeVector functions
function nodes available in programs
unsigned int max_depth
max depth of programs
string ml
machine learner used with Feat
vector< float > term_weights
probability weighting of terminals
bool normalize
whether to normalize the input data
vector< std::string > longitudinalMap
NodeVector terminals
terminal nodes available in programs vector storing longitudinal data keys
vector< char > ttypes
program terminal types ('f', 'b')
vector< char > otypes
program output types ('f', 'b')
vector< float > op_weights
probability weighting of functions
vector< size_t > roots() const
returns indices of root nodes
void make_program(const NodeVector &functions, const NodeVector &terminals, int max_d, const vector< float > &term_weights, const vector< float > &op_weights, int dim, char otype, vector< string > longitudinalMap, const vector< char > &term_types)