Vcsn  2.8
Be Rational
epsilon-remover-separate_BASE_35473.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdexcept>
4 #include <type_traits>
5 #include <unordered_map>
6 #include <unordered_set>
7 #include <utility>
8 #include <vector>
9 
10 #include <vcsn/algos/copy.hh>
11 #include <vcsn/algos/dot.hh>
12 #include <vcsn/algos/fwd.hh>
13 #include <vcsn/core/kind.hh>
14 #include <vcsn/misc/debug-level.hh>
15 #include <vcsn/misc/direction.hh>
18 
19 #define STATS
20 
21 namespace vcsn
22 {
23  namespace detail
24  {
29  template <Automaton Aut,
30  bool has_one = labelset_t_of<Aut>::has_one()>
31  class epsilon_remover_separate
32  {
33  using automaton_t = std::remove_cv_t<Aut>;
35  using weight_t = typename weightset_t::value_t;
37 
44 
51 
52  public:
56  epsilon_remover_separate(const automaton_t& aut, bool prune = true)
57  : debug_(debug_level())
58  , ws_(*aut->weightset())
59  , prune_(prune)
60  , d2p_(states_size(aut),
61  aut_proper_t::element_type::null_state())
62  , p2d_(states_size(aut),
63  aut_dirty_t::element_type::null_state())
64  {
65  auto dirty_ctx = dirty_ctx_t{{}, ws_};
66  auto proper_ctx = make_proper_context(aut->context());
67  aut_dirty_ = make_shared_ptr<aut_dirty_t>(dirty_ctx);
68  aut_proper_ = make_shared_ptr<aut_proper_t>(proper_ctx);
69 
70  auto pcopier = make_copier(aut, aut_proper_);
71  auto dcopier = make_copier(aut, aut_dirty_);
72 
73  using state_t = state_t_of<automaton_t>;
74  using transition_t = transition_t_of<automaton_t>;
75  pcopier([](state_t) { return true; },
76  [&aut](transition_t t) {
77  return !aut->labelset()->is_one(aut->label_of(t));
78  }
79  );
80  dcopier([&aut](state_t s) {
81  return (!in(aut, s, aut->labelset()->one()).empty()
82  || !out(aut, s, aut->labelset()->one()).empty());
83  },
84  [&aut](transition_t t) {
85  return aut->labelset()->is_one(aut->label_of(t));
86  }
87  );
88 
89  const auto& dorigins = dcopier.state_map();
90  const auto& porigins = pcopier.state_map();
91  for (const auto& dp : dorigins)
92  {
93  auto pp = porigins.find(dp.first);
94  assert(pp != porigins.end());
95  d2p_[dp.second] = pp->second;
96  p2d_[pp->second] = dp.second;
97  }
98  }
99 
100  private:
103 
106  {
107  auto dirty_s = p2d_[proper_s];
108  if (auto p = profile_(proper_s))
109  {
110  auto in_dirty = in(aut_dirty_, dirty_s).size();
111  auto in_proper = in(aut_proper_, proper_s).size();
112  auto out_dirty = out(aut_dirty_, dirty_s).size();
113  auto out_proper = all_out(aut_proper_, proper_s).size();
114 
115  p->update(in_dirty, in_proper + in_dirty,
116  out_dirty, out_proper + out_dirty);
117  }
118  }
119 
122  void build_heap_()
123  {
124  for (auto s: aut_dirty_->states())
125  // We don't care about states without incoming spontaneous
126  // transitions.
127  if (in(aut_dirty_, s).size())
128  {
129  auto proper_s = d2p_[s];
130  auto h = todo_.emplace(profile_t{proper_s, 0, 0, 0, 0});
131  handles_.emplace(proper_s, h);
132  update_profile_(proper_s);
133  }
134  }
135 
137  void show_heap_() const
138  {
139  const char* sep = "";
140  for (auto i = todo_.ordered_begin(), end = todo_.ordered_end();
141  i != end; ++i)
142  {
143  std::cerr << sep << *i;
144  sep = " > ";
145  }
146  }
147 
151  {
152  if (3 < debug_)
153  {
154  std::cerr << "update heap (" << s << " : ";
155  show_heap_();
156  }
157  auto i = handles_.find(s);
158  if (i != handles_.end())
159  todo_.update(i->second);
160  if (3 < debug_)
161  {
162  std::cerr << ") => ";
163  show_heap_();
164  std::cerr << '\n';
165  }
166  }
167 
168 #ifdef STATS
169  unsigned added_ = 0;
170  unsigned removed_ = 0;
171 #endif
172 
176  {
177  auto i = handles_.find(s);
178  if (i == handles_.end())
179  return nullptr;
180  else
181  return &*i->second;
182  }
183 
195  void remover_on(state_proper_t proper_s, state_dirty_t dirty_s)
196  {
197  // The star of the weight of the loop on 's' (1 if no loop).
198  weight_t star = ws_.one();
199  using state_weight_t = std::pair<state_dirty_t, weight_t>;
200  auto closure = std::vector<state_weight_t>{};
201 
202 #ifdef STATS
203  // Number of transitions that will be removed.
204  auto removed = in(aut_dirty_, dirty_s).size();
205 #endif
206  // Iterate on a copy: we remove these transitions in the loop.
207  for (auto t : make_vector(in(aut_dirty_, dirty_s)))
208  {
209  weight_t weight = aut_dirty_->weight_of(t);
210  auto src = aut_dirty_->src_of(t);
211  if (src == dirty_s) //loop
212  star = ws_.star(weight);
213  else
214  closure.emplace_back(src, weight);
215  // Delete incoming epsilon transitions.
216  aut_dirty_->del_transition(t);
217  }
218 
219  /*
220  For each transition (t : s -- label|weight --> dst),
221  for each former
222  epsilon transition closure->first -- e|closure->second --> s
223  a transition
224  (closure->first -- label | closure->second*weight --> dst)
225  is added to the automaton (add, not set !!)
226 
227  If (s) is final with weight (weight),
228  for each former
229  epsilon transition closure->first -- e|closure->second --> s
230  pair-second * weight is added to the final weight
231  of closure->first
232  */
233 
234 
235  // TODO: factoring with lambda
236  for (auto t: out(aut_dirty_, dirty_s))
237  {
238  weight_t blow = ws_.mul(star, aut_dirty_->weight_of(t));
239  aut_dirty_->set_weight(t, blow);
240 
241  auto dst = aut_dirty_->dst_of(t);
242  for (auto pair: closure)
243  {
244  auto src = pair.first;
245  weight_t w = ws_.mul(pair.second, blow);
246  aut_dirty_->add_transition(src, dst, {}, w);
247  }
248  }
249 
250  for (auto t: all_out(aut_proper_, proper_s))
251  {
252  weight_t blow = ws_.mul(star, aut_proper_->weight_of(t));
253  aut_proper_->set_weight(t, blow);
254 
255  label_proper_t label = aut_proper_->label_of(t);
256  auto dst = aut_proper_->dst_of(t);
257  for (auto pair: closure)
258  {
259  auto src = pair.first;
260  weight_t w = ws_.mul(pair.second, blow);
261  aut_proper_->add_transition(d2p_[src], dst, label, w);
262  }
263  }
264 #ifdef STATS
265  // Number of transition that have been added.
266  auto added = (all_out(aut_proper_, proper_s).size()
267  + out(aut_dirty_, dirty_s).size()) * closure.size();
268 #endif
269  if (prune_
270  && in(aut_dirty_, dirty_s).empty()
271  && all_in(aut_proper_, proper_s).empty())
272  {
273 #ifdef STATS
274  removed += (all_out(aut_proper_, proper_s).size()
275  + out(aut_dirty_, dirty_s).size());
276 #endif
277  aut_proper_->del_state(proper_s);
278  aut_dirty_->del_state(dirty_s);
279  }
280 #ifdef STATS
281  added_ += added;
282  removed_ += removed;
283  if (1 < debug_)
284  std::cerr << " -" << removed << "+" << added
285  << " (-" << removed_ << "+" << added_ << ")";
286 #endif
287  }
288 
289  public:
292  {
293  state_dirty_t dirty_s = p2d_[proper_s];
294  if (dirty_s == aut_dirty_->null_state())
295  return false;
296  else
297  return !in(aut_dirty_, dirty_s).empty();
298  }
299 
301  void remover_on(state_proper_t proper_s)
302  {
303  state_dirty_t dirty_s = p2d_[proper_s];
304  if (dirty_s != aut_dirty_->null_state())
305  remover_on(proper_s, dirty_s);
306  }
307 
321  {
322  if (4 < debug_)
323  {
324  dot(aut_proper_, std::cerr) << '\n';
325  dot(aut_dirty_, std::cerr) << '\n';
326  }
327  build_heap_();
328  /* For each state (s), for each incoming epsilon-transitions
329  (t), if (t) is a loop, the star of its weight is computed,
330  otherwise, (t) is stored into the closure list. Then (t)
331  is removed. */
332 
333  // The neighbors of s: their profiles need to be updated after
334  // s was processed.
335  auto neighbors = std::unordered_set<state_proper_t>{};
336  while (!todo_.empty())
337  {
338  if (2 < debug_)
339  {
340  std::cerr << "Before: ";
341  show_heap_();
342  std::cerr << '\n';
343  }
344  auto p = todo_.top();
345  todo_.pop();
346  if (1 < debug_)
347  std::cerr << "Remove: " << p;
348 
349  state_proper_t proper_s = p.state;
350  auto dirty_s = p2d_[proper_s];
351  handles_.erase(proper_s);
352 
353  // Adjacent states.
354  neighbors.clear();
355  for (auto t: in(aut_proper_, proper_s))
356  if (aut_proper_->src_of(t) != proper_s)
357  neighbors.emplace(aut_proper_->src_of(t));
358  for (auto t: out(aut_proper_, proper_s))
359  if (aut_proper_->dst_of(t) != proper_s)
360  neighbors.emplace(aut_proper_->dst_of(t));
361 
362  if (dirty_s != aut_dirty_->null_state())
363  {
364  state_dirty_t n;
365  for (auto t: in(aut_dirty_, dirty_s))
366  if ((n = aut_dirty_->src_of(t)) != dirty_s)
367  neighbors.emplace(d2p_[n]);
368  for (auto t: out(aut_dirty_, dirty_s))
369  if ((n = aut_dirty_->dst_of(t)) != dirty_s)
370  neighbors.emplace(d2p_[n]);
371 
372  remover_on(proper_s, dirty_s);
373  }
374 
375  // Update all neighbors and then the heap.
376  for (auto n: neighbors)
377  update_profile_(n);
378  for (auto n: neighbors)
379  update_heap_(n);
380  if (1 < debug_)
381  std::cerr << " #tr: "
382  << transitions(aut_dirty_).size()
383  << "/" << (transitions(aut_dirty_).size()
384  + transitions(aut_proper_).size())
385  << '\n';
386  if (2 < debug_)
387  {
388  std::cerr << "After: ";
389  show_heap_();
390  std::cerr << '\n';
391  }
392  if (4 < debug_)
393  {
394  dot(aut_proper_, std::cerr) << '\n';
395  dot(aut_dirty_, std::cerr) << '\n';
396  }
397  if (2 < debug_)
398  std::cerr << '\n';
399  }
400 #ifdef STATS
401  if (0 < debug_)
402  std::cerr << "Total transitions -" << removed_
403  << "+" << added_ << '\n';
404 #endif
405 
406  return aut_proper_;
407  }
408 
411  {
412  return aut_proper_;
413  }
414 
415  private:
417  int debug_;
418 
422 
426 
428  const weightset_t& ws_;
429 
432  heap_t todo_;
434  std::unordered_map<state_proper_t, typename heap_t::handle_type> handles_;
435 
437  bool prune_;
438 
439  std::vector<state_proper_t> d2p_; // dirty states -> proper states
440  std::vector<state_dirty_t> p2d_; // proper states -> dirty states
441  };
442 
443  template <Automaton Aut>
444  class epsilon_remover_separate<Aut, false>
445  {
446  using automaton_t = std::remove_cv_t<Aut>;
448  public:
450  : aut_(aut)
451  {}
452 
454  {
455  return copy(aut_);
456  }
457 
459  {
460  return aut_;
461  }
462 
463  private:
464  automaton_t aut_;
465  };
466  }
467 } // namespace vcsn
std::shared_ptr< detail::mutable_automaton_impl< Context > > mutable_automaton
Definition: fwd.hh:25
void remover_on(state_proper_t proper_s)
Wrapper around remover_on() which does a lookup of the dirty state.
void remover_on(state_proper_t proper_s, state_dirty_t dirty_s)
For each state (s), for each incoming epsilon-transitions (t), if (t) is a loop, the star of its weig...
typename detail::weightset_t_of_impl< base_t< ValueSet > >::type weightset_t_of
Definition: traits.hh:67
bool prune_
Whether to prune states that become inaccessible.
epsilon_remover_separate(const automaton_t &aut, bool prune=true)
Get ready to eliminate spontaneous transitions.
aut_dirty_t aut_dirty_
The sponteanous part.
typename detail::transition_t_of_impl< base_t< ValueSet > >::type transition_t_of
Definition: traits.hh:65
typename detail::state_t_of_impl< base_t< ValueSet > >::type state_t_of
Definition: traits.hh:64
size_t states_size(const Aut &aut)
The largest state number, plus one.
Definition: automaton.hh:41
std::vector< state_proper_t > d2p_
dirty states -> proper states.
profile_t * profile_(state_proper_t s) const
The profile of state s, or nullptr if it is not to be processed.
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
Definition: vector.hh:18
This class contains the core of the proper algorithm.
void build_heap_()
Build the profiles and the heap for states with incoming spontaneous transitions. ...
vcsn::min_fibonacci_heap< profile_t > heap_t
Min-heap to decide the order of state-elimination.
auto all_in(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions entering state s.
Definition: automaton.hh:116
aut_proper_t get_proper()
Return the proper part. Needed by lazy algorithm.
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
Definition: automaton.hh:67
const weightset_t & ws_
Shorthand to the weightset.
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
Definition: pair.hh:252
auto copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans) -> decltype(keep_state(input->null_state()), keep_trans(input->null_transition()), make_fresh_automaton< AutIn, AutOut >(input))
A copy of input keeping only its states that are accepted by keep_state, and transitions accepted by ...
Definition: copy.hh:322
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
std::ostream & dot(const Aut &aut, std::ostream &out=std::cout, format fmt={}, bool mathjax=false)
Print an automaton in Graphviz&#39;s Dot format.
Definition: dot.hh:435
aut_proper_t operator()()
Remove all the states with incoming spontaneous transitions.
void update_profile_(state_proper_t proper_s)
Update the profile of s.
Definition: a-star.hh:8
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Definition: automaton.hh:135
mutable_automaton< dirty_ctx_t > aut_dirty_t
Spontaneous automaton.
void update_heap_(state_proper_t s)
Update the heap for s.
typename detail::label_t_of_impl< base_t< ValueSet > >::type label_t_of
Definition: traits.hh:62
This is used by some epsilon removal algorithms.
detail::copier< AutIn, AutOut > make_copier(const AutIn &in, AutOut &out, bool safe=true)
Build an automaton copier.
Definition: copy.hh:256
std::unordered_map< state_proper_t, typename heap_t::handle_type > handles_
Map: state -> heap-handle.
fresh_automaton_t_of< Aut, proper_ctx_t > aut_proper_t
Proper automaton.
void show_heap_() const
Show the heap, for debugging.
boost::heap::fibonacci_heap< Elt, detail::comparator_t< Elt > > min_fibonacci_heap
value_impl< detail::label_tag > label
Definition: fwd.hh:32
int debug_
Debug level. The higher, the more details are reported.
std::vector< state_dirty_t > p2d_
proper states -> dirty states.
#define Automaton
Definition: automaton.hh:23
static int debug_level()
Debug level set in the user&#39;s environment.
Definition: debug-level.hh:11
auto transitions(const Aut &aut) -> decltype(all_transitions(aut, is_special_t< Aut >
All the transition indexes between visible states.
Definition: automaton.hh:248
auto make_proper_context(const context< LabelSet, WeightSet > &ctx) -> proper_context< context< LabelSet, WeightSet >>
From a context, its non-nullable context.
Definition: labelset.hh:216
bool state_has_spontaneous_in(state_proper_t proper_s) const
Whether the given state has incoming spontaneous transitions.
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Given an automaton type, the type of its copies.
Definition: traits.hh:82
value_impl< detail::weight_tag > weight
Definition: fwd.hh:34
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:86