randtgba.cc 11.6 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
37
38
#include "tgbaalgos/emptiness.hh"
#include "tgbaalgos/gtec/gtec.hh"
#include "tgbaalgos/magic.hh"
39
40
#include "tgbaalgos/se05.hh"
#include "tgbaalgos/tau03.hh"
41
#include "tgbaalgos/tau03opt.hh"
42
43
#include "tgbaalgos/replayrun.hh"

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

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
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;
      }
  }
};

143
144
145
146
147
148
149
150
151
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;

152
153
154
  bool opt_z = false;
  bool opt_Z = false;

155
  bool opt_dot = false;
156
157
158
  int opt_ec = 0;
  int opt_ec_seed = 0;
  bool opt_replay = false;
159
160
161

  int argn = 0;

162
163
  int exit_code = 0;

164
165
166
  typedef std::map<std::string, ce_stat> stats_type;
  stats_type stats;

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  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"))
	{
184
	  if (argc < argn + 2)
185
186
187
	    syntax(argv[0]);
	  opt_d = to_float(argv[++argn]);
	}
188
189
190
191
192
193
      else if (!strcmp(argv[argn], "-e"))
	{
	  if (argc < argn + 2)
	    syntax(argv[0]);
	  opt_ec = to_int(argv[++argn]);
	}
194
195
196
197
198
199
      else if (!strcmp(argv[argn], "-g"))
	{
	  opt_dot = true;
	}
      else if (!strcmp(argv[argn], "-n"))
	{
200
	  if (argc < argn + 2)
201
202
203
	    syntax(argv[0]);
	  opt_n = to_int(argv[++argn]);
	}
204
205
206
207
208
209
      else if (!strcmp(argv[argn], "-r"))
	{
	  opt_replay = true;
	  if (!opt_ec)
	    opt_ec = 1;
	}
210
211
      else if (!strcmp(argv[argn], "-s"))
	{
212
	  if (argc < argn + 2)
213
	    syntax(argv[0]);
214
215
	  opt_ec_seed = to_int(argv[++argn]);
	  spot::srand(opt_ec_seed);
216
217
218
	}
      else if (!strcmp(argv[argn], "-t"))
	{
219
	  if (argc < argn + 2)
220
221
222
	    syntax(argv[0]);
	  opt_t = to_float(argv[++argn]);
	}
223
224
225
226
227
228
229
230
      else if (!strcmp(argv[argn], "-z"))
	{
	  opt_z = true;
	}
      else if (!strcmp(argv[argn], "-Z"))
	{
	  opt_Z = opt_z = true;
	}
231
232
233
234
235
236
237
238
239
240
      else
	{
	  ap->insert(static_cast<spot::ltl::atomic_prop*>
		     (env.require(argv[argn])));
	}
    }

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


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
  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);

274
275
276
277
278
	  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);
279
280
281
282

              ec_obj.push_back(spot::explicit_tau03_opt_search(a));
              ec_name.push_back("explicit_tau03_opt_search");
              ec_safe.push_back(true);
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
	  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);
	    }

	  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)
	    {
311
	      std::string algo = ec_name[i];
312
	      std::cout.width(32);
313
	      std::cout << algo << ": ";
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
	      spot::emptiness_check_result* res = ec_obj[i]->check();
	      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";
333
334
			      if (opt_z)
				stats[algo].count(run);
335
			    }
336
337
338
339
			  if (opt_z)
			    std::cout << " [" << run->prefix.size()
				      << " + " << run->cycle.size()
				      << "]";
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
			  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;
		    }

		}
364
365
366

	      if (opt_Z)
		ec_obj[i]->print_stats(std::cout);
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
	      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);
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

  if (!stats.empty())
    {
      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);
      for (stats_type::const_iterator i = stats.begin(); i != stats.end(); ++i)
	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;
      for (stats_type::const_iterator i = stats.begin(); i != stats.end(); ++i)
	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;

    }

450
451
452
453
454
455
456
457
458
  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;
    }
459
460
461
462
463
464

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

  delete ap;
465
466
  delete dict;
  return exit_code;
467
}