17 #ifndef VCSN_ALGORITHMS_EVALUATION_HXX
18 # define VCSN_ALGORITHMS_EVALUATION_HXX
22 # include <vaucanson/misc/usual_macros.hh>
23 # include <vaucanson/automata/concept/transducer_base.hh>
40 template<
typename SS,
typename SM,
typename V,
41 typename Series_t,
typename Trans_t,
44 do_partial_evaluation(
const algebra::Series<SS, SM>&,
46 const TransducerBase<V>&,
48 const typename Trans_t::hstate_t& p,
52 typedef typename Trans_t::value_t W;
53 typedef typename output_projection_helper<V, W>::ret Auto_t;
54 AUTOMATON_TYPES(Auto_t);
55 AUTOMATON_TYPES_(Trans_t, t_);
56 typedef typename Auto_t::set_t Auto_set_t;
57 typedef typename Auto_set_t::series_set_t Auto_series_set_t;
58 typedef series_set_elt_t exp_t;
59 typedef t_series_set_elt_t t_exp_t;
60 typedef std::map<t_hstate_t, std::pair<t_hstate_t, t_hstate_t> >
62 typedef std::map<t_hstate_t, hstate_t> state_state_map_t;
63 typedef std::pair<t_hstate_t, exp_t> se_pair_t;
64 Auto_set_t auto_structure(Auto_series_set_t(S.structure().series().semiring()));
72 Auto_t A(auto_structure);
82 state_state_map_t Sp_to_S;
83 for_all_const_states_(t_, q, Sp)
85 hstate_t pSp = Sp_to_S[*q] = S.get_state(
size_t(*q));
98 Trans_t tmp_trans(Sp.structure());
102 Trans_t pro(Sp.structure());
103 state_pair_map_t sp_m;
104 pro = product(tmp_trans, Sp, sp_m);
107 std::map<typename Trans_t::hstate_t, typename Trans_t::hstate_t> states_map_for_sp_m;
108 for_all_iterator (
typename state_pair_map_t::iterator, i, sp_m)
109 states_map_for_sp_m.insert(std::make_pair(pro.get_state(
size_t(i->first)), i->first));
112 Auto_t G(auto_structure);
113 state_state_map_t proj_m;
114 G = image(pro, proj_m);
116 std::map<typename Trans_t::hstate_t, typename Trans_t::hstate_t> states_map_for_proj_m;
117 for_all_iterator (typename state_state_map_t::iterator, i, proj_m)
118 states_map_for_proj_m.insert(std::make_pair(G.get_state(
size_t(i->first)), i->first));
121 const hstate_t i = G.add_state();
122 for_all_const_initial_states(r, G)
124 exp_t old_label = G.get_initial(*r);
125 G.add_series_transition(i, *r, old_label);
136 state_state_map_t state_of, is_state_of;
137 for_all_const_states_(t_, u, Sp)
139 hstate_t new_state = G.add_state();
140 state_of[*u] = new_state;
141 is_state_of[new_state] = *u;
142 G.set_final(new_state);
150 for_all_const_states(ig, G)
152 if (*ig != i && !G.is_final(*ig))
154 t_hstate_t t = sp_m[states_map_for_sp_m[proj_m[states_map_for_proj_m[*ig]]]].first;
155 t_hstate_t u = sp_m[states_map_for_sp_m[proj_m[states_map_for_proj_m[*ig]]]].second;
157 if (tmp_trans.is_final(t))
158 G.add_spontaneous(*ig, state_of[u]);
172 se_pair_t my_pair = std::make_pair(Sp_to_S[is_state_of[(*p).first]], p->second);
177 template <
typename S1,
typename T1,
178 typename S2,
typename T2,
189 template<
typename S,
typename Auto_t,
typename M,
typename Chooser_t>
191 do_partial_elimination(
const AutomataBase<S>&,
194 M& state_exp_pair_set)
196 AUTOMATON_TYPES(Auto_t);
197 typedef typename std::set<htransition_t> htransition_set_t;
198 typedef typename Auto_t::hstate_t hstate_t;
199 typedef std::map<hstate_t, series_set_elt_t> sums_t;
201 typename htransition_set_t::const_iterator i, j;
203 htransition_set_t transitions;
209 std::map<hstate_t, hstate_t> states_map;
210 for (state_iterator i = a.states().begin(), j = b.states().begin(),
211 end_ = a.states().end();
214 states_map.insert(std::make_pair(*j, *i));
220 int num = b.final().size() + b.initial().size();
222 while (
int(b.states().size()) != num)
224 series_set_elt_t loop_sum(b.series());
225 sums_t in_sums, out_sums;
226 typename sums_t::iterator f;
228 if (b.is_initial(q) || b.is_final(q))
232 for (delta_iterator e(b.value(), q); ! e.done(); e.next())
233 transitions.insert(*e);
234 for (i = transitions.begin(); i != transitions.end(); i = j)
238 if (b.dst_of(*i) == q)
239 loop_sum += b.series_of(*i);
240 else if ((f = out_sums.find(b.dst_of(*i))) !=
242 f->second += b.series_of(*i);
244 out_sums.insert(std::make_pair(b.dst_of(*i), b.series_of(*i)));
245 b.del_transition(*i);
248 for (rdelta_iterator e(b.value(), q); ! e.done(); e.next())
249 transitions.insert(*e);
250 for (i = transitions.begin(); i != transitions.end(); i = j)
254 if ((f = in_sums.find(b.src_of(*i))) !=
256 f->second += b.series_of(*i);
258 in_sums.insert(std::make_pair(b.src_of(*i), b.series_of(*i)));
259 b.del_transition(*i);
262 for_all_const_(sums_t, in, in_sums)
263 for_all_const_(sums_t, out, out_sums)
265 series_set_elt_t res = in->second * loop_sum * out->second;
266 b.add_series_transition(in->first, out->first, res);
271 typedef std::map<hstate_t, series_set_elt_t> se_map_t;
272 typedef std::pair<hstate_t, series_set_elt_t> state_exp_pair_t;
277 for_all_transitions(e, b)
279 hstate_t dst = b.dst_of(*e);
280 typename se_map_t::iterator i = se_m.find(dst);
282 se_m.insert(std::make_pair(dst,
283 series_set_elt_t (b.structure().series(),
286 i->second += b.label_of(*e);
289 for_all_final_states(p, b)
291 typename se_map_t::iterator i = se_m.find(*p);
293 state_exp_pair_set.insert(std::make_pair(states_map[*p], i->second));
304 template<
typename A,
typename T,
typename M>
307 M& state_exp_pair_set)
309 do_partial_elimination(a.structure(),
317 #endif // ! VCSN_ALGORITHMS_EVALUATION_HXX