24 for (
const auto& p : other)
25 this->push_back(p->clone());
33 for (
const auto& p : other)
34 this->push_back(p->clone());
48 for (
unsigned i = start;
i<=end; ++
i)
49 v.push_back(this->at(
i).get());
61 vector<size_t> indices;
63 for (
size_t i = this->size();
i>0; --
i)
65 if (total_arity <= 0 ){
66 indices.push_back(
i-1);
72 total_arity += this->at(
i-1)->total_arity();
76 std::reverse(indices.begin(), indices.end());
98 if (i<0 || i > this->size())
107 if (this->at(
i)->total_arity()==0)
112 std::map<char, unsigned int>
arity = this->at(
i)->arity;
127 for (
unsigned int j = 0; j<
arity.at(
'f'); ++j)
128 i = subtree(--
i,
'f', indent+indent);
132 for (
unsigned int j = 0; j<
arity.at(
'b'); ++j)
133 i2 = subtree(--i2,
'b', indent+indent);
137 for (
unsigned int j = 0; j<
arity.at(
'c'); ++j)
138 i3 = subtree(--i3,
'c', indent+indent);
142 for (
unsigned int j = 0; j<
arity.at(
'z'); ++j)
143 i4 = subtree(--i4,
'z', indent+indent);
147 return std::min(
i,i4);
152 if (weights.size()==0)
return;
164 for (
unsigned i = 0;
i< this->size(); ++
i)
166 if (this->at(
i)->isNodeDx())
170 if (weights.at(count).size() == nd->
W.size())
171 nd->
W = weights.at(count);
174 string error =
"mismatch in size btw weights[" +
179 "].size() (" +
to_string(weights.at(count).size()) +
180 ") != W.size() ("+
to_string(nd->
W.size()) +
"\n";
191 vector<vector<float>> weights;
192 for (
unsigned i = 0;
i< this->size(); ++
i)
194 if (this->at(
i)->isNodeDx())
196 weights.push_back(
dynamic_cast<NodeDx*
>(this->at(
i).get())->
W);
203 vector<string> longitudinalMap)
208 std::map<string, std::pair<vector<ArrayXf>, vector<ArrayXf>>> Z;
210 MatrixXf X = MatrixXf::Zero(num_features,2);
211 VectorXf y = VectorXf::Zero(2);
213 for(
auto key : longitudinalMap)
215 Z[key].first.push_back(ArrayXf::Zero(2));
216 Z[key].first.push_back(ArrayXf::Zero(2));
217 Z[key].second.push_back(ArrayXf::Zero(2));
218 Z[key].second.push_back(ArrayXf::Zero(2));
221 Data data(X, y, Z,
false);
224 for (
const auto& n : *
this){
226 if (n->isNodeTrain())
228 if (state.
check(n->arity))
229 n->evaluate(data, state);
232 std::cout <<
"Error: ";
233 for (
const auto& p: *
this) std::cout << p->name <<
" ";
234 std::cout <<
"is not a valid program because ";
235 std::cout << n->name <<
" at pos " <<
i <<
"is not satisfied\n";
245 const vector<float>& term_weights,
246 const vector<float>& op_weights,
247 char otype,
const vector<char>& term_types)
264 if (max_d == 0 ||
r.
rnd_flt() < terminals.size()/(terminals.size()+functions.size()))
273 for (
size_t i = 0;
i<terminals.size(); ++
i)
275 if (terminals.at(
i)->otype ==
otype)
278 tw.push_back(term_weights.at(
i));
288 if(ti.size() > 0 && tw.size() > 0)
292 push_back(t->rnd_clone());
297 for (
const auto& t : terminals)
298 ttypes += t->name +
": " + t->otype +
"\n";
299 std::ostringstream msg;
300 msg <<
"Error: make_tree couldn't find a terminal of "
301 <<
"type " <<
otype <<
". terminal types:\n" << ttypes ;
311 bool fterms =
in(term_types,
'f');
312 bool bterms =
in(term_types,
'b');
313 bool cterms =
in(term_types,
'c');
314 bool zterms =
in(term_types,
'z');
318 for (
size_t i = 0;
i<functions.size(); ++
i)
319 if (functions.at(
i)->otype==
otype &&
320 (max_d>1 || functions.at(
i)->arity.at(
'f')==0 || fterms) &&
321 (max_d>1 || functions.at(
i)->arity.at(
'b')==0 || bterms) &&
322 (max_d>1 || functions.at(
i)->arity.at(
'c')==0 || cterms) &&
323 (max_d>1 || functions.at(
i)->arity.at(
'z')==0 || zterms))
326 fw.push_back(op_weights.at(
i));
332 make_tree(functions, terminals, 0, term_weights,
333 op_weights,
otype, term_types);
339 "results in incomplete programs.");
350 map<char, unsigned> chosen_arity = back()->arity;
352 vector<char> type_order = {
'f',
'b',
'c',
'z'};
353 for (
auto type : type_order)
355 for (
size_t i = 0;
i < chosen_arity.at(type); ++
i)
357 make_tree(functions, terminals, max_d-1, term_weights,
358 op_weights, type, term_types);
370 const vector<float>& term_weights,
371 const vector<float>& op_weights,
373 vector<string> longitudinalMap,
374 const vector<char>& term_types)
376 for (
int i = 0;
i<dim; ++
i)
378 make_tree(functions, terminals, max_d, term_weights, op_weights,
otype,
383 std::reverse(begin(), end());
384 assert(is_valid_program(terminals.size(), longitudinalMap));
385 if (!is_valid_program(terminals.size(), longitudinalMap))
395 for (
const auto& n : nv)
417 else if (n->isNodeTrain())
420 else if (n->isNodeDx())
444 for (
const auto& k : j)
446 string node_name = k.at(
"name").get<
string>();
450 node_name = k.at(
"name").get<
string>() +
"_"
476 else if (n->isNodeTrain())
479 else if (n->isNodeDx())
499 nv.push_back(n->clone());
data holding X, y, and Z data
T random_choice(const vector< T > &v)
float rnd_flt(float min=0.0, float max=1.0)
#define THROW_LENGTH_ERROR(err)
#define THROW_RUNTIME_ERROR(err)
#define THROW_INVALID_ARGUMENT(err)
namespace representing various operations on population individuals used in Feat
void from_json(const json &j, NodeVector &nv)
void to_json(json &j, const NodeVector &nv)
bool in(const vector< T > v, const T &i)
check if element is in vector.
std::string to_string(const T &value)
template function to convert objects to string for logging
contains various types of State actually used by feat
bool check(std::map< char, unsigned int > &arity)
checks if arity of node provided satisfies the node names in various string State
an extension of a vector of unique pointers to nodes
NodeVector & operator=(NodeVector const &other)
vector< size_t > roots() const
returns indices of root nodes
vector< Node * > get_data(int start=0, int end=0)
returns vector of raw pointers to nodes in [start,end], or all if both are zero
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)
bool is_valid_program(unsigned num_features, vector< string > longitudinalMap)
void set_weights(vector< vector< float >> &weights)
void make_tree(const NodeVector &functions, const NodeVector &terminals, int max_d, const vector< float > &term_weights, const vector< float > &op_weights, char otype, const vector< char > &term_types)
vector< vector< float > > get_weights()
size_t subtree(size_t i, char otype='0', string indent="> ") const
std::map< std::string, Node * > node_map