17 #ifndef AWALI_ALGOS_DETERMINIZE_HXX
18 # define AWALI_ALGOS_DETERMINIZE_HXX
23 # include <type_traits>
36 namespace awali {
namespace sttc {
48 template <
typename Aut,
unsigned N>
52 "determinize: requires free labelset");
54 "determinize: requires Boolean weights");
71 for (
auto t : input_->final_transitions())
72 finals_.set(input_->src_of(t));
81 pre.set(input_->pre());
92 auto i = map_.find(ss);
93 if (i == std::end(map_))
95 res = output_->add_state();
97 if ((ss & finals_).any())
98 output_->set_final(res);
111 std::map<label_t, state_set, internal::less<labelset_t_of<Aut>>> ml;
112 while (!todo_.empty())
114 auto ss = std::move(todo_.top());
117 src = output_->pre();
130 auto i = successors_.find(s);
131 if (i == successors_.end())
133 i = successors_.emplace(s, label_map_t{}).first;
135 for (
auto t : input_->out(s))
137 auto l = input_->label_of(t);
138 j[l].set(input_->dst_of(t));
143 for (
const auto& p : i->second)
145 auto j = ml.find(p.first);
147 ml[p.first] = p.second;
149 j->second |= p.second;
154 for (
const auto& e : ml)
155 output_->new_transition(src,
state(e.second), e.first);
161 auto history = std::make_shared<partition_history<automaton_t>>(input_);
162 output_->set_history(history);
163 if(!input_->get_name().empty()) {
164 output_->set_desc(
"Determinization of "+input_->get_name());
165 output_->set_name(
"det-"+input_->get_name());
168 output_->set_desc(
"Determinization");
169 output_->set_name(
"det");
171 for (
const auto& p: map_)
173 std::set<state_t>
from;
174 const auto& ss = p.first;
177 from.emplace(it.next());
178 history->add_state(p.second, std::move(
from));
184 using map = std::unordered_map<state_set, state_t>;
193 using stack = std::stack<state_set>;
200 using label_map_t = std::unordered_map<label_t, state_set>;
201 using successors_t = std::unordered_map<state_t, label_map_t>;
202 successors_t successors_;
211 template <
typename Aut>
215 "determinize: requires free labelset");
217 "determinize: requires Boolean weights");
235 for (
auto t : input_->final_transitions())
236 finals_.emplace(input_->src_of(t));
245 pre.emplace(input_->pre());
254 auto i = map_.find(ss);
255 if (i == std::end(map_))
257 res = output_->add_state();
270 std::map<label_t, state_set, internal::less<labelset_t_of<Aut>>> ml;
271 while (!todo_.empty())
273 auto ss = std::move(todo_.top());
276 src = output_->pre();
286 for(
auto t : input_->all_out(s))
287 if(input_->dst_of(t)==input_->post())
290 auto j = ml.find(input_->label_of(t));
293 st.emplace(input_->dst_of(t));
294 ml[input_->label_of(t)] = st;
297 j->second.emplace(input_->dst_of(t));
301 output_->set_final(src);
304 for (
const auto& e : ml)
305 output_->new_transition(src,
state(e.second), e.first);
311 auto history = std::make_shared<partition_history<automaton_t>>(input_);
312 output_->set_history(history);
313 for (
const auto& p: map_)
315 std::set<state_t>
from;
316 const auto& ss = p.first;
319 history->add_state(p.second, std::move(
from));
325 using map = std::map<state_set, state_t>;
334 using stack = std::stack<state_set>;
341 using label_map_t = std::map<label_t, state_set>;
360 template <
typename Aut>
364 "determinize: requires free labelset");
411 template<
typename Pred>
415 unknown_ = output_->null_state();
416 for (
auto t : input_->initial_transitions())
417 ss.emplace(input_->dst_of(t), input_->weight_of(t));
418 output_->set_initial(state_(ss,0,pred(ss)));
422 std::pair<state_name_t, weight_t>,
424 while (!todo_.empty())
426 ss = std::move(todo_.front().first);
427 unsigned depth = todo_.front().second;
431 for (
const auto& p : ss)
435 for (
auto t : input_->out(s))
437 auto l = input_->label_of(t);
438 auto dst = input_->dst_of(t);
439 auto w = ws_.mul(v, input_->weight_of(t));
443 if (dests.find(l)==dests.end())
444 dests.emplace(l, make_pair(ns_.
zero(), ws_.zero()));
447 d.second = ws_.add(d.second, w);
450 for (
auto& d : dests)
451 output_->new_transition(src,
452 state_(d.second.first, depth+1, pred(d.second.first)),
465 auto i = map_.find(name);
466 if (i == std::end(map_))
467 if(true_state && (limit_ <0 || depth <= (
unsigned) limit_)) {
468 res = output_->add_state();
470 for (
const auto& p : name)
471 if (input_->is_final(p.first))
472 output_->add_final(res,
474 input_->get_final_weight(p.first)));
476 todo_.push(std::make_pair(name,depth));
479 if(unknown_ == output_->null_state()) {
480 unknown_ = output_->add_state();
481 for(
auto l : output_->labelset()->genset())
482 output_->new_transition(unknown_, unknown_, l);
483 output_->set_state_name(unknown_,
"...");
493 std::map<state_name_t, state_t, internal::less<state_nameset_t>> map_;
510 unsigned state_size_ = input_->max_state() + 1;
513 using queue = std::queue<std::pair<state_name_t,unsigned>>;
The Boolean semring.
Definition: b.hh:38
carries the algebraic settings of automata
Definition: context.hh:40
The subset construction automaton from another.
Definition: determinize.hxx:50
std::bitset< N > state_set
Set of (input) states.
Definition: determinize.hxx:62
Aut automaton_t
Definition: determinize.hxx:57
determinization_bitset_impl(const automaton_t &a)
Build the determinizer.
Definition: determinize.hxx:66
automaton_nocv_t operator()()
Determinize all accessible states.
Definition: determinize.hxx:108
mutable_automaton< context_t_of< Aut > > automaton_nocv_t
Definition: determinize.hxx:58
state_t state(const state_set &ss)
The state for set of states ss.
Definition: determinize.hxx:87
label_t_of< automaton_t > label_t
Definition: determinize.hxx:59
void set_history()
Definition: determinize.hxx:160
context_t_of< automaton_t > context_t
Definition: determinize.hxx:60
The subset construction automaton from another.
Definition: determinize.hxx:213
determinization_set_impl(const automaton_t &a)
Build the determinizer.
Definition: determinize.hxx:230
Aut automaton_t
Definition: determinize.hxx:220
mutable_automaton< context_t_of< Aut > > automaton_nocv_t
Definition: determinize.hxx:221
void set_history()
Definition: determinize.hxx:310
label_t_of< automaton_t > label_t
Definition: determinize.hxx:222
std::set< state_t > state_set
Set of (input) states.
Definition: determinize.hxx:226
context_t_of< automaton_t > context_t
Definition: determinize.hxx:223
state_t state(const state_set &ss)
The state for set of states ss.
Definition: determinize.hxx:251
automaton_nocv_t operator()()
Determinize all accessible states.
Definition: determinize.hxx:267
The weighted determinization of weighted automaton.
Definition: determinize.hxx:362
weight_t_of< automaton_t > weight_t
Definition: determinize.hxx:371
automaton_t operator()()
Definition: determinize.hxx:402
automaton_t operator()(Pred pred)
The determinization of weighted automaton the state is added to the result only if the predicate pred...
Definition: determinize.hxx:412
label_t_of< automaton_t > label_t
Definition: determinize.hxx:369
weightset_t_of< automaton_t > weightset_t
Definition: determinize.hxx:370
detweighted_algo_impl(const automaton_t &a, int lim=-1)
Build the weighted determinizer.
Definition: determinize.hxx:396
Aut automaton_t
Definition: determinize.hxx:367
typename state_nameset_t::value_t state_name_t
Definition: determinize.hxx:391
polynomialset< context< stateset, weightset_t > > state_nameset_t
Definition: determinize.hxx:390
adapter of std::less to wrap less_than method of valuesets
Definition: map.hh:61
const weightset_ptr & weightset() const
Definition: polynomialset.hh:107
std::map< label_t, weight_t, internal::less< labelset_t > > value_t
Definition: polynomialset.hh:83
value_t & add_here(value_t &v, const value_t &p) const
v += p.
Definition: polynomialset.hh:130
const value_t & zero() const
Definition: polynomialset.hh:375
The semiring of floating Numbers.
Definition: r.hh:35
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
Definition: tuple.hh:134
json_ast_t from(std::istream &i)
Builds a json_ast_t from an input stream.
static constexpr TOP< void > value
Definition: priority.hh:93
auto get_iterator(const std::bitset< N > &bs) -> bitset_iterator< N >
Definition: bitset.hh:48
typename internal::label_t_of_impl< internal::base_t< ValueSet > >::type label_t_of
Helper to retrieve the type of the labels of a value set.
Definition: traits.hh:71
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:931
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
An output state is a list of weighted input states.
Definition: determinize.hxx:375
void kind_t
Definition: determinize.hxx:381
static bool less_than(state_t l, state_t r)
Definition: determinize.hxx:382
automaton_t aut_
Definition: determinize.hxx:387
stateset(const automaton_t &aut)
Definition: determinize.hxx:376
state_t value_t
Definition: determinize.hxx:380