Commit 774a266b authored by Tomáš Babiak's avatar Tomáš Babiak Committed by Alexandre Duret-Lutz
Browse files

degen: introduce three optimizations

* src/tgbaalgos/degen.cc, src/tgbaalgos/degen.hh: Add three
options use_z_level, use_cust_acc_orders, and use_lvl_cache.
parent 35e16a0b
......@@ -24,6 +24,10 @@
#include "ltlast/constant.hh"
#include <deque>
#include <vector>
#include "tgbaalgos/scc.hh"
#include "tgba/bddprint.hh"
//#define DEGEN_DEBUG
namespace spot
{
......@@ -94,6 +98,12 @@ namespace spot
(*old)->destroy();
}
}
size_t
size()
{
return m.size();
}
};
// Acceptance set common to all outgoing transitions of some state.
......@@ -144,10 +154,90 @@ namespace spot
return i->second.second;
}
};
// Order of accepting sets (for one SCC)
class acc_order
{
std::vector<bdd> order_;
bdd found_;
public:
unsigned
next_level(bdd all, int slevel, bdd acc)
{
bdd temp = acc;
if (all != found_)
{
// Check for new conditions in acc
if ((acc & found_) != acc)
{
bdd acc_t = acc;
while (acc_t != bddfalse)
{
bdd next = bdd_satone(acc_t);
acc_t -= next;
// Add new condition
if ((next & found_) != next)
{
order_.push_back(next);
found_ |= next;
}
}
}
}
acc = temp;
unsigned next = slevel;
while (next < order_.size()
&& (acc & order_[next]) == order_[next])
++next;
return next;
}
void
print(int scc, const bdd_dict* dict)
{
std::vector<bdd>::iterator i;
std::cout << "Order_" << scc << ":\t";
for (i=order_.begin(); i!=order_.end(); i++)
{
bdd_print_acc(std::cout, dict, *i);
std::cout << ", ";
}
std::cout << std::endl;
}
};
// Accepting order for each SCC
class scc_orders
{
bdd all_;
std::map<int, acc_order> orders_;
public:
scc_orders(bdd all): all_(all)
{
}
unsigned
next_level(int scc, int slevel, bdd acc)
{
return orders_[scc].next_level(all_, slevel, acc);
}
void
print(const bdd_dict* dict)
{
std::map<int, acc_order>::iterator i;
for (i = orders_.begin(); i != orders_.end(); i++)
i->second.print(i->first, dict);
}
};
}
sba*
degeneralize(const tgba* a)
degeneralize(const tgba* a, bool use_z_lvl, bool use_cust_acc_orders,
bool use_lvl_cache)
{
bdd_dict* dict = a->get_dict();
......@@ -184,6 +274,9 @@ namespace spot
}
}
// Initialize scc_orders
scc_orders orders(a->all_acceptance_conditions());
outgoing_acc outgoing(a);
// Make sure we always use the same pointer for identical states
......@@ -202,6 +295,17 @@ namespace spot
typedef std::map<int, state_explicit_number::transition*> tr_cache_t;
tr_cache_t tr_cache;
// State level cash
typedef std::map<const state*, int> lvl_cache_t;
lvl_cache_t lvl_cache;
// Compute SCCs in order to use custom acc order for each SCC
// or lvl_cache
scc_map m(a);
if (use_cust_acc_orders || use_lvl_cache)
m.build_map();
queue_t todo;
const state* s0 = uniq(a->get_init_state());
......@@ -229,9 +333,25 @@ namespace spot
delete it;
}
#ifdef DEGEN_DEBUG
std::map<const state*, int>names;
names[s.first] = 1;
ds2num[s] = 10000*names[s.first] + 100*s.second + m.scc_of_state(s.first);
#else
ds2num[s] = 0;
#endif
todo.push_back(s);
// If use_lvl_cache is on insert initial state to level cash
// Level cash stores first encountered level for each state.
// When entering an SCC first the lvl_cache is checked.
// If such state exists level from chache is used.
// If not, a new level (starting with 0) is computed.
if (use_lvl_cache)
lvl_cache[s.first] = s.second;
while (!todo.empty())
{
s = todo.front();
......@@ -245,11 +365,27 @@ namespace spot
if (is_acc)
slevel = 0;
// Check SCC for state s
int s_scc = -1;
if (use_lvl_cache || use_cust_acc_orders)
s_scc = m.scc_of_state(s.first);
tgba_succ_iterator* i = a->succ_iter(s.first);
for (i->first(); !i->done(); i->next())
{
degen_state d(uniq(i->current_state()), 0);
#ifdef DEGEN_DEBUG
if (names.find(d.first) == names.end())
names[d.first] = uniq.size();
#endif
// Check whether the target's SCC is accepting
bool is_scc_acc = false;
int scc = m.scc_of_state(i->current_state());
if (m.accepting(scc))
is_scc_acc = true;
// The old level is slevel. What should be the new one?
bdd acc = i->current_acceptance_conditions();
bdd otheracc = outgoing.common_acc(d.first);
......@@ -320,15 +456,36 @@ namespace spot
// address= {Paris, France},
// month = {December}
// }
unsigned next = slevel;
if (is_scc_acc)
{
acc |= otheracc;
// If use_z_lvl is on, start with level zero 0 when swhitching SCCs
unsigned next = (!use_z_lvl || s_scc == scc)?slevel:0;
// If lvl_cache is used and switching SCCs, use level from cahce
if (use_lvl_cache && s_scc != scc && lvl_cache.find(d.first) != lvl_cache.end())
{
d.second = lvl_cache[d.first];
}
else
{
// If using custom acc orders, get next level for this scc
if (use_cust_acc_orders)
d.second = orders.next_level(scc, next, acc);
// Else compute level according the global acc order
else
{
// Consider both the current acceptance sets, and the
// acceptance sets common to the outgoing transitions of
// the destination state.
acc |= otheracc;
while (next < order.size() && bdd_implies(order[next], acc))
while (next < order.size()
&& (acc & order[next]) == order[next])
++next;
d.second = next;
}
}
}
// Have we already seen this destination?
int dest;
......@@ -339,9 +496,16 @@ namespace spot
}
else
{
#ifdef DEGEN_DEBUG
dest = 10000*names[d.first] + 100*d.second + scc;
#else
dest = ds2num.size();
#endif
ds2num[d] = dest;
todo.push_back(d);
// Insert new state to cash
if (use_lvl_cache && lvl_cache.find(d.first) == lvl_cache.end())
lvl_cache[d.first] = d.second;
}
state_explicit_number::transition*& t =
......@@ -366,6 +530,20 @@ namespace spot
delete i;
tr_cache.clear();
}
#ifdef DEGEN_DEBUG
std::vector<bdd>::iterator i;
std::cout << "Orig. order: \t";
for (i=order.begin(); i!=order.end(); i++)
{
bdd_print_acc(std::cout, dict, *i);
std::cout << ", ";
}
std::cout << std::endl;
orders.print(dict);
#endif
res->merge_transitions();
return res;
}
}
......@@ -35,7 +35,8 @@ namespace spot
///
/// \see tgba_sba_proxy, tgba_tba_proxy
/// \ingroup tgba_misc
sba* degeneralize(const tgba* a);
sba* degeneralize(const tgba* a, bool use_z_lvl = true, bool use_cust_acc_orders = true,
bool use_lvl_cache = true);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment