-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
281 lines (257 loc) · 9.63 KB
/
main.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <unordered_set>
#include <algorithm>
#include <utility>
using std::pair;
#include <string>
using std::string;
#include <iostream>
using std::cout;
//
// types + util --------------------------------------------------------------------------------------
//
enum NodeType {
ADAM,
EVE
};
struct ParityNode {
char name;
unsigned int prio;
NodeType type;
bool operator == (ParityNode const& other) const {
return name == other.name;
}
};
struct node_hash {
size_t operator () (ParityNode const& node) const {
return std::hash<char>()(node.name);
}
};
using NodeSet = std::unordered_set<ParityNode, node_hash>;
string node_set_string(NodeSet const & s) {
string result = "{";
for (auto& node : s) {
result += node.name;
result += ", ";
}
if (result.size() > 1) {
result.pop_back();
result.pop_back();
}
result += "}";
return result;
}
struct Edge {
ParityNode node1;
ParityNode node2;
Edge(NodeSet const& nodes, char char1, char char2)
: node1(*std::find_if(begin(nodes), end(nodes), [=](ParityNode const& node){ return node.name == char1; })),
node2(*std::find_if(begin(nodes), end(nodes), [=](ParityNode const& node){ return node.name == char2; }))
{}
bool operator == (Edge const& other) const {
return node1.name == other.node1.name && node2.name == other.node2.name;
}
};
struct edge_hash {
size_t operator () (Edge const& edge) const {
return std::hash<string>()(string{edge.node1.name, edge.node2.name});
}
};
using EdgeSet = std::unordered_set<Edge, edge_hash>;
NodeSet set_difference(NodeSet const& nodes1, NodeSet const& nodes2) {
NodeSet difference;
for (auto& node : nodes1) {
if (!nodes2.contains(node)) {
difference.insert(node);
}
}
return difference;
}
NodeSet set_union(NodeSet const& nodes1, NodeSet const& nodes2) {
NodeSet union_set = nodes1;
union_set.insert(begin(nodes2), end(nodes2));
return union_set;
}
//
// example game data --------------------------------------------------------------------------------
//
static NodeSet const NODES = {
ParityNode{'a', 3, EVE},
ParityNode{'b', 3, ADAM},
ParityNode{'c', 2, EVE},
ParityNode{'d', 1, EVE},
ParityNode{'e', 2, ADAM}
/*ParityNode{'f', 4, EVE},
ParityNode{'g', 1, ADAM},
ParityNode{'h', 2, EVE},
ParityNode{'i', 3, ADAM}*/
};
static EdgeSet const EDGES = {
Edge(NODES, 'a', 'e'),
Edge(NODES, 'e', 'd'),
Edge(NODES, 'd', 'e'),
Edge(NODES, 'd', 'c'),
Edge(NODES, 'c', 'b'),
Edge(NODES, 'b', 'c'),
Edge(NODES, 'a', 'b'),
Edge(NODES, 'b', 'a')
/*Edge(NODES, 'a', 'f'),
Edge(NODES, 'f', 'a'),
Edge(NODES, 'a', 'b'),
Edge(NODES, 'g', 'f'),
Edge(NODES, 'b', 'f'),
Edge(NODES, 'g', 'a'),
Edge(NODES, 'g', 'b'),
Edge(NODES, 'b', 'g'),
Edge(NODES, 'e', 'g'),
Edge(NODES, 'e', 'c'),
Edge(NODES, 'b', 'e'),
Edge(NODES, 'h', 'e'),
Edge(NODES, 'g', 'h'),
Edge(NODES, 'h', 'g'),
Edge(NODES, 'b', 'c'),
Edge(NODES, 'c', 'b'),
Edge(NODES, 'c', 'h'),
Edge(NODES, 'h', 'c'),
Edge(NODES, 'c', 'd'),
Edge(NODES, 'd', 'c'),
Edge(NODES, 'h', 'i'),
Edge(NODES, 'i', 'h'),
Edge(NODES, 'h', 'd'),
Edge(NODES, 'i', 'c'),
Edge(NODES, 'd', 'i'),
Edge(NODES, 'i', 'd')*/
};
//
// mcnaughton zielonka algo code -----------------------------------------------------------------------------------
//
NodeSet reach_attr(NodeType type, NodeSet const& k, NodeSet const& nodes, EdgeSet const& edges) {
NodeSet attr = k;
bool nodes_added = true;
while (nodes_added) {
nodes_added = false;
for (auto& node : nodes) {
if (!attr.contains(node)) {
if (node.type == type) {
if (std::find_if(begin(edges), end(edges), [&](Edge const& edge){
// player can reach k from node
return edge.node1.name == node.name && attr.contains(edge.node2);
}) != end(edges)) {
nodes_added = true;
attr.insert(node);
}
} else {
if (std::find_if(begin(edges), end(edges), [&](Edge const& edge){
// opponent can not avoid going to k
return edge.node1.name == node.name && !attr.contains(edge.node2);
}) == end(edges)) {
nodes_added = true;
attr.insert(node);
}
}
}
}
}
return attr;
}
// find the winning regions for eve and adam
pair<NodeSet, NodeSet> mcnaughtonzielonka(NodeSet const& nodes, EdgeSet const& edges) {
// find highest prio
unsigned int max_prio = 0;
for (auto& node : nodes) {
max_prio = std::max(max_prio, node.prio);
}
cout << "max prio is " << max_prio << "\n";
// base case
if (max_prio == 0) {
// eve wins
cout << "return base case prio 0: W_E = " << node_set_string(nodes) << ", W_A = {}\n";
return {nodes, NodeSet{}};
}
// find nodes with that prio
NodeSet k;
for (auto& node : nodes) {
if (node.prio == max_prio) {
k.insert(node);
}
}
cout << "K = " << node_set_string(k) << "\n";
if (max_prio % 2 == 0) {
// calculate eve's reach attractor of K
NodeSet attr = reach_attr(EVE, k, nodes, edges);
cout << "Attr_E = " << node_set_string(attr) << "\n";
// create a new subgame without the attractor
NodeSet subgame_nodes = set_difference(nodes, attr);
EdgeSet subgame_edges;
for (auto& edge : edges) {
if (!attr.contains(edge.node1) && !attr.contains(edge.node2)) {
subgame_edges.insert(edge);
}
}
cout << "created subgame with nodes: " << node_set_string(subgame_nodes) << "\n";
// calculate winning regions recursively
auto [eve_region, adam_region] = mcnaughtonzielonka(subgame_nodes, subgame_edges);
if (eve_region == set_difference(nodes, attr)) {
cout << "W_A' == Q\\Attr_E -> return W_E = " << node_set_string(nodes) << ", W_A = {}\n";
return {nodes, NodeSet{}};
}
auto opponent_attr = reach_attr(ADAM, adam_region, nodes, edges);
cout << "B = Attr_A(W_A') = " << node_set_string(opponent_attr) << "\n";
// create a new subgame without the opponent attractor
NodeSet opp_subgame_nodes = set_difference(nodes, opponent_attr);
EdgeSet opp_subgame_edges;
for (auto& edge : edges) {
if (!opponent_attr.contains(edge.node1) && !opponent_attr.contains(edge.node2)) {
opp_subgame_edges.insert(edge);
}
}
cout << "subgame with Q\\B: " << node_set_string(opp_subgame_nodes) << "\n";
// winning regions of game without the opponent attractor
auto [opp_eve_region, opp_adam_region] = mcnaughtonzielonka(opp_subgame_nodes, opp_subgame_edges);
cout << "Eve: return W_E = " << node_set_string(opp_eve_region) << ", W_A = W_A' u B = " << node_set_string(set_union(opp_adam_region, opponent_attr)) << "\n";
return {opp_eve_region, set_union(opp_adam_region, opponent_attr)};
} else {
// calculate adam's reach attractor of K
NodeSet attr = reach_attr(ADAM, k, nodes, edges);
cout << "Attr_A = " << node_set_string(attr) << "\n";
// create a new subgame without the attractor
NodeSet subgame_nodes = set_difference(nodes, attr);
EdgeSet subgame_edges;
for (auto& edge : edges) {
if (!attr.contains(edge.node1) && !attr.contains(edge.node2)) {
subgame_edges.insert(edge);
}
}
cout << "created subgame with nodes: " << node_set_string(subgame_nodes) << "\n";
// calculate winning regions recursively
auto [eve_region, adam_region] = mcnaughtonzielonka(subgame_nodes, subgame_edges);
if (adam_region == set_difference(nodes, attr)) {
cout << "W_A' == Q\\Attr_E -> return W_E = {}, W_A = " << node_set_string(nodes) << "\n";
return {NodeSet{}, nodes};
}
auto opponent_attr = reach_attr(EVE, eve_region, nodes, edges);
cout << "B = Attr_E(W_E') = " << node_set_string(opponent_attr) << "\n";
// create a new subgame without the opponent attractor
NodeSet opp_subgame_nodes = set_difference(nodes, opponent_attr);
EdgeSet opp_subgame_edges;
for (auto& edge : edges) {
if (!opponent_attr.contains(edge.node1) && !opponent_attr.contains(edge.node2)) {
opp_subgame_edges.insert(edge);
}
}
cout << "subgame without Q\\B: " << node_set_string(opp_subgame_nodes) << "\n";
// winning regions of game without the opponent attractor
auto [opp_eve_region, opp_adam_region] = mcnaughtonzielonka(opp_subgame_nodes, opp_subgame_edges);
cout << "Adam: return W_E = W_E' u B = " << node_set_string(set_union(opp_eve_region, opponent_attr)) << ", W_A = " << node_set_string(opp_adam_region) << "\n";
return {set_union(opp_eve_region, opponent_attr), opp_adam_region};
}
}
//
// test ----------------------------------------------------------------------------------------------
//
int main() {
auto [eve_region, adam_region] = mcnaughtonzielonka(NODES, EDGES);
cout << "-----------------------------------------------------\n";
cout << "EVE: " << node_set_string(eve_region) << "\n";
cout << "ADAM: " << node_set_string(adam_region) << std::endl;
return 0;
}