-
Notifications
You must be signed in to change notification settings - Fork 10
/
nudge.cpp
326 lines (298 loc) · 9.74 KB
/
nudge.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright 2017-2018 Tom van Dijk, Johannes Kepler University Linz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>
#include <iostream>
#include <fstream>
#include <random>
#include "cxxopts.hpp"
#include "oink/game.hpp"
using namespace std;
using namespace pg;
int
rng(int low, int high)
{
static random_device rand_dev;
static mt19937 generator(rand_dev());
return uniform_int_distribution<int>(low, high)(generator);
}
/**
* Tarjan's SCC algorithm, modified to only compute the bottom SCC
*/
void
tarjan(Game *game, int n, std::vector<int> &res, bool nonempty)
{
// initialize
unsigned n_vertices = game->vertexcount();
int *low = new int[n_vertices];
memset(low, 0, sizeof(int[n_vertices]));
int pre = 0;
// search stack "st"
std::stack<int> st;
st.push(n);
while (!st.empty()) { // st is never empty
int idx = st.top();
if (low[idx] == 0) {
// first time we see it
low[idx] = ++pre;
res.push_back(idx);
}
int min = low[idx];
bool pushed = false;
for (auto curedge = game->outs(idx); *curedge != -1; curedge++) {
int to = *curedge;
if (low[to] == 0) {
// not visited
st.push(to);
pushed = true;
break;
} else {
// visited it, update min
if (low[to] < min) min = low[to];
}
}
if (pushed) continue; // we pushed...
if (min < low[idx]) {
low[idx] = min;
st.pop();
continue;
}
/**
* At this point, we found a bottom SCC. Now check if it is empty.
* A SCC is "empty" if it contains no edges, i.e., consists of 1 vertex without self-loops.
*/
if (nonempty) {
if (res.back() == idx and !game->has_edge(idx, idx)) {
// it has no edges!
res.pop_back();
st.pop();
if (st.empty()) break;
continue;
}
}
// found bottom SCC; remove vertices not in the bottom SCC...
if (res.front() != idx) res.erase(res.begin(), std::find(res.begin(), res.end(), idx));
break;
}
delete[] low;
}
bool
nudge(Game *game, int profile)
{
/**
* Select a random vertex
*/
int n = rng(0, game->vertexcount()-1);
/**
* Select a random action
* action=0: change owner of the vertex
* action=1: change priority of the vertex
* action=2: remove the vertex without forwarding edges
* action=3: remove the vertex and forward edges if it has only 1 outgoing edge
* action=4: for one predecessor replace the edge to me by all my edges
* action=5: remove a random edge (if 2+ outgoing edges)
* action=6: add a random edge
*/
int action;
if (profile == 0) {
// only actions 0 1 2 3 5 : only change/remove, no adding edges
action = (int[5]){0,1,2,3,5}[rng(0, 4)];
} else if (profile == 1) {
// only actions 0 1 5 6 : just change edges and owner/priority
action = (int[4]){0,1,5,6}[rng(0, 3)];
} else if (profile == 2) {
// only actions 0 1 2 3 4 5 : only add edge when removing vertices
action = (int[6]){0,1,2,3,4,5}[rng(0, 5)];
} else {
action = rng(0, 6);
}
/**
* Perform the action
*/
if (action == 0) {
// flip owner of the vertex
game->set_owner(n, 1-game->owner(n));
return true;
} else if (action == 1) {
// change priority of the vertex
game->set_priority(n, rng(0, game->vertexcount()*2));
return true;
} else if (action == 2) {
// remove the vertex, but only if no incoming edge is a solo edge
for (auto curedge = game->ins(n); *curedge != -1; curedge++) {
if (game->outcount(*curedge) == 1) return false;
}
pg::bitset mask(game->vertexcount());
mask.set();
mask[n] = false;
Game *subgame = game->extract_subgame(mask);
game->swap(*subgame);
delete subgame;
return true;
} else if (action == 3) {
// remove the vertex and forward incoming edges if it has only 1 outgoing edge
if (game->outcount(n) == 1 and game->outs(n)[0] != n) {
game->vec_init();
for (auto curedge = game->ins(n); *curedge != -1; curedge++) {
int from = *curedge;
// add each edge "n -> to" to "from -> to"
for (auto curedge = game->outs(n); *curedge != -1; curedge++) {
game->vec_add_edge(from, *curedge);
}
}
game->vec_finish();
// remove the vertex
pg::bitset mask(game->vertexcount());
mask.set();
mask[n] = false;
Game *subgame = game->extract_subgame(mask);
game->swap(*subgame);
delete subgame;
return true;
}
} else if (action == 4) {
// for one random predecessor replace the edge to me by all my edges
if (game->incount(n) > 0) {
int from = game->ins(n)[rng(0, game->incount(n)-1)];
if (from != n) {
game->vec_init();
game->vec_remove_edge(from, n);
for (auto curedge = game->outs(n); *curedge != -1; curedge++) {
game->vec_add_edge(from, *curedge);
}
game->vec_finish();
return true;
}
}
} else if (action == 5) {
// remove a random edge (if there are outgoing edges to remove)
if (game->outcount(n) > 1) {
int edge = rng(0, game->outcount(n)-1);
int m = game->outs(n)[edge];
game->vec_init();
game->vec_remove_edge(n, m);
game->vec_finish();
return true;
}
} else if (action == 6) {
// add a random edge
game->vec_init();
bool res = game->vec_add_edge(n, rng(0, game->vertexcount()-1));
game->vec_finish();
if (res) return true;
}
return false;
}
/**
* Change the game a bit.
*/
int
main(int argc, char **argv)
{
cxxopts::Options opts(argv[0], "Parity game solver");
opts.add_options()
("input", "Input parity game", cxxopts::value<std::string>())
("output", "Output parity game", cxxopts::value<std::string>())
("help", "Print help")
("m,modify", "Modify graph with profile 0=only remove, 1=stable #vertices, 2=everything", cxxopts::value<int>())
("b,bottom-scc", "Obtain random bottom SCC before writing")
("i,inflate", "Inflate before writing")
("c,compress", "Compress before writing")
("r,renumber", "Renumber before writing")
("o,order", "Order vertices by priority before writing")
("u,unlabel", "Remove labels")
("evenodd", "Swap players")
("minmax", "Turn a mingame into a maxgame and vice versa")
;
opts.parse_positional(std::vector<std::string>({"input", "output"}));
auto options = opts.parse(argc, argv);
if (options.count("help")) {
std::cout << opts.help() << std::endl;
return 0;
}
/**
* Read a game from file or stdin.
*/
Game *game = new Game();
try {
if (options.count("input")) {
std::ifstream file(options["input"].as<std::string>());
game->parse_pgsolver(file, false);
file.close();
} else {
game->parse_pgsolver(std::cin, false);
}
} catch (const char *err) {
std::cerr << "parsing error: " << err << std::endl;
return -1;
}
/**
* Check if we have to modify the game randomly.
*/
if (options.count("modify")) {
int profile = options["modify"].as<int>();
while (nudge(game, profile) == false) {}
}
/**
* If asked, compute a bottom SCC from a random vertex
*/
if (options.count("b")) {
std::vector<int> scc;
tarjan(game, rng(0, game->vertexcount()-1), scc, true);
Game *sub = game->extract_subgame(scc);
delete game;
game = sub;
}
/**
* Reindex before transformations
*/
int *mapping = new int[game->vertexcount()];
game->sort(mapping);
/**
* Perform even/odd or min/max transformations
*/
if (options.count("evenodd")) game->evenodd();
if (options.count("minmax")) game->minmax();
/**
* Inflate/compress/renumber
*/
if (options.count("i")) game->inflate();
if (options.count("c")) game->compress();
if (options.count("r")) game->renumber();
/**
* Either reindex again (-o) or undo previous reindex
*/
if (options.count("o")) game->sort();
else game->permute(mapping);
/**
* Remove labels
*/
if (options.count("u")) {
for (int i=0; i<game->vertexcount(); i++) game->set_label(i, "");
}
/**
* Write to output file or to stdout
*/
if (options.count("output")) {
std::ofstream file(options["output"].as<std::string>());
game->write_pgsolver(file);
file.close();
} else {
game->write_pgsolver(std::cout);
}
delete game;
delete[] mapping;
return 0;
}