-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.cpp
More file actions
128 lines (103 loc) · 3.66 KB
/
task3.cpp
File metadata and controls
128 lines (103 loc) · 3.66 KB
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
#include "main.h"
BDD runTask3(Cudd& mgr, vector<Place>& places, vector<Transition>& transitions) {
cout << "\n==========================================" << endl;
cout << " TASK 3: SYMBOLIC REACHABILITY (BDD) " << endl;
cout << "==========================================" << endl;
auto start_time = chrono::high_resolution_clock::now();
int numPlaces = places.size();
map<Place*, int> placeToIndex;
for (int i = 0; i < numPlaces; ++i) {
placeToIndex[&places[i]] = i;
}
vector<BDD> x(numPlaces);
vector<BDD> y(numPlaces);
for (int i = 0; i < numPlaces; ++i) {
x[i] = mgr.bddVar(2 * i);
y[i] = mgr.bddVar(2 * i + 1);
}
BDD M0 = mgr.bddOne();
for (int i = 0; i < numPlaces; ++i) {
if (places[i].token > 0) {
M0 *= x[i];
} else {
M0 *= !x[i];
}
}
BDD T = mgr.bddZero();
for (const auto& t : transitions) {
BDD t_rel = mgr.bddOne();
vector<bool> isAffected(numPlaces, false);
for (Place* p : t.inputs) {
int idx = placeToIndex[p];
t_rel *= x[idx];
}
for (Place* p : t.inputs) {
int idx = placeToIndex[p];
bool isSelfLoop = false;
for (Place* out : t.outputs) {
if (out == p) { isSelfLoop = true; break; }
}
if (!isSelfLoop) {
t_rel *= !y[idx];
isAffected[idx] = true;
}
}
for (Place* p : t.outputs) {
int idx = placeToIndex[p];
t_rel *= y[idx];
isAffected[idx] = true;
}
for (int i = 0; i < numPlaces; ++i) {
if (!isAffected[i]) {
t_rel *= (x[i].Xnor(y[i]));
}
}
T += t_rel;
}
BDD R_current = M0;
BDD R_prev = mgr.bddZero();
BDD x_cube = mgr.bddOne();
for (int i = 0; i < numPlaces; ++i) x_cube *= x[i];
int steps = 0;
while (R_current != R_prev) {
R_prev = R_current;
steps++;
BDD next_y = R_current.AndAbstract(T, x_cube);
BDD next_x = next_y.SwapVariables(y, x);
R_current += next_x;
}
auto end_time = chrono::high_resolution_clock::now();
chrono::duration<double> diff = end_time - start_time;
cout << "Iterations: " << steps << endl;
cout << "Total Reachable Markings: " << (long long)R_current.CountMinterm(numPlaces) << endl;
cout << "BDD Nodes (Reachable Set): " << R_current.nodeCount() << endl;
cout << "Runtime: " << diff.count() << " s" << endl;
// Print
cout << "\n[Reachable States - Sorted by Sequence Logic]" << endl;
cout << "Place(s) ID / Marking:" << endl;
for (int i = 0; i < numPlaces; ++i) cout << places[i].id << " ";
cout << "\n------------------------" << endl;
vector<string> discovered_states;
DdGen *gen;
int *cube;
CUDD_VALUE_TYPE val;
Cudd_ForeachCube(mgr.getManager(), R_current.getNode(), gen, cube, val) {
string state_str = "";
for (int i = 0; i < numPlaces; i++) {
int index = 2 * i;
if (cube[index] == 0) state_str += "0";
else if (cube[index] == 1) state_str += "1";
else state_str += "- ";
for (size_t j = 0; j < places[i].id.length(); ++j) {
state_str += " ";
}
}
discovered_states.push_back(state_str);
}
sort(discovered_states.begin(), discovered_states.end(), greater<string>());
for (const auto& s : discovered_states) {
cout << s << endl;
}
cout << "==========================================" << endl;
return R_current;
}