minimize.cc 17.9 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2010, 2011, 2012, 2013, 2014 Laboratoire de Recherche
// et Développement de l'Epita (LRDE).
4
5
6
7
8
//
// 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
9
// the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
// (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
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19

20
21
22
23
24
25
26
27
28

//#define TRACE

#ifdef TRACE
#  define trace std::cerr
#else
#  define trace while (0) std::cerr
#endif

29
#include <queue>
30
31
32
#include <deque>
#include <set>
#include <list>
33
#include <vector>
34
#include <sstream>
35
36
37
#include "minimize.hh"
#include "ltlast/allnodes.hh"
#include "misc/hash.hh"
38
#include "misc/bddlt.hh"
39
#include "tgba/tgbaproduct.hh"
40
#include "tgbaalgos/powerset.hh"
41
42
43
#include "tgbaalgos/gtec/gtec.hh"
#include "tgbaalgos/safety.hh"
#include "tgbaalgos/sccfilter.hh"
44
#include "tgbaalgos/scc.hh"
45
#include "tgbaalgos/ltl2tgba_fm.hh"
46
#include "tgbaalgos/bfssteps.hh"
47
#include "tgbaalgos/isdet.hh"
48
#include "tgbaalgos/dtgbacomp.hh"
49
50
51

namespace spot
{
52
53
  // FIXME: do we really want to use unordered_set instead of set here?
  // This calls for benchmarking.
54
55
56
57
  typedef std::unordered_set<const state*,
			     state_ptr_hash, state_ptr_equal> hash_set;
  typedef std::unordered_map<const state*, unsigned,
			     state_ptr_hash, state_ptr_equal> hash_map;
58

59
60
61
  namespace
  {
    static std::ostream&
62
63
64
    dump_hash_set(const hash_set* hs,
		  const const_tgba_ptr& aut,
		  std::ostream& out)
65
    {
66
      out << '{';
67
68
69
70
71
72
      const char* sep = "";
      for (hash_set::const_iterator i = hs->begin(); i != hs->end(); ++i)
	{
	  out << sep << aut->format_state(*i);
	  sep = ", ";
	}
73
      out << '}';
74
75
76
77
      return out;
    }

    static std::string
78
    format_hash_set(const hash_set* hs, const_tgba_ptr aut)
79
80
81
82
83
84
85
    {
      std::ostringstream s;
      dump_hash_set(hs, aut, s);
      return s.str();
    }
  }

86
  // Find all states of an automaton.
87
  void build_state_set(const const_tgba_ptr& a, hash_set* seen)
88
  {
Felix Abecassis's avatar
Felix Abecassis committed
89
    std::queue<const state*> tovisit;
90
    // Perform breadth-first traversal.
Felix Abecassis's avatar
Felix Abecassis committed
91
    const state* init = a->get_init_state();
92
    tovisit.push(init);
93
    seen->insert(init);
94
95
    while (!tovisit.empty())
      {
96
97
98
99
	const state* src = tovisit.front();
	tovisit.pop();

	for (auto sit: a->succ(src))
100
	  {
101
102
103
104
105
106
107
108
109
110
	    const state* dst = sit->current_state();
	    // Is it a new state ?
	    if (seen->find(dst) == seen->end())
	      {
		// Register the successor for later processing.
		tovisit.push(dst);
		seen->insert(dst);
	      }
	    else
	      dst->destroy();
111
	  }
112
113
114
115
116
      }
  }

  // From the base automaton and the list of sets, build the minimal
  // resulting automaton
117
118
119
  tgba_digraph_ptr build_result(const const_tgba_ptr& a,
				std::list<hash_set*>& sets,
				hash_set* final)
120
  {
121
    auto dict = a->get_dict();
122
    auto res = make_tgba_digraph(dict);
123
    res->copy_ap_of(a);
124
    res->prop_state_based_acc();
125

126
127
128
129
130
131
    // For each set, create a state in the resulting automaton.
    // For a state s, state_num[s] is the number of the state in the minimal
    // automaton.
    hash_map state_num;
    std::list<hash_set*>::iterator sit;
    for (sit = sets.begin(); sit != sets.end(); ++sit)
132
133
134
      {
	hash_set::iterator hit;
	hash_set* h = *sit;
135
	unsigned num = res->new_state();
136
137
138
	for (hit = h->begin(); hit != h->end(); ++hit)
	  state_num[*hit] = num;
      }
139

140
141
    // For each transition in the initial automaton, add the corresponding
    // transition in res.
142

143
    if (!final->empty())
144
      res->set_single_acceptance_set();
145

146
    for (sit = sets.begin(); sit != sets.end(); ++sit)
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
      {
	hash_set::iterator hit;
	hash_set* h = *sit;

	// Pick one state.
	const state* src = *h->begin();
	unsigned src_num = state_num[src];
	bool accepting = (final->find(src) != final->end());

	// Connect it to all destinations.
	for (auto succit: a->succ(src))
	  {
	    const state* dst = succit->current_state();
	    hash_map::const_iterator i = state_num.find(dst);
	    dst->destroy();
	    if (i == state_num.end()) // Ignore useless destinations.
	      continue;
164
165
	    res->new_acc_transition(src_num, i->second,
				    succit->current_condition(), accepting);
166
167
	  }
      }
168
    res->merge_transitions();
169
170
171
172
173
174
175
    if (res->num_states() > 0)
      {
	const state* init_state = a->get_init_state();
	unsigned init_num = state_num[init_state];
	init_state->destroy();
	res->set_init_state(init_num);
      }
176
177
178
    return res;
  }

179
180
181
182
183
184

  namespace
  {

    struct wdba_search_acc_loop : public bfs_steps
    {
185
      wdba_search_acc_loop(const const_tgba_ptr& det_a,
186
187
188
189
			   unsigned scc_n, scc_map& sm,
			   power_map& pm, const state* dest)
	: bfs_steps(det_a), scc_n(scc_n), sm(sm), pm(pm), dest(dest)
      {
190
	seen(dest);
191
192
193
194
195
      }

      virtual const state*
      filter(const state* s)
      {
196
	s = seen(s);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
	if (sm.scc_of_state(s) != scc_n)
	  return 0;
	return s;
      }

      virtual bool
      match(tgba_run::step&, const state* to)
      {
	return to == dest;
      }

      unsigned scc_n;
      scc_map& sm;
      power_map& pm;
      const state* dest;
212
      state_unicity_table seen;
213
214
215
216
    };


    bool
217
218
219
    wdba_scc_is_accepting(const const_tgba_digraph_ptr& det_a, unsigned scc_n,
			  const const_tgba_ptr& orig_a, scc_map& sm,
			  power_map& pm)
220
    {
221

222
223
224
225
226
227
228
229
230
231
232
      // Get some state from the SCC #n.
      const state* start = sm.one_state_of(scc_n)->clone();

      // Find a loop around START in SCC #n.
      wdba_search_acc_loop wsal(det_a, scc_n, sm, pm, start);
      tgba_run::steps loop;
      const state* reached = wsal.search(start, loop);
      assert(reached == start);
      (void)reached;

      // Build an automaton representing this loop.
233
      auto loop_a = make_tgba_digraph(det_a->get_dict());
234
235
      tgba_run::steps::const_iterator i;
      int loop_size = loop.size();
236
      loop_a->new_states(loop_size);
237
238
239
      int n;
      for (n = 1, i = loop.begin(); n < loop_size; ++n, ++i)
	{
240
	  loop_a->new_transition(n - 1, n, i->label);
241
	  i->s->destroy();
242
243
	}
      assert(i != loop.end());
244
      loop_a->new_transition(n - 1, 0, i->label);
245
      i->s->destroy();
246
247
      assert(++i == loop.end());

248
249
      loop_a->set_init_state(0U);
      const state* loop_a_init = loop_a->get_init_state();
250
251
252
253
254

      // Check if the loop is accepting in the original automaton.
      bool accepting = false;

      // Iterate on each original state corresponding to start.
255
256
257
      const power_map::power_state& ps =
	pm.states_of(det_a->state_number(start));
      for (auto& it: ps)
258
259
260
	{
	  // Contrustruct a product between
	  // LOOP_A, and ORIG_A starting in *IT.
261
	  // FIXME: This could be sped up a lot!
262
	  if (!product_at(loop_a, orig_a, loop_a_init, it)->is_empty())
263
264
265
266
	    {
	      accepting = true;
	      break;
	    }
267
268
	}

269
      loop_a_init->destroy();
270
271
272
273
274
      return accepting;
    }

  }

275
276
  tgba_digraph_ptr minimize_dfa(const const_tgba_digraph_ptr& det_a,
				hash_set* final, hash_set* non_final)
277
  {
278
279
280
    typedef std::list<hash_set*> partition_t;
    partition_t cur_run;
    partition_t next_run;
281

282
283
    // The list of equivalent states.
    partition_t done;
284

285
    hash_map state_set_map;
286

287
288
    // Size of det_a
    unsigned size = final->size() + non_final->size();
289
290
    // Use bdd variables to number sets.  set_num is the first variable
    // available.
291
292
    unsigned set_num =
      det_a->get_dict()->register_anonymous_variables(size, det_a);
293
294
295
296
297
298

    std::set<int> free_var;
    for (unsigned i = set_num; i < set_num + size; ++i)
      free_var.insert(i);
    std::map<int, int> used_var;

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
299
300
    hash_set* final_copy;

301
302
303
304
305
306
    if (!final->empty())
      {
	unsigned s = final->size();
	used_var[set_num] = s;
	free_var.erase(set_num);
	if (s > 1)
307
	  cur_run.push_back(final);
308
309
310
311
312
	else
	  done.push_back(final);
	for (hash_set::const_iterator i = final->begin();
	     i != final->end(); ++i)
	  state_set_map[*i] = set_num;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
313
314

	final_copy = new hash_set(*final);
315
      }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
316
317
318
319
320
    else
      {
	final_copy = final;
      }

321
322
323
324
325
326
327
    if (!non_final->empty())
      {
	unsigned s = non_final->size();
	unsigned num = set_num + 1;
	used_var[num] = s;
	free_var.erase(num);
	if (s > 1)
328
	  cur_run.push_back(non_final);
329
330
331
332
333
334
	else
	  done.push_back(non_final);
	for (hash_set::const_iterator i = non_final->begin();
	     i != non_final->end(); ++i)
	  state_set_map[*i] = num;
      }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
335
336
337
338
    else
      {
	delete non_final;
      }
339

340
341
    // A bdd_states_map is a list of formulae (in a BDD form) associated with a
    // destination set of states.
342
343
344
345
346
    typedef std::map<bdd, hash_set*, bdd_less_than> bdd_states_map;

    bool did_split = true;

    while (did_split)
347
      {
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
	did_split = false;
	while (!cur_run.empty())
	  {
	    // Get a set to process.
	    hash_set* cur = cur_run.front();
	    cur_run.pop_front();

	    trace << "processing " << format_hash_set(cur, det_a) << std::endl;

	    hash_set::iterator hi;
	    bdd_states_map bdd_map;
	    for (hi = cur->begin(); hi != cur->end(); ++hi)
	      {
		const state* src = *hi;
		bdd f = bddfalse;
363
		for (auto si: det_a->succ(src))
364
365
		  {
		    const state* dst = si->current_state();
366
		    hash_map::const_iterator i = state_set_map.find(dst);
367
		    dst->destroy();
368
369
370
371
372
373
374
375
376
		    if (i == state_set_map.end())
		      // The destination state is not in our
		      // partition.  This can happen if the initial
		      // FINAL and NON_FINAL supplied to the algorithm
		      // do not cover the whole automaton (because we
		      // want to ignore some useless states).  Simply
		      // ignore these states here.
		      continue;
		    f |= (bdd_ithvar(i->second) & si->current_condition());
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
		  }

		// Have we already seen this formula ?
		bdd_states_map::iterator bsi = bdd_map.find(f);
		if (bsi == bdd_map.end())
		  {
		    // No, create a new set.
		    hash_set* new_set = new hash_set;
		    new_set->insert(src);
		    bdd_map[f] = new_set;
		  }
		else
		  {
		    // Yes, add the current state to the set.
		    bsi->second->insert(src);
		  }
	      }

	    bdd_states_map::iterator bsi = bdd_map.begin();
	    if (bdd_map.size() == 1)
	      {
		// The set was not split.
		trace << "set " << format_hash_set(bsi->second, det_a)
		      << " was not split" << std::endl;
		next_run.push_back(bsi->second);
	      }
	    else
	      {
405
		did_split = true;
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
		for (; bsi != bdd_map.end(); ++bsi)
		  {
		    hash_set* set = bsi->second;
		    // Free the number associated to these states.
		    unsigned num = state_set_map[*set->begin()];
		    assert(used_var.find(num) != used_var.end());
		    unsigned left = (used_var[num] -= set->size());
		    // Make sure LEFT does not become negative (hence bigger
		    // than SIZE when read as unsigned)
		    assert(left < size);
		    if (left == 0)
		      {
			used_var.erase(num);
			free_var.insert(num);
		      }
		    // Pick a free number
		    assert(!free_var.empty());
		    num = *free_var.begin();
		    free_var.erase(free_var.begin());
		    used_var[num] = set->size();
		    for (hash_set::iterator hit = set->begin();
			 hit != set->end(); ++hit)
		      state_set_map[*hit] = num;
		    // Trivial sets can't be splitted any further.
		    if (set->size() == 1)
		      {
			trace << "set " << format_hash_set(set, det_a)
			      << " is minimal" << std::endl;
			done.push_back(set);
		      }
		    else
		      {
			trace << "set " << format_hash_set(set, det_a)
			      << " should be processed further" << std::endl;
			next_run.push_back(set);
		      }
		  }
	      }
	    delete cur;
	  }
	if (did_split)
	  trace << "splitting did occur during this pass." << std::endl;
	else
	  trace << "splitting did not occur during this pass." << std::endl;
	std::swap(cur_run, next_run);
451
      }
452
453
454
455
456
457

    done.splice(done.end(), cur_run);

#ifdef TRACE
    trace << "Final partition: ";
    for (partition_t::const_iterator i = done.begin(); i != done.end(); ++i)
458
      trace << format_hash_set(*i, det_a) << ' ';
459
460
    trace << std::endl;
#endif
Felix Abecassis's avatar
Felix Abecassis committed
461
462

    // Build the result.
463
    auto res = build_result(det_a, done, final_copy);
Felix Abecassis's avatar
Felix Abecassis committed
464
465
466
467

    // Free all the allocated memory.
    delete final_copy;
    hash_map::iterator hit;
468
469
470
    for (hit = state_set_map.begin(); hit != state_set_map.end();)
      {
	hash_map::iterator old = hit++;
471
	old->first->destroy();
472
      }
Felix Abecassis's avatar
Felix Abecassis committed
473
474
475
476
    std::list<hash_set*>::iterator it;
    for (it = done.begin(); it != done.end(); ++it)
      delete *it;

477
478
    return res;
  }
479

480

481
  tgba_digraph_ptr minimize_monitor(const const_tgba_ptr& a)
482
483
  {
    hash_set* final = new hash_set;
484
    hash_set* non_final = new hash_set;
485
    tgba_digraph_ptr det_a;
486
487
488
489
490

    {
      power_map pm;
      det_a = tgba_powerset(a, pm);
    }
491
492

    // non_final contain all states.
493
    // final is empty: there is no acceptance condition
494
    build_state_set(det_a, non_final);
495
496
497
498
    auto res = minimize_dfa(det_a, final, non_final);
    res->prop_deterministic();
    res->prop_inherently_weak();
    return res;
499
500
  }

501
  tgba_digraph_ptr minimize_wdba(const const_tgba_ptr& a)
502
503
  {
    hash_set* final = new hash_set;
504
505
    hash_set* non_final = new hash_set;

506
    tgba_digraph_ptr det_a;
507
508
509
510
511

    {
      power_map pm;
      det_a = tgba_powerset(a, pm);

512
513
514
515
516
      // For each SCC of the deterministic automaton, determine if it
      // is accepting or not.

      // This corresponds to the algorithm in Fig. 1 of "Efficient
      // minimization of deterministic weak omega-automata" written by
517
      // Christof Löding and published in Information Processing
518
519
520
521
522
      // Letters 79 (2001) pp 105--109.

      // We also keep track of whether an SCC is useless
      // (i.e., it is not the start of any accepting word).

523
524
525
      scc_map sm(det_a);
      sm.build_map();
      unsigned scc_count = sm.scc_count();
526
527
      // SCC that have been marked as useless.
      std::vector<bool> useless(scc_count);
528
529
530
531
532
533
534
      // The "color".  Even number correspond to
      // accepting SCCs.
      std::vector<unsigned> d(scc_count);

      // An even number larger than scc_count.
      unsigned k = (scc_count | 1) + 1;

535
      // SCC are numbered in topological order
536
      // (but in the reverse order as Löding's)
537
      for (unsigned m = 0; m < scc_count; ++m)
538
	{
539
	  bool is_useless = true;
540
541
	  bool transient = sm.trivial(m);
	  const scc_map::succ_type& succ = sm.succ(m);
542

543
	  if (transient && succ.empty())
544
	    {
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
	      // A trivial SCC without successor is useless.
	      useless[m] = true;
	      d[m] = k - 1;
	      continue;
	    }

	  // Compute the minimum color l of the successors.
	  // Also SCCs are useless if all their successor are
	  // useless.
	  unsigned l = k;
	  for (scc_map::succ_type::const_iterator j = succ.begin();
	       j != succ.end(); ++j)
	    {
	      is_useless &= useless[j->first];
	      unsigned dj = d[j->first];
	      if (dj < l)
		l = dj;
	    }

	  if (transient)
	    {
	      d[m] = l;
567
568
569
570
	    }
	  else
	    {
	      // Regular SCCs are accepting if any of their loop
571
572
	      // corresponds to an accepted word in the original
	      // automaton.
573
	      if (wdba_scc_is_accepting(det_a, m, a, sm, pm))
574
575
		{
		  is_useless = false;
576
		  d[m] = l & ~1; // largest even number inferior or equal
577
578
579
		}
	      else
		{
580
		  d[m] = (l - 1) | 1; // largest odd number inferior or equal
581
		}
582
	    }
583

584
	  useless[m] = is_useless;
585

586
587
	  if (!is_useless)
	    {
588
	      hash_set* dest_set = (d[m] & 1) ? non_final : final;
589
	      const std::list<const state*>& l = sm.states_of(m);
590
591
592
593
	      std::list<const state*>::const_iterator il;
	      for (il = l.begin(); il != l.end(); ++il)
		dest_set->insert((*il)->clone());
	    }
594
595
596
	}
    }

597
598
599
600
    auto res = minimize_dfa(det_a, final, non_final);
    res->prop_deterministic();
    res->prop_inherently_weak();
    return res;
601
602
  }

603
604
605
606
  tgba_digraph_ptr
  minimize_obligation(const const_tgba_digraph_ptr& aut_f,
		      const ltl::formula* f,
		      const_tgba_digraph_ptr aut_neg_f,
607
		      bool reject_bigger)
608
  {
609
    auto min_aut_f = minimize_wdba(aut_f);
610

611
612
613
    if (reject_bigger)
      {
	// Abort if min_aut_f has more states than aut_f.
614
	unsigned orig_states = aut_f->num_states();
615
	if (orig_states < min_aut_f->num_states())
616
	  return std::const_pointer_cast<tgba_digraph>(aut_f);
617
618
      }

619
620
621
622
623
    // If the input automaton was already weak and deterministic, the
    // output is necessary correct.
    if (aut_f->is_inherently_weak() && aut_f->is_deterministic())
      return min_aut_f;

624
625
626
627
628
    // if f is a syntactic obligation formula, the WDBA minimization
    // must be correct.
    if (f && f->is_syntactic_obligation())
      return min_aut_f;

629
    // If aut_f is a guarantee automaton, the WDBA minimization must be
630
    // correct.
631
    if (is_guarantee_automaton(aut_f))
632
      return min_aut_f;
633
634
635
636

    // Build negation automaton if not supplied.
    if (!aut_neg_f)
      {
637
638
639
640
641
642
643
644
645
	if (f)
	  {
	    // If we know the formula, simply build the automaton for
	    // its negation.
	    const ltl::formula* neg_f =
	      ltl::unop::instance(ltl::unop::Not, f->clone());
	    aut_neg_f = ltl_to_tgba_fm(neg_f, aut_f->get_dict());
	    neg_f->destroy();
	    // Remove useless SCCs.
646
	    aut_neg_f = scc_filter(aut_neg_f, true);
647
648
649
650
651
	  }
	else if (is_deterministic(aut_f))
	  {
	    // If the automaton is deterministic, complementing is
	    // easy.
652
	    aut_neg_f = dtgba_complement(aut_f);
653
654
655
656
	  }
	else
	  {
	    // Otherwise, we cannot check if the minimization is safe.
657
	    return nullptr;
658
	  }
659
660
      }

661
    // If the negation is a guarantee automaton, then the
662
    // minimization is correct.
663
    if (is_guarantee_automaton(aut_neg_f))
664
665
666
667
668
669
      {
	return min_aut_f;
      }

    bool ok = false;

670
    if (product(min_aut_f, aut_neg_f)->is_empty())
671
      {
672
	// Complement the minimized WDBA.
673
674
	assert(min_aut_f->is_inherently_weak());
	auto neg_min_aut_f = dtgba_complement(min_aut_f);
675
676
677
678
	if (product(aut_f, neg_min_aut_f)->is_empty())
	  // Finally, we are now sure that it was safe
	  // to minimize the automaton.
	  ok = true;
679
680
681
682
      }

    if (ok)
      return min_aut_f;
683
    return std::const_pointer_cast<tgba_digraph>(aut_f);
684
  }
685
}