Version: 1.0
drag2optimal.cpp
Go to the documentation of this file.
1 // Copyright(c) 2019
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // *Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and / or other materials provided with the distribution.
13 //
14 // * Neither the name of GRAPHGEN nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "drag2optimal.h"
30 
31 #include <iostream>
32 #include <map>
33 #include <vector>
34 
35 #include "drag_statistics.h"
36 
38  if (n2->isleaf() || n1 == n2 || visited_fl[n2])
39  return;
40  visited_fl[n2] = true;
41 
42  if (n1 != n2->left && EqualTrees(n1, n2->left)) {
43  n2->left = n1;
44  }
45 
46  if (n1 != n2->right && EqualTrees(n1, n2->right)) {
47  n2->right = n1;
48  }
49 
50  FindAndLinkIdentiesDagRec(n1, n2->left, visited_fl);
51  FindAndLinkIdentiesDagRec(n1, n2->right, visited_fl);
52 }
53 
54 // Recursive auxiliary function for the conversion of a DAG into DAG with no equivalent subgraphs
56  std::map<BinaryDrag<conact>::node*, bool> visited_fl;
57  FindAndLinkIdentiesDagRec(n, t.GetRoot(), visited_fl);
58  visited_n[n] = true;
59 
60  if (!n->isleaf()) {
61  if (!visited_n[n->left])
62  Dag2DagUsingIdentiesRec(n->left, t, visited_n);
63  if (!visited_n[n->right])
64  Dag2DagUsingIdentiesRec(n->right, t, visited_n);
65  }
66 }
67 
68 // Converts dag to dag using identies between subtrees
70  std::map<BinaryDrag<conact>::node*, bool> visited_n;
71  Dag2DagUsingIdentiesRec(t.GetRoot(), t, visited_n);
72 }
73 
75  if (n2->isleaf() || n1 == n2 || visited_fl[n2])
76  return;
77  visited_fl[n2] = true;
78 
79  if (n1 != n2->left && equivalent_trees(n1, n2->left)) {
80  n2->left = n1;
81  }
82 
83  if (n1 != n2->right && equivalent_trees(n1, n2->right)) {
84  n2->right = n1;
85  }
86 
87  FindAndLinkEquivalencesDagRec(n1, n2->left, visited_fl);
88  FindAndLinkEquivalencesDagRec(n1, n2->right, visited_fl);
89 }
90 
91 // Recursive auxiliary function for the conversion of a DAG into DAG with no equivalent subgraphs
92 void Dag2DagUsingEquivalencesRec(BinaryDrag<conact>::node *n, BinaryDrag<conact>& t, std::map<BinaryDrag<conact>::node*, bool> &visited_n, bool considering_leaves) {
93  std::map<BinaryDrag<conact>::node*, bool> visited_fl;
94 
95  if (!n->isleaf() || considering_leaves) {
96  FindAndLinkEquivalencesDagRec(n, t.GetRoot(), visited_fl);
97  }
98  visited_n[n] = true;
99 
100  if (!n->isleaf()) {
101  if (!visited_n[n->left])
102  Dag2DagUsingEquivalencesRec(n->left, t, visited_n, considering_leaves);
103  if (!visited_n[n->right])
104  Dag2DagUsingEquivalencesRec(n->right, t, visited_n, considering_leaves);
105  }
106 }
107 
108 // Converts dag to dag using equivalences between subtrees
109 void Dag2DagUsingEquivalences(BinaryDrag<conact>& t, bool considering_leaves) {
110  std::map<BinaryDrag<conact>::node*, bool> visited_n;
111  Dag2DagUsingEquivalencesRec(t.GetRoot(), t, visited_n, considering_leaves);
112 }
113 
114 // Given a dag with multiple actions on leaves this function generate all possible dags with only one action per leaf
115 void Dag2OptimalDagRec(BinaryDrag<conact>& t, BinaryDrag<conact>::node* n, BinaryDrag<conact> &best_tree, size_t &best_nodes, size_t &best_leaves, std::map<const BinaryDrag<conact>::node*, bool> &visited_n, uint &counter) {
117  if (n->isleaf()) {
118  // leaf with multiple action
119  std::vector<uint> actions_list = n->data.actions();
120  if (actions_list.size() > 1) {
121  for (size_t i = 0; i < actions_list.size() - 1; ++i) {
122  std::map<const BinaryDrag<conact>::node*, bool> visited_node_cur;
123  n->data.action = 0;
124  n->data.action.set(actions_list[i] - 1);
125  nt = t;
126  Dag2OptimalDagRec(nt, nt.GetRoot(), best_tree, best_nodes, best_leaves, visited_node_cur, counter);
127  }
128  n->data.action = 0;
129  n->data.action.set(actions_list[actions_list.size() - 1] - 1);
130  }
131  return;
132  }
133 
134  if (!visited_n[n->left]) {
135  // left node not already visited
136  visited_n[n->left] = true;
137  Dag2OptimalDagRec(t, n->left, best_tree, best_nodes, best_leaves, visited_n, counter);
138  }
139 
140  if (!visited_n[n->right]) {
141  // right node not already visited
142  visited_n[n->right] = true;
143  Dag2OptimalDagRec(t, n->right, best_tree, best_nodes, best_leaves, visited_n, counter);
144  }
145 
146  if (t.GetRoot() == n) {
147  counter++;
148  BinaryDrag<conact> dag = t;
150 
151  BinaryDragStatistics ds(dag);
152 
153  if (best_nodes > ds.Nodes()) {
154  best_nodes = ds.Nodes();
155  best_leaves = ds.Leaves();
156  best_tree = dag;
157  }
158  else if (best_nodes == ds.Nodes() && best_leaves > ds.Leaves()) {
159  best_leaves = ds.Leaves();
160  best_tree = dag;
161  }
162 
163  if (counter % 1000 == 0) {
164  std::cout << counter / 1000 << "\r";
165  }
166  }
167 }
168 
169 
170 // Converts a tree into dag minimizing the number of nodes (Note: this is "necessary" when the leaves of a tree contain multiple actions)
171 // USES NUMBER OF NODES TO PICK THE OPTIMAL DAG
173  std::vector<BinaryDrag<conact>> trees;
174  BinaryDrag<conact> best_tree;
175  std::map<const BinaryDrag<conact>::node*, bool> visited_nodes;
176  uint counter = 0;
177  size_t best_nodes = std::numeric_limits<size_t>::max();
178  size_t best_leaves = std::numeric_limits<size_t>::max();
179  Dag2OptimalDagRec(t, t.GetRoot(), best_tree, best_nodes, best_leaves, visited_nodes, counter);
180  std::cout << "** Vector size:" << counter << " **\n";
181  std::cout << "** Counter:" << counter << " **\n";
182 
183  t = best_tree;
184 
185  /*for (size_t i = 0; i < trees.size(); ++i) {
186  DrawDagOnFile("tree_" + to_string(i), trees[i]);
187  Dag2DagUsingIdenties(trees[i]);
188  DrawDagOnFile("dag_" + to_string(i), trees[i]);
189  }
190 
191  BestDagFromList(trees, t);*/
192 }
A BinaryDrag is the GRAPHGEN implementation of a Binary Directed Rooted Acyclic Graph (DRAG in short)
Definition: drag.h:28
node * GetRoot() const
Returns the last root of the BinaryDrag.
Definition: drag.h:66
Calculates the statistics of a binary drag with one or multiple roots.
auto Nodes() const
Returns the number of unique nodes inside the DRAG.
auto Leaves() const
Returns the number of unique leaves inside the DRAG.
bool EqualTrees(const BinaryDrag< conact >::node *n1, const BinaryDrag< conact >::node *n2)
Checks whether two (sub)trees 'n1' and 'n2' are equal or nor.
bool equivalent_trees(const BinaryDrag< conact >::node *n1, const BinaryDrag< conact >::node *n2)
Definition: conact_tree.cpp:86
uint32_t uint
void FindAndLinkEquivalencesDagRec(BinaryDrag< conact >::node *n1, BinaryDrag< conact >::node *n2, std::map< BinaryDrag< conact >::node *, bool > &visited_fl)
void FindAndLinkIdentiesDagRec(BinaryDrag< conact >::node *n1, BinaryDrag< conact >::node *n2, std::map< BinaryDrag< conact >::node *, bool > &visited_fl)
void Dag2OptimalDagRec(BinaryDrag< conact > &t, BinaryDrag< conact >::node *n, BinaryDrag< conact > &best_tree, size_t &best_nodes, size_t &best_leaves, std::map< const BinaryDrag< conact >::node *, bool > &visited_n, uint &counter)
void Dag2DagUsingIdenties(BinaryDrag< conact > &t)
void Dag2DagUsingEquivalencesRec(BinaryDrag< conact >::node *n, BinaryDrag< conact > &t, std::map< BinaryDrag< conact >::node *, bool > &visited_n, bool considering_leaves)
void Dag2DagUsingEquivalences(BinaryDrag< conact > &t, bool considering_leaves)
void Dag2OptimalDag(BinaryDrag< conact > &t)
void Dag2DagUsingIdentiesRec(BinaryDrag< conact >::node *n, BinaryDrag< conact > &t, std::map< BinaryDrag< conact >::node *, bool > &visited_n)