7 #include <boost/heap/binomial_heap.hpp> 8 #include <boost/optional.hpp> 12 #include <vcsn/dyn/fwd.hh> 29 template <Automaton Aut>
49 using profile_t = std::tuple<state_t, word_t, weight_t>;
55 if (labelset_t::less(std::get<1>(l), std::get<1>(r)))
57 else if (labelset_t::less(std::get<1>(r), std::get<1>(l)))
60 return std::get<0>(l) < std::get<0>(r);
76 boost::optional<unsigned> len)
79 num = !len ? 1 : std::numeric_limits<unsigned>::max();
81 len = std::numeric_limits<unsigned>::max();
93 return shortest_(1, std::numeric_limits<unsigned>::max(), src, dst);
106 template <
typename LabelSet = labelset_t_of<context_t>>
110 -> std::enable_if_t<LabelSet::is_free(), polynomial_t>
114 if (len != std::numeric_limits<unsigned>::max())
117 using queue_t = std::deque<profile_t>;
121 auto output =
ps_.zero();
123 !queue.empty() && i < len && output.size() < num;
127 for (
const auto& sm: queue)
130 std::tie(s, l, w) = sm;
133 auto t_dst =
aut_->dst_of(t);
134 auto nw =
ws_.mul(w,
aut_->weight_of(t));
135 if (t_dst ==
aut_->post() && t_dst == dst)
136 ps_.add_here(output, l, std::move(nw));
137 else if (
aut_->src_of(t) ==
aut_->pre()
138 || t_dst ==
aut_->post())
139 q2.emplace_back(t_dst, l, std::move(nw));
140 else if (t_dst == dst)
141 ps_.add_here(output,
ls_.mul(l,
aut_->label_of(t)),
144 q2.emplace_back(t_dst,
154 for (
const auto& m: output)
167 template <
typename LabelSet = labelset_t_of<context_t>>
171 -> std::enable_if_t<!LabelSet::is_free(), polynomial_t>
177 boost::heap::compare<profile_less>>;
179 auto queue = queue_t{};
180 queue.emplace(src,
ls_.one(),
ws_.one());
184 while (!queue.empty())
187 std::tie(s, l, w) = queue.top();
199 while (!queue.empty()
200 && std::get<0>(queue.top()) == s
201 &&
ls_.equal(std::get<1>(queue.top()), l))
203 w =
ws_.add(w, std::get<2>(queue.top()));
209 auto t_dst =
aut_->dst_of(t);
210 auto nw =
ws_.mul(w,
aut_->weight_of(t));
211 if (t_dst ==
aut_->post() && t_dst == dst)
212 ps_.add_here(
res, l, std::move(nw));
213 else if (
aut_->src_of(t) ==
aut_->pre()
214 || t_dst ==
aut_->post())
215 queue.emplace(t_dst, l, std::move(nw));
216 else if (t_dst == dst)
218 auto nl =
ls_.mul(l,
aut_->label_of(t));
220 if (
ls_.size(nl) <= len)
221 ps_.add_here(
res, std::move(nl), std::move(nw));
225 auto nl =
ls_.mul(l,
aut_->label_of(t));
227 if (
ls_.size(nl) <= len)
228 queue.emplace(t_dst, std::move(nl), std::move(nw));
236 || (num ==
res.size()
237 && !
ls_.equal(std::get<1>(queue.top()), l)))
246 template <
typename Queue>
249 const char* sep =
"";
250 for (
auto i = q.ordered_begin(), end = q.ordered_end();
255 aut_->print_state_name(std::get<0>(*i),
os) <<
":<";
256 ws_.print(std::get<2>(*i),
os) <<
'>';
257 ls_.print(std::get<1>(*i),
os);
278 template <Automaton Aut>
281 boost::optional<unsigned> num = {},
282 boost::optional<unsigned> len = {})
294 template <Automaton Aut>
307 template <Automaton Aut>
311 return shortest(aut, boost::none, len);
320 template <Automaton Aut,
typename Num,
typename Len>
323 boost::optional<unsigned> num,
324 boost::optional<unsigned> len)
326 const auto& a = aut->
as<Aut>();
std::ostringstream os
The output stream: the corresponding C++ snippet to compile.
detail::enumerater< Aut >::polynomial_t enumerate(const Aut &aut, unsigned len)
The approximated behavior of an automaton.
context_t_of< Aut > context_t
const labelset_t & ls_
Shorthand to the (word) labelset.
auto shortest_(unsigned num, unsigned len, state_t_of< Aut > src=Aut::element_type::pre(), state_t_of< Aut > dst=Aut::element_type::post()) -> std::enable_if_t< LabelSet::is_free(), polynomial_t >
Case of free labelsets (e.g., lal or lal x lal).
auto & as()
Extract wrapped typed automaton.
labelset_t_of< polynomialset_t > labelset_t
Wordset.
enumerater(const automaton_t &aut)
Prepare to compute an approximation of the behavior.
bool operator()(const profile_t &r, const profile_t &l) const
Whether l < r (as this is a max heap).
typename detail::state_t_of_impl< base_t< ValueSet > >::type state_t_of
weightset_t_of< automaton_t > weightset_t
std::tuple< state_t, word_t, weight_t > profile_t
Used in the case of non-free labelsets.
word_t_of< automaton_t > word_t
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
typename detail::weightset_t_of_impl< base_t< ValueSet > >::type weightset_t_of
weight_t_of< automaton_t > weight_t
typename detail::weight_t_of_impl< base_t< ValueSet > >::type weight_t_of
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
auto make_word_polynomialset(const Ctx &ctx) -> word_polynomialset_t< Ctx >
The polynomialset of words of a labelset (not necessarily on words itself).
value_impl< detail::polynomial_tag > polynomial
typename labelset_t_of< base_t< ValueSet > >::word_t word_t_of
polynomial_t operator()(state_t src, state_t dst)
The approximated behavior of a part of the automaton (looks for one word of unspecified length)...
Provide a variadic mul on top of a binary mul(), and one().
const polynomialset_t ps_
Shorthand to the polynomialset of words.
Compute the shortest words accepted by an automaton.
void show_heap_(const Queue &q, std::ostream &os=std::cerr) const
Show the heap, for debugging.
detail::enumerater< Aut >::polynomial_t shortest(const Aut &aut, boost::optional< unsigned > num={}, boost::optional< unsigned > len={})
The approximated behavior of an automaton.
polynomial_t operator()(boost::optional< unsigned > num, boost::optional< unsigned > len)
The approximated behavior of the automaton.
const weightset_t & ws_
Shorthand to the weightset.
auto shortest_(unsigned num, unsigned len, state_t_of< Aut > src=Aut::element_type::pre(), state_t_of< Aut > dst=Aut::element_type::post()) -> std::enable_if_t<!LabelSet::is_free(), polynomial_t >
Case of non free labelsets (e.g., law, lan x lan).
automaton_t aut_
The automaton whose behavior to approximate.
state_t_of< automaton_t > state_t
typename polynomialset_t::value_t polynomial_t
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of