Version: 1.0
find_optimal_drag.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_FIND_OPTIMAL_DRAG_H_
8 #define GRAPHGEN_FIND_OPTIMAL_DRAG_H_
9 
10 #include <bitset>
11 #include <iostream>
12 #include <iomanip>
13 #include <unordered_set>
14 #include <mutex>
15 #include <thread>
16 
17 #include "pool.h"
18 
19 #include "conact_tree.h"
20 #include "remove_equal_subtrees.h"
21 
23  std::vector<BinaryDrag<conact>::node*> lma_; // leaves with multiple actions
24  std::unordered_set<BinaryDrag<conact>::node*> visited_; // utility set to remember already visited nodes
26 
29  uint best_nodes_ = std::numeric_limits<uint>::max();
30  uint best_leaves_ = std::numeric_limits<uint>::max();
31 
32  std::mutex best_tree_mutex_;
33  thread_pool *pool_ = nullptr;
34 
35  FindOptimalDrag(BinaryDrag<conact> t) : t_{ std::move(t) } {
37  }
38 
39  // This method fill the lma_ variable with all the leaves that have multiple actions. This
40  // vector (lma_) will be used by the backtrack algorithm to generate all the possible trees
41  // (i.e all the possible trees with one action per leaf).
43  if (visited_.count(n) > 0) {
44  return;
45  }
46  visited_.insert(n);
47  if (n->isleaf()) {
48  if (n->data.action.count() > 1) {
49  lma_.push_back(n);
50  }
51  return;
52  }
55  }
56 
58  {
59  //RemoveEqualSubtrees sc(t.GetRoot()); TODO originale
60  RemoveEqualSubtrees sc(t);
61 
62  std::lock_guard<std::mutex> lock(best_tree_mutex_);
63  if (best_nodes_ > sc.nodes_ || (best_nodes_ == sc.nodes_ && best_leaves_ > sc.leaves_)) {
64  best_nodes_ = sc.nodes_;
65  best_leaves_ = sc.leaves_;
66  best_tree_ = std::move(t);
67  std::cout << "\rbest_nodes_ = " << best_nodes_ << " - best_leaves_ = " << best_leaves_ << "\n";
68  }
69 
70  if (counter_ == 0) {
71  std::cout << " 0%";
72  }
73 
74  if (++counter_ % 79626 == 0) {
75  std::cout << "\r" << std::setfill(' ') << std::setw(3) << counter_ / 79626 << "%";
76  }
77  }
78 
79  void GenerateAllTreesRec(int cur_leaf)
80  {
81  if (cur_leaf == lma_.size()) {
82  // We have a tree without multiple actions
83 
85 
86  return;
87  }
88 
89  auto action_bs = lma_[cur_leaf]->data.action;
90  auto actions = lma_[cur_leaf]->data.actions(); // vector of actions ("uint")
91 
92  for (size_t i = 0; i < actions.size(); ++i) {
93  std::bitset<131/*CTBE needs 131 bits*/> bs;
94  bs.set(actions[i] - 1);
95  lma_[cur_leaf]->data.action = bs;
96  GenerateAllTreesRec(cur_leaf + 1);
97  }
98  lma_[cur_leaf]->data.action = action_bs;
99  }
100 
102  {
103  pool_ = new thread_pool(8, 8);
105  delete pool_;
106  }
107 };
108 
109 #endif // GRAPHGEN_FIND_OPTIMAL_DRAG_H_
node * GetRoot() const
Returns the last root of the BinaryDrag.
Definition: drag.h:66
void enqueue_work(F &&f, Args &&... args)
Definition: pool.h:48
uint32_t uint
BinaryDrag< conact > best_tree_
void GetLeavesWithMultipleActionsRec(BinaryDrag< conact >::node *n)
thread_pool * pool_
std::mutex best_tree_mutex_
void GenerateAllTreesRec(int cur_leaf)
std::vector< BinaryDrag< conact >::node * > lma_
BinaryDrag< conact > t_
void ReduceAndUpdateBest(BinaryDrag< conact > t)
FindOptimalDrag(BinaryDrag< conact > t)
std::unordered_set< BinaryDrag< conact >::node * > visited_
This class allows to "remove" equal subtrees from a BinaryDrag.