postproc.cc 12.4 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2012, 2013, 2014, 2015 Laboratoire de Recherche et
3
// 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
#include <spot/twaalgos/postproc.hh>
#include <spot/twaalgos/minimize.hh>
#include <spot/twaalgos/simulation.hh>
#include <spot/twaalgos/sccfilter.hh>
#include <spot/twaalgos/degen.hh>
#include <spot/twaalgos/stripacc.hh>
26
#include <cstdlib>
27 28 29 30 31 32 33 34 35
#include <spot/misc/optionmap.hh>
#include <spot/twaalgos/powerset.hh>
#include <spot/twaalgos/isdet.hh>
#include <spot/twaalgos/dtbasat.hh>
#include <spot/twaalgos/dtwasat.hh>
#include <spot/twaalgos/complete.hh>
#include <spot/twaalgos/totgba.hh>
#include <spot/twaalgos/sbacc.hh>
#include <spot/twaalgos/sepsets.hh>
36 37 38

namespace spot
{
39 40
  namespace
  {
41 42
    static twa_graph_ptr
    ensure_ba(twa_graph_ptr& a)
43
    {
44
      if (a->num_sets() == 0)
45
	{
46
	  auto m = a->set_buchi();
47
	  for (auto& t: a->edges())
48 49 50 51 52
	    t.acc = m;
	}
      return a;
    }
  }
53

54 55
  postprocessor::postprocessor(const option_map* opt)
    : type_(TGBA), pref_(Small), level_(High),
56
      degen_reset_(true), degen_order_(false), degen_cache_(true),
57 58
      degen_lskip_(true), degen_lowinit_(false), simul_(-1),
      scc_filter_(-1), ba_simul_(-1),
59 60
      tba_determinisation_(false), sat_minimize_(0), sat_acc_(0),
      sat_states_(0), state_based_(false), wdba_minimize_(true)
61 62 63
  {
    if (opt)
      {
64 65 66
	degen_order_ = opt->get("degen-order", 0);
	degen_reset_ = opt->get("degen-reset", 1);
	degen_cache_ = opt->get("degen-lcache", 1);
67
	degen_lskip_ = opt->get("degen-lskip", 1);
68
	degen_lowinit_ = opt->get("degen-lowinit", 0);
69
	simul_ = opt->get("simul", -1);
70
	scc_filter_ = opt->get("scc-filter", -1);
71
	ba_simul_ = opt->get("ba-simul", -1);
72
	tba_determinisation_ = opt->get("tba-det", 0);
73 74
	sat_minimize_ = opt->get("sat-minimize", 0);
	sat_acc_ = opt->get("sat-acc", 0);
75
	sat_states_ = opt->get("sat-states", 0);
76
	state_based_ = opt->get("state-based", 0);
77
	wdba_minimize_ = opt->get("wdba-minimize", 1);
78 79 80 81 82 83 84 85 86 87 88 89 90

	if (sat_acc_ && sat_minimize_ == 0)
	  sat_minimize_ = 1;	// 2?
	if (sat_states_ && sat_minimize_ == 0)
	  sat_minimize_ = 1;
	if (sat_minimize_)
	  {
	    tba_determinisation_ = 1;
	    if (sat_acc_ <= 0)
	      sat_acc_ = -1;
	    if (sat_states_ <= 0)
	      sat_states_ = -1;
	  }
91 92 93
      }
  }

94 95
  twa_graph_ptr
  postprocessor::do_simul(const twa_graph_ptr& a, int opt)
96
  {
97 98
    if (!has_separate_sets(a))
      return a;
99
    switch (opt)
100 101 102 103 104 105 106 107 108 109 110 111 112
      {
      case 0:
	return a;
      case 1:
	return simulation(a);
      case 2:
	return cosimulation(a);
      case 3:
      default:
	return iterated_simulations(a);
      }
  }

113
  twa_graph_ptr
114
  postprocessor::do_sba_simul(const twa_graph_ptr& a, int opt)
115
  {
116 117
    if (ba_simul_ <= 0)
      return a;
118 119 120 121 122 123 124 125 126 127 128 129 130 131
    switch (opt)
      {
      case 0:
	return a;
      case 1:
	return simulation_sba(a);
      case 2:
	return cosimulation_sba(a);
      case 3:
      default:
	return iterated_simulations_sba(a);
      }
  }

132 133
  twa_graph_ptr
  postprocessor::do_degen(const twa_graph_ptr& a)
134
  {
135 136
    auto d = degeneralize(a,
			  degen_reset_, degen_order_,
137 138
			  degen_cache_, degen_lskip_,
			  degen_lowinit_);
139
    return do_sba_simul(d, ba_simul_);
140 141
  }

142 143 144 145 146
  twa_graph_ptr
  postprocessor::do_scc_filter(const twa_graph_ptr& a, bool arg)
  {
    if (scc_filter_ == 0)
      return a;
147 148
    // If the automaton is weak, using transition-based acceptance
    // won't help, so let's preserve it.
149
    if ((state_based_ || a->prop_inherently_weak()) && a->prop_state_acc())
150
      return scc_filter_states(a, arg);
151 152 153 154 155 156 157 158 159 160
    else
      return scc_filter(a, arg);
  }

  twa_graph_ptr
  postprocessor::do_scc_filter(const twa_graph_ptr& a)
  {
    return do_scc_filter(a, scc_filter_ > 1);
  }

161 162
#define PREF_ (pref_ & (Small | Deterministic))
#define COMP_ (pref_ & Complete)
163
#define SBACC_ (pref_ & SBAcc)
164

165
  twa_graph_ptr
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
166
  postprocessor::run(twa_graph_ptr a, formula f)
167
  {
168 169 170 171 172 173 174 175 176 177
    if (simul_ < 0)
      simul_ = (level_ == Low) ? 1 : 3;
    if (ba_simul_ < 0)
      ba_simul_ = (level_ == High) ? 3 : 0;
    if (scc_filter_ < 0)
      scc_filter_ = 1;
    if (type_ == BA || SBACC_)
      state_based_ = true;

    bool tgb_used = false;
178
    if (type_ != Generic && !a->acc().is_generalized_buchi())
179 180 181 182
      {
	a = to_generalized_buchi(a);
	tgb_used = true;
      }
183

184
    if (PREF_ == Any && level_ == Low)
185 186
      if (type_ == Generic
	  || type_ == TGBA
187
	  || (type_ == BA && a->is_sba())
188
	  || (type_ == Monitor && a->num_sets() == 0))
189
	{
190
	  if (tgb_used)
191
	    a = do_scc_filter(a, true);
192
	  if (COMP_)
193
	    a = complete(a);
194 195
	  if (SBACC_)
	    a = sbacc(a);
196 197
	  return a;
	}
198

199
    int original_acc = a->num_sets();
200

201
    // Remove useless SCCs.
202
    if (type_ == Monitor)
203 204 205
      // Do not bother about acceptance conditions, they will be
      // ignored.
      a = scc_filter_states(a);
206
    else
207
      a = do_scc_filter(a, (PREF_ == Any));
208

209 210
    if (type_ == Monitor)
      {
211
	if (PREF_ == Deterministic)
212
	  a = minimize_monitor(a);
213
	else
214 215
	  strip_acceptance_here(a);

216
	if (PREF_ == Any)
217 218
	  return a;

219 220
	a = do_simul(a, simul_);

221 222
	// For Small,High we return the smallest between the output of
	// the simulation, and that of the deterministic minimization.
223
	if (PREF_ == Small && level_ == High && simul_)
224
	  {
225 226 227
	    auto m = minimize_monitor(a);
	    if (m->num_states() < a->num_states())
	      a = m;
228
	  }
229
	if (COMP_)
230
	  a = complete(a);
231
	return a;
232 233
      }

234
    if (PREF_ == Any)
235 236
      {
	if (type_ == BA)
237
	  a = do_degen(a);
238
	if (COMP_)
239
	  a = complete(a);
240 241
	if (SBACC_)
	  a = sbacc(a);
242 243 244
	return a;
      }

245
    bool dba_is_wdba = false;
246
    bool dba_is_minimal = false;
247 248
    twa_graph_ptr dba = nullptr;
    twa_graph_ptr sim = nullptr;
249 250 251

    // (Small,Low) is the only configuration where we do not run
    // WDBA-minimization.
252
    if ((PREF_ != Small || level_ != Low) && wdba_minimize_)
253
      {
254
	bool reject_bigger = (PREF_ == Small) && (level_ == Medium);
255
	dba = minimize_obligation(a, f, nullptr, reject_bigger);
256
	if (dba && dba->prop_inherently_weak() && dba->prop_deterministic())
257 258 259 260 261 262 263
	  {
	    // The WDBA is a BA, so no degeneralization is required.
	    // We just need to add an acceptance set if there is none.
	    dba_is_minimal = dba_is_wdba = true;
	    if (type_ == BA)
	      ensure_ba(dba);
	  }
264
	else
265 266 267 268
	  {
	    // Minimization failed.
	    dba = nullptr;
	  }
269 270 271 272
      }

    // Run a simulation when wdba failed (or was not run), or
    // at hard levels if we want a small output.
273
    if (!dba || (level_ == High && PREF_ == Small))
274
      {
275
	if (((SBACC_ && a->prop_state_acc())
276 277
	     || (type_ == BA && a->is_sba()))
	    && !tba_determinisation_)
278
	  {
279
	    sim = do_sba_simul(a, ba_simul_);
280 281 282 283 284 285 286 287
	  }
	else
	  {
	    sim = do_simul(a, simul_);
	    // Degeneralize the result of the simulation if needed.
	    // No need to do that if tba_determinisation_ will be used.
	    if (type_ == BA && !tba_determinisation_)
	      sim = do_degen(sim);
288 289
	    else if (SBACC_ && !tba_determinisation_)
	      sim = sbacc(sim);
290
	  }
291 292
      }

293 294 295 296
    // If WDBA failed, but the simulation returned a deterministic
    // automaton, use it as dba.
    assert(dba || sim);
    if (!dba && is_deterministic(sim))
297 298 299 300 301
      {
	std::swap(sim, dba);
	// We postponed degeneralization above i case we would need
	// to perform TBA-determinisation, but now it is clear
	// that we won't perform it.  So do degeneralize.
302
	if (tba_determinisation_)
303
	  {
304 305 306 307 308 309 310 311 312 313
	    if (type_ == BA)
	      {
		dba = do_degen(dba);
		assert(is_deterministic(dba));
	      }
	    else if (SBACC_)
	      {
		dba = sbacc(dba);
		assert(is_deterministic(dba));
	      }
314 315
	  }
      }
316 317 318

    // If we don't have a DBA, attempt tba-determinization if requested.
    if (tba_determinisation_ && !dba)
319
      {
320
	twa_graph_ptr tmpd = nullptr;
321
	if (PREF_ == Deterministic
322
	    && f
323
	    && f.is_syntactic_recurrence()
324
	    && sim->num_sets() > 1)
325
	  tmpd = degeneralize_tba(sim);
326

327
	auto in = tmpd ? tmpd : sim;
328

329
	// These thresholds are arbitrary.
330 331 332 333 334 335 336 337 338 339
	//
	// For producing Small automata, we assume that a
	// deterministic automaton that is twice the size of the
	// original will never get reduced to a smaller one.  We also
	// do not want more than 2^13 cycles in an SCC.
	//
	// For Deterministic automata, we accept automata that
	// are 8 times bigger, with no more that 2^15 cycle per SCC.
	// The cycle threshold is the most important limit here.  You
	// may up it if you want to try producing larger automata.
340
	auto tmp =
341
	  tba_determinize_check(in,
342 343
				(PREF_ == Small) ? 2 : 8,
				1 << ((PREF_ == Small) ? 13 : 15),
344
				f);
345
	if (tmp && tmp != in)
346 347 348 349
	  {
	    // There is no point in running the reverse simulation on
	    // a deterministic automaton, since all prefixes are
	    // unique.
350
	    dba = simulation(tmp);
351
	  }
352
	if (dba && PREF_ == Deterministic)
353 354
	  {
	    // disregard the result of the simulation.
355
	    sim = nullptr;
356
	  }
357 358 359 360 361 362
	else
	  {
	    // degeneralize sim, because we did not do it earlier
	    if (type_ == BA)
	      sim = do_degen(sim);
	  }
363 364
      }

365 366 367
    // Now dba contains either the result of WDBA-minimization (in
    // that case dba_is_wdba=true), or some deterministic automaton
    // that is either the result of the simulation or of the
368 369 370 371
    // TBA-determinization (dba_is_wdba=false in both cases).  If the
    // dba is a WDBA, we do not have to run SAT-minimization.  A
    // negative value in sat_minimize_ can for its use for debugging.
    if (sat_minimize_ && dba && (!dba_is_wdba || sat_minimize_ < 0))
372
      {
373 374 375 376 377 378 379
	unsigned target_acc;
	if (type_ == BA)
	  target_acc = 1;
	else if (sat_acc_ != -1)
	  target_acc = sat_acc_;
	else
	  // Take the number of acceptance conditions from the input
380
	  // automaton, not from dba, because dba often has been
381 382
	  // degeneralized by tba_determinize_check().  Make sure it
	  // is at least 1.
383
	  target_acc = original_acc > 0 ? original_acc : 1;
384

385
	const_twa_graph_ptr in = nullptr;
386
	if (target_acc == 1)
387 388 389 390
	  {
	    // If we are seeking a minimal DBA with unknown number of
	    // states, then we should start from the degeneralized,
	    // because the input TBA might be smaller.
391
	    if (state_based_)
392
	      in = degeneralize(dba);
393
	    else if (dba->num_sets() != 1)
394
	      in = degeneralize_tba(dba);
395
	    else
396 397 398 399 400
	      in = dba;
	  }
	else
	  {
	    in = dba;
401
	  }
402

403
	twa_graph_ptr res = complete(in);
404
	if (target_acc == 1)
405
	  {
406
	    if (sat_states_ != -1)
407
	      res = dtba_sat_synthetize(res, sat_states_, state_based_);
408
	    else if (sat_minimize_ == 1 || sat_minimize_ == -1)
409
	      res = dtba_sat_minimize(res, state_based_);
410
	    else  // sat_minimize_ == 2
411
	      res = dtba_sat_minimize_dichotomy(res, state_based_);
412 413 414 415
	  }
	else
	  {
	    if (sat_states_ != -1)
416
	      res = dtwa_sat_synthetize
417 418
		(res, target_acc,
		 acc_cond::acc_code::generalized_buchi(target_acc),
419
		 sat_states_, state_based_);
420
	    else if (sat_minimize_ == 1 || sat_minimize_ == -1)
421
	      res = dtwa_sat_minimize
422 423
		(res, target_acc,
		 acc_cond::acc_code::generalized_buchi(target_acc),
424
		 state_based_);
425
	    else  // sat_minimize_ == 2
426
	      res = dtwa_sat_minimize_dichotomy
427 428
		(res, target_acc,
		 acc_cond::acc_code::generalized_buchi(target_acc),
429
		 state_based_);
430
	  }
431 432

	if (res)
433
	  {
434
	    dba = do_scc_filter(res, true);
435
	    dba_is_minimal = true;
436 437 438
	  }
      }

439 440 441
    // Degeneralize the dba resulting from tba-determinization or
    // sat-minimization (which is a TBA) if requested and needed.
    if (dba && !dba_is_wdba && type_ == BA
442
	&& !(dba_is_minimal && state_based_ && dba->num_sets() == 1))
443
      dba = degeneralize(dba);
444 445 446

    if (dba && sim)
      {
447
	if (dba->num_states() > sim->num_states())
448
	  dba = nullptr;
449
	else
450
	  sim = nullptr;
451 452
      }

453
    if (level_ == High && scc_filter_ != 0)
454
      {
455
	if (dba)
456
	  {
457 458
	    // Do that even for WDBA, to remove marks from transitions
	    // leaving trivial SCCs.
459
	    dba = do_scc_filter(dba, true);
460
	    assert(!sim);
461 462 463
	  }
	else if (sim)
	  {
464
	    sim = do_scc_filter(sim, true);
465
	    assert(!dba);
466
	  }
467
      }
468 469 470

    sim = dba ? dba : sim;

471
    if (COMP_)
472
      sim = complete(sim);
473 474
    if (SBACC_)
      sim = sbacc(sim);
475

476
    return sim;
477 478
  }
}