-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
522 lines (424 loc) · 11.9 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "state_space.hpp"
#include "transitional_state_space.hpp"
#include "paramset.hpp"
#include "buchi_automaton.hpp"
#include "bio_parser.hpp"
#include <iostream>
#include <deque>
#include <queue>
#include "succ.hpp"
/*
search for a key in a map and set its value to minimal
*/
template <typename T, typename U>
U min_combine(std::map<T, U> & map, T const & key, U value)
{
typename std::map<T, U>::iterator it = map.find(key);
if (it == map.end())
it = map.insert(std::make_pair(key, value)).first;
else
{
using std::min;
it->second = (min)(it->second, value);
}
return it->second;
}
template <typename T>
class single_elem_enum
{
public:
explicit single_elem_enum(T const & value)
: m_valid(true), m_value(value)
{
}
bool valid() const
{
return m_valid;
}
void next()
{
m_valid = false;
}
T get() const
{
return m_value;
}
private:
T m_value;
bool m_valid;
};
#ifdef __GNUC__
#include <sys/time.h>
long long my_clock()
{
timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec*1000 + tv.tv_usec/1000;
}
#else
#include <windows.h>
long long my_clock()
{
return GetTickCount();
}
#endif
class default_pmc_visitor
{
public:
long long time;
bool show_counterexamples;
bool show_base_coloring;
bool verbose;
std::size_t reachable_vertices;
explicit default_pmc_visitor(bool show_counterexamples, bool show_base_coloring, bool verbose)
: show_counterexamples(show_counterexamples), show_base_coloring(show_base_coloring), verbose(verbose), reachable_vertices(0)
{
}
void progress(int step, int max)
{
long long value = 0;
if (step == 0)
{
time = my_clock();
}
else
{
long long new_time = my_clock();
value = new_time - time;
time = new_time;
}
std::cout << step << "/" << max << ": " << value << "ms \r" << std::flush;
}
void succ_bfs_levels(std::size_t levels)
{
if (verbose)
std::cout << "bfs levels: " << levels << std::endl;
}
template <typename Coloring>
void base_coloring(Coloring const & c)
{
if (verbose)
{
for (auto ci = c.begin(); ci != c.end(); ++ci)
if (!ci->second.empty())
++reachable_vertices;
}
if (!show_base_coloring)
return;
for (auto ci = c.begin(); ci != c.end(); ++ci)
std::cout << ci->first << ": " << ci->second << std::endl;
}
template <typename Paramset, typename Vertex>
void counter_example_self_loop(Vertex const & witness, Paramset const & p) const
{
if (!show_counterexamples)
return;
std::cout << "* " << p << " " << std::endl;
std::cout << witness << " <- " << witness << std::endl;
}
template <typename Coloring, typename Vertex>
void counter_example(Coloring const & c, Coloring const & nested_c, Vertex const & witness) const
{
typedef typename Coloring::value_type::second_type Paramset;
if (!show_counterexamples)
return;
std::cout << "* " << nested_c.find(witness)->second << std::endl;
Vertex current = witness;
std::size_t region = 0;
for (;;)
{
Paramset const & p = nested_c.find(current)->second;
typename Paramset::region_t const & r = p.regions()[region];
std::cout << current << "<-" << r.tag << std::endl;
current = r.tag;
if (current == witness)
break;
Paramset const & new_p = nested_c.find(current)->second;
for (std::size_t i = 0; i < new_p.regions().size(); ++i)
{
typename Paramset::region_t const & new_r = new_p.regions()[i];
bool subset = true;
for (std::size_t j = 0; j < new_r.box.size(); ++j)
{
if (r.box[j].first < new_r.box[j].first
|| r.box[j].second > new_r.box[j].second)
{
subset = false;
break;
}
}
if (subset)
{
region = i;
break;
}
}
}
}
};
struct no_crop
{
template <typename Vertex, typename Paramset>
void operator()(Vertex const &, Paramset &)
{
}
};
template <typename Structure, typename Paramset, typename Visitor>
Paramset naive(Structure const & s, Paramset const & pp, Visitor visitor)
{
Paramset res;
visitor.progress(0, 0);
std::map<typename Structure::vertex_descriptor, Paramset> c
= succ(s, typename Structure::init_enumerator(s), pp, no_crop(), visitor);
int final_count = 0;
for (auto ci = c.begin(); ci != c.end(); ++ci)
{
if (s.final(ci->first))
++final_count;
}
int final_index = 0;
for (auto ci = c.begin(); ci != c.end(); ++ci)
{
if (s.final(ci->first))
{
visitor.progress(++final_index, final_count);
std::map<typename Structure::vertex_descriptor, Paramset> nested_c = succ(s, single_elem_enum<typename Structure::vertex_descriptor>(ci->first), ci->second, no_crop());
Paramset const & vp = nested_c[ci->first];
if (res.set_union(vp))
visitor.counter_example(c, nested_c, ci->first);
}
}
return res;
}
template <typename Coloring>
struct allowed_crop
{
explicit allowed_crop(Coloring const & c)
: m_c(c)
{
}
template <typename Vertex, typename Paramset>
void operator()(Vertex const & v, Paramset & p)
{
typename Coloring::const_iterator ci = m_c.find(v);
if (ci == m_c.end())
p.clear();
else
p.set_intersection(ci->second);
}
private:
Coloring const & m_c;
};
template <typename Coloring>
struct forbidden_crop
{
explicit forbidden_crop(Coloring const & c)
: m_c(c)
{
}
template <typename Vertex, typename Paramset>
void operator()(Vertex const & v, Paramset & p)
{
typename Coloring::const_iterator ci = m_c.find(v);
if (ci != m_c.end())
p.set_difference(ci->second);
}
private:
Coloring const & m_c;
};
template <typename Structure, typename Paramset, typename Visitor, typename Succ>
Paramset nonnaive(Structure const & s, Paramset const & pp, Visitor & visitor, Succ succ)
{
Paramset res;
visitor.progress(0, 0);
typedef std::map<typename Structure::vertex_descriptor, Paramset> coloring_type;
coloring_type c = succ(s, typename Structure::init_enumerator(s), pp, no_crop(), visitor);
visitor.base_coloring(c);
int final_count = 0;
for (auto ci = c.begin(); ci != c.end(); ++ci)
{
if (!ci->second.empty() && s.final(ci->first))
++final_count;
}
coloring_type forbidden_coloring;
int final_index = 0;
for (auto ci = c.begin(); ci != c.end(); ++ci)
{
if (!ci->second.empty() && s.final(ci->first))
{
visitor.progress(++final_index, final_count);
Paramset p = ci->second;
s.self_loop(ci->first, p);
if (res.set_union(p))
visitor.counter_example_self_loop(ci->first, p);
p = ci->second;
p.set_difference(res);
std::map<typename Structure::vertex_descriptor, Paramset> nested_c
= succ(s, single_elem_enum<typename Structure::vertex_descriptor>(ci->first), p,
forbidden_crop<coloring_type>(forbidden_coloring), visitor);
Paramset const & vp = nested_c[ci->first];
if (res.set_union(vp))
visitor.counter_example(c, nested_c, ci->first);
p = pp;
p.set_difference(res);
if (p.empty())
break;
// FIXME: the second succ run should be over the transposed graph
#if 0
std::map<typename Structure::vertex_descriptor, Paramset> scc_c = succ(s, single_elem_enum<typename Structure::vertex_descriptor>(ci->first), vp, allowed_crop<coloring_type>(nested_c));
for (auto ci = scc_c.begin(); ci != scc_c.end(); ++ci)
forbidden_coloring[ci->first].set_union(std::move(ci->second));
#endif
}
}
return res;
}
int main(int argc, char * argv[])
{
typedef state_space<float> ss_t;
typedef buchi_automaton<ss_t::proposition> ba_t;
std::map<std::string, ba_t> bas;
ss_t ss2;
std::vector<std::string> varnames;
std::map<std::string, schedule_entry> schedule;
transitional_state_space<float> ss3(ss2);
typedef ba_sync_product<transitional_state_space<float>::proposition, transitional_state_space<float> > sba_t;
typedef paramset<float, sba_t::vertex_descriptor> pp_t;
pp_t pp3;
parse_bio(std::cin, ss2, pp3, bas, varnames, schedule);
std::size_t thread_count = 1;
bool show_counterexamples = false;
bool show_base_coloring = false;
bool verbose = false;
// Parse arguments
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "-j")
thread_count = boost::lexical_cast<std::size_t>(argv[++i]);
if (arg == "-c")
show_counterexamples = true;
if (arg == "-b")
show_base_coloring = true;
if (arg == "-s")
std::cout << ss3 << std::endl;
if (arg == "-v")
verbose = true;
if (arg == "-u")
ss2.m_final.clear();
if (arg == "-t")
{
std::cout << "thresholds:" << std::endl;
for (std::size_t i = 0; i < ss2.m_thresholds.size(); ++i)
{
std::cout << varnames[i] << ":";
for (std::size_t j = 0; j < ss2.m_thresholds[i].size(); ++j)
{
if (j > 0)
std::cout << ',';
std::cout << ss2.m_thresholds[i][j];
}
std::cout << std::endl;
}
}
if (arg == "parse")
return 0;
if (arg == "plot")
{
float param[] = { boost::lexical_cast<float>(argv[i+1]) };
float ideal_len = 0.1f;
float step = 0.5f;
if (i+2 < argc)
ideal_len = boost::lexical_cast<float>(argv[i+2]);
if (i+3 < argc)
step = boost::lexical_cast<float>(argv[i+3]);
float xmin = ss2.m_thresholds[0].front();
float xmax = ss2.m_thresholds[0].back();
float ymin = ss2.m_thresholds[1].front();
float ymax = ss2.m_thresholds[1].back();
if (i+4 < argc)
xmin = boost::lexical_cast<float>(argv[i+4]);
if (i+5 < argc)
xmax = boost::lexical_cast<float>(argv[i+5]);
if (i+6 < argc)
ymin = boost::lexical_cast<float>(argv[i+6]);
if (i+7 < argc)
ymax = boost::lexical_cast<float>(argv[i+7]);
float ideal_leny = ideal_len / (xmax - xmin) * (ymax - ymin);
float stepy = step / (xmax - xmin) * (ymax - ymin);
const float null_thr = ideal_len / 5;
std::cout << "set nokey\n"
"plot '-' using 1:2:3:4 with vectors\n";
for (float x = xmin; x < xmax; x += step)
{
for (float y = ymin; y < ymax; y += stepy)
{
float state[] = { x, y };
float vec[] = { ss2.m_model.value(0, state, param), ss2.m_model.value(1, state, param) };
float len = std::sqrt(vec[0]*vec[0]+vec[1]*vec[1]);
vec[0] /= len;
vec[1] /= len;
#if 1
if (vec[0] < -null_thr)
vec[0] = -ideal_len;
else if (vec[0] > null_thr)
vec[0] = ideal_len;
else
vec[0] = 0;
if (vec[1] < -null_thr)
vec[1] = -ideal_leny;
else if (vec[1] > null_thr)
vec[1] = ideal_leny;
else
vec[1] = 0;
#else
vec[0] *= ideal_len;
vec[1] *= ideal_len;
#endif
std::cout << x << " "
<< y << " "
<< vec[0] << " "
<< vec[1] << "\n";
}
}
std::cout << "e\npause mouse" << std::endl;
return 0;
}
if (arg == "-i")
{
std::cout << ss2.m_init << std::endl;
}
if (arg == "-f")
{
std::cout << ss2.m_final << std::endl;
}
if (arg == "-r")
{
std::size_t amount = 5;
if (i + 1 < argc)
amount = boost::lexical_cast<std::size_t>(argv[++i]);
ss3.refine_thresholds(amount);
}
}
std::deque<std::pair<std::string, pp_t> > sch_queue;
sch_queue.push_back(std::make_pair("0", std::move(pp3)));
for (; !sch_queue.empty(); sch_queue.pop_front())
{
schedule_entry const & sch = schedule[sch_queue.front().first];
std::cout << "# " << sch_queue.front().first << ": " << sch_queue.front().second << std::endl;
sba_t sba(bas[sch_queue.front().first], ss3);
default_pmc_visitor visitor(show_counterexamples, show_base_coloring, verbose);
pp_t violating_parameters = nonnaive(sba, sch_queue.front().second, visitor, succ(thread_count));
if (verbose)
std::cout << "reachable vertices: " << visitor.reachable_vertices << std::endl;
pp_t correct_parameters = sch_queue.front().second;
correct_parameters.set_difference(violating_parameters);
std::cout << sch.succ_message << ": " << sch.succ_next << ": " << correct_parameters << std::endl;
if (!sch.succ_next.empty())
sch_queue.push_back(std::make_pair(sch.succ_next, std::move(correct_parameters)));
std::cout << sch.fail_message << ": " << sch.fail_next << ": " << violating_parameters << std::endl;
if (!sch.fail_next.empty())
sch_queue.push_back(std::make_pair(sch.fail_next, std::move(violating_parameters)));
std::cout << std::endl;
}
}