Commit 8dbc9424 authored by Denis Poitrenaud's avatar Denis Poitrenaud
Browse files

* src/tgbaalgos/ndfs_result.hh: Rewrite the computation of accepting

runs.
* src/tgbaalgos/bfssteps.hh, src/tgbaalgos/bfssteps.cc: Add the method
finalize witch compute (by default) the traversed path.
* src/tgbaalgos/magic.cc, src/tgbaalgos/se05.cc: Fix a bug concerning
the heap used for bit state hashing version and ajust the prototype of
has_been_visited and pop_notify.
* src/tgbaalgos/tau03.cc, src/tgbaalgos/tau03opt.cc: ajust the prototype
of has_been_visited and pop_notify.
parent 0c2c12a8
2004-12-20 Denis Poitrenaud <Denis.Poitrenaud@lip6.fr>
* src/tgbaalgos/ndfs_result.hh: Rewrite the computation of accepting
runs.
* src/tgbaalgos/bfssteps.hh, src/tgbaalgos/bfssteps.cc: Add the method
finalize witch compute (by default) the traversed path.
* src/tgbaalgos/magic.cc, src/tgbaalgos/se05.cc: Fix a bug concerning
the heap used for bit state hashing version and ajust the prototype of
has_been_visited and pop_notify.
* src/tgbaalgos/tau03.cc, src/tgbaalgos/tau03opt.cc: ajust the prototype
of has_been_visited and pop_notify.
2004-12-17 Alexandre Duret-Lutz <adl@src.lip6.fr>
* src/tgbaalgos/ndfs_result.hh: Include misc/hash.hh.
......
......@@ -19,7 +19,6 @@
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
#include <map>
#include <deque>
#include <utility>
#include "tgba/tgba.hh"
......@@ -37,6 +36,28 @@ namespace spot
{
}
void
bfs_steps::finalize(const std::map<const state*, tgba_run::step,
state_ptr_less_than>& father, const tgba_run::step& s,
const state* start, tgba_run::steps& l)
{
tgba_run::steps p;
tgba_run::step current = s;
for (;;)
{
tgba_run::step tmp = current;
tmp.s = tmp.s->clone();
p.push_front(tmp);
if (current.s == start)
break;
std::map<const state*, tgba_run::step,
state_ptr_less_than>::const_iterator it = father.find(current.s);
assert(it!=father.end());
current = it->second;
}
l.splice(l.end(), p);
}
const state*
bfs_steps::search(const state* start, tgba_run::steps& l)
{
......@@ -68,19 +89,7 @@ namespace spot
if (match(s, dest))
{
// Found it!
tgba_run::steps p;
for (;;)
{
tgba_run::step tmp = s;
tmp.s = tmp.s->clone();
p.push_front(tmp);
if (s.s == start)
break;
s = father[s.s];
}
l.splice(l.end(), p);
finalize(father, s, start, l);
delete i;
return dest;
}
......
......@@ -22,6 +22,8 @@
#ifndef SPOT_TGBAALGOS_BFSSTEPS_HH
# define SPOT_TGBAALGOS_BFSSTEPS_HH
#include <map>
#include "tgba/state.hh"
#include "emptiness.hh"
namespace spot
......@@ -82,6 +84,14 @@ namespace spot
/// augmented with the shortest past that ends with this
/// transition.
virtual bool match(tgba_run::step& step, const state* dest) = 0;
virtual void finalize(const std::map<const state*,
tgba_run::step,
state_ptr_less_than>& father,
const tgba_run::step& s,
const state* start,
tgba_run::steps& l);
protected:
const tgba* a_; ///< The spot::tgba we are searching into.
};
......
......@@ -377,21 +377,14 @@ namespace spot
h.insert(std::make_pair(s, c));
}
void pop_notify(const state*)
void pop_notify(const state*) const
{
}
bool has_been_visited(const state*& s) const
bool has_been_visited(const state* s) const
{
hash_type::const_iterator it = h.find(s);
if (it==h.end())
return false;
if (s!=it->first)
{
delete s;
s = it->first;
}
return true;
return (it != h.end());
}
private:
......@@ -452,15 +445,15 @@ namespace spot
cr.set_color(c);
}
void pop_notify(const state* s)
void pop_notify(const state* s) const
{
delete s;
}
bool has_been_visited(const state*& s) const
bool has_been_visited(const state* s) const
{
size_t ha = s->hash();
return color((h[ha%size] >> (ha%4)) & 3U) != WHITE;
return color((h[ha%size] >> ((ha%4)*2)) & 3U) != WHITE;
}
private:
......
......@@ -22,25 +22,24 @@
#ifndef SPOT_TGBAALGOS_NDFS_RESULT_HH
# define SPOT_TGBAALGOS_NDFS_RESULT_HH
//#define TRACE
//#define NDFSR_TRACE
#include <iostream>
#ifdef TRACE
#define trace std::cerr
#ifdef NDFSR_TRACE
#define ndfsr_trace std::cerr
#else
#define trace while (0) std::cerr
#define ndfsr_trace while (0) std::cerr
#endif
#include <cassert>
#include <list>
#include "misc/hash.hh"
#include "tgba/tgba.hh"
#include "emptiness.hh"
#include "emptiness_stats.hh"
#include "bfssteps.hh"
#include "misc/hash.hh"
/// FIXME:
/// * Add the necessary calls to pop_notify.
namespace spot
{
......@@ -73,11 +72,6 @@ namespace spot
virtual ~ndfs_result()
{
while (!st1.empty())
{
delete st1.front().it;
st1.pop_front();
}
}
virtual tgba_run* accepting_run()
......@@ -87,60 +81,107 @@ namespace spot
assert(!stb.empty());
tgba_run* run = new tgba_run;
const state* target = str.empty()?stb.front().s:str.front().s;
bdd covered_acc = bddfalse;
typename stack_type::const_reverse_iterator i, j;
accepting_transitions_list acc_trans;
i = j = stb.rbegin(); ++j;
for (; i->s->compare(target) != 0; ++i, ++j)
{
tgba_run::step s = { i->s->clone(), j->label, j->acc };
run->prefix.push_back(s);
}
const state* start;
if (!str.empty())
if (str.empty())
start = stb.front().s->clone();
else
{
typename stack_type::const_reverse_iterator end = stb.rend();
for (; j != end; ++i, ++j)
start = str.front().s->clone();
if (a_->number_of_acceptance_conditions() == 0)
{
covered_acc |= j->acc;
tgba_run::step s = { i->s->clone(), j->label, j->acc };
run->cycle.push_back(s);
// take arbitrarily the last transition on the red stack
stack_type::const_iterator i, j;
i = j = str.begin(); ++i;
if (i == str.end())
i = stb.begin();
transition t = { i->s->clone(), j->label, j->acc,
j->s->clone() };
assert(h_.has_been_visited(t.source));
assert(h_.has_been_visited(t.dest));
acc_trans.push_back(t);
}
else
{
// ignore the prefix
stack_type::const_reverse_iterator i, j;
j = str.rbegin();
covered_acc |= j->acc;
tgba_run::step s = { i->s->clone(), j->label, j->acc };
run->cycle.push_back(s);
i = j = stb.rbegin(); ++j;
for (; i->s->compare(start) != 0; ++i, ++j)
{
}
i = j; ++j;
end = str.rend();
for (; j != end; ++i, ++j)
{
covered_acc |= j->acc;
tgba_run::step s = { i->s->clone(), j->label, j->acc };
run->cycle.push_back(s);
stack_type::const_reverse_iterator end = stb.rend();
for (; j != end; ++i, ++j)
{
if ((covered_acc & j->acc) != j->acc)
{
transition t = { i->s->clone(), j->label, j->acc,
j->s->clone() };
assert(h_.has_been_visited(t.source));
assert(h_.has_been_visited(t.dest));
acc_trans.push_back(t);
covered_acc |= j->acc;
}
}
j = str.rbegin();
if ((covered_acc & j->acc) != j->acc)
{
transition t = { i->s->clone(), j->label, j->acc,
j->s->clone() };
assert(h_.has_been_visited(t.source));
assert(h_.has_been_visited(t.dest));
acc_trans.push_back(t);
covered_acc |= j->acc;
}
i = j; ++j;
end = str.rend();
for (; j != end; ++i, ++j)
{
if ((covered_acc & j->acc) != j->acc)
{
transition t = { i->s->clone(), j->label, j->acc,
j->s->clone() };
assert(h_.has_been_visited(t.source));
assert(h_.has_been_visited(t.dest));
acc_trans.push_back(t);
covered_acc |= j->acc;
}
}
}
}
if (a_->all_acceptance_conditions() != covered_acc)
{
// try if any to minimize the first loop in run->cycle ??
// what transitions have to be preserved (it depend on
// the detection (in the blue or red dfs) ??
tgba_succ_iterator* i = a_->succ_iter(target);
i->first();
st1.push_front(stack_item(target, i, bddfalse, bddfalse));
bool b = dfs(target, run->cycle, covered_acc);
bool b = dfs(start, acc_trans, covered_acc);
assert(b);
(void)b;
while (!st1.empty())
{
delete st1.front().it;
st1.pop_front();
}
}
delete start;
assert(!acc_trans.empty());
tgba_run* run = new tgba_run;
// construct run->cycle from acc_trans.
construct_cycle(run, acc_trans);
// construct run->prefix (a minimal path from the initial state to any
// state of run->cycle) and adjust the cycle to the state reached by the
// prefix.
construct_prefix(run);
for (typename accepting_transitions_list::const_iterator i =
acc_trans.begin();
i != acc_trans.end(); ++i)
{
delete i->source;
delete i->dest;
}
return run;
......@@ -149,26 +190,175 @@ namespace spot
private:
const ndfs_search& ms_;
const heap& h_;
stack_type st1;
struct transition {
const state* source;
bdd label;
bdd acc;
const state* dest;
};
typedef std::list<transition> accepting_transitions_list;
typedef Sgi::hash_set<const state*,
state_ptr_hash, state_ptr_equal> state_set;
class shortest_path: public bfs_steps
void clean(stack_type& st1, state_set& seen, state_set& dead)
{
while (!st1.empty())
{
delete st1.front().it;
st1.pop_front();
}
for (state_set::iterator i = seen.begin(); i != seen.end();)
{
const state* s = *i;
++i;
delete s;
}
for (state_set::iterator i = dead.begin(); i != dead.end();)
{
const state* s = *i;
++i;
delete s;
}
}
bool dfs(const state* target, accepting_transitions_list& acc_trans,
bdd& covered_acc)
{
assert(h_.has_been_visited(target));
stack_type st1;
state_set seen, dead;
const state* start = target->clone();
seen.insert(start);
tgba_succ_iterator* i = a_->succ_iter(start);
i->first();
st1.push_front(stack_item(start, i, bddfalse, bddfalse));
while (!st1.empty())
{
stack_item& f = st1.front();
ndfsr_trace << "DFS1 treats: " << a_->format_state(f.s)
<< std::endl;
if (!f.it->done())
{
const state *s_prime = f.it->current_state();
ndfsr_trace << " Visit the successor: "
<< a_->format_state(s_prime) << std::endl;
bdd label = f.it->current_condition();
bdd acc = f.it->current_acceptance_conditions();
f.it->next();
if (h_.has_been_visited(s_prime))
{
if (dead.find(s_prime) != dead.end())
{
ndfsr_trace << " it is dead, pop it" << std::endl;
delete s_prime;
}
else if (seen.find(s_prime) == seen.end())
{
ndfsr_trace << " it is not seen, go down" << std::endl;
seen.insert(s_prime);
tgba_succ_iterator* i = a_->succ_iter(s_prime);
i->first();
st1.push_front(stack_item(s_prime, i, label, acc));
}
else if ((acc & covered_acc) != acc)
{
ndfsr_trace << " a propagation is needed, "
<< "start a search" << std::endl;
if (search(s_prime, target, dead))
{
transition t = { f.s->clone(), label, acc,
s_prime->clone() };
assert(h_.has_been_visited(t.source));
assert(h_.has_been_visited(t.dest));
acc_trans.push_back(t);
covered_acc |= acc;
if (covered_acc == a_->all_acceptance_conditions())
{
clean(st1, seen, dead);
delete s_prime;
return true;
}
}
delete s_prime;
}
else
{
ndfsr_trace << " already seen, pop it" << std::endl;
delete s_prime;
}
}
else
{
ndfsr_trace << " not seen during the search, pop it"
<< std::endl;
delete s_prime;
}
}
else
{
ndfsr_trace << " all the successors have been visited"
<< std::endl;
stack_item f_dest(f);
delete st1.front().it;
st1.pop_front();
if (!st1.empty() && (f_dest.acc & covered_acc) != f_dest.acc)
{
ndfsr_trace << " a propagation is needed, start a search"
<< std::endl;
if (search(f_dest.s, target, dead))
{
transition t = { st1.front().s->clone(),
f_dest.label, f_dest.acc,
f_dest.s->clone() };
assert(h_.has_been_visited(t.source));
assert(h_.has_been_visited(t.dest));
acc_trans.push_back(t);
covered_acc |= f_dest.acc;
if (covered_acc == a_->all_acceptance_conditions())
{
clean(st1, seen, dead);
return true;
}
}
}
else
{
ndfsr_trace << " no propagation needed, pop it"
<< std::endl;
}
}
}
clean(st1, seen, dead);
return false;
}
class test_path: public bfs_steps
{
public:
shortest_path(const tgba* a, const state* t,
test_path(const tgba* a, const state* t,
const state_set& d, const heap& h)
: bfs_steps(a), target(t), dead(d), h(h)
{
}
~shortest_path()
~test_path()
{
state_set::const_iterator i = seen.begin();
while (i != seen.end())
{
const state* ptr = *i;
++i;
delete ptr;
}
}
const state*
search(const state* start, tgba_run::steps& l)
const state* search(const state* start, tgba_run::steps& l)
{
const state* s = filter(start);
if (s)
......@@ -177,28 +367,30 @@ namespace spot
return 0;
}
const state*
filter(const state* s)
const state* filter(const state* s)
{
if (!h.has_been_visited(s))
if (!h.has_been_visited(s) || seen.find(s) != seen.end() ||
dead.find(s) != dead.end())
{
delete s;
return 0;
}
if (dead.find(s) != dead.end())
return 0;
seen.insert(s);
return s;
}
const state_set&
get_seen() const
void finalize(
const std::map<const state*, tgba_run::step, state_ptr_less_than>&,
const tgba_run::step&, const state*, tgba_run::steps&)
{
}
const state_set& get_seen() const
{
return seen;
}
bool
match(tgba_run::step&, const state* dest)
bool match(tgba_run::step&, const state* dest)
{
return target->compare(dest) == 0;
}
......@@ -210,139 +402,228 @@ namespace spot
const heap& h;
};
void complete_cycle(tgba_run::steps& cycle, bdd& covered_acc,
const tgba_run::step& start, tgba_run::steps& path,
const state_set& dead)
bool search(const state* start, const state* target, state_set& dead)
{
tgba_run::steps new_cycle;
tgba_run::steps path;
if (start->compare(target) == 0)
return true;
// find the minimal path between st1.back().s and st1.front().s
if (st1.back().s->compare(st1.front().s)!=0)
test_path s(a_, target, dead, h_);
const state* res = s.search(start->clone(), path);
if (res)
{
shortest_path s(a_, st1.front().s, dead, h_);
const state* res = s.search(st1.back().s, new_cycle);
assert(res && res->compare(st1.front().s) == 0);
(void)res;
for (tgba_run::steps::const_iterator it = new_cycle.begin();
it != new_cycle.end(); ++it)
covered_acc |= it->acc;
assert(res->compare(target) == 0);
return true;
}
else
{
state_set::const_iterator it;
for (it = s.get_seen().begin(); it != s.get_seen().end(); ++it)
dead.insert((*it)->clone());
return false;
}
}
// traverse the accepting transition
covered_acc |= start.acc;
tgba_run::step s = { st1.front().s->clone(), start.label, start.acc };
new_cycle.push_back(s);
typedef Sgi::hash_multimap<const state*, transition,
state_ptr_hash, state_ptr_equal> m_source_trans;
// follow the minimal path returning to st1.back().s
for (tgba_run::steps::const_iterator it = path.begin();
it != path.end(); ++it)
covered_acc |= it->acc;
new_cycle.splice(new_cycle.end(), path);
class min_path: public bfs_steps
{
public:
min_path(const tgba* a, const m_source_trans& target, const heap& h)