reductgba_sim.cc 15.5 KB
Newer Older
1
// Copyright (C) 2004, 2005, 2007  Laboratoire d'Informatique de Paris 6 (LIP6),
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
// 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 "reductgba_sim.hh"
#include "tgba/bddprint.hh"

namespace spot
{
  ///////////////////////////////////////////////////////////////////////
  // spoiler_node

  spoiler_node::spoiler_node(const state* d_node,
			     const state* s_node,
			     int num)
  {
    num_ = num;
    sc_ = new state_couple(d_node, s_node);
    lnode_succ = new sn_v;
    lnode_pred = new sn_v;
38
    not_win = false;
39
40
41
42
43
44
45
46
47
48
49
  }

  spoiler_node::~spoiler_node()
  {
    lnode_succ->clear();
    lnode_pred->clear();
    delete lnode_succ;
    delete lnode_pred;
    delete sc_;
  }

50
  bool
51
52
  spoiler_node::add_succ(spoiler_node* n)
  {
53
54
55
    bool exist = false;
    for (sn_v::iterator i = lnode_succ->begin();
	 i != lnode_succ->end(); ++i)
martinez's avatar
martinez committed
56
57
      if ((*i == n) ||
	  ((*i)->compare(n) == true))
58
59
60
	exist = true;
    if (exist)
      return false;
61

62
    lnode_succ->push_back(n);
martinez's avatar
martinez committed
63
    n->add_pred(this);
64
    return true;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  }

  void
  spoiler_node::del_succ(spoiler_node* n)
  {
    for (sn_v::iterator i = lnode_succ->begin();
	 i != lnode_succ->end();)
      {
	if (*i == n)
	  {
	    i = lnode_succ->erase(i);
	  }
	else
	  ++i;
      }
  }

  void
  spoiler_node::add_pred(spoiler_node* n)
  {
martinez's avatar
martinez committed
85
    lnode_pred->push_back(n);
86
87
88
89
90
91
92
93
94
95
  }

  void
  spoiler_node::del_pred()
  {
    for (sn_v::iterator i = lnode_pred->begin();
	 i != lnode_pred->end(); ++i)
      (*i)->del_succ(this);
  }

martinez's avatar
martinez committed
96
97
98
99
  bool
  spoiler_node::set_win()
  {
    bool change = not_win;
100
    for (std::vector<spoiler_node*>::iterator i = lnode_succ->begin();
martinez's avatar
martinez committed
101
102
103
104
105
	 i != lnode_succ->end(); ++i)
      {
	not_win |= (*i)->not_win;
      }
    if (change != not_win)
106
      for (std::vector<spoiler_node*>::iterator i = lnode_pred->begin();
martinez's avatar
martinez committed
107
108
109
110
111
112
	   i != lnode_pred->end(); ++i)
	(*i)->set_win();

    return change != not_win;
  }

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
  std::string
  spoiler_node::to_string(const tgba* a)
  {
    std::ostringstream os;
    os << num_
       << " [shape=box, label=\"("
       << a->format_state(sc_->first)
       << ", "
       << a->format_state(sc_->second)
       << ")\"]"
       << std::endl;
    return os.str();
  }

  std::string
  spoiler_node::succ_to_string()
  {
    std::ostringstream os;
    sn_v::iterator i;
    for (i = lnode_succ->begin(); i != lnode_succ->end(); ++i)
      {
	os << num_ << " -> " << (*i)->num_ << std::endl;
      }
    return os.str();
  }

139
140
141
142
143
144
145
  bool
  spoiler_node::compare(spoiler_node* n)
  {
    return (((sc_->first)->compare((n->get_pair())->first) == 0) &&
	    ((sc_->second)->compare((n->get_pair())->second) == 0));
  }

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
  int
  spoiler_node::get_nb_succ()
  {
    return lnode_succ->size();
  }

  const state*
  spoiler_node::get_spoiler_node()
  {
    return sc_->first;
  }

  const state*
  spoiler_node::get_duplicator_node()
  {
    return sc_->second;
  }

  state_couple*
  spoiler_node::get_pair()
  {
    return sc_;
  }

  ///////////////////////////////////////////////////////////////////////
  // duplicator_node

  duplicator_node::duplicator_node(const state* d_node,
				   const state* s_node,
				   bdd l,
				   bdd a,
				   int num)
    : spoiler_node(d_node, s_node, num),
      label_(l),
      acc_(a)
  {
  }

  duplicator_node::~duplicator_node()
  {
  }

  bool
  duplicator_node::set_win()
  {
    bool change = not_win;

    if (!this->get_nb_succ())
      not_win = true;
    else
      {
	not_win = true;
198
	for (std::vector<spoiler_node*>::iterator i = lnode_succ->begin();
199
200
201
202
203
	     i != lnode_succ->end(); ++i)
	  {
	    not_win &= (*i)->not_win;
	  }
      }
martinez's avatar
martinez committed
204
    if (change != not_win)
205
      for (std::vector<spoiler_node*>::iterator i = lnode_pred->begin();
martinez's avatar
martinez committed
206
207
	   i != lnode_pred->end(); ++i)
	(*i)->set_win();
208

209
    return change != not_win;
210
211
212
213
214
215
216
217
218
219
220
221
  }

  std::string
  duplicator_node::to_string(const tgba* a)
  {
    std::ostringstream os;
    os << num_
       << " [shape=box, label=\"("
       << a->format_state(sc_->first)
       << ", "
       << a->format_state(sc_->second)
       << ", ";
222
223
    bdd_print_acc(os, a->get_dict(), label_);
    os << ", ";
224
225
226
227
228
229
    bdd_print_acc(os, a->get_dict(), acc_);
    os << ")\"]"
       << std::endl;
    return os.str();
  }

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  bool
  duplicator_node::compare(spoiler_node* n)
  {
    return (this->spoiler_node::compare(n) &&
	    (label_ == dynamic_cast<duplicator_node*>(n)->get_label()) &&
	    (acc_ == dynamic_cast<duplicator_node*>(n)->get_acc()));
  }

  bdd
  duplicator_node::get_label() const
  {
    return label_;
  }

  bdd
  duplicator_node::get_acc() const
  {
    return acc_;
  }

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
  bool
  duplicator_node::match(bdd l, bdd a)
  {
    return ((l == label_) && (a == acc_));
  }

  bool
  duplicator_node::implies(bdd l, bdd a)
  {
    // if (a | !b) == true then (a => b).
    return (((l | !label_) == bddtrue) &&
	    ((a | !acc_) == bddtrue));
  }

  ///////////////////////////////////////////////////////////////////////
  // parity_game_graph

  void
  parity_game_graph::start()
  {
  }

  void
  parity_game_graph::end()
  {
  }

  void
  parity_game_graph::process_state(const state* s,
279
280
				   int,
				   tgba_succ_iterator*)
281
282
283
284
285
  {
    tgba_state_.push_back(s);
  }

  void
286
287
288
  parity_game_graph::process_link(int,
				  int,
				  const tgba_succ_iterator*)
289
290
291
292
293
294
  {
  }

  void
  parity_game_graph::print(std::ostream& os)
  {
295
296
    std::vector<spoiler_node*>::iterator i1;
    std::vector<duplicator_node*>::iterator i2;
297
298
299
300
301
302
303
304
305
306
307
308

    int n = 0;

    os << "digraph G {" << std::endl;

    os << "{" << std::endl
       << "rank = same;" << std::endl
       << "node [color=red];" << std::endl;
    for (i1 = spoiler_vertice_.begin();
	 i1 != spoiler_vertice_.end(); ++i1)
      {
	os << (*i1)->to_string(automata_);
309
	++n;
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
	if (n > 20)
	  {
	    n = 0;
	    os << "}" << std::endl << std::endl
	       << "{" << std::endl
	       << "rank = same" << std::endl
	       << "node [color=red];" << std::endl;
	  }
      }
    os << "}" << std::endl;

    n = 0;
    os << "{" << std::endl
       << "rank = same;" << std::endl
       << "node [color=green];" << std::endl;
    for (i2 = duplicator_vertice_.begin();
	 i2 != duplicator_vertice_.end(); ++i2)
      {
	os << (*i2)->to_string(automata_);
329
	++n;
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
	if (n > 20)
	  {
	    n = 0;
	    os << "}" << std::endl << std::endl
	       << "{" << std::endl
	       << "rank = same" << std::endl
	       << "node [color=green];" << std::endl;
	  }
      }
    os << "}" << std::endl << std::endl;

    os << "edge [color=red];" << std::endl;
    for (i1 = spoiler_vertice_.begin();
	 i1 != spoiler_vertice_.end(); ++i1)
      {
	os << (*i1)->succ_to_string();
      }

    os << std::endl
       << "edge [color=green];" << std::endl;
    for (i2 = duplicator_vertice_.begin();
	 i2 != duplicator_vertice_.end(); ++i2)
      {
	os << (*i2)->succ_to_string();
      }

    os << "}" << std::endl;

  }

  parity_game_graph::~parity_game_graph()
  {
362
363
    std::vector<spoiler_node*>::iterator i1;
    std::vector<duplicator_node*>::iterator i2;
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

    for (i1 = spoiler_vertice_.begin();
	 i1 != spoiler_vertice_.end(); ++i1)
      {
	delete *i1;
      }

    for (i2 = duplicator_vertice_.begin();
	 i2 != duplicator_vertice_.end(); ++i2)
      {
	delete *i2;
      }

    spoiler_vertice_.clear();
    duplicator_vertice_.clear();
  }

  parity_game_graph::parity_game_graph(const tgba* a)
    : tgba_reachable_iterator_breadth_first(a)
  {
    this->run();
    nb_node_parity_game = 0;
  }

  ///////////////////////////////////////////////////////////////////////
  // parity_game_graph_direct

  void
martinez's avatar
martinez committed
392
  parity_game_graph_direct::build_graph()
393
  {
394
    tgba_succ_iterator* si = 0;
395
    typedef std::pair<bdd, bdd> couple_bdd;
396
    couple_bdd *p = 0;
397
    std::vector<couple_bdd*>* trans = 0;
398
    bool exist = false;
399
    spot::state* s = 0;
400

401
    for (std::vector<const state*>::iterator i = tgba_state_.begin();
402
403
404
405
	 i != tgba_state_.end(); ++i)
      {

	// spoiler node are all state couple (i,j)
406
	for (std::vector<const state*>::iterator j = tgba_state_.begin();
407
408
409
410
411
412
413
414
415
416
	     j != tgba_state_.end(); ++j)
	  {
	    spoiler_node* n1 = new spoiler_node(*i,
						*j,
						nb_node_parity_game++);
	    spoiler_vertice_.push_back(n1);
	  }

	// duplicator node are all state couple where
	// the first state i are reachable.
417
418
	trans = new std::vector<couple_bdd*>;
        for (std::vector<const state*>::iterator j = tgba_state_.begin();
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
	     j != tgba_state_.end(); ++j)
	  {
	    si = automata_->succ_iter(*j);
	    for (si->first(); !si->done(); si->next())
	      {

		// if there exist a predecessor of i named j
		s = si->current_state();
		if (s->compare(*i) == 0)
		  {

		    // p is the label of the transition j->i
		    p = new couple_bdd(si->current_condition(),
				       si->current_acceptance_conditions());

		    // If an other predecessor of i has the same label p
		    // to reach i, then we don't compute the duplicator node.
		    exist = false;
437
		    for (std::vector<couple_bdd*>::iterator v
438
439
440
441
442
443
444
445
446
447
448
449
450
			   = trans->begin();
			 v != trans->end(); ++v)
		      {
			if ((si->current_condition() == (*v)->first) &&
			    (si->current_acceptance_conditions()
			     == (*v)->second))
			  exist = true;
		      }

		    if (!exist)
		      {
			// We build all the state couple with the label p.
			trans->push_back(p);
451
			for (std::vector<const state*>::iterator s
452
453
454
455
			       = tgba_state_.begin();
			     s != tgba_state_.end(); ++s)
			  {
			    duplicator_node* n2
456
457
458
459
460
461
462
			      = new
			      duplicator_node(*i,
					      *s,
					      si->current_condition(),
					      si
					      ->current_acceptance_conditions(),
					      nb_node_parity_game++);
463
464
465
466
467
468
469
470
471
472
			    duplicator_vertice_.push_back(n2);
			  }
		      }
		    else
		      delete p;
		  }
		delete s;
	      }
	    delete si;
	  }
473
	std::vector<couple_bdd*>::iterator i2;
474
475
476
477
478
479
480
481
482
483
484
485
486
	for (i2 = trans->begin(); i2 != trans->end(); ++i2)
	  {
	    delete *i2;
	  }
	delete trans;
      }
  }

  void
  parity_game_graph_direct::build_link()
  {
    int nb_ds = 0;
    int nb_sd = 0;
487
    spot::state* s = 0;
488
489

    // for each couple of (spoiler, duplicator)
490
    for (std::vector<spoiler_node*>::iterator i
491
492
	   = spoiler_vertice_.begin(); i != spoiler_vertice_.end(); ++i)
      {
493
	for (std::vector<duplicator_node*>::iterator j
494
495
496
497
	       = duplicator_vertice_.begin();
	     j != duplicator_vertice_.end(); ++j)
	  {
	    // We add a link between a duplicator and a spoiler.
498
499
	    if ((*j)->get_spoiler_node()
		->compare((*i)->get_spoiler_node()) == 0)
500
501
502
503
504
505
506
507
508
509
510
	      {
		tgba_succ_iterator* si
		  = automata_->succ_iter((*j)->get_duplicator_node());
		for (si->first(); !si->done(); si->next())
		  {
		    s = si->current_state();
		    if ((s->compare((*i)->get_duplicator_node()) == 0) &&
			(*j)->implies(si->current_condition(),
				      si->current_acceptance_conditions()))
		      {
			(*j)->add_succ(*i);
511
			++nb_ds;
512
513
514
515
516
517
518
		      }
		    delete s;
		  }
		delete si;
	      }

	    // We add a link between a spoiler and a duplicator.
519
520
	    if ((*j)->get_duplicator_node()
		->compare((*i)->get_duplicator_node()) == 0)
521
522
523
524
525
526
527
528
529
530
531
	      {
		tgba_succ_iterator* si
		  = automata_->succ_iter((*i)->get_spoiler_node());
		for (si->first(); !si->done(); si->next())
		  {
		    s = si->current_state();
		    if ((s->compare((*j)->get_spoiler_node()) == 0) &&
			(*j)->match(si->current_condition(),
				    si->current_acceptance_conditions()))
		      {
			(*i)->add_succ(*j);
532
			++nb_sd;
533
534
535
536
537
538
539
540
541
		      }
		    delete s;
		  }
		delete si;
	      }
	  }
      }
  }

martinez's avatar
martinez committed
542
543
  void
  parity_game_graph_direct::lift()
544
545
546
547
548
549
  {
    bool change = true;

    while (change)
      {
	change = false;
550
	for (std::vector<duplicator_node*>::iterator i
551
552
553
554
555
	       = duplicator_vertice_.begin();
	     i != duplicator_vertice_.end(); ++i)
	  {
	    change |= (*i)->set_win();
	  }
556
	for (std::vector<spoiler_node*>::iterator i
557
558
559
560
561
562
563
564
	       = spoiler_vertice_.begin();
	     i != spoiler_vertice_.end(); ++i)
	  {
	    change |= (*i)->set_win();
	  }
      }
  }

martinez's avatar
martinez committed
565
  direct_simulation_relation*
566
567
  parity_game_graph_direct::get_relation()
  {
martinez's avatar
martinez committed
568
    direct_simulation_relation* rel = new direct_simulation_relation;
569
    state_couple* p = 0;
570
571
    seen_map::iterator j;

572
    for (std::vector<spoiler_node*>::iterator i
573
574
575
576
577
578
579
580
581
582
583
584
585
	   = spoiler_vertice_.begin();
	 i != spoiler_vertice_.end(); ++i)
      {
	if (!(*i)->not_win)
	  {
	    p = new state_couple((*i)->get_spoiler_node(),
				 (*i)->get_duplicator_node());
	    rel->push_back(p);


	    // We remove the state in rel from seen
	    // because the destructor of
	    // tgba_reachable_iterator_breadth_first
586
	    // delete all instance of state.
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606


   	    if ((j = seen.find(p->first)) != seen.end())
	      seen.erase(j);
	    if ((j = seen.find(p->second)) != seen.end())
	      seen.erase(j);

	  }
      }

    return rel;
  }

  parity_game_graph_direct::~parity_game_graph_direct()
  {
  }

  parity_game_graph_direct::parity_game_graph_direct(const tgba* a)
    : parity_game_graph(a)
  {
martinez's avatar
martinez committed
607
    this->build_graph();
608
    this->build_link();
martinez's avatar
martinez committed
609
    this->lift();
610
611
612
613
  }

  ///////////////////////////////////////////////////////////////////////

martinez's avatar
martinez committed
614
  direct_simulation_relation*
615
616
617
  get_direct_relation_simulation(const tgba* f,
				 std::ostream& os,
				 int opt)
618
619
  {
    parity_game_graph_direct* G = new parity_game_graph_direct(f);
martinez's avatar
martinez committed
620
    direct_simulation_relation* rel = G->get_relation();
621
    if (opt == 1)
622
      G->print(os);
623
624
625
626
627
    delete G;
    return rel;
  }

  void
martinez's avatar
martinez committed
628
  free_relation_simulation(direct_simulation_relation* rel)
629
  {
630
    if (rel == 0)
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
      return;

    Sgi::hash_map<const spot::state*, int,
      state_ptr_hash, state_ptr_equal> seen;
    Sgi::hash_map<const spot::state*, int,
      state_ptr_hash, state_ptr_equal>::iterator j;

    simulation_relation::iterator i;
    for (i = rel->begin(); i != rel->end(); ++i)
      {
	if ((j = seen.find((*i)->first)) == seen.end())
	    seen[(*i)->first] = 0;

	if ((j = seen.find((*i)->second)) == seen.end())
	    seen[(*i)->second] = 0;

	delete *i;
      }
    delete rel;
650
    rel = 0;
651
652
653
654
655

    for (j = seen.begin(); j != seen.end();)
      {
	const state* ptr = j->first;
	++j;
656
	delete ptr;
657
658
659
660
661
662
663
664
665
666
667
668
669
670
      }
  }

  bool
  is_include(const tgba*, const tgba*)
  {
    return false;
  }

  tgba*
  reduc_tgba_sim(const tgba* f, int opt)
  {
    spot::tgba_reduc* automatareduc = new  spot::tgba_reduc(f);

martinez's avatar
martinez committed
671
    if (opt & (Reduce_quotient_Dir_Sim | Reduce_transition_Dir_Sim))
672
      {
martinez's avatar
martinez committed
673
	direct_simulation_relation* rel
674
	  = get_direct_relation_simulation(automatareduc, std::cout);
675

676
677
	assert(rel);

678
	automatareduc->display_rel_sim(rel, std::cout);
martinez's avatar
martinez committed
679
680
	automatareduc->quotient_state(rel);
	automatareduc->delete_transitions(rel);
681
682
683
684

	free_relation_simulation(rel);
      }
    else
martinez's avatar
martinez committed
685
      if (opt & (Reduce_quotient_Del_Sim | Reduce_transition_Del_Sim))
686
	{
martinez's avatar
martinez committed
687
	  delayed_simulation_relation* rel
688
	    = get_delayed_relation_simulation(automatareduc, std::cout);
689

690
691
	  assert(rel);

692
	  automatareduc->display_rel_sim(rel, std::cout);
martinez's avatar
martinez committed
693
694
	  automatareduc->quotient_state(rel);
	  automatareduc->delete_transitions(rel);
695
696
697
698
699
700
701
702
703
704
705
706
707

	  free_relation_simulation(rel);
	}

    if (opt & Reduce_Scc)
      {
	automatareduc->prune_scc();
      }

    return automatareduc;
  }

}