-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROBDD.cpp
228 lines (214 loc) · 6.04 KB
/
ROBDD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "ROBDD.hpp"
#include <fstream>
bool BDD_node::operator==(const BDD_node &rhs) const
{
return this->var == rhs.var && this->low == rhs.low && this->high == rhs.high;
}
ROBDD::ROBDD(bool order_by_ascii)
{
this->zero = new BDD_node({0, nullptr, nullptr});
this->one = new BDD_node({1, nullptr, nullptr});
this->node_table[*(this->zero)] = this->zero;
this->node_table[*(this->one)] = this->one;
this->order_by_ascii = order_by_ascii;
}
BDD_node *ROBDD::get_one() const
{
return this->one;
}
BDD_node *ROBDD::get_zero() const
{
return this->zero;
}
bool ROBDD::empty()
{
return this->root == nullptr;
}
unsigned int ROBDD::get_ID(std::string var)
{
// new var
if (this->var_to_ID.find(var) == this->var_to_ID.end())
{
this->var_to_ID[var] = avail_ID;
this->ID_to_var[avail_ID] = var;
avail_ID++;
}
return this->var_to_ID[var];
}
BDD_node *ROBDD::make_node(unsigned int ID, BDD_node *low, BDD_node *high)
{
if (low == high)
return low;
auto found = this->node_table.find(BDD_node{ID, low, high});
if (found != this->node_table.end())
return found->second;
else
{
BDD_node *new_node = new BDD_node({ID, low, high});
this->node_table[*new_node] = new_node;
return new_node;
}
}void ROBDD::set_root(BDD_node *node)
{
this->root = node;
}
BDD_node *ROBDD::calc(binary_op op, BDD_node *left, BDD_node *right)
{
switch (op)
{
case OP_AND:
if (left->var == 1 and right->var == 1)
return this->one;
else
return this->zero;
break;
case OP_OR:
if (left->var == 1 or right->var == 1)
return this->one;
else
return this->zero;
break;
case OP_THEN:
if (left->var == 0 or right->var == 1)
return this->one;
else
return this->zero;
break;
case OP_XNOR:
if (left->var == right->var)
return this->one;
else
return this->zero;
break;
case OP_XOR:
if (left->var != right->var)
return this->one;
else
return this->zero;
break;
}
return nullptr;
}
BDD_node *ROBDD::apply(binary_op op, BDD_node *left, BDD_node *right)
{
return this->_apply(op, left, right);
this->apply_table[op].clear();
}
BDD_node *ROBDD::_apply(binary_op op, BDD_node *left, BDD_node *right)
{
auto less = [&](BDD_node *lhs, BDD_node *rhs)
{
return this->order_by_ascii ? this->ID_to_var[lhs->var] < this->ID_to_var[rhs->var] : lhs->var < rhs->var;
};
auto found = this->apply_table[op].find(std::pair<BDD_node *, BDD_node *>(left, right));
BDD_node *res;
if (found != this->apply_table[op].end())
res = found->second;
else if (left->var <= 1 and right->var <= 1)
res = this->calc(op, left, right);
else if (left->var == right->var) // promote the shared var
{
auto l = this->_apply(op, left->low, right->low);
auto r = this->_apply(op, left->high, right->high);
res = this->make_node(left->var, l, r);
}
else if ((less(left, right) and left->var > 1) or right->var <= 1) // unfold left BDD tree
{
auto l = this->_apply(op, left->low, right);
auto r = this->_apply(op, left->high, right);
res = this->make_node(left->var, l, r);
}
else // unfold right BDD tree
{
auto l = this->_apply(op, left, right->low);
auto r = this->_apply(op, left, right->high);
res = this->make_node(right->var, l, r);
}
this->apply_table[op][std::pair<BDD_node *, BDD_node *>(left, right)] = res;
return res;
}
void ROBDD::set_root(BDD_node *node)
{
this->root = node;
}
bool ROBDD::_SAT(BDD_node *node, bool all_sat = false)
{
if (node->var == 1)
return true;
else if (node->var == 0)
return false;
if (all_sat)
return this->_SAT(node->high, true) & this->_SAT(node->low, true);
else
return this->_SAT(node->high) | this->_SAT(node->low);
}
bool ROBDD::SAT(bool all_sat = false)
{
return this->_SAT(this->root, all_sat);
}
unsigned int ROBDD::_SATCOUNT(BDD_node *node)
{
if (node->var == 1)
return 1;
else if (node->var == 0)
return 0;
return this->_SATCOUNT(node->high) + this->_SATCOUNT(node->low);
}
unsigned int ROBDD::SATCOUNT()
{
return this->_SATCOUNT(this->root);
}
void ROBDD::output(std::ofstream &out)
{
this->printed.clear();
this->printed[this->zero] = 0;
this->printed[this->one] = 1;
this->uuid = 2;
out << "digraph ROBDD {\n"
<< "fontname=\"Helvetica,Arial,sans-serif\"\n"
<< "node [fontname=\"Helvetica,Arial,sans-serif\"]\n"
<< "edge [fontname=\"Helvetica,Arial,sans-serif\"]\n"
<< "node [shape=circle];\n";
this->_output(this->root, out);
out << "}";
this->printed.clear();
}
void ROBDD::_output(BDD_node *node, std::ofstream &out)
{
// print node itself
if (this->printed.find(node) == this->printed.end())
{
this->printed[node] = uuid++;
out << "\"";
out << this->printed[node]; // use uuid to identify nodes
out << "\" [label=\"";
out << this->ID_to_var[node->var];
out << "\"]" << std::endl; // use label as node name
}
else if (this->printed[node] < 2) // 0 or 1
{
out << "\"";
out << this->printed[node]; // use uuid to identify nodes
out << "\"" << std::endl;
}
// no subnodes
if (node->var <= 1)
return;
// print links to subnodes
// left
if (this->printed.find(node->low) == this->printed.end())
_output(node->low, out);
out << "\"";
out << this->printed[node];
out << "\" -> \"";
out << this->printed[node->low];
out << "\" [style = dotted]" << std::endl;
// right
if (this->printed.find(node->high) == this->printed.end())
_output(node->high, out);
out << "\"";
out << this->printed[node];
out << "\" -> \"";
out << this->printed[node->high];
out << "\"" << std::endl;
}