7 #include <boost/heap/binomial_heap.hpp>
14 #include <vcsn/dyn/fwd.hh>
36 template <Automaton Aut>
56 using profile_t = std::tuple<state_t, word_t, weight_t>;
71 if (weightset_t::less(std::get<2>(l), std::get<2>(r)))
73 else if (weightset_t::less(std::get<2>(r), std::get<2>(l)))
75 else if (labelset_t::less(std::get<1>(l), std::get<1>(r)))
77 else if (labelset_t::less(std::get<1>(r), std::get<1>(l)))
79 else if (std::get<0>(r) == automaton_t::element_type::post())
81 else if (std::get<0>(l) == automaton_t::element_type::post())
84 return std::get<0>(l) < std::get<0>(r);
89 boost::heap::compare<profile_less>>;
103 "lightest(n > 1): requires automaton without lightening cycles");
111 queue.emplace(
aut_->pre(),
ls_.one(),
ws_.one());
115 while (!queue.empty() && num != res.size())
118 std::tie(s, l, w) = queue.top();
125 && std::get<0>(queue.top()) == s
126 &&
ls_.equal(std::get<1>(queue.top()), l))
128 while (!queue.empty()
129 && std::get<0>(queue.top()) == s
130 &&
ls_.equal(std::get<1>(queue.top()), l))
132 w =
ws_.add(w, std::get<2>(queue.top()));
135 queue.emplace(s, l, w);
139 if (s ==
aut_->post())
140 ps_.add_here(res, std::move(l), std::move(w));
144 auto dst =
aut_->dst_of(t);
145 auto nw =
ws_.mul(w,
aut_->weight_of(t));
146 if (
aut_->src_of(t) ==
aut_->pre() || dst ==
aut_->post())
147 queue.emplace(dst, l, std::move(nw));
150 auto nl =
ls_.mul(l,
aut_->label_of(t));
151 queue.emplace(dst, std::move(nl), std::move(nw));
162 const char* sep =
"";
163 for (
auto i = q.ordered_begin(), end = q.ordered_end();
168 aut_->print_state_name(std::get<0>(*i),
os) <<
":<";
169 ws_.print(std::get<2>(*i),
os);
171 ls_.print(std::get<1>(*i),
os);
189 template <Automaton Aut>
191 lightest(
const Aut& aut,
unsigned num = 1,
const std::string& algo =
"auto")
196 auto res = ps.zero();
198 for (
const auto&
path : paths)
200 ps.add_here(res, *m);
203 else if ((algo ==
"auto" && num != 1) || algo ==
"breadth-first")
216 raise(
"lightest: invalid algorithm: ", algo);
225 template <Automaton Aut,
typename Num,
typename String>
229 const auto& a = aut->
as<Aut>();
231 return {ps,
lightest(a, num, algo)};
std::vector< path_t_of< Aut > > k_lightest_path(const Aut &aut, state_t_of< Aut > src, state_t_of< Aut > dst, unsigned k)
weight_t_of< automaton_t > weight_t
weightset_t_of< automaton_t > weightset_t
typename polynomialset_t::value_t polynomial_t
const polynomialset_t ps_
state_t_of< automaton_t > state_t
predecessors_t_of< Aut > format_lightest(const Aut &aut, const std::vector< transition_t_of< Aut >> &path)
A state-indexed vector of predecessor transitions from the path path.
auto make_word_polynomialset(const Ctx &ctx) -> word_polynomialset_t< Ctx >
The polynomialset of words of a labelset (not necessarily on words itself).
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Provide a variadic mul on top of a binary mul(), and one().
void show_heap_(const queue_t &q, std::ostream &os=std::cerr)
Show the heap, for debugging.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
bool operator()(const profile_t &r, const profile_t &l) const
Whether l < r (as this is a max heap).
polynomial_t operator()(unsigned num)
The approximated behavior of the automaton.
polynomial_t lightest_(unsigned num)
std::ostringstream os
The output stream: the corresponding C++ snippet to compile.
typename polynomialset_t::monomial_t monomial_t
std::tuple< state_t, word_t, weight_t > profile_t
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
polynomial lightest(const automaton &aut, unsigned num, const std::string &algo)
Bridge.
automaton_t aut_
The automaton whose behavior to approximate.
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
typename labelset_t_of< base_t< ValueSet >>::word_t word_t_of
detail::word_polynomialset_t< context_t_of< Aut > >::value_t lightest(const Aut &aut, unsigned num=1, const std::string &algo="auto")
The approximated behavior of an automaton.
std::enable_if_t< weightset_t_of< Aut >::has_lightening_weights(), bool > has_lightening_cycle(const Aut &aut)
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
The lightest algorithm computes the paths between pre and post with the smallest weight possible...
lightest_impl(const automaton_t &aut)
Prepare to compute an approximation of the behavior.
labelset_t_of< polynomialset_t > labelset_t
Wordset.
auto & as()
Extract wrapped typed automaton.
auto path_monomial(const Aut &aut, const std::vector< transition_t_of< Aut >> &path, state_t_of< Aut > src=Aut::element_type::pre(), state_t_of< Aut > dst=Aut::element_type::post()) -> boost::optional< typename detail::word_polynomialset_t< context_t_of< Aut >>::monomial_t >
Given a path (typically computed by lightest_path), the corresponding monomial (label, weight).
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
std::vector< transition_t_of< Aut > > lightest_path(const Aut &aut, state_t_of< Aut > source, state_t_of< Aut > dest, a_star_tag)
boost::heap::binomial_heap< profile_t, boost::heap::compare< profile_less >> queue_t
value_impl< detail::polynomial_tag > polynomial
word_t_of< automaton_t > word_t
context_t_of< Aut > context_t
Paths in filesystems, i.e., file names.