dtbasat.cc 21.2 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
#include <sstream>
#include "dtbasat.hh"
#include "reachiter.hh"
#include <map>
#include <utility>
#include "scc.hh"
#include "tgba/bddprint.hh"
#include "ltlast/constant.hh"
#include "stats.hh"
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
31
#include "misc/satsolver.hh"
32
33
#include "misc/timer.hh"
#include "dotty.hh"
34

35
36
37
// 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.
38
//
39
40
41
42
// 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 (dtba-sat.dbg) will be created with a list
// of all positive variables in the result and their meaning.
43
44
45
46

#define DEBUG 0
#if DEBUG
#define dout out << "c "
47
#define trace std::cerr
48
#else
49
50
#define dout while (0) std::cout
#define trace dout
51
52
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
#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());
      }
    };

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    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());
      }
    };

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
143
144
145
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
    struct state_pair
    {
      int a;
      int b;

      state_pair(int a, int b)
	: a(a), b(b)
      {
      }

      bool operator<(const state_pair& other) const
      {
	if (this->a < other.a)
	  return true;
	if (this->a > other.a)
	  return false;
	if (this->b < other.b)
	  return true;
	if (this->b > other.b)
	  return false;
	return false;
      }
    };

    struct path
    {
      int src_cand;
      int src_ref;
      int dst_cand;
      int dst_ref;

      path(int src_cand, int src_ref,
	   int dst_cand, int dst_ref)
	: src_cand(src_cand), src_ref(src_ref),
	  dst_cand(dst_cand), dst_ref(dst_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;
	return false;
      }

    };

    std::ostream& operator<<(std::ostream& os, const state_pair& p)
    {
180
      os << '<' << p.a << ',' << p.b << '>';
181
182
183
184
185
      return os;
    }

    std::ostream& operator<<(std::ostream& os, const transition& t)
    {
186
      os << '<' << t.src << ','
187
	 << bdd_format_formula(debug_dict, t.cond)
188
	 << ',' << t.dst << '>';
189
190
191
192
193
      return os;
    }

    std::ostream& operator<<(std::ostream& os, const path& p)
    {
194
195
196
197
198
      os << '<'
	 << p.src_cand << ','
	 << p.src_ref << ','
	 << p.dst_cand << ','
	 << p.dst_ref << '>';
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
      return os;
    }

    struct dict
    {
      typedef std::map<transition, int> trans_map;
      trans_map transid;
      trans_map transacc;
      typedef std::map<int, transition> rev_map;
      rev_map revtransid;
      rev_map revtransacc;

      std::map<state_pair, int> prodid;
      std::map<path, int> pathid_ref;
      std::map<path, int> pathid_cand;
      int nvars;
215
216
217
      typedef std::unordered_map<const state*, int,
				 state_ptr_hash, state_ptr_equal> state_map;
      typedef std::unordered_map<int, const state*> int_map;
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
      state_map state_to_int;
      int_map int_to_state;
      int cand_size;

      ~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();
      }
    };


    class filler_dfs: public tgba_reachable_iterator_depth_first
    {
    protected:
      dict& d;
      int size_;
      bdd ap_;
238
      bool state_based_;
239
      scc_map& sm_;
240
    public:
241
242
      filler_dfs(const tgba* aut, dict& d, bdd ap, bool state_based,
		 scc_map& sm)
243
	: tgba_reachable_iterator_depth_first(aut), d(d), ap_(ap),
244
	  state_based_(state_based), sm_(sm)
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
      {
	d.nvars = 0;
      }

      int size()
      {
	return size_;
      }

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

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

261
	// Reverse the "seen" map.  States are labeled from 1 to size_.
262
263
	for (dict::state_map::const_iterator i2 = seen.begin();
	     i2 != seen.end(); ++i2)
264
265
266
	  d.int_to_state[i2->second] = i2->first;

	for (int i = 1; i <= size_; ++i)
267
	  {
268
269
	    unsigned i_scc = sm_.scc_of_state(d.int_to_state[i]);

270
	    bool is_trivial = sm_.trivial(i_scc);
271

272
273
274
275
	    for (int j = 1; j <= d.cand_size; ++j)
	      {
		d.prodid[state_pair(j, i)] = ++d.nvars;

276
277
		// skip trivial SCCs
		if (is_trivial)
278
		  continue;
279

280
		for (int k = 1; k <= size_; ++k)
281
		  {
282
		    if (sm_.scc_of_state(d.int_to_state[k]) != i_scc)
283
284
		      continue;
		    for (int l = 1; l <= d.cand_size; ++l)
285
		    {
286
287
		      if (i == k && j == l)
			continue;
288
289
290
291
		      path p(j, i, l, k);
		      d.pathid_ref[p] = ++d.nvars;
		      d.pathid_cand[p] = ++d.nvars;
		    }
292
		  }
293
294
295
296
297
298
	      }
	  }

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

	for (int i = 1; i <= d.cand_size; ++i)
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
	  {
	    int transacc = -1;
	    if (state_based_)
	      // All outgoing transitions use the same acceptance variable.
	      transacc = ++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;

		    transition t(i, one, j);
		    d.transid[t] = ++d.nvars;
315
		    d.revtransid.emplace(d.nvars, t);
316
317
		    int ta = d.transacc[t] =
		      state_based_ ? transacc : ++d.nvars;
318
		    d.revtransacc.emplace(ta, t);
319
320
321
		  }
	      }
	  }
322
323
324
      }
    };

325
326
    typedef std::pair<int, int> sat_stats;

327
    static
328
329
    sat_stats dtba_to_sat(std::ostream& out, const tgba* ref,
			  dict& d, bool state_based)
330
    {
331
      clause_counter nclauses;
332
333
334
335
336
337
      int ref_size = 0;

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

338
339
340
341
342
343
344
345
346
347
348
349
      // Count the number of atomic propositions
      int nap = 0;
      {
	bdd cur = ap;
	while (cur != bddtrue)
	  {
	    ++nap;
	    cur = bdd_high(cur);
	  }
	nap = 1 << nap;
      }

350
351
      // Number all the SAT variable we may need.
      {
352
	filler_dfs f(ref, d, ap, state_based, sm);
353
354
355
356
357
358
359
360
	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";
361
	  return std::make_pair(1, 2);
362
363
364
365
366
367
368
	}

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

#if DEBUG
      debug_dict = ref->get_dict();
369
370
      dout << "ref_size: " << ref_size << '\n';
      dout << "cand_size: " << d.cand_size << '\n';
371
372
#endif

373
374
375
376
377
378
379
380
381
382
383
384
      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];
385
		dout << "¬" << t << '\n';
386
387
388
389
390
		out << -ti << " 0\n";
		++nclauses;
	      }
 	  ++j;
 	}
391
      if (!nclauses.nb_clauses())
392
 	dout << "(none)\n";
393

394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
      dout << "(1) 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 << " ∨ ";
		}
412
	      out << '\n';
413
414
415
416
417
418
419
#endif

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

420
		  out << ti << ' ';
421
422
423
424
425
426
427
428
		}
	      out << "0\n";

	      ++nclauses;
	    }
	}

      dout << "(2) the initial state is reachable\n";
429
      dout << state_pair(1, 1) << '\n';
430
431
432
433
434
435
436
437
438
439
440
      out << d.prodid[state_pair(1, 1)] << " 0\n";
      ++nclauses;

      for (std::map<state_pair, int>::const_iterator pit = d.prodid.begin();
	   pit != d.prodid.end(); ++pit)
	{
	  int q1 = pit->first.a;
	  int q1p = pit->first.b;

	  dout << "(3) augmenting paths based on Cand[" << q1
	       << "] and Ref[" << q1p << "]\n";
441
	  for (auto it: ref->succ(d.int_to_state[q1p]))
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
	    {
	      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];

		      state_pair p2(q2, dp);
		      int succ = d.prodid[p2];

461
462
463
		      if (pit->second == succ)
			continue;

464
465
		      dout << pit->first << " ∧ " << t << "δ → " << p2 << '\n';
		      out << -pit->second << ' ' << -ti << ' '
466
467
468
469
470
471
472
473
474
475
476
477
			  << succ << " 0\n";
		      ++nclauses;
		    }
		}
	    }
	}

      bdd all_acc = ref->all_acceptance_conditions();

      // construction of contraints (4,5) : all loops in the product
      // where no accepting run is detected in the ref. automaton,
      // must also be marked as not accepting in the cand. automaton
478
479
480
      for (int q1p = 1; q1p <= ref_size; ++q1p)
	{
	  unsigned q1p_scc = sm.scc_of_state(d.int_to_state[q1p]);
481
482
	  if (sm.trivial(q1p_scc))
	    continue;
483
484
485
486
487
488
	  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;
489
490
491
492
	      for (int q1 = 1; q1 <= d.cand_size; ++q1)
		for (int q2 = 1; q2 <= d.cand_size; ++q2)
		  {
		    path p1(q1, q1p, q2, q2p);
493

494
		    dout << "(4&5) matching paths from reference based on "
495
			 << p1 << '\n';
496

497
498
499
500
501
		    int pid1;
		    if (q1 == q2 && q1p == q2p)
		      pid1 = d.prodid[state_pair(q1, q1p)];
		    else
		      pid1 = d.pathid_ref[p1];
502

503
		    for (auto it: ref->succ(d.int_to_state[q2p]))
504
505
506
507
		      {
			const state* dps = it->current_state();
			// Skip destinations not in the SCC.
			if (sm.scc_of_state(dps) != q1p_scc)
508
509
			  {
			    dps->destroy();
510
511
512
513
			    continue;
			  }
			int dp = d.state_to_int[dps];
			dps->destroy();
514

515
516
517
518
519
			if (it->current_acceptance_conditions() == all_acc)
			  continue;
			for (int q3 = 1; q3 <= d.cand_size; ++q3)
			  {
			    if (dp == q1p && q3 == q1) // (4) looping
520
			      {
521
522
				bdd all = it->current_condition();
				while (all != bddfalse)
523
				  {
524
525
526
527
528
529
530
531
532
				    bdd s = bdd_satoneset(all, ap, bddfalse);
				    all -= s;

				    transition t(q2, s, q1);
				    int ti = d.transid[t];
				    int ta = d.transacc[t];

				    dout << p1 << "R ∧ " << t << "δ → ¬" << t
					 << "F\n";
533
				    out << -pid1 << ' ' << -ti << ' '
534
535
536
					<< -ta << " 0\n";
				    ++nclauses;
				  }
537
538


539
540
541
542
543
			      }
			    else // (5) not looping
			      {
				path p2 = path(q1, q1p, q3, dp);
				int pid2 = d.pathid_ref[p2];
544

545
546
				if (pid1 == pid2)
				  continue;
547

548
549
				bdd all = it->current_condition();
				while (all != bddfalse)
550
				  {
551
552
553
554
555
556
557
558
				    bdd s = bdd_satoneset(all, ap, bddfalse);
				    all -= s;

				    transition t(q2, s, q3);
				    int ti = d.transid[t];

				    dout << p1 << "R ∧ " << t << "δ → " << p2
					 << "R\n";
559
				    out << -pid1 << ' ' << -ti << ' '
560
561
					<< pid2 << " 0\n";
				    ++nclauses;
562
563
564
565
				  }
			      }
			  }
		      }
566
		  }
567
568
	    }
	}
569
570
571
      // construction of contraints (6,7): all loops in the product
      // where accepting run is detected in the ref. automaton, must
      // also be marked as accepting in the candidate.
572
573
574
      for (int q1p = 1; q1p <= ref_size; ++q1p)
	{
	  unsigned q1p_scc = sm.scc_of_state(d.int_to_state[q1p]);
575
576
	  if (sm.trivial(q1p_scc))
	    continue;
577
578
579
580
581
582
583
584
585
586
587
	  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;
	      for (int q1 = 1; q1 <= d.cand_size; ++q1)
		for (int q2 = 1; q2 <= d.cand_size; ++q2)
		  {
		    path p1(q1, q1p, q2, q2p);
		    dout << "(6&7) matching paths from candidate based on "
588
			 << p1 << '\n';
589
590
591
592
593
594

		    int pid1;
		    if (q1 == q2 && q1p == q2p)
		      pid1 = d.prodid[state_pair(q1, q1p)];
		    else
		      pid1 = d.pathid_cand[p1];
595

596
		    for (auto it: ref->succ(d.int_to_state[q2p]))
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
		      {
			const state* dps = it->current_state();
			// Skip destinations not in the SCC.
			if (sm.scc_of_state(dps) != q1p_scc)
			  {
			    dps->destroy();
			    continue;
			  }
			int dp = d.state_to_int[dps];
			dps->destroy();
			for (int q3 = 1; q3 <= d.cand_size; q3++)
			  {
			    if (dp == q1p && q3 == q1) // (6) looping
			      {
				// We only care about the looping case if
				// it is accepting in the reference.
				if (it->current_acceptance_conditions()
				    != all_acc)
				  continue;
				bdd all = it->current_condition();
				while (all != bddfalse)
				  {
				    bdd s = bdd_satoneset(all, ap, bddfalse);
				    all -= s;

				    transition t(q2, s, q1);
				    int ti = d.transid[t];
				    int ta = d.transacc[t];

				    dout << p1 << "C ∧ " << t << "δ → " << t
					 << "F\n";
628
				    out << -pid1 << ' ' << -ti << ' ' << ta
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
					<< " 0\n";
				    ++nclauses;
				  }
			      }
			    else // (7) no loop
			      {
				path p2 = path(q1, q1p, q3, dp);
				int pid2 = d.pathid_cand[p2];

				if (pid1 == pid2)
				  continue;

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

				    transition t(q2, s, q3);
				    int ti = d.transid[t];
				    int ta = d.transacc[t];

				    dout << p1 << "C ∧ " << t << "δ ∧ ¬"
					 << t << "F → " << p2 << "C\n";

654
655
				    out << -pid1 << ' ' << -ti << ' '
					<< ta << ' ' << pid2 << " 0\n";
656
657
658
659
660
661
662
663
				    ++nclauses;
				  }
			      }
			  }
		      }
		  }
	    }
	}
664
      out.seekp(0);
665
      out << "p cnf " << d.nvars << ' ' << nclauses.nb_clauses();
666
      return std::make_pair(d.nvars, nclauses.nb_clauses());
667
668
    }

669
    static tgba_digraph*
670
671
    sat_build(const satsolver::solution& solution, dict& satdict,
	      const tgba* aut, bool state_based)
672
    {
673
674
      auto autdict = aut->get_dict();
      auto a = new tgba_digraph(autdict);
675
676
677
678
679
680
      autdict->register_all_variables_of(aut, a);

      const ltl::formula* t = ltl::constant::true_instance();
      bdd acc = bdd_ithvar(autdict->register_acceptance_variable(t, a));
      a->set_acceptance_conditions(acc);

681
      a->new_states(satdict.cand_size);
682

683
684
      unsigned last_aut_trans = -1U;
      const transition* last_sat_trans = nullptr;
685
686
687
688

#if DEBUG
      std::fstream out("dtba-sat.dbg",
		       std::ios_base::trunc | std::ios_base::out);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
689
      out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
690
691
692
693
      std::set<int> positive;
#endif

      dout << "--- transition variables ---\n";
694
      std::set<int> acc_states;
695
      std::set<src_cond> seen_trans;
696
      for (int v: solution)
697
698
699
700
701
702
703
704
705
706
707
708
	{
	  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())
	    {
709
710
711
712
	      // 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)
		{
713
		  bdd accept = bddfalse;
714
715
716
		  // Mark the transition as accepting if the source is.
		  if (state_based
		      && acc_states.find(t->second.src) != acc_states.end())
717
718
		    accept = acc;

719
720
721
722
		  last_aut_trans = a->new_transition(t->second.src - 1,
						     t->second.dst - 1,
						     t->second.cond,
						     accept);
723
724
725
		  last_sat_trans = &t->second;

		  dout << v << '\t' << t->second << \n";
726
		}
727
728
729
730
731
732
	    }
	  else
	    {
	      t = satdict.revtransacc.find(v);
	      if (t != satdict.revtransacc.end())
		{
733
		  dout << v << '\t' << t->second << "F\n";
734
		  if (last_sat_trans && t->second == *last_sat_trans)
735
736
737
738
		    {
		      assert(!state_based);
		      // This assumes that the SAT solvers output
		      // variables in increasing order.
739
		      a->trans_data(last_aut_trans).acc = acc;
740
741
742
743
744
745
746
747
748
749
		    }
		  else if (state_based)
		    {
		      // Accepting translations actually correspond to
		      // states and are announced before listing
		      // outgoing transitions.  Again, this assumes
		      // that the SAT solvers output variables in
		      // increasing order.
		      acc_states.insert(t->second.src);
		    }
750
751
752
753
754
		}
	    }
	}
#if DEBUG
      dout << "--- state_pair variables ---\n";
755
756
757
      for (auto pit: satdict.prodid)
	if (positive.find(pit.second) != positive.end())
	  dout << pit.second << '\t' << pit.first << "C\n";
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
758
	else
759
	  dout << -pit.second << "\t¬" << pit.first << "C\n";
760
761

      dout << "--- pathid_cand variables ---\n";
762
763
764
      for (auto pit: satdict.pathid_cand)
	if (positive.find(pit.second) != positive.end())
	  dout << pit.second << '\t' << pit.first << "C\n";
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
765
	else
766
	  dout << -pit.second << "\t¬" << pit.first << "C\n";
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
767

768
      dout << "--- pathid_ref variables ---\n";
769
770
771
      for (auto pit: satdict.pathid_ref)
	if (positive.find(pit.second) != positive.end())
	  dout << pit.second << '\t' << pit.first << "R\n";
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
772
	else
773
	  dout << -pit.second << "\t¬" << pit.first << "C\n";
774
775
776
777
778
779
#endif
      a->merge_transitions();
      return a;
    }
  }

780
  tgba_digraph*
781
782
  dtba_sat_synthetize(const tgba* a, int target_state_number,
		      bool state_based)
783
  {
784
    if (target_state_number == 0)
785
      return nullptr;
786
787
    trace << "dtba_sat_synthetize(..., states = " << target_state_number
	  << ", state_based = " << state_based << ")\n";
788
789
    dict d;
    d.cand_size = target_state_number;
790

791
792
    satsolver solver;
    satsolver::solution_pair solution;
793

794
795
796
797
798
    timer_map t;
    t.start("encode");
    sat_stats s = dtba_to_sat(solver(), a, d, state_based);
    t.stop("encode");
    t.start("solve");
799
    solution = solver.get_solution();
800
    t.stop("solve");
801

802
    tgba_digraph* res = nullptr;
803
804
    if (!solution.second.empty())
      res = sat_build(solution.second, d, a, state_based);
805

806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
    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);

834
    trace << "dtba_sat_synthetize(...) = " << res << '\n';
835
836
837
    return res;
  }

838
  tgba_digraph*
839
840
841
842
  dtba_sat_minimize(const tgba* a, bool state_based)
  {
    int n_states = stats_reachable(a).states;

843
    tgba_digraph* prev = nullptr;
844
    for (;;)
845
      {
846
	tgba_digraph* next =
847
	  dtba_sat_synthetize(prev ? prev : a, --n_states, state_based);
848
	if (!next)
849
	  break;
850
851
852
	else
	  n_states = stats_reachable(next).states;

853
854
	delete prev;
	prev = next;
855
      }
856
857
    return prev;
  }
858

859
  tgba_digraph*
860
861
862
863
864
  dtba_sat_minimize_dichotomy(const tgba* a, bool state_based)
  {
    int max_states = stats_reachable(a).states - 1;
    int min_states = 1;

865
    tgba_digraph* prev = nullptr;
866
    while (min_states <= max_states)
867
      {
868
	int target = (max_states + min_states) / 2;
869
	tgba_digraph* next =
870
	  dtba_sat_synthetize(prev ? prev : a, target, state_based);
871
	if (!next)
872
	  {
873
874
875
876
877
878
	    min_states = target + 1;
	  }
	else
	  {
	    delete prev;
	    prev = next;
879
	    max_states = stats_reachable(next).states - 1;
880
881
	  }
      }
882
    return prev;
883
884
  }
}