postproc.cc 20.2 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2012-2019 Laboratoire de Recherche et Développement
3
// 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
#include "config.h"
21 22 23 24 25 26
#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>
27
#include <cstdlib>
28 29 30 31 32 33 34 35 36
#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>
37
#include <spot/twaalgos/determinize.hh>
38
#include <spot/twaalgos/alternation.hh>
39
#include <spot/twaalgos/parity.hh>
40
#include <spot/twaalgos/cobuchi.hh>
41
#include <spot/twaalgos/rabin2parity.hh>
42
#include <spot/twaalgos/cleanacc.hh>
43 44 45

namespace spot
{
46 47
  namespace
  {
48 49
    static twa_graph_ptr
    ensure_ba(twa_graph_ptr& a)
50
    {
51
      if (a->num_sets() == 0)
52 53 54 55 56
        {
          auto m = a->set_buchi();
          for (auto& t: a->edges())
            t.acc = m;
        }
57 58 59
      return a;
    }
  }
60

61 62 63 64
  postprocessor::postprocessor(const option_map* opt)
  {
    if (opt)
      {
65 66 67 68 69
        degen_order_ = opt->get("degen-order", 0);
        degen_reset_ = opt->get("degen-reset", 1);
        degen_cache_ = opt->get("degen-lcache", 1);
        degen_lskip_ = opt->get("degen-lskip", 1);
        degen_lowinit_ = opt->get("degen-lowinit", 0);
70
        degen_remscc_ = opt->get("degen-remscc", 1);
71 72 73
        det_scc_ = opt->get("det-scc", 1);
        det_simul_ = opt->get("det-simul", 1);
        det_stutter_ = opt->get("det-stutter", 1);
74 75
        det_max_states_ = opt->get("det-max-states", -1);
        det_max_edges_ = opt->get("det-max-edges", -1);
76 77 78 79 80
        simul_ = opt->get("simul", -1);
        scc_filter_ = opt->get("scc-filter", -1);
        ba_simul_ = opt->get("ba-simul", -1);
        tba_determinisation_ = opt->get("tba-det", 0);
        sat_minimize_ = opt->get("sat-minimize", 0);
81 82
        sat_incr_steps_ = opt->get("sat-incr-steps", -2); // -2 or any num < -1
        sat_langmap_ = opt->get("sat-langmap", 0);
83 84 85 86 87 88
        sat_acc_ = opt->get("sat-acc", 0);
        sat_states_ = opt->get("sat-states", 0);
        state_based_ = opt->get("state-based", 0);
        wdba_minimize_ = opt->get("wdba-minimize", 1);

        if (sat_acc_ && sat_minimize_ == 0)
89
          sat_minimize_ = 1;        // Dicho.
90 91 92 93 94 95 96 97 98 99
        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;
          }
100 101 102 103 104 105

        // Set default param value if not provided and used.
        if (sat_minimize_ == 2 && sat_incr_steps_ < 0) // Assume algorithm.
            sat_incr_steps_ = 6;
        else if (sat_minimize_ == 3 && sat_incr_steps_ < -1) // Incr algorithm.
            sat_incr_steps_ = 2;
106 107 108
      }
  }

109
  twa_graph_ptr
110
  postprocessor::do_simul(const twa_graph_ptr& a, int opt) const
111
  {
112 113
    if (!has_separate_sets(a))
      return a;
114
    switch (opt)
115 116
      {
      case 0:
117
        return a;
118
      case 1:
119
        return simulation(a);
120
      case 2:
121
        return cosimulation(a);
122 123
      case 3:
      default:
124
        return iterated_simulations(a);
125 126 127
      }
  }

128
  twa_graph_ptr
129
  postprocessor::do_sba_simul(const twa_graph_ptr& a, int opt) const
130
  {
131 132
    if (ba_simul_ <= 0)
      return a;
133 134 135
    switch (opt)
      {
      case 0:
136
        return a;
137
      case 1:
138
        return simulation_sba(a);
139
      case 2:
140
        return cosimulation_sba(a);
141 142
      case 3:
      default:
143
        return iterated_simulations_sba(a);
144 145 146
      }
  }

147
  twa_graph_ptr
148
  postprocessor::do_degen(const twa_graph_ptr& a) const
149
  {
150
    auto d = degeneralize(a,
151 152
                          degen_reset_, degen_order_,
                          degen_cache_, degen_lskip_,
153
                          degen_lowinit_, degen_remscc_);
154
    return do_sba_simul(d, ba_simul_);
155 156
  }

157
  twa_graph_ptr
158
  postprocessor::do_degen_tba(const twa_graph_ptr& a) const
159 160 161 162 163 164 165
  {
    return degeneralize_tba(a,
                            degen_reset_, degen_order_,
                            degen_cache_, degen_lskip_,
                            degen_lowinit_, degen_remscc_);
  }

166
  twa_graph_ptr
167
  postprocessor::do_scc_filter(const twa_graph_ptr& a, bool arg) const
168 169 170
  {
    if (scc_filter_ == 0)
      return a;
171
    if (state_based_ && a->prop_state_acc().is_true())
172
      return scc_filter_states(a, arg);
173 174 175 176 177
    else
      return scc_filter(a, arg);
  }

  twa_graph_ptr
178
  postprocessor::do_scc_filter(const twa_graph_ptr& a) const
179 180 181 182
  {
    return do_scc_filter(a, scc_filter_ > 1);
  }

183 184
#define PREF_ (pref_ & (Small | Deterministic))
#define COMP_ (pref_ & Complete)
185
#define SBACC_ (pref_ & SBAcc)
186
#define COLORED_ (pref_ & Colored)
187

188 189 190 191 192 193 194 195 196 197 198 199 200

  twa_graph_ptr
  postprocessor::finalize(twa_graph_ptr tmp) const
  {
    if (COMP_)
      tmp = complete(tmp);
    bool want_parity = type_ & Parity;
    if (want_parity && tmp->acc().is_generalized_buchi())
      tmp = SBACC_ ? do_degen(tmp) : do_degen_tba(tmp);
    if (SBACC_)
      tmp = sbacc(tmp);
    if (want_parity)
      {
201
        reduce_parity_here(tmp, COLORED_);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        parity_kind kind = parity_kind_any;
        parity_style style = parity_style_any;
        if ((type_ & ParityMin) == ParityMin)
          kind = parity_kind_min;
        else if ((type_ & ParityMax) == ParityMax)
          kind = parity_kind_max;
        if ((type_ & ParityOdd) == ParityOdd)
          style = parity_style_odd;
        else if ((type_ & ParityEven) == ParityEven)
          style = parity_style_even;
        change_parity_here(tmp, kind, style);
      }
    return tmp;
  }

217
  twa_graph_ptr
218
  postprocessor::run(twa_graph_ptr a, formula f)
219
  {
220 221 222 223 224 225 226 227 228
    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;

229
    bool via_gba = (type_ == BA) || (type_ == TGBA) || (type_ == Monitor);
230
    bool want_parity = type_ & Parity;
231 232 233
    if (COLORED_ && !want_parity)
      throw std::runtime_error("postprocessor: the Colored setting only works "
                               "for parity acceptance");
234

235 236 237 238 239 240 241 242


    // Attempt to simplify the acceptance condition, unless this is a
    // parity automaton and we want parity acceptance in the output.
    auto simplify_acc = [&](twa_graph_ptr in)
      {
        if (PREF_ == Any && level_ == Low)
          return in;
243 244 245 246 247
        bool isparity = in->acc().is_parity();
        if (isparity && in->is_existential()
            && (type_ == Generic || want_parity))
          return reduce_parity(in);
        if (!(want_parity && isparity))
248 249 250 251 252 253
          {
            if (level_ == High)
              return simplify_acceptance(in);
            else
              return cleanup_acceptance(in);
          }
254
        return cleanup_parity(in);
255 256 257
      };
    a = simplify_acc(a);

258
    if (!a->is_existential() &&
259 260 261 262 263 264 265
        // We will probably have to revisit this condition later.
        // Currently, the intent is that postprocessor should never
        // return an alternating automaton, unless it is called with
        // its laxest settings.
        !(type_ == Generic && PREF_ == Any && level_ == Low))
      a = remove_alternation(a);

266
    if ((via_gba && !a->acc().is_generalized_buchi())
267
        || (want_parity && !a->acc().is_parity()))
268
      {
269
        twa_graph_ptr b = nullptr;
270 271
        if (want_parity && is_deterministic(a) &&
            !a->acc().is_generalized_buchi())
272
          b = iar_maybe(a);
273 274 275
        // possible only if a was deterministic and (Rabin-like or Streett-like)
        // and we want parity and a is not a TGBA
        // NB: on a TGBA, degeneralization is better than IAR
276 277 278 279
        if (b)
          a = b;
        else
          a = to_generalized_buchi(a);
280 281
        if (PREF_ == Any && level_ == Low)
          a = do_scc_filter(a, true);
282
      }
283

284
    if (PREF_ == Any && level_ == Low
285 286 287
        && (type_ == Generic
            || type_ == TGBA
            || (type_ == BA && a->is_sba())
288
            || (type_ == Monitor && a->num_sets() == 0)
289 290
            || (want_parity && a->acc().is_parity())
            || (type_ == CoBuchi && a->acc().is_co_buchi())))
291
      return finalize(a);
292

293
    int original_acc = a->num_sets();
294

295
    // Remove useless SCCs.
296
    if (type_ == Monitor)
297 298 299
      // Do not bother about acceptance conditions, they will be
      // ignored.
      a = scc_filter_states(a);
300
    else
301
      a = do_scc_filter(a, (PREF_ == Any));
302

303 304
    if (type_ == Monitor)
      {
305 306 307 308 309
        if (PREF_ == Deterministic)
          a = minimize_monitor(a);
        else
          strip_acceptance_here(a);

310
        if (PREF_ != Any)
311
          {
312 313 314 315 316
            if (PREF_ != Deterministic)
              a = do_simul(a, simul_);

            // For Small,High we return the smallest between the output of
            // the simulation, and that of the deterministic minimization.
317
            // Prefer the deterministic automaton in case of equality.
318 319 320
            if (PREF_ == Small && level_ == High && simul_)
              {
                auto m = minimize_monitor(a);
321
                if (m->num_states() <= a->num_states())
322 323 324
                  a = m;
              }
            a->remove_unused_ap();
325
          }
326
        return finalize(a);
327 328
      }

329
    if (PREF_ == Any)
330
      {
331 332
        if (type_ == BA)
          a = do_degen(a);
333 334
        else if (type_ == CoBuchi)
          a = to_nca(a);
335
        return finalize(a);
336 337
      }

338
    bool dba_is_wdba = false;
339
    bool dba_is_minimal = false;
340 341
    twa_graph_ptr dba = nullptr;
    twa_graph_ptr sim = nullptr;
342

343 344 345 346 347 348
    output_aborter aborter_
      (det_max_states_ >= 0 ? static_cast<unsigned>(det_max_states_) : -1U,
       det_max_edges_ >= 0 ? static_cast<unsigned>(det_max_edges_) : -1U);
    output_aborter* aborter =
      (det_max_states_ >= 0 || det_max_edges_ >= 0) ? &aborter_ : nullptr;

349 350
    // (Small,Low) is the only configuration where we do not run
    // WDBA-minimization.
351
    if ((PREF_ != Small || level_ != Low) && wdba_minimize_)
352
      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
353
        bool reject_bigger = (PREF_ == Small) && (level_ <= Medium);
354
        dba = minimize_obligation(a, f, nullptr, reject_bigger, aborter);
355 356
        if (dba
            && dba->prop_inherently_weak().is_true()
357
            && dba->prop_universal().is_true())
358 359 360 361 362 363 364 365 366 367 368 369
          {
            // 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);
          }
        else
          {
            // Minimization failed.
            dba = nullptr;
          }
370 371 372 373
      }

    // Run a simulation when wdba failed (or was not run), or
    // at hard levels if we want a small output.
374
    if (!dba || (level_ == High && PREF_ == Small))
375
      {
376 377 378 379 380 381 382 383 384 385 386 387 388
        if (((SBACC_ && a->prop_state_acc().is_true())
             || (type_ == BA && a->is_sba()))
            && !tba_determinisation_)
          {
            sim = do_sba_simul(a, ba_simul_);
          }
        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);
389 390
            else if (want_parity && !sim->acc().is_parity())
              sim = do_degen_tba(sim);
391 392 393
            else if (SBACC_ && !tba_determinisation_)
              sim = sbacc(sim);
          }
394 395
      }

396 397 398 399
    // If WDBA failed, but the simulation returned a deterministic
    // automaton, use it as dba.
    assert(dba || sim);
    if (!dba && is_deterministic(sim))
400
      {
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        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.
        if (tba_determinisation_)
          {
            if (type_ == BA)
              {
                dba = do_degen(dba);
                assert(is_deterministic(dba));
              }
            else if (SBACC_)
              {
                dba = sbacc(dba);
                assert(is_deterministic(dba));
              }
          }
418
      }
419 420 421

    // If we don't have a DBA, attempt tba-determinization if requested.
    if (tba_determinisation_ && !dba)
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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
        twa_graph_ptr tmpd = nullptr;
        if (PREF_ == Deterministic
            && f
            && f.is_syntactic_recurrence()
            && sim->num_sets() > 1)
          tmpd = degeneralize_tba(sim);

        auto in = tmpd ? tmpd : sim;

        // These thresholds are arbitrary.
        //
        // 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.
        auto tmp =
          tba_determinize_check(in,
                                (PREF_ == Small) ? 2 : 8,
                                1 << ((PREF_ == Small) ? 13 : 15),
                                f);
        if (tmp && tmp != in)
          {
            // There is no point in running the reverse simulation on
            // a deterministic automaton, since all prefixes are
            // unique.
            dba = simulation(tmp);
          }
        if (dba && PREF_ == Deterministic)
          {
            // disregard the result of the simulation.
            sim = nullptr;
          }
        else
          {
            // degeneralize sim, because we did not do it earlier
            if (type_ == BA)
              sim = do_degen(sim);
          }
466 467
      }

468
    if ((PREF_ == Deterministic && (type_ == Generic || want_parity)) && !dba)
469
      {
470
        dba = tgba_determinize(to_generalized_buchi(sim),
471 472 473 474 475 476 477 478 479 480 481
                               false, det_scc_, det_simul_, det_stutter_,
                               aborter);
        // Setting det-max-states or det-max-edges may cause tgba_determinize
        // to fail.
        if (dba)
          {
            dba = simplify_acc(dba);
            if (level_ != Low)
              dba = simulation(dba);
            sim = nullptr;
          }
482 483
      }

484 485 486
    // 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
487 488 489 490
    // TBA-determinization (dba_is_wdba=false in both cases), or a
    // parity automaton coming from tgba_determinize().  If the dba is
    // a WDBA, we do not have to run SAT-minimization.  A negative
    // value in sat_minimize_ can force its use for debugging.
491
    if (sat_minimize_ && dba && (!dba_is_wdba || sat_minimize_ < 0))
492
      {
493 494
        if (type_ == Generic)
          throw std::runtime_error
495
            ("postproc() not yet updated to mix sat-minimize and Generic");
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
        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
          // automaton, not from dba, because dba often has been
          // degeneralized by tba_determinize_check().  Make sure it
          // is at least 1.
          target_acc = original_acc > 0 ? original_acc : 1;

        const_twa_graph_ptr in = nullptr;
        if (target_acc == 1)
          {
            // 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.
            if (state_based_)
              in = degeneralize(dba);
            else
              in = degeneralize_tba(dba);
          }
        else
          {
            in = dba;
          }

        twa_graph_ptr res = complete(in);
        if (target_acc == 1)
          {
            if (sat_states_ != -1)
              res = dtba_sat_synthetize(res, sat_states_, state_based_);
529
            else if (sat_minimize_ == 1)
530
              res = dtba_sat_minimize_dichotomy
531 532 533 534
                (res, state_based_, sat_langmap_);
            else if (sat_minimize_ == 2)
              res = dtba_sat_minimize_assume(res, state_based_, -1,
                                             sat_incr_steps_);
535
            else if (sat_minimize_ == 3)
536 537 538 539
              res = dtba_sat_minimize_incr(res, state_based_, -1,
                                           sat_incr_steps_);
            else // if (sat_minimize == 4 || sat_minimize == -1)
              res = dtba_sat_minimize(res, state_based_);
540 541 542 543 544 545 546 547
          }
        else
          {
            if (sat_states_ != -1)
              res = dtwa_sat_synthetize
                (res, target_acc,
                 acc_cond::acc_code::generalized_buchi(target_acc),
                 sat_states_, state_based_);
548 549
            else if (sat_minimize_ == 1)
              res = dtwa_sat_minimize_dichotomy
550 551
                (res, target_acc,
                 acc_cond::acc_code::generalized_buchi(target_acc),
552
                 state_based_, sat_langmap_);
553
            else if (sat_minimize_ == 2)
554
              res = dtwa_sat_minimize_assume
555 556
                (res, target_acc,
                 acc_cond::acc_code::generalized_buchi(target_acc),
557
                 state_based_, -1, false, sat_incr_steps_);
558
            else if (sat_minimize_ == 3)
559 560 561
              res = dtwa_sat_minimize_incr
                (res, target_acc,
                 acc_cond::acc_code::generalized_buchi(target_acc),
562 563 564
                 state_based_, -1, false, sat_incr_steps_);
            else // if (sat_minimize_ == 4 || sat_minimize_ == -1)
              res = dtwa_sat_minimize
565 566
                (res, target_acc,
                 acc_cond::acc_code::generalized_buchi(target_acc),
567
                 state_based_);
568
          }
569 570 571 572 573 574

        if (res)
          {
            dba = do_scc_filter(res, true);
            dba_is_minimal = true;
          }
575 576
      }

577 578 579
    // 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
580
        && !(dba_is_minimal && state_based_ && dba->num_sets() == 1))
581
      dba = degeneralize(dba);
582 583 584

    if (dba && sim)
      {
585 586 587 588
        if (dba->num_states() > sim->num_states())
          dba = nullptr;
        else
          sim = nullptr;
589 590
      }

591
    if (level_ == High && scc_filter_ != 0)
592
      {
593 594 595 596 597 598 599 600 601 602 603 604
        if (dba)
          {
            // Do that even for WDBA, to remove marks from transitions
            // leaving trivial SCCs.
            dba = do_scc_filter(dba, true);
            assert(!sim);
          }
        else if (sim)
          {
            sim = do_scc_filter(sim, true);
            assert(!dba);
          }
605
      }
606 607 608

    sim = dba ? dba : sim;

609
    sim->remove_unused_ap();
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

    if (type_ == CoBuchi)
      {
        unsigned ns = sim->num_states();
        if (PREF_ == Deterministic)
          sim = to_dca(sim);
        else
          sim = to_nca(sim);

        // if the input of to_dca/to_nca was weak, the number of
        // states has not changed, and running simulation is useless.
        if (level_ != Low && ns < sim->num_states())
          sim = do_simul(sim, simul_);
      }

625
    return finalize(sim);
626 627
  }
}