randaut.cc 12.1 KB
Newer Older
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2012, 2013, 2014, 2015 Laboratoire de Recherche et
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
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
// Développement de l'Epita (LRDE).
//
// 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 3 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 this program.  If not, see <http://www.gnu.org/licenses/>.

#include "common_sys.hh"

#include <iostream>
#include <fstream>
#include <argp.h>
#include <cstdlib>
#include <sstream>
#include <iterator>
#include "error.h"
29
#include "argmatch.h"
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
30
31
32
33

#include "common_setup.hh"
#include "common_range.hh"
#include "common_cout.hh"
34
#include "common_aoutput.hh"
35
#include "common_conv.hh"
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
36
37

#include "ltlenv/defaultenv.hh"
38
#include "misc/timer.hh"
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
39
40
#include "misc/random.hh"

41
#include "twa/bddprint.hh"
42
43
#include "twaalgos/randomgraph.hh"
#include "twaalgos/canonicalize.hh"
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
44
45
46
47
48
49


const char argp_program_doc[] = "\
Generate random connected automata.\n\n\
The automata are built over the atomic propositions named by PROPS...\n\
or, if N is a nonnegative number, using N arbitrary names.\n\
50
51
52
If the density is set to D, and the number of states to Q, the degree\n\
of each state follows a normal distribution with mean 1+(Q-1)D and\n\
variance (Q-1)D(1-D).  In particular, for D=0 all states have a single\n\
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
53
54
55
56
57
successor, while for D=1 all states are interconnected.\v\
Examples:\n\
\n\
This builds a random neverclaim with 4 states and labeled using the two\n\
atomic propositions \"a\" and \"b\":\n\
58
  % randaut --spin -Q4 a b\n\
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
59
60
61
\n\
This builds three random, complete, and deterministic TGBA with 5 to 10\n\
states, 1 to 3 acceptance sets, and three atomic propositions:\n\
62
  % randaut -n3 -D -H -Q5..10 -A1..3 3\n\
63
64
65
66
\n\
Build 3 random, complete, and deterministic Rabin automata\n\
with 2 to 3 acceptance pairs, state-based acceptance, 8 states, \n\
a high density of transitions, and 3 to 4 atomic propositions:\n\
67
  % randaut -n3 -D -H -Q8 -d.8 -S -A 'Rabin 2..3' 3..4\n\
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
68
69
";

70
71
enum {
  OPT_SEED = 1,
72
  OPT_COLORED,
73
};
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
74
75
76
77

static const argp_option options[] =
  {
    /**************************************************/
78
79
80
    { 0, 0, 0, 0, "Generation:", 1 },
    { "acceptance", 'A', "ACCEPTANCE", 0,
      "specify the acceptance type of the automaton", 0 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
81
82
83
84
85
    { "acc-probability", 'a', "FLOAT", 0,
      "probability that a transition belong to one acceptance set (0.2)", 0 },
    { "automata", 'n', "INT", 0, "number of automata to output (1)\n"\
      "use a negative value for unbounded generation", 0 },
    { "ba", 'B', 0, 0,
86
      "build a Buchi automaton (implies --acceptance=Buchi --state-acc)", 0 },
87
88
89
    { "colored", OPT_COLORED, 0, 0,
      "build an automaton in which each transition (or state if combined with "
      "-S) belong to a single acceptance set", 0 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
90
91
92
    { "density", 'd', "FLOAT", 0, "density of the transitions (0.2)", 0 },
    { "deterministic", 'D', 0, 0, "build a complete, deterministic automaton ",
      0 },
93
    { "unique", 'u', 0, 0,
94
95
      "do not output the same automaton twice (same in the sense that they "\
      "are isomorphic)", 0 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
96
97
    { "seed", OPT_SEED, "INT", 0,
      "seed for the random number generator (0)", 0 },
98
99
100
    { "states", 'Q', "RANGE", 0, "number of states to output (10)", 0 },
    { "state-based-acceptance", 'S', 0, 0, "used state-based acceptance", 0 },
    { "sbacc", 0, 0, OPTION_ALIAS, 0, 0 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
101
    RANGE_DOC,
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    { 0, 0, 0, 0, "ACCEPTANCE may be either a RANGE (in which case "
      "generalized Büchi is assumed), or an arbitrary acceptance formula "
      "such as 'Fin(0)|Inf(1)&Fin(2)' in the same syntax as in the HOA "
      "format, or one of the following patterns:\n"
      "  none\n"
      "  all\n"
      "  Buchi\n"
      "  co-Buchi\n"
      "  generalized-Buchi RANGE\n"
      "  generalized-co-Buchi RANGE\n"
      "  Rabin RANGE\n"
      "  Streett RANGE\n"
      "  generalized-Rabin INT RANGE RANGE ... RANGE\n"
      "  parity (min|max|rand) (odd|even|rand) RANGE\n"
      "  random RANGE\n"
      "  random RANGE PROBABILITY\n"
      "The random acceptance condition uses each set only once, "
      "unless a probability (to reuse the set again every time it is used) "
      "is given.", 2 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
121
    /**************************************************/
122
    { 0, 0, 0, 0, "Miscellaneous options:", -1 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
123
124
125
126
127
128
    { 0, 0, 0, 0, 0, 0 }
  };


static const struct argp_child children[] =
  {
129
130
    { &aoutput_argp, 0, 0, 3 },
    { &aoutput_o_format_argp, 0, 0, 4 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
131
132
133
134
    { &misc_argp, 0, 0, -1 },
    { 0, 0, 0, 0 }
  };

135
static const char* opt_acceptance = nullptr;
136
typedef spot::twa_graph::graph_t::trans_storage_t tr_t;
137
typedef std::set<std::vector<tr_t>> unique_aut_t;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
138
static spot::ltl::atomic_prop_set aprops;
139
static range ap_count_given = {-1, -2}; // Must be two different negative val
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
140
static int opt_seed = 0;
141
static const char* opt_seed_str = "0";
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
142
143
144
static int opt_automata = 1;
static range opt_states = { 10, 10 };
static float opt_density = 0.2;
145
static range opt_acc_sets = { -1, 0 };
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
146
147
148
static float opt_acc_prob = 0.2;
static bool opt_deterministic = false;
static bool opt_state_acc = false;
149
static bool opt_colored = false;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
150
static bool ba_wanted = false;
151
152
static bool generic_wanted = false;
static bool gba_wanted = false;
153
static std::unique_ptr<unique_aut_t> opt_uniq = nullptr;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
154
155
156
157
158
159
160
161

static void
ba_options()
{
  opt_acc_sets = { 1, 1 };
  opt_state_acc = true;
}

162
163
164
165
166
167
168
169
170
171
172
173
// Range should have the form 12..34 or 12:34, maybe with spaces.  The
// characters between '.' and ':' include all digits plus '/', but the
// parser will later choke on '/' if it is used, so let's not worry
// here.
static bool
looks_like_a_range(const char* str)
{
  while (*str == ' ' || (*str >= '.' && *str <= ':'))
    ++str;
  return !*str;
}

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
static int
parse_opt(int key, char* arg, struct argp_state* as)
{
  // This switch is alphabetically-ordered.
  switch (key)
    {
    case '8':
      spot::enable_utf8();
      break;
    case 'a':
      opt_acc_prob = to_float(arg);
      if (opt_acc_prob < 0.0 || opt_acc_prob > 1.0)
	error(2, 0, "probability of acceptance set membership "
	      "should be between 0.0 and 1.0");
      break;
    case 'A':
190
191
192
193
194
195
196
197
198
199
200
201
202
203
      if (looks_like_a_range(arg))
	{
	  opt_acc_sets = parse_range(arg);
	  if (opt_acc_sets.min > opt_acc_sets.max)
	    std::swap(opt_acc_sets.min, opt_acc_sets.max);
	  if (opt_acc_sets.min < 0)
	    error(2, 0, "number of acceptance sets should be positive");
	  gba_wanted = true;
	}
      else
	{
	  opt_acceptance = arg;
	  generic_wanted = true;
	}
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
      break;
    case 'B':
      ba_options();
      ba_wanted = true;
      break;
    case 'd':
      opt_density = to_float(arg);
      if (opt_density < 0.0 || opt_density > 1.0)
	error(2, 0, "density should be between 0.0 and 1.0");
      break;
    case 'D':
      opt_deterministic = true;
      break;
    case 'n':
      opt_automata = to_int(arg);
      break;
220
    case 'Q':
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
221
222
223
224
      opt_states = parse_range(arg);
      if (opt_states.min > opt_states.max)
	std::swap(opt_states.min, opt_states.max);
      break;
225
226
227
    case 'S':
      opt_state_acc = true;
      break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
228
229
230
231
    case 'u':
      opt_uniq =
        std::unique_ptr<unique_aut_t>(new std::set<std::vector<tr_t>>());
      break;
232
233
234
    case OPT_COLORED:
      opt_colored = true;
      break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
235
236
    case OPT_SEED:
      opt_seed = to_int(arg);
237
      opt_seed_str = arg;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
238
239
240
241
242
243
244
245
246
      break;
    case ARGP_KEY_ARG:
      // If this is the unique non-option argument, it can
      // be a number of atomic propositions to build.
      //
      // argp reorganizes argv[] so that options always come before
      // non-options.  So if as->argc == as->next we know this is the
      // last non-option argument, and if aprops.empty() we know this
      // is the also the first one.
247
      if (aprops.empty() && as->argc == as->next && looks_like_a_range(arg))
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
248
	{
249
250
251
252
253
	  ap_count_given = parse_range(arg);
	  // Create the set once if the count is fixed.
	  if (ap_count_given.min == ap_count_given.max)
	    aprops = spot::ltl::create_atomic_prop_set(ap_count_given.min);
	  break;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
254
	}
255
      aprops.insert(spot::ltl::default_environment::instance().require(arg));
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
256
257
258
259
260
261
262
263
264
265
266
267
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}


int
main(int argc, char** argv)
{
268
269
  strcpy(F_doc, "seed number");
  strcpy(L_doc, "automaton number");
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
270
271
272
273
274
275
276
277
278
279
  setup(argv);

  const argp ap = { options, parse_opt, "N|PROP...", argp_program_doc,
		    children, 0, 0 };

  if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, 0, 0))
    exit(err);

  // running 'randaut 0' is one way to generate automata using no
  // atomic propositions so do not complain in that case.
280
  if (aprops.empty() && ap_count_given.max < 0)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
281
282
283
    error(2, 0, "No atomic proposition supplied?   Run '%s --help' for usage.",
	  program_name);

284
285
286
287
288
  if (generic_wanted && automaton_format == Spin)
    error(2, 0, "--spin implies --ba so should not be used with --acceptance");
  if (generic_wanted && ba_wanted)
    error(2, 0, "--acceptance and --ba may not be used together");

289
  if (automaton_format == Spin && opt_acc_sets.max > 1)
290
    error(2, 0, "--spin is incompatible with --acceptance=%d..%d",
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
291
292
	  opt_acc_sets.min, opt_acc_sets.max);
  if (ba_wanted && opt_acc_sets.min != 1 && opt_acc_sets.max != 1)
293
    error(2, 0, "--ba is incompatible with --acceptance=%d..%d",
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
294
	  opt_acc_sets.min, opt_acc_sets.max);
295
296
297
298
299
  if (ba_wanted && generic_wanted)
    error(2, 0, "--ba is incompatible with --acceptance=%s", opt_acceptance);

  if (automaton_format == Spin)
    ba_options();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
300

301
302
303
304
305
306
307
308
309
310
  if (opt_colored && opt_acc_sets.min == -1 && !generic_wanted)
    error(2, 0, "--colored requires at least one acceptance set; "
	  "use --acceptance");
  if (opt_colored && opt_acc_sets.min == 0)
    error(2, 0, "--colored requires at least one acceptance set; "
	  "fix the range of --acceptance");

  if (opt_acc_sets.min == -1)
    opt_acc_sets.min = 0;

311
312
313
314
  try
    {
      spot::srand(opt_seed);
      auto d = spot::make_bdd_dict();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
315

316
      automaton_printer printer;
317

318
319
      constexpr unsigned max_trials = 10000;
      unsigned trials = max_trials;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
320

321
      int automaton_num = 0;
322

323
324
325
326
327
      for (;;)
	{
	  spot::stopwatch sw;
	  sw.start();

328
329
330
331
332
333
334
335
	  if (ap_count_given.max > 0
	      && ap_count_given.min != ap_count_given.max)
	    {
	      spot::ltl::destroy_atomic_prop_set(aprops);
	      int c = spot::rrand(ap_count_given.min, ap_count_given.max);
	      aprops = spot::ltl::create_atomic_prop_set(c);
	    }

336
337
338
339
340
341
342
343
	  int size = opt_states.min;
	  if (size != opt_states.max)
	    size = spot::rrand(size, opt_states.max);

	  int accs = opt_acc_sets.min;
	  if (accs != opt_acc_sets.max)
	    accs = spot::rrand(accs, opt_acc_sets.max);

344
345
346
347
348
	  spot::acc_cond::acc_code code;
	  if (opt_acceptance)
	    {
	      code = spot::parse_acc_code(opt_acceptance);
	      accs = code.used_sets().max_set();
349
350
351
	      if (opt_colored && accs == 0)
		error(2, 0, "--colored requires at least one acceptance set; "
		      "fix the range of --acceptance");
352
353
	    }

354
355
356
	  auto aut =
	    spot::random_graph(size, opt_density, &aprops, d,
			       accs, opt_acc_prob, 0.5,
357
358
			       opt_deterministic, opt_state_acc,
			       opt_colored);
359

360
361
	  if (opt_acceptance)
	    aut->set_acceptance(accs, code);
362

363
	  if (opt_uniq)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
364
	    {
365
	      auto tmp = spot::canonicalize
366
		(make_twa_graph(aut, spot::twa::prop_set::all()));
367
368
369
370
371
372
373
374
375
376
377
	      std::vector<tr_t> trans(tmp->transition_vector().begin() + 1,
				      tmp->transition_vector().end());
	      if (!opt_uniq->emplace(trans).second)
		{
		  --trials;
		  if (trials == 0)
		    error(2, 0, "failed to generate a new unique automaton"
			  " after %d trials", max_trials);
		  continue;
		}
	      trials = max_trials;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
378
	    }
379

380
	  auto runtime = sw.stop();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
381

382
383
	  printer.print(aut, nullptr,
			opt_seed_str, automaton_num, runtime, nullptr);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
384

385
386
387
388
389
390
391
392
393
	  ++automaton_num;
	  if (opt_automata > 0 && automaton_num >= opt_automata)
	    break;
	}
    }
  catch (const std::runtime_error& e)
    {
      error(2, 0, "%s", e.what());
    }
394
  spot::ltl::destroy_atomic_prop_set(aprops);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
395
}