dtgbasat.cc 25.3 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2013, 2014 Laboratoire de Recherche et Développement
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// de l'Epita.
//
// 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 <iostream>
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
21
#include <fstream>
22
23
24
25
26
27
28
29
30
31
#include <sstream>
#include "dtgbasat.hh"
#include "reachiter.hh"
#include <map>
#include <utility>
#include "scc.hh"
#include "tgba/bddprint.hh"
#include "ltlast/constant.hh"
#include "stats.hh"
#include "ltlenv/defaultenv.hh"
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
32
#include "misc/satsolver.hh"
33
#include "misc/timer.hh"
34
#include "isweakscc.hh"
35
#include "dotty.hh"
36

37
38
39
// If you set the SPOT_TMPKEEP environment variable the temporary
// file used to communicate with the sat solver will be left in
// the current directory.
40
//
41
42
43
44
// Additionally, if the following DEBUG macro is set to 1, the CNF
// file will be output with a comment before each clause, and an
// additional output file (dtgba-sat.dbg) will be created with a list
// of all positive variables in the result and their meaning.
45
46
47
48

#define DEBUG 0
#if DEBUG
#define dout out << "c "
49
#define trace std::cerr
50
#else
51
52
#define dout while (0) std::cout
#define trace dout
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
#endif

namespace spot
{
  namespace
  {
    static bdd_dict* debug_dict = 0;

    struct transition
    {
      int src;
      bdd cond;
      int dst;

      transition(int src, bdd cond, int dst)
	: src(src), cond(cond), dst(dst)
      {
      }

      bool operator<(const transition& other) const
      {
	if (this->src < other.src)
	  return true;
	if (this->src > other.src)
	  return false;
	if (this->dst < other.dst)
	  return true;
	if (this->dst > other.dst)
	  return false;
	return this->cond.id() < other.cond.id();
      }

      bool operator==(const transition& other) const
      {
	return (this->src == other.src
		&& this->dst == other.dst
		&& this->cond.id() == other.cond.id());
      }
    };

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    struct src_cond
    {
      int src;
      bdd cond;

      src_cond(int src, bdd cond)
	: src(src), cond(cond)
      {
      }

      bool operator<(const src_cond& other) const
      {
	if (this->src < other.src)
	  return true;
	if (this->src > other.src)
	  return false;
	return this->cond.id() < other.cond.id();
      }

      bool operator==(const src_cond& other) const
      {
	return (this->src == other.src
		&& this->cond.id() == other.cond.id());
      }
    };

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    struct transition_acc
    {
      int src;
      bdd cond;
      bdd acc;
      int dst;

      transition_acc(int src, bdd cond, bdd acc, int dst)
	: src(src), cond(cond), acc(acc), dst(dst)
      {
      }

      bool operator<(const transition_acc& other) const
      {
	if (this->src < other.src)
	  return true;
	if (this->src > other.src)
	  return false;
	if (this->dst < other.dst)
	  return true;
	if (this->dst > other.dst)
	  return false;
	if (this->cond.id() < other.cond.id())
	  return true;
	if (this->cond.id() > other.cond.id())
	  return false;
	return this->acc.id() < other.acc.id();
      }

      bool operator==(const transition_acc& other) const
      {
	return (this->src == other.src
		&& this->dst == other.dst
		&& this->cond.id() == other.cond.id()
		&& this->acc.id() == other.acc.id());
      }
    };

    struct path
    {
      int src_cand;
      int src_ref;
      int dst_cand;
      int dst_ref;
      bdd acc_cand;
      bdd acc_ref;

166
167
168
169
170
171
172
      path(int src_cand, int src_ref)
	: src_cand(src_cand), src_ref(src_ref),
	  dst_cand(src_cand), dst_ref(src_ref),
	  acc_cand(bddfalse), acc_ref(bddfalse)
      {
      }

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
      path(int src_cand, int src_ref,
	   int dst_cand, int dst_ref,
	   bdd acc_cand, bdd acc_ref)
	: src_cand(src_cand), src_ref(src_ref),
	  dst_cand(dst_cand), dst_ref(dst_ref),
	  acc_cand(acc_cand), acc_ref(acc_ref)
      {
      }

      bool operator<(const path& other) const
      {
	if (this->src_cand < other.src_cand)
	  return true;
	if (this->src_cand > other.src_cand)
	  return false;
	if (this->src_ref < other.src_ref)
	  return true;
	if (this->src_ref > other.src_ref)
	  return false;
	if (this->dst_cand < other.dst_cand)
	  return true;
	if (this->dst_cand > other.dst_cand)
	  return false;
	if (this->dst_ref < other.dst_ref)
	  return true;
	if (this->dst_ref > other.dst_ref)
	  return false;
	if (this->acc_ref.id() < other.acc_ref.id())
	  return true;
	if (this->acc_ref.id() > other.acc_ref.id())
	  return false;
	if (this->acc_cand.id() < other.acc_cand.id())
	  return true;
	if (this->acc_cand.id() > other.acc_cand.id())
	  return false;

	return false;
      }

    };

    std::ostream& operator<<(std::ostream& os, const transition& t)
    {
216
      os << '<' << t.src << ','
217
	 << bdd_format_formula(debug_dict, t.cond)
218
	 << ',' << t.dst << '>';
219
220
221
222
223
224
      return os;
    }


    std::ostream& operator<<(std::ostream& os, const transition_acc& t)
    {
225
226
      os << '<' << t.src << ','
	 << bdd_format_formula(debug_dict, t.cond) << ','
227
	 << bdd_format_accset(debug_dict, t.acc)
228
	 << ',' << t.dst << '>';
229
230
231
232
233
      return os;
    }

    std::ostream& operator<<(std::ostream& os, const path& p)
    {
234
235
236
237
      os << '<'
	 << p.src_cand << ','
	 << p.src_ref << ','
	 << p.dst_cand << ','
238
239
	 << p.dst_ref << ", "
	 << bdd_format_accset(debug_dict, p.acc_cand) << ", "
240
	 << bdd_format_accset(debug_dict, p.acc_ref) << '>';
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
      return os;
    }

    struct dict
    {
      dict(const tgba* a)
	: aut(a)
      {
      }

      const tgba* aut;
      typedef std::map<transition, int> trans_map;
      typedef std::map<transition_acc, int> trans_acc_map;
      trans_map transid;
      trans_acc_map transaccid;
      typedef std::map<int, transition> rev_map;
      typedef std::map<int, transition_acc> rev_acc_map;
      rev_map revtransid;
      rev_acc_map revtransaccid;

      std::map<path, int> pathid;
      int nvars;
263
264
265
      typedef std::unordered_map<const state*, int,
				 state_ptr_hash, state_ptr_equal> state_map;
      typedef std::unordered_map<int, const state*> int_map;
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
      state_map state_to_int;
      int_map int_to_state;
      int cand_size;
      unsigned int cand_nacc;
      std::vector<bdd> cand_acc; // size cand_nacc

      std::vector<bdd> all_cand_acc;
      std::vector<bdd> all_ref_acc;

      bdd cand_all_acc;
      bdd ref_all_acc;

      ~dict()
      {
	state_map::const_iterator s = state_to_int.begin();
	while (s != state_to_int.end())
	  // Always advance the iterator before deleting the key.
	  s++->first->destroy();

	aut->get_dict()->unregister_all_my_variables(this);
      }
    };


    class filler_dfs: public tgba_reachable_iterator_depth_first
    {
    protected:
      dict& d;
      int size_;
      bdd ap_;
296
      bool state_based_;
297
      scc_map& sm_;
298
    public:
299
300
      filler_dfs(const tgba* aut, dict& d, bdd ap, bool state_based,
		 scc_map& sm)
301
	: tgba_reachable_iterator_depth_first(aut), d(d), ap_(ap),
302
	  state_based_(state_based), sm_(sm)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
      {
	d.nvars = 0;

	bdd_dict* bd = aut->get_dict();
	ltl::default_environment& env = ltl::default_environment::instance();

	d.cand_acc.resize(d.cand_nacc);
	d.all_cand_acc.push_back(bddfalse);

	bdd allneg = bddtrue;
	for (unsigned n = 0; n < d.cand_nacc; ++n)
	  {
	    std::ostringstream s;
	    s << n;
	    const ltl::formula* af = env.require(s.str());
	    int v = bd->register_acceptance_variable(af, &d);
	    af->destroy();
	    d.cand_acc[n] = bdd_ithvar(v);
	    allneg &= bdd_nithvar(v);
	  }
	for (unsigned n = 0; n < d.cand_nacc; ++n)
	  {
	    bdd c = bdd_exist(allneg, d.cand_acc[n]) & d.cand_acc[n];
	    d.cand_acc[n] = c;

	    size_t s = d.all_cand_acc.size();
	    for (size_t i = 0; i < s; ++i)
	      d.all_cand_acc.push_back(d.all_cand_acc[i] | c);
	  }
	d.cand_all_acc = bdd_support(allneg);
	d.ref_all_acc = bdd_support(aut->all_acceptance_conditions());

	bdd refall = d.ref_all_acc;
	bdd refnegall = aut->neg_acceptance_conditions();

	d.all_ref_acc.push_back(bddfalse);
	while (refall != bddtrue)
	  {
	    bdd v = bdd_ithvar(bdd_var(refall));
	    bdd c = bdd_exist(refnegall, v) & v;

	    size_t s = d.all_ref_acc.size();
	    for (size_t i = 0; i < s; ++i)
	      d.all_ref_acc.push_back(d.all_ref_acc[i] | c);

	    refall = bdd_high(refall);
	  }
      }

      int size()
      {
	return size_;
      }

      void end()
      {
	size_ = seen.size();

	if (d.cand_size == -1)
	  d.cand_size = size_ - 1;

364
365
	for (dict::state_map::const_iterator i2 = seen.begin();
	     i2 != seen.end(); ++i2)
366
367
368
	  d.int_to_state[i2->second] = i2->first;

	for (int i = 1; i <= size_; ++i)
369
	  {
370
	    unsigned i_scc = sm_.scc_of_state(d.int_to_state[i]);
371
372
	    bool is_weak = is_weak_scc(sm_, i_scc);

373
374
	    for (int j = 1; j <= d.cand_size; ++j)
	      {
375
		for (int k = 1; k <= size_; ++k)
376
		  {
377
		    if (sm_.scc_of_state(d.int_to_state[k]) != i_scc)
378
379
380
		      continue;
		    for (int l = 1; l <= d.cand_size; ++l)
		      {
381
382
			size_t sfp = is_weak ? 1 : d.all_ref_acc.size();
			for (size_t fp = 0; fp < sfp; ++fp)
383
			  {
384
385
			    size_t sf = d.all_cand_acc.size();
			    for (size_t f = 0; f < sf; ++f)
386
387
388
389
390
391
392
393
394
395
			      {
				path p(j, i, l, k,
				       d.all_cand_acc[f],
				       d.all_ref_acc[fp]);
				d.pathid[p] = ++d.nvars;
			      }

			  }
		      }
		  }
396
397
398
399
400
	      }
	  }

	std::swap(d.state_to_int, seen);

401
402
403
404
	if (!state_based_)
	  {
	    for (int i = 1; i <= d.cand_size; ++i)
	      for (int j = 1; j <= d.cand_size; ++j)
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
		  bdd all = bddtrue;
		  while (all != bddfalse)
		    {
		      bdd one = bdd_satoneset(all, ap_, bddfalse);
		      all -= one;

		      transition t(i, one, j);
		      d.transid[t] = ++d.nvars;
		      d.revtransid.insert(dict::rev_map::
					  value_type(d.nvars, t));

		      // Create the variable for the accepting transition
		      // immediately afterwards.  It helps parsing the
		      // result.
		      for (unsigned n = 0; n < d.cand_nacc; ++n)
			{
			  transition_acc ta(i, one, d.cand_acc[n], j);
			  d.transaccid[ta] = ++d.nvars;
			  d.revtransaccid.insert(dict::rev_acc_map::
						 value_type(d.nvars, ta));
			}
		    }
		}
	  }
	else // state based
	  {
	    for (int i = 1; i <= d.cand_size; ++i)
	      for (unsigned n = 0; n < d.cand_nacc; ++n)
		{
		  ++d.nvars;
		  for (int j = 1; j <= d.cand_size; ++j)
		    {
		      bdd all = bddtrue;
		      while (all != bddfalse)
			{
			  bdd one = bdd_satoneset(all, ap_, bddfalse);
			  all -= one;
443

444
445
446
447
448
449
450
451
452
453
454
455
			  transition_acc ta(i, one, d.cand_acc[n], j);
			  d.transaccid[ta] = d.nvars;
			  d.revtransaccid.insert(dict::rev_acc_map::
						 value_type(d.nvars, ta));
			}
		    }
		}
	    for (int i = 1; i <= d.cand_size; ++i)
	      for (int j = 1; j <= d.cand_size; ++j)
		{
		  bdd all = bddtrue;
		  while (all != bddfalse)
456
		    {
457
458
459
460
461
462
463
		      bdd one = bdd_satoneset(all, ap_, bddfalse);
		      all -= one;

		      transition t(i, one, j);
		      d.transid[t] = ++d.nvars;
		      d.revtransid.insert(dict::rev_map::
					  value_type(d.nvars, t));
464
465
		    }
		}
466
	  }
467
468
469
      }
    };

470
471
    typedef std::pair<int, int> sat_stats;

472
    static
473
474
    sat_stats dtgba_to_sat(std::ostream& out, const tgba* ref,
			   dict& d, bool state_based)
475
    {
476
      clause_counter nclauses;
477
478
479
480
481
482
      int ref_size = 0;

      scc_map sm(ref);
      sm.build_map();
      bdd ap = sm.aprec_set_of(sm.initial());

483
484
485
486
487
488
489
490
491
492
493
      // Count the number of atomic propositions
      int nap = 0;
      {
	bdd cur = ap;
	while (cur != bddtrue)
	  {
	    ++nap;
	    cur = bdd_high(cur);
	  }
	nap = 1 << nap;
      }
494
495
496

      // Number all the SAT variable we may need.
      {
497
	filler_dfs f(ref, d, ap, state_based, sm);
498
499
500
501
502
503
504
505
	f.run();
	ref_size = f.size();
      }

      // empty automaton is impossible
      if (d.cand_size == 0)
	{
	  out << "p cnf 1 2\n-1 0\n1 0\n";
506
	  return std::make_pair(1, 2);
507
508
509
510
511
	}

      // An empty line for the header
      out << "                                                 \n";

512
513
#if DEBUG
      debug_dict = ref->get_dict();
514
515
      dout << "ref_size: " << ref_size << '\n';
      dout << "cand_size: " << d.cand_size << '\n';
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#endif

      dout << "symmetry-breaking clauses\n";
      int j = 0;
      bdd all = bddtrue;
      while (all != bddfalse)
 	{
 	  bdd s = bdd_satoneset(all, ap, bddfalse);
 	  all -= s;
 	  for (int i = 1; i < d.cand_size; ++i)
 	    for (int k = (i - 1) * nap + j + 3; k <= d.cand_size; ++k)
	      {
		transition t(i, s, k);
		int ti = d.transid[t];
530
		dout << "¬" << t << '\n';
531
532
533
534
535
		out << -ti << " 0\n";
		++nclauses;
	      }
 	  ++j;
 	}
536
      if (!nclauses.nb_clauses())
537
538
 	dout << "(none)\n";

539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
      dout << "(8) the candidate automaton is complete\n";
      for (int q1 = 1; q1 <= d.cand_size; ++q1)
	{
	  bdd all = bddtrue;
	  while (all != bddfalse)
	    {
	      bdd s = bdd_satoneset(all, ap, bddfalse);
	      all -= s;

#if DEBUG
	      dout;
	      for (int q2 = 1; q2 <= d.cand_size; q2++)
		{
		  transition t(q1, s, q2);
		  out << t << "δ";
		  if (q2 != d.cand_size)
		    out << " ∨ ";
		}
557
	      out << '\n';
558
559
560
561
562
563
564
#endif

	      for (int q2 = 1; q2 <= d.cand_size; q2++)
		{
		  transition t(q1, s, q2);
		  int ti = d.transid[t];

565
		  out << ti << ' ';
566
567
568
569
570
571
572
		}
	      out << "0\n";
	      ++nclauses;
	    }
	}

      dout << "(9) the initial state is reachable\n";
573
      dout << path(1, 1) << '\n';
574
      out << d.pathid[path(1, 1)] << " 0\n";
575
576
      ++nclauses;

577
578
      for (int q1 = 1; q1 <= d.cand_size; ++q1)
	for (int q1p = 1; q1p <= ref_size; ++q1p)
579
580
581
	{
	  dout << "(10) augmenting paths based on Cand[" << q1
	       << "] and Ref[" << q1p << "]\n";
582
583
584
	  path p1(q1, q1p);
	  int p1id = d.pathid[p1];

585
	  for (auto it: ref->succ(d.int_to_state[q1p]))
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
	    {
	      const state* dps = it->current_state();
	      int dp = d.state_to_int[dps];
	      dps->destroy();

	      bdd all = it->current_condition();
	      while (all != bddfalse)
		{
		  bdd s = bdd_satoneset(all, ap, bddfalse);
		  all -= s;

		  for (int q2 = 1; q2 <= d.cand_size; q2++)
		    {
		      transition t(q1, s, q2);
		      int ti = d.transid[t];

602
603
		      path p2(q2, dp);
		      int succ = d.pathid[p2];
604

605
		      if (p1id == succ)
606
607
			continue;

608
609
		      dout << p1 << " ∧ " << t << "δ → " << p2 << '\n';
		      out << -p1id << ' ' << -ti << ' ' << succ << " 0\n";
610
611
612
613
614
615
616
617
618
		      ++nclauses;
		    }
		}
	    }
	}

      bdd all_acc = ref->all_acceptance_conditions();

      // construction of constraints (11,12,13)
619
620
621
622
623
624
625
626
627
      for (int q1p = 1; q1p <= ref_size; ++q1p)
	{
	  unsigned q1p_scc = sm.scc_of_state(d.int_to_state[q1p]);
	  for (int q2p = 1; q2p <= ref_size; ++q2p)
	    {
	      // We are only interested in transition that can form a
	      // cycle, so they must belong to the same SCC.
	      if (sm.scc_of_state(d.int_to_state[q2p]) != q1p_scc)
		continue;
628
629
630
	      bool is_weak = is_weak_scc(sm, q1p_scc);
	      bool is_acc = sm.accepting(q1p_scc);

631
632
633
634
	      for (int q1 = 1; q1 <= d.cand_size; ++q1)
		for (int q2 = 1; q2 <= d.cand_size; ++q2)
		  {
		    size_t sf = d.all_cand_acc.size();
635
		    size_t sfp = is_weak ? 1 : d.all_ref_acc.size();
636
637
638
639
640
		    for (size_t f = 0; f < sf; ++f)
		      for (size_t fp = 0; fp < sfp; ++fp)
			{
			  path p(q1, q1p, q2, q2p,
				 d.all_cand_acc[f], d.all_ref_acc[fp]);
641

642
			  dout << "(11&12&13) paths from " << p << '\n';
643

644
			  int pid = d.pathid[p];
645

646
			  for (auto it: ref->succ(d.int_to_state[q2p]))
647
			    {
648
649
650
			      const state* dps = it->current_state();
			      // Skip destinations not in the SCC.
			      if (sm.scc_of_state(dps) != q1p_scc)
651
				{
652
653
654
655
656
				  dps->destroy();
				  continue;
				}
			      int dp = d.state_to_int[dps];
			      dps->destroy();
657

658
659
660
661
662
			      for (int q3 = 1; q3 <= d.cand_size; ++q3)
				{
				  bdd all = it->current_condition();
				  bdd curacc =
				    it->current_acceptance_conditions();
663

664
				  while (all != bddfalse)
665
				    {
666
667
				      bdd l = bdd_satoneset(all, ap, bddfalse);
				      all -= l;
668

669
670
				      transition t(q2, l, q3);
				      int ti = d.transid[t];
671

672
				      if (dp == q1p && q3 == q1) // (11,12) loop
673
					{
674
675
676
677
					  if ((!is_acc) ||
					      (!is_weak &&
					       (curacc |
						d.all_ref_acc[fp]) != all_acc))
678
					    {
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#if DEBUG
					      dout << "(11) " << p << " ∧ "
						   << t << "δ → ¬(";

					      bdd all_ = d.all_cand_acc.back();
					      all_ -= d.all_cand_acc[f];
					      bool notfirst = false;
					      while (all_ != bddfalse)
						{
						  bdd one = bdd_satone(all_);
						  all_ -= one;

						  transition_acc ta(q2, l,
								    one, q1);
						  if (notfirst)
						    out << " ∧ ";
						  else
						    notfirst = true;
						  out << ta << "FC";
						}
					      out << ")\n";
700
#endif // DEBUG
701
					      out << -pid << ' ' << -ti;
702
703
704
705
706
707
708
709
710
711
712
713
714

					      // 11
					      bdd all_f = d.all_cand_acc.back();
					      all_f -= d.all_cand_acc[f];
					      while (all_f != bddfalse)
						{
						  bdd one = bdd_satone(all_f);
						  all_f -= one;

						  transition_acc ta(q2, l,
								    one, q1);
						  int tai = d.transaccid[ta];
						  assert(tai != 0);
715
						  out << ' ' << -tai;
716
717
						}
					      out << " 0\n";
718
719
					      ++nclauses;
					    }
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
					  else
					    {
#if DEBUG
					      dout << "(12) " << p << " ∧ "
						   << t << "δ → (";

					      bdd all_ = d.all_cand_acc.back();
					      all_ -= d.all_cand_acc[f];
					      bool notfirst = false;
					      while (all_ != bddfalse)
						{
						  bdd one = bdd_satone(all_);
						  all_ -= one;

						  transition_acc ta(q2, l,
								    one, q1);
						  if (notfirst)
						    out << " ∧ ";
						  else
						    notfirst = true;
						  out << ta << "FC";
						}
					      out << ")\n";
#endif // DEBUG
					      // 12
					      bdd all_f = d.all_cand_acc.back();
					      all_f -= d.all_cand_acc[f];
					      while (all_f != bddfalse)
						{
						  bdd one = bdd_satone(all_f);
						  all_f -= one;

						  transition_acc ta(q2, l,
								    one, q1);
						  int tai = d.transaccid[ta];
						  assert(tai != 0);

757
758
						  out << -pid << ' ' << -ti
						      << ' ' << tai << " 0\n";
759
760
761
						  ++nclauses;
						}
					    }
762
					}
763
				      // (13) augmenting paths (always).
764
				      {
765
766
					size_t sf = d.all_cand_acc.size();
					for (size_t f = 0; f < sf; ++f)
767
768
					  {

769
770
					    bdd f2 = p.acc_cand |
					      d.all_cand_acc[f];
771
772
773
					    bdd f2p = bddfalse;
					    if (!is_weak)
					      f2p = p.acc_ref | curacc;
774

775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
					    path p2(p.src_cand, p.src_ref,
						    q3, dp, f2, f2p);
					    int p2id = d.pathid[p2];
					    if (pid == p2id)
					      continue;
#if DEBUG
					    dout << "(13) " << p << " ∧ "
						 << t << "δ ";

					    bdd biga_ = d.all_cand_acc[f];
					    while (biga_ != bddfalse)
					      {
						bdd a = bdd_satone(biga_);
						biga_ -= a;

						transition_acc ta(q2, l, a, q3);
						out <<  " ∧ " << ta << "FC";
					      }
					    biga_ = d.all_cand_acc.back()
					      - d.all_cand_acc[f];
					    while (biga_ != bddfalse)
					      {
						bdd a = bdd_satone(biga_);
						biga_ -= a;

						transition_acc ta(q2, l, a, q3);
						out << " ∧ ¬" << ta << "FC";
					      }
803
					    out << " → " << p2 << '\n';
804
#endif
805
					    out << -pid << ' ' << -ti << ' ';
806
807
808
809
810
811
812
813
					    bdd biga = d.all_cand_acc[f];
					    while (biga != bddfalse)
					      {
						bdd a = bdd_satone(biga);
						biga -= a;

						transition_acc ta(q2, l, a, q3);
						int tai = d.transaccid[ta];
814
						out << -tai << ' ';
815
816
817
818
819
820
821
822
823
824
					      }
					    biga = d.all_cand_acc.back()
					      - d.all_cand_acc[f];
					    while (biga != bddfalse)
					      {
						bdd a = bdd_satone(biga);
						biga -= a;

						transition_acc ta(q2, l, a, q3);
						int tai = d.transaccid[ta];
825
						out << tai << ' ';
826
827
828
829
					      }

					    out << p2id << " 0\n";
					    ++nclauses;
830
831
					  }
				      }
832
				    }
833
834
835
				}
			    }
			}
836
837
838
		  }
	    }
	}
839
      out.seekp(0);
840
      out << "p cnf " << d.nvars << ' ' << nclauses.nb_clauses();
841
      return std::make_pair(d.nvars, nclauses.nb_clauses());
842
843
    }

844
    static tgba_digraph*
845
846
    sat_build(const satsolver::solution& solution, dict& satdict,
	      const tgba* aut, bool state_based)
847
    {
848
849
      auto autdict = aut->get_dict();
      auto a = new tgba_digraph(autdict);
850
851
852
853
      autdict->register_all_variables_of(aut, a);
      autdict->unregister_all_typed_variables(bdd_dict::acc, aut);
      a->set_acceptance_conditions(satdict.all_cand_acc.back());

854
855
      auto& g = a->get_graph();
      g.new_states(satdict.cand_size);
856

857
858
859
860
      // Last transition set in the automaton.
      unsigned last_aut_trans = -1U;
      // Last transition read from the SAT result.
      const transition* last_sat_trans = nullptr;
861
862
863
864

#if DEBUG
      std::fstream out("dtgba-sat.dbg",
		       std::ios_base::trunc | std::ios_base::out);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
865
      out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
866
867
868
869
      std::set<int> positive;
#endif

      dout << "--- transition variables ---\n";
870
      std::map<int, bdd> state_acc;
871
      std::set<src_cond> seen_trans;
872
      for (int v: solution)
873
874
875
876
877
878
879
880
881
882
883
884
	{
	  if (v < 0)  // FIXME: maybe we can have (v < NNN)?
	    continue;

#if DEBUG
	  positive.insert(v);
#endif

	  dict::rev_map::const_iterator t = satdict.revtransid.find(v);

	  if (t != satdict.revtransid.end())
	    {
885
886
887
888
	      // Skip (s,l,d2) if we have already seen some (s,l,d1).
	      if (seen_trans.insert(src_cond(t->second.src,
					     t->second.cond)).second)
		{
889
		  bdd acc = bddfalse;
890
891
		  if (state_based)
		    {
892
		      auto i = state_acc.find(t->second.src);
893
		      if (i != state_acc.end())
894
			acc = i->second;
895
		    }
896
897
898
899
900
901
902
903

		  last_aut_trans = g.new_transition(t->second.src - 1,
						    t->second.dst - 1,
						    t->second.cond,
						    acc);
		  last_sat_trans = &t->second;

		  dout << v << '\t' << t->second << \n";
904
		}
905
906
907
908
909
910
911
912
913
	    }
	  else
	    {
	      dict::rev_acc_map::const_iterator ta;
	      ta = satdict.revtransaccid.find(v);
	      // This assumes that the sat solvers output variables in
	      // increasing order.
	      if (ta != satdict.revtransaccid.end())
		{
914
		  dout << v << '\t' << ta->second << "F\n";
915
916
917
918
919

		  if (last_sat_trans &&
		      ta->second.src == last_sat_trans->src &&
		      ta->second.cond == last_sat_trans->cond &&
		      ta->second.dst == last_sat_trans->dst)
920
921
		    {
		      assert(!state_based);
922
		      g.trans_data(last_aut_trans).acc |= ta->second.acc;
923
924
925
926
927
		    }
		  else if (state_based)
		    {
		      state_acc[ta->second.src] |= ta->second.acc;
		    }
928
929
930
931
		}
	    }
	}
#if DEBUG
932
      dout << "--- pathid variables ---\n";
933
934
935
      for (auto pit: satdict.pathid)
	if (positive.find(pit.second) != positive.end())
	  dout << pit.second << '\t' << pit.first << "C\n";
936
937
938
939
940
941
942
943
#endif

      a->merge_transitions();

      return a;
    }
  }

944
  tgba_digraph*
945
946
  dtgba_sat_synthetize(const tgba* a, unsigned target_acc_number,
		       int target_state_number, bool state_based)
947
  {
948
    if (target_state_number == 0)
949
      return nullptr;
950
951
952
953
    trace << "dtgba_sat_synthetize(..., acc = " << target_acc_number
	  << ", states = " << target_state_number
	  << ", state_based = " << state_based << ")\n";

954
955
956
    dict d(a);
    d.cand_size = target_state_number;
    d.cand_nacc = target_acc_number;
957

958
959
    satsolver solver;
    satsolver::solution_pair solution;
960

961
962
963
964
965
    timer_map t;
    t.start("encode");
    sat_stats s = dtgba_to_sat(solver(), a, d, state_based);
    t.stop("encode");
    t.start("solve");
966
    solution = solver.get_solution();
967
    t.stop("solve");
968

969
    tgba_digraph* res = nullptr;
970
971
    if (!solution.second.empty())
      res = sat_build(solution.second, d, a, state_based);
972

973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
    static const char* log = getenv("SPOT_SATLOG");
    if (log)
      {
	std::fstream out(log,
			 std::ios_base::app | std::ios_base::out);
	out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
	const timer& te = t.timer("encode");
	const timer& ts = t.timer("solve");
	out << target_state_number << ',';
	if (res)
	  {
	    tgba_sub_statistics st = sub_stats_reachable(res);
	    out << st.states << ',' << st.transitions
		<< ',' << st.sub_transitions;
	  }
	else
	  {
	    out << ",,";
	  }
	out << ','
	    << s.first << ',' << s.second << ','
	    << te.utime() << ',' << te.stime() << ','
	    << ts.utime() << ',' << ts.stime() << '\n';
      }
    static const char* show = getenv("SPOT_SATSHOW");
    if (show && res)
      dotty_reachable(std::cout, res);

1001
    trace << "dtgba_sat_synthetize(...) = " << res << '\n';
1002
1003
1004
    return res;
  }

1005
  tgba_digraph*
1006
1007
1008
1009
1010
  dtgba_sat_minimize(const tgba* a, unsigned target_acc_number,
		     bool state_based)
  {
    int n_states = stats_reachable(a).states;

1011
    tgba_digraph* prev = nullptr;
1012
    for (;;)
1013
      {
1014
	tgba_digraph* next =
1015
1016
	  dtgba_sat_synthetize(prev ? prev : a, target_acc_number,
			       --n_states, state_based);
1017
	if (!next)
1018
	  break;
1019
1020
	else
	  n_states = stats_reachable(next).states;
1021
1022
	delete prev;
	prev = next;
1023
      }
1024
1025
1026
    return prev;
  }

1027
  tgba_digraph*
1028
1029
1030
1031
1032
  dtgba_sat_minimize_dichotomy(const tgba* a, unsigned target_acc_number,
			       bool state_based)
  {
    int max_states = stats_reachable(a).states - 1;
    int min_states = 1;
1033

1034
    tgba_digraph* prev = nullptr;
1035
    while (min_states <= max_states)
1036
      {
1037
	int target = (max_states + min_states) / 2;
1038
	tgba_digraph* next =
1039
1040
	  dtgba_sat_synthetize(prev ? prev : a, target_acc_number, target,
			       state_based);
1041
	if (!next)
1042
	  {
1043
1044
1045
1046
1047
1048
	    min_states = target + 1;
	  }
	else
	  {
	    delete prev;
	    prev = next;
1049
	    max_states = stats_reachable(next).states - 1;
1050
1051
	  }
      }
1052
    return prev;
1053
1054
1055
  }

}