randtgba.cc 14.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Copyright (C) 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
// dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre
// et Marie Curie.
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

#include <cstdlib>
#include <iostream>
24
#include <iomanip>
25
#include <sstream>
26
#include <utility>
27
28
29
30
31
32
33
34
35
#include "ltlvisit/apcollect.hh"
#include "ltlvisit/destroy.hh"
#include "tgbaalgos/randomgraph.hh"
#include "tgbaalgos/save.hh"
#include "ltlenv/defaultenv.hh"
#include "ltlast/atomic_prop.hh"
#include "tgbaalgos/dotty.hh"
#include "misc/random.hh"

36
#include "tgbaalgos/emptiness.hh"
37
#include "tgbaalgos/emptiness_stats.hh"
38
39
#include "tgbaalgos/gtec/gtec.hh"
#include "tgbaalgos/magic.hh"
40
41
#include "tgbaalgos/se05.hh"
#include "tgbaalgos/tau03.hh"
42
#include "tgbaalgos/tau03opt.hh"
43
44
#include "tgbaalgos/replayrun.hh"

45
46
47
48
49
50
void
syntax(char* prog)
{
  std::cerr << "Usage: "<< prog << " [OPTIONS...] PROPS..." << std::endl
	    << std::endl
	    << "Options:" << std::endl
51
	    << "  -a N F  number of acceptance conditions and probability that"
52
53
	    << " one is true" << std::endl
	    << "            [0 0.0]" << std::endl
54
	    << "  -d F    density of the graph [0.2]" << std::endl
55
56
	    << "  -e N    compare result of all "
	    << "emptiness checks on N randomly generated graphs" << std::endl
57
58
	    << "  -g      output in dot format" << std::endl
	    << "  -n N    number of nodes of the graph [20]" << std::endl
59
60
	    << "  -r      compute and replay acceptance runs (implies -e)"
	    << std::endl
61
62
	    << "  -s N    seed for the random number generator" << std::endl
	    << "  -t F    probability of the atomic propositions to be true"
63
	    << " [0.5]" << std::endl
64
65
66
67
	    << "  -z      display statistics about computed counter-examples"
	    << std::endl
	    << "  -Z      like -z, but print extra statistics after the run"
	    << " of each algorithm" << std::endl
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
	    << "Where:" << std::endl
	    << "  F      are floats between 0.0 and 1.0 inclusive" << std::endl
	    << "  N      are positive integers" << std::endl
	    << "  PROPS  are the atomic properties to use on transitions"
	    << std::endl;
  exit(2);
}

int
to_int(const char* s)
{
  char* endptr;
  int res = strtol(s, &endptr, 10);
  if (*endptr)
    {
      std::cerr << "Failed to parse `" << s << "' as an integer." << std::endl;
      exit(1);
    }
  return res;
}

float
to_float(const char* s)
{
  char* endptr;
  float res = strtof(s, &endptr);
  if (*endptr)
    {
      std::cerr << "Failed to parse `" << s << "' as a float." << std::endl;
      exit(1);
    }
  return res;
}

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
struct ec_stat
{
  int min_states;
  int max_states;
  int tot_states;
  int min_transitions;
  int max_transitions;
  int tot_transitions;
  int min_max_depth;
  int max_max_depth;
  int tot_max_depth;
  int n;

  ec_stat()
    : n(0)
  {
  }

  void
  count(const spot::ec_statistics* ec)
  {
    int s = ec->states();
    int t = ec->transitions();
    int m = ec->max_depth();
    if (n++)
      {
	min_states = std::min(min_states, s);
	max_states = std::max(max_states, s);
	tot_states += s;
	min_transitions = std::min(min_transitions, t);
	max_transitions = std::max(max_transitions, t);
	tot_transitions += t;
	min_max_depth = std::min(min_max_depth, m);
	max_max_depth = std::max(max_max_depth, m);
	tot_max_depth += m;
      }
    else
      {
	min_states = max_states = tot_states = s;
	min_transitions = max_transitions = tot_transitions = t;
	min_max_depth = max_max_depth = tot_max_depth = m;
      }
  }
};

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
struct ce_stat
{
  int min_prefix;
  int max_prefix;
  int tot_prefix;
  int min_cycle;
  int max_cycle;
  int tot_cycle;
  int min_run;
  int max_run;
  int n;

  ce_stat()
    : n(0)
  {
  }

  void
  count(const spot::tgba_run* run)
  {
    int p = run->prefix.size();
    int c = run->cycle.size();
    if (n++)
      {
	min_prefix = std::min(min_prefix, p);
	max_prefix = std::max(max_prefix, p);
	tot_prefix += p;
	min_cycle = std::min(min_cycle, c);
	max_cycle = std::max(max_cycle, c);
	tot_cycle += c;
	min_run = std::min(min_run, c + p);
	max_run = std::max(max_run, c + p);
      }
    else
      {
	min_prefix = max_prefix = tot_prefix = p;
	min_cycle = max_cycle = tot_cycle = c;
	min_run = max_run = c + p;
      }
  }
};

189
190
191
192
193
194
195
196
197
int
main(int argc, char** argv)
{
  int opt_n_acc = 0;
  float opt_a = 0.0;
  float opt_d = 0.2;
  int opt_n = 20;
  float opt_t = 0.5;

198
199
200
  bool opt_z = false;
  bool opt_Z = false;

201
  bool opt_dot = false;
202
203
204
  int opt_ec = 0;
  int opt_ec_seed = 0;
  bool opt_replay = false;
205
206
207

  int argn = 0;

208
209
  int exit_code = 0;

210
211
212
213
214
  typedef std::map<std::string, ec_stat> ec_stats_type;
  ec_stats_type ec_stats;

  typedef std::map<std::string, ce_stat> ce_stats_type;
  ce_stats_type ce_stats;
215

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  spot::ltl::environment& env(spot::ltl::default_environment::instance());
  spot::ltl::atomic_prop_set* ap = new spot::ltl::atomic_prop_set;

  if (argc <= 1)
    syntax(argv[0]);

  while (++argn < argc)
    {
      if (!strcmp(argv[argn], "-a"))
	{
	  if (argc < argn + 3)
	    syntax(argv[0]);
	  opt_n_acc = to_int(argv[++argn]);
	  opt_a = to_float(argv[++argn]);
	}
      else if (!strcmp(argv[argn], "-d"))
	{
233
	  if (argc < argn + 2)
234
235
236
	    syntax(argv[0]);
	  opt_d = to_float(argv[++argn]);
	}
237
238
239
240
241
242
      else if (!strcmp(argv[argn], "-e"))
	{
	  if (argc < argn + 2)
	    syntax(argv[0]);
	  opt_ec = to_int(argv[++argn]);
	}
243
244
245
246
247
248
      else if (!strcmp(argv[argn], "-g"))
	{
	  opt_dot = true;
	}
      else if (!strcmp(argv[argn], "-n"))
	{
249
	  if (argc < argn + 2)
250
251
252
	    syntax(argv[0]);
	  opt_n = to_int(argv[++argn]);
	}
253
254
255
256
257
258
      else if (!strcmp(argv[argn], "-r"))
	{
	  opt_replay = true;
	  if (!opt_ec)
	    opt_ec = 1;
	}
259
260
      else if (!strcmp(argv[argn], "-s"))
	{
261
	  if (argc < argn + 2)
262
	    syntax(argv[0]);
263
264
	  opt_ec_seed = to_int(argv[++argn]);
	  spot::srand(opt_ec_seed);
265
266
267
	}
      else if (!strcmp(argv[argn], "-t"))
	{
268
	  if (argc < argn + 2)
269
270
271
	    syntax(argv[0]);
	  opt_t = to_float(argv[++argn]);
	}
272
273
274
275
276
277
278
279
      else if (!strcmp(argv[argn], "-z"))
	{
	  opt_z = true;
	}
      else if (!strcmp(argv[argn], "-Z"))
	{
	  opt_Z = opt_z = true;
	}
280
281
282
283
284
285
286
287
288
289
      else
	{
	  ap->insert(static_cast<spot::ltl::atomic_prop*>
		     (env.require(argv[argn])));
	}
    }

  spot::bdd_dict* dict = new spot::bdd_dict();


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
  std::set<int> failed_seeds;
  do
    {
      if (opt_ec)
	{
	  std::cout << "seed: " << opt_ec_seed << std::endl;
	  spot::srand(opt_ec_seed);
	}

      spot::tgba* a = spot::random_graph(opt_n, opt_d, ap, dict,
					 opt_n_acc, opt_a, opt_t, &env);

      if (!opt_ec)
	{
	  if (opt_dot)
	    dotty_reachable(std::cout, a);
	  else
	    tgba_save_reachable(std::cout, a);
	}
      else
	{
	  std::vector<spot::emptiness_check*> ec_obj;
	  std::vector<std::string> ec_name;
	  std::vector<bool> ec_safe;

	  ec_obj.push_back(new spot::couvreur99_check(a));
	  ec_name.push_back("couvreur99");
	  ec_safe.push_back(true);

	  ec_obj.push_back(new spot::couvreur99_check_shy(a));
	  ec_name.push_back("couvreur99_shy");
	  ec_safe.push_back(true);

	  if (opt_n_acc <= 1)
	    {
	      ec_obj.push_back(spot::explicit_magic_search(a));
	      ec_name.push_back("explicit_magic_search");
	      ec_safe.push_back(true);

	      ec_obj.push_back(spot::bit_state_hashing_magic_search(a, 4096));
	      ec_name.push_back("bit_state_hashing_magic_search");
	      ec_safe.push_back(false);

	      ec_obj.push_back(spot::explicit_se05_search(a));
	      ec_name.push_back("explicit_se05");
	      ec_safe.push_back(true);

	      ec_obj.push_back(spot::bit_state_hashing_se05_search(a, 4096));
	      ec_name.push_back("bit_state_hashing_se05_search");
	      ec_safe.push_back(false);
	    }

342
343
344
345
346
347
348
349
350
351
352
	  if (opt_n_acc >= 1)
	    {
              ec_obj.push_back(spot::explicit_tau03_search(a));
              ec_name.push_back("explicit_tau03_search");
              ec_safe.push_back(true);
            }

          ec_obj.push_back(spot::explicit_tau03_opt_search(a));
          ec_name.push_back("explicit_tau03_opt_search");
          ec_safe.push_back(true);

353
354
355
356
357
358
359
	  int n_ec = ec_obj.size();
	  int n_empty = 0;
	  int n_non_empty = 0;
	  int n_maybe_empty = 0;

	  for (int i = 0; i < n_ec; ++i)
	    {
360
	      std::string algo = ec_name[i];
361
	      std::cout.width(32);
362
	      std::cout << algo << ": ";
363
	      spot::emptiness_check_result* res = ec_obj[i]->check();
364
365
366
367
	      const spot::ec_statistics* ecs =
                       dynamic_cast<const spot::ec_statistics*>(ec_obj[i]);
              if (opt_z && ecs)
		ec_stats[algo].count(ecs);
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
	      if (res)
		{
		  std::cout << "accepting run exists";
		  ++n_non_empty;
		  if (opt_replay)
		    {
		      spot::tgba_run* run = res->accepting_run();
		      if (run)
			{
			  std::ostringstream s;
			  if (!spot::replay_tgba_run(s, a, run))
			    {
			      std::cout << ", but could not replay it (ERROR!)";
			      failed_seeds.insert(opt_ec_seed);
			    }
			  else
			    {
			      std::cout << ", computed OK";
386
			      if (opt_z)
387
				ce_stats[algo].count(run);
388
			    }
389
390
391
392
			  if (opt_z)
			    std::cout << " [" << run->prefix.size()
				      << " + " << run->cycle.size()
				      << "]";
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
			  delete run;
			}
		      else
			{
			  std::cout << ", not computed";
			}
		    }
		  std::cout << std::endl;
		  delete res;
		}
	      else
		{
		  if (ec_safe[i])
		    {
		      std::cout << "empty language" << std::endl;
		      ++n_empty;
		    }
		  else
		    {
		      std::cout << "maybe empty language" << std::endl;
		      ++n_maybe_empty;
		    }

		}
417
418
419

	      if (opt_Z)
		ec_obj[i]->print_stats(std::cout);
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
	      delete ec_obj[i];
	    }

	  assert(n_empty + n_non_empty + n_maybe_empty == n_ec);

	  if ((n_empty == 0 && (n_non_empty + n_maybe_empty) != n_ec)
	      || (n_empty != 0 && n_non_empty != 0))
	    {
	      std::cout << "ERROR: not all algorithms agree" << std::endl;
	      failed_seeds.insert(opt_ec_seed);
	    }
	}

      delete a;

      if (opt_ec)
	{
	  --opt_ec;
	  ++opt_ec_seed;
	}
    }
  while (opt_ec);
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
  if (!ec_stats.empty())
    {
      std::cout << "Statistics about emptiness checkers:" << std::endl;
      std::cout << std::setw(32) << ""
		<< " |       states      |    transitions    |"
		<< std::endl << std::setw(32) << "algorithm"
		<< " |  min < mean < max |  min < mean < max |   n "
		<< std::endl
		<< std::setw(79) << std::setfill('-') << "" << std::setfill(' ')
		<< std::endl << std::setprecision(3);
      for (ec_stats_type::const_iterator i = ec_stats.begin();
                                                i != ec_stats.end(); ++i)
	std::cout << std::setw(32) << i->first << " |"
		  << std::setw(5) << i->second.min_states
		  << " "
		  << std::setw(6)
		  << static_cast<float>(i->second.tot_states) / i->second.n
		  << " "
		  << std::setw(5) << i->second.max_states
		  << " |"
		  << std::setw(5) << i->second.min_transitions
		  << " "
		  << std::setw(6)
		  << static_cast<float>(i->second.tot_transitions) / i->second.n
		  << " "
		  << std::setw(5) << i->second.max_transitions
		  << " |"
		  << std::setw(5) << i->second.n
		  << std::endl;
      std::cout << std::setw(79) << std::setfill('-') << "" << std::setfill(' ')
		<< std::endl
		<< std::setw(32) << ""
		<< " |   maximal depth   |"
		<< std::endl << std::setw(32) << "algorithm"
		<< " |  min < mean < max |   n "
		<< std::endl
		<< std::setw(59) << std::setfill('-') << "" << std::setfill(' ')
		<< std::endl;
      for (ec_stats_type::const_iterator i = ec_stats.begin();
                                                i != ec_stats.end(); ++i)
	std::cout << std::setw(32) << i->first << " |"
		  << std::setw(5)
		  << i->second.min_max_depth
		  << " "
		  << std::setw(6)
		  << static_cast<float>(i->second.tot_max_depth) / i->second.n
		  << " "
		  << std::setw(5)
		  << i->second.max_max_depth
		  << " |"
		  << std::setw(5) << i->second.n
		  << std::endl;
    }

  if (!ce_stats.empty())
498
499
500
501
502
503
504
505
506
    {
      std::cout << "Statistics about counter-examples:" << std::endl;
      std::cout << std::setw(32) << ""
		<< " |       prefix      |       cycle       |"
		<< std::endl << std::setw(32) << "algorithm"
		<< " |  min < mean < max |  min < mean < max |   n "
		<< std::endl
		<< std::setw(79) << std::setfill('-') << "" << std::setfill(' ')
		<< std::endl << std::setprecision(3);
507
508
      for (ce_stats_type::const_iterator i = ce_stats.begin();
                                                i != ce_stats.end(); ++i)
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
	std::cout << std::setw(32) << i->first << " |"
		  << std::setw(5) << i->second.min_prefix
		  << " "
		  << std::setw(6)
		  << static_cast<float>(i->second.tot_prefix) / i->second.n
		  << " "
		  << std::setw(5) << i->second.max_prefix
		  << " |"
		  << std::setw(5) << i->second.min_cycle
		  << " "
		  << std::setw(6)
		  << static_cast<float>(i->second.tot_cycle) / i->second.n
		  << " "
		  << std::setw(5) << i->second.max_cycle
		  << " |"
		  << std::setw(5) << i->second.n
		  << std::endl;
      std::cout << std::setw(79) << std::setfill('-') << "" << std::setfill(' ')
		<< std::endl
		<< std::setw(32) << ""
		<< " |        runs       |       total      |"
		<< std::endl << std::setw(32) << "algorithm"
		<< " |  min < mean < max | pre.  cyc.  runs |   n "
		<< std::endl
		<< std::setw(79) << std::setfill('-') << "" << std::setfill(' ')
		<< std::endl;
535
536
      for (ce_stats_type::const_iterator i = ce_stats.begin();
                                                i != ce_stats.end(); ++i)
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
	std::cout << std::setw(32) << i->first << " |"
		  << std::setw(5)
		  << i->second.min_run
		  << " "
		  << std::setw(6)
		  << static_cast<float>(i->second.tot_prefix
					+ i->second.tot_cycle) / i->second.n
		  << " "
		  << std::setw(5)
		  << i->second.max_run
		  << " |"
		  << std::setw(5) << i->second.tot_prefix
		  << " "
		  << std::setw(5) << i->second.tot_cycle
		  << " "
		  << std::setw(5) << i->second.tot_prefix + i->second.tot_cycle
		  << " |"
		  << std::setw(5) << i->second.n
		  << std::endl;

    }

559
560
561
562
563
564
565
566
567
  if (!failed_seeds.empty())
    {
      exit_code = 1;
      std::cout << "The check failed for the following seeds:";
      for (std::set<int>::const_iterator i = failed_seeds.begin();
	   i != failed_seeds.end(); ++i)
	std::cout << " " << *i;
      std::cout << std::endl;
    }
568
569
570
571
572
573

  for (spot::ltl::atomic_prop_set::iterator i = ap->begin();
       i != ap->end(); ++i)
    spot::ltl::destroy(*i);

  delete ap;
574
575
  delete dict;
  return exit_code;
576
}