Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
push-weights.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_PUSH_WEIGHTS_HH
2 # define VCSN_ALGOS_PUSH_WEIGHTS_HH
3 
4 # include <queue>
5 # include <unordered_map>
6 
7 # include <vcsn/algos/accessible.hh>
8 # include <vcsn/algos/compose.hh>
9 # include <vcsn/algos/distance.hh>
11 # include <vcsn/dyn/automaton.hh>
12 # include <vcsn/dyn/fwd.hh>
13 # include <vcsn/labelset/tupleset.hh>
16 
17 namespace vcsn
18 {
19  /*--------------.
20  | push weights. |
21  `--------------*/
22 
25  template <typename Aut>
26  weight_t_of<Aut>
28  {
29  auto d = ss_shortest_distance(aut, s0);
30  return d[aut->post()];
31  }
32 
35  template <typename Aut>
36  std::unordered_map<state_t_of<Aut>, weight_t_of<Aut>>
38  {
39  std::unordered_map<state_t_of<Aut>, weight_t_of<Aut>> res;
40  for (auto s : aut->states())
41  {
42  auto w = shortest_distance_to_finals(aut, s);
43  res.emplace(s, w);
44  }
45  return res;
46  }
47 
51  template <typename Aut>
52  auto
53  push_weights(const Aut& aut)
54  -> decltype(::vcsn::copy(aut))
55  {
56  auto res = ::vcsn::copy(aut);
57  auto distances = shortest_distance_to_finals(res);
58  auto ws = *res->weightset();
59  distances[res->post()] = ws.one();
60  for (auto t : res->all_transitions())
61  {
62  auto ds = distances[res->src_of(t)];
63  auto de = distances[res->dst_of(t)];
64  auto w = ws.mul(res->weight_of(t), de);
65  if (res->src_of(t) == res->pre())
66  res->set_weight(t, de);
67  else if (!ws.is_zero(ds))
68  res->set_weight(t, ws.rdiv(w, ds));
69  }
70  return res;
71  }
72 
73  namespace dyn
74  {
75  namespace detail
76  {
78  template <typename Aut>
79  automaton
80  push_weights(const automaton& aut)
81  {
82  const auto& a = aut->as<Aut>();
84  }
85 
87  (const automaton& aut) -> automaton);
88  }
89  }
90 
91 } // namespace vcsn
92 
93 #endif // !VCSN_ALGOS_PUSH_WEIGHTS_HH
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
weight_t_of< Aut > shortest_distance_to_finals(Aut aut, state_t_of< Aut > s0)
Find shorhest of s0 to the final states of aut by using single source shortest distance.
Definition: push-weights.hh:27
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:77
AutOut copy(const AutIn &input, Pred keep_state)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:171
std::unordered_map< state_t_of< Aut >, weight_t_of< Aut > > ss_shortest_distance(Aut aut, state_t_of< Aut > s0)
Single source shortest distance.
Definition: distance.hh:23
auto push_weights(const Aut &aut) -> decltype(::vcsn::copy(aut))
The algorithm weight pushing.
Definition: push-weights.hh:53
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:37
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
automaton push_weights(const automaton &aut)
Bridge.
Definition: push-weights.hh:80