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);
75 boost::optional<unsigned> len)
78 num = !len ? 1 : std::numeric_limits<unsigned>::max();
80 len = std::numeric_limits<unsigned>::max();
95 template <
typename LabelSet = labelset_t_of<context_t>>
97 -> std::enable_if_t<LabelSet::is_free(), polynomial_t>
101 if (len != std::numeric_limits<unsigned>::max())
104 using queue_t = std::deque<profile_t>;
110 !queue.empty() && i < len && output.size() < num;
114 for (
const auto& sm: queue)
117 std::tie(s, l, w) = sm;
120 auto dst =
aut_->dst_of(t);
121 auto nw =
ws_.mul(w,
aut_->weight_of(t));
123 q2.emplace_back(dst, l, std::move(nw));
124 else if (dst ==
aut_->post())
125 ps_.add_here(output, l, std::move(nw));
136 for (
const auto& m: output)
138 ps_.add_here(res, m);
149 template <
typename LabelSet = labelset_t_of<context_t>>
151 -> std::enable_if_t<!LabelSet::is_free(), polynomial_t>
157 boost::heap::compare<profile_less>>;
159 auto queue = queue_t{};
160 queue.emplace(
aut_->pre(),
ls_.one(),
ws_.one());
164 while (!queue.empty())
167 std::tie(s, l, w) = queue.top();
179 while (!queue.empty()
180 && std::get<0>(queue.top()) == s
181 &&
ls_.equal(std::get<1>(queue.top()), l))
183 w =
ws_.add(w, std::get<2>(queue.top()));
189 auto dst =
aut_->dst_of(t);
190 auto nw =
ws_.mul(w,
aut_->weight_of(t));
192 queue.emplace(dst, l, std::move(nw));
193 else if (dst ==
aut_->post())
194 ps_.add_here(res, l, std::move(nw));
197 auto nl =
ls_.mul(l,
aut_->label_of(t));
199 if (
ls_.size(nl) <= len)
200 queue.emplace(dst, std::move(nl), std::move(nw));
208 || (num == res.size()
209 && !
ls_.equal(std::get<1>(queue.top()), l)))
218 template <
typename Queue>
221 const char* sep =
"";
222 for (
auto i = q.ordered_begin(), end = q.ordered_end();
227 aut_->print_state_name(std::get<0>(*i),
os) <<
":<";
228 ws_.print(std::get<2>(*i),
os) <<
'>';
229 ls_.print(std::get<1>(*i),
os);
250 template <Automaton Aut>
253 boost::optional<unsigned> num = {},
254 boost::optional<unsigned> len = {})
256 auto enumerater = detail::enumerater<Aut>{aut};
257 return enumerater(num, len);
265 template <Automaton Aut>
266 typename detail::enumerater<Aut>::polynomial_t
269 return shortest(aut, boost::none, len);
278 template <Automaton Aut,
typename Num,
typename Len>
281 boost::optional<unsigned> num,
282 boost::optional<unsigned> len)
284 const auto& a = aut->
as<Aut>();
weightset_t_of< automaton_t > weightset_t
void show_heap_(const Queue &q, std::ostream &os=std::cerr) const
Show the heap, for debugging.
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
typename labelset_t_of< base_t< ValueSet >>::word_t word_t_of
detail::enumerater< Aut >::polynomial_t shortest(const Aut &aut, boost::optional< unsigned > num={}, boost::optional< unsigned > len={})
The approximated behavior of an automaton.
enumerater(const automaton_t &aut)
Prepare to compute an approximation of the behavior.
polynomial shortest(const automaton &aut, boost::optional< unsigned > num, boost::optional< unsigned > len)
Bridge.
polynomial_t operator()(boost::optional< unsigned > num, boost::optional< unsigned > len)
The approximated behavior of the automaton.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
typename polynomialset_t::value_t polynomial_t
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
word_t_of< automaton_t > word_t
labelset_t_of< polynomialset_t > labelset_t
Wordset.
std::tuple< state_t, word_t, weight_t > profile_t
Used in the case of non-free labelsets.
const labelset_t & ls_
Shorthand to the (word) labelset.
state_t_of< automaton_t > state_t
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
automaton_t aut_
The automaton whose behavior to approximate.
value_impl< detail::polynomial_tag > polynomial
auto make_word_polynomialset(const Ctx &ctx) -> word_polynomialset_t< Ctx >
The polynomialset of words of a labelset (not necessarily on words itself).
bool operator()(const profile_t &r, const profile_t &l) const
Whether l < r (as this is a max heap).
context_t_of< Aut > context_t
auto shortest_(unsigned num, unsigned len) -> std::enable_if_t< LabelSet::is_free(), polynomial_t >
Case of free labelsets (e.g., lal or lal x lal).
Compute the shortest words accepted by an automaton.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
auto & as()
Extract wrapped typed automaton.
auto shortest_(unsigned num, unsigned len) -> std::enable_if_t<!LabelSet::is_free(), polynomial_t >
Case of non free labelsets (e.g., law, lan x lan).
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.
Provide a variadic mul on top of a binary mul(), and one().
auto all_out(const Aut &aut, state_t_of< Aut > s)
Indexes of transitions leaving state s.
const polynomialset_t ps_
Shorthand to the polynomialset of words.
weight_t_of< automaton_t > weight_t
const weightset_t & ws_
Shorthand to the weightset.