23 for (
int i = 0; i < numPlanes; ++i)
24 storage.push_back(map<
size_t, vector<tree<Node>>>());
28 void append(
const int& storage_n,
const size_t& key,
const tree<Brush::Node> Tree) {
32 storage[storage_n][key] = vector<tree<Node>>();
37 auto& storage_it =
storage[storage_n][key];
40 size_t new_size = Tree.begin().node->get_size();
42 auto it = storage_it.begin();
43 for (; it != storage_it.end(); ++it) {
44 size_t curr_size = it->begin().node->get_size();
46 if (curr_size > new_size) {
49 }
else if (curr_size == new_size) {
51 auto it1 = it->begin();
52 auto it2 = Tree.begin();
54 auto end1 = it->end();
55 auto end2 = Tree.end();
57 bool trees_equal =
true;
58 for (; it1 != end1 && it2 != end2; ++it1, ++it2) {
59 if (it1.node->data.get_node_hash(
false)
60 != it2.node->data.get_node_hash(
false) ){
66 if (trees_equal && it1 == end1 && it2 == end2) {
78 storage_it.insert(it, Tree);
81 vector<tree<Node>>
getList(
const int& storage_n,
const size_t& key) {
82 auto it =
storage[storage_n].find(key);
83 if (it !=
storage[storage_n].end())
95 vector<size_t>
keys(
const int& storage_n) {
96 vector<size_t> result;
97 for (
const auto& pair :
storage[storage_n])
98 result.push_back(pair.first);
105 for (
size_t plane_idx = 0; plane_idx <
storage.size(); ++plane_idx) {
106 for (
const auto& kv :
storage[plane_idx]) {
107 size_t key = kv.first;
108 const auto& trees = kv.second;
110 for (
const auto& t : trees) {
111 vector<string> row(prefix);
112 row.push_back(std::to_string(plane_idx));
113 row.push_back(std::to_string(key));
114 row.push_back(t.begin().node->get_model());
123 vector<map<size_t, vector<tree<Node>>>>
storage;
130 void init(
int hashSize,
const Dataset &data,
int numPlanes);
135 template<ProgramType P>
141 while(spot != program.
Tree.end_post())
146 if (spot.node->data.get_prob_change() > 0
152 if (program.
size_at(spot,
true) <= 30
164 template<ProgramType P>
179 auto original_predictions = simplified_program.
predict(d);
184 while(spot != simplified_program.
Tree.end_post())
190 if (spot.node->data.get_prob_change() > 0 && !spot.node->data.weight_is_fixed
205 float threshold = 1e-5;
207 float best_distance = threshold;
208 tree<Node> best_branch;
209 for (
const auto& cand : res.value()) {
211 const tree<Node> original_branch(spot);
212 const tree<Node> simplified_branch(cand);
218 simplified_program.
Tree.erase_children(spot);
220 spot = simplified_program.
Tree.move_ontop(spot, simplified_branch.begin());
222 auto new_predictions = simplified_program.
predict(d);
224 float diff = (original_predictions.template cast<float>() - new_predictions.template cast<float>()).square().mean();
226 if (diff < best_distance) {
227 best_distance = diff;
232 simplified_program.
Tree.erase_children(spot);
233 spot = simplified_program.
Tree.move_ontop(spot, original_branch.begin());
235 if (best_distance < threshold) {
239 original = spot.node->get_model();
241 simplified_program.
Tree.erase_children(spot);
243 const tree<Node> best_branch_copy(best_branch);
245 spot = simplified_program.
Tree.move_ontop(spot, best_branch_copy.begin());
248 records->push_back({0, 0,
"inexact",
250 original, spot.node->get_model(),
261 program.
Tree = simplified_program.
Tree;
263 return simplified_program;
269 template<ProgramType P>
272 const tree<Node> tree_copy(spot);
278 auto hashes =
hash<P>(spot, d);
280 for (
size_t i = 0; i < hashes.size(); ++i)
290 "run_id",
"DataType",
"Plane",
"Key",
"Tree"};
305 template<ProgramType P>
317 ArrayXf floatClippedInput;
320 ArrayXXf inputPoint = (*spot.node).
template predict<ArrayXXf>(d);
321 floatClippedInput = Eigen::Map<ArrayXf>(inputPoint.data(), inputPoint.size()).head(
inputDim).template cast<float>();
324 ArrayXb inputPointB = (*spot.node).
template predict<ArrayXb>(d);
325 floatClippedInput = inputPointB.template cast<float>();
328 ArrayXi inputPointI = (*spot.node).
template predict<ArrayXi>(d);
329 floatClippedInput = inputPointI.template cast<float>();
332 floatClippedInput = (*spot.node).
template predict<ArrayXf>(d);
341 assert(floatClippedInput.size() ==
inputDim &&
342 "You need to pass a dataset with inputDim samples to the simplification.");
345 float floatClippedInput_mean = floatClippedInput.mean();
350 if (std::isnan(floatClippedInput_mean) || std::isinf(floatClippedInput_mean)) {
354 floatClippedInput = floatClippedInput - floatClippedInput_mean;
359 vector<size_t> hashes;
360 for (
size_t planeIdx = 0; planeIdx <
uniformPlanes.size(); ++planeIdx)
363 Eigen::ArrayXf projection = plane * floatClippedInput.matrix();
364 Eigen::Array<bool, Eigen::Dynamic, 1> comparison = (projection > 0);
366 size_t input_hash = 0;
367 for (
int i = 0; i < comparison.size(); ++i) {
369 input_hash |= comparison(i) ? 1 : 0;
372 hashes.push_back(input_hash);
378 template<ProgramType P>
383 int spot_size = spot.node->get_size();
386 vector<tree<Node>> matches = {};
388 vector<size_t> hashes =
hash<P>(spot, d);
390 for (
int i = 0; i < hashes.size(); ++i){
394 vector<tree<Node>> newCandidates =
equivalentExpressions[spot.node->data.ret_type].getList(i, hashes[i]);
396 if (newCandidates.size() == 0)
400 for (
const auto& cand : newCandidates) {
401 if (cand.begin().node->get_size() < spot_size) {
402 matches.push_back(cand);
403 if (++count >= 25)
break;
411 if (matches.size() > 0)
holds variable type data.
vector< map< size_t, vector< tree< Node > > > > storage
vector< tree< Node > > getList(const int &storage_n, const size_t &key)
void append(const int &storage_n, const size_t &key, const tree< Brush::Node > Tree)
vector< size_t > keys(const int &storage_n)
HashStorage(int numPlanes=10)
void print(const vector< string > &prefix, Util::CsvWriter &log) const
writes one row per stored tree: prefix..., plane, key, tree
void log_simplification_table(Util::CsvWriter &log, const string &run_id)
dumps every stored expression. Expects a writer opened with simplification_table_header.
string dt_to_string(DataType dt)
optional< vector< tree< Node > > > query(TreeIter &spot, const Dataset &d)
vector< size_t > hash(TreeIter &spot, const Dataset &d)
vector< MatrixXf > uniformPlanes
void index(TreeIter &spot, const Dataset &d)
Program< P > simplify_tree(Program< P > &program, const SearchSpace &ss, const Dataset &d, SimplificationRecords *records=nullptr)
void analyze_tree(Program< P > &program, const SearchSpace &ss, const Dataset &d)
static const vector< string > simplification_table_header
void init(int hashSize, const Dataset &data, int numPlanes)
std::unordered_map< DataType, HashStorage > equivalentExpressions
Minimal RFC 4180 CSV writer used by the run logs.
void write_row(const std::vector< std::string > &fields)
std::vector< SimplificationRecord > SimplificationRecords
< nsga2 selection operator for getting the front
auto Isnt(DataType dt) -> bool
Eigen::Array< bool, Eigen::Dynamic, 1 > ArrayXb
tree< Node >::pre_order_iterator TreeIter
Eigen::Array< int, Eigen::Dynamic, 1 > ArrayXi
class holding the data for a node in a tree.
An individual program, a.k.a. model.
TreeType predict(const Dataset &d)
the standard predict function. Returns the output of the Tree directly.
int size_at(Iter &top, bool include_weight=true) const
count the size of a given subtree, optionally including the weights in weighted nodes....
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...