// -*- coding: utf-8 -*-
// Copyright (C) 2011, 2013, 2014 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
// Copyright (C) 2004, 2005 Laboratoire d'Informatique de Paris 6 (LIP6),
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
// et Marie Curie.
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 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 .
/// FIXME:
/// * Test some heuristics on the order of visit of the successors in the blue
/// dfs:
/// - favorize the arcs conducting to the blue stack (the states of color
/// cyan)
/// - in this category, favorize the labelled arcs
/// - for the remaining ones, favorize the arcs labelled by the greatest
/// number of new acceptance conditions (notice that this number may evolve
/// after the visit of previous successors).
///
/// * Add a bit-state hashing version.
//#define TRACE
#include
#ifdef TRACE
#define trace std::cerr
#else
#define trace while (0) std::cerr
#endif
#include
#include
#include
#include "misc/hash.hh"
#include "tgba/tgba.hh"
#include "emptiness.hh"
#include "emptiness_stats.hh"
#include "tau03opt.hh"
#include "weight.hh"
#include "ndfs_result.hxx"
namespace spot
{
namespace
{
enum color {WHITE, CYAN, BLUE};
/// \brief Emptiness checker on spot::tgba automata having a finite number
/// of acceptance conditions (i.e. a TGBA).
template
class tau03_opt_search : public emptiness_check, public ec_statistics
{
public:
/// \brief Initialize the search algorithm on the automaton \a a
tau03_opt_search(const const_tgba_ptr& a, size_t size, option_map o)
: emptiness_check(a, o),
current_weight(a->neg_acceptance_conditions()),
h(size),
all_acc(a->all_acceptance_conditions()),
use_condition_stack(o.get("condstack")),
use_ordering(use_condition_stack && o.get("ordering")),
use_weights(o.get("weights", 1)),
use_red_weights(use_weights && o.get("redweights", 1))
{
if (use_ordering)
{
bdd all_conds = all_acc;
while (all_conds != bddfalse)
{
bdd acc = bdd_satone(all_conds);
cond.push_back(acc);
all_conds -= acc;
}
}
}
virtual ~tau03_opt_search()
{
// Release all iterators on the stacks.
while (!st_blue.empty())
{
h.pop_notify(st_blue.front().s);
a_->release_iter(st_blue.front().it);
st_blue.pop_front();
}
while (!st_red.empty())
{
h.pop_notify(st_red.front().s);
a_->release_iter(st_red.front().it);
st_red.pop_front();
}
}
/// \brief Perform an emptiness check.
///
/// \return non null pointer iff the algorithm has found an
/// accepting path.
virtual emptiness_check_result_ptr check()
{
if (!st_blue.empty())
return nullptr;
assert(st_red.empty());
const state* s0 = a_->get_init_state();
inc_states();
h.add_new_state(s0, CYAN, current_weight);
push(st_blue, s0, bddfalse, bddfalse);
auto t = std::static_pointer_cast
(this->emptiness_check::shared_from_this());
if (dfs_blue())
return std::make_shared, heap>>(t);
return nullptr;
}
virtual std::ostream& print_stats(std::ostream &os) const
{
os << states() << " distinct nodes visited" << std::endl;
os << transitions() << " transitions explored" << std::endl;
os << max_depth() << " nodes for the maximal stack depth" << std::endl;
return os;
}
const heap& get_heap() const
{
return h;
}
const stack_type& get_st_blue() const
{
return st_blue;
}
const stack_type& get_st_red() const
{
return st_red;
}
private:
void push(stack_type& st, const state* s,
const bdd& label, const bdd& acc)
{
inc_depth();
tgba_succ_iterator* i = a_->succ_iter(s);
i->first();
st.emplace_front(s, i, label, acc);
}
void pop(stack_type& st)
{
dec_depth();
a_->release_iter(st.front().it);
st.pop_front();
}
bdd project_acc(bdd acc) const
{
bdd result = bddfalse;
for (std::vector::const_iterator i = cond.begin();
i != cond.end() && (acc & *i) != bddfalse;
++i)
result |= *i;
return result;
}
/// \brief weight of the state on top of the blue stack.
weight current_weight;
/// \brief Stack of the blue dfs.
stack_type st_blue;
/// \brief Stack of the red dfs.
stack_type st_red;
/// \brief Map where each visited state is colored
/// by the last dfs visiting it.
heap h;
/// The unique acceptance condition of the automaton \a a.
bdd all_acc;
/// Whether to use the "condition stack".
bool use_condition_stack;
/// Whether to use an ordering between the acceptance conditions.
/// Effective only if using the condition stack.
bool use_ordering;
/// Whether to use weights to abort earlier.
bool use_weights;
/// Whether to use weights in the red dfs.
bool use_red_weights;
/// Ordering of the acceptance conditions.
std::vector cond;
bool dfs_blue()
{
while (!st_blue.empty())
{
stack_item& f = st_blue.front();
trace << "DFS_BLUE treats: " << a_->format_state(f.s) << std::endl;
if (!f.it->done())
{
const state *s_prime = f.it->current_state();
trace << " Visit the successor: "
<< a_->format_state(s_prime) << std::endl;
bdd label = f.it->current_condition();
bdd acc = f.it->current_acceptance_conditions();
// Go down the edge (f.s,