00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef VCSN_ALGORITHMS_EVALUATION_HXX
00018 # define VCSN_ALGORITHMS_EVALUATION_HXX
00019
00020 # include <vaucanson/algorithms/internal/evaluation.hh>
00021
00022 # include <vaucanson/automata/concept/transducer_base.hh>
00023
00024 # include <vaucanson/algorithms/product.hh>
00025 # include <vaucanson/algorithms/trim.hh>
00026 # include <vaucanson/algorithms/standard.hh>
00027 # include <vaucanson/algorithms/standard_of.hh>
00028 # include <vaucanson/algorithms/aut_to_exp.hh>
00029 # include <vaucanson/algorithms/extension.hh>
00030 # include <vaucanson/algorithms/projection.hh>
00031 # include <vaucanson/algorithms/aut_to_exp.hh>
00032 # include <vaucanson/misc/usual_macros.hh>
00033
00034 namespace vcsn {
00035
00036 template <typename SA, typename ST, typename SRET,
00037 typename Auto_t, typename Trans_t, typename Ret_t>
00038 void
00039 do_evaluation(const AutomataBase<SA>&,
00040 const TransducerBase<ST>&,
00041 const AutomataBase<SRET>&,
00042 const Auto_t& a,
00043 const Trans_t& t,
00044 Ret_t& ret)
00045 {
00046 image(trim(product(t, extension(a, t))), ret);
00047 }
00048
00049 template<typename SA, typename TA, typename ST,
00050 typename TT, typename SRET, typename TRET>
00051 void
00052 evaluation(const Element<SA, TA>& a, const Element<ST, TT>& t,
00053 Element<SRET, TRET>& ret)
00054 {
00055 do_evaluation(a.structure(), t.structure(), ret.structure(), a, t, ret);
00056 }
00057
00058 template<typename E, typename S, typename Trans_t, typename M>
00059 void
00060 do_partial_evaluation(const E& exp,
00061 const TransducerBase<S>&,
00062 const Trans_t& t,
00063 M& state_exp_pair_set)
00064 {
00065 typedef typename Trans_t::value_t T;
00066 typedef typename output_projection_helper<S, T>::ret Auto_t;
00067 typedef typename Auto_t::set_t::series_set_t Auto_series_set_t;
00068 typename Auto_t::set_t
00069 auto_structure(Auto_series_set_t(t.structure().series().semiring()));
00070 Auto_t tmp_auto(auto_structure);
00071
00072 AUTOMATON_TYPES(Auto_t);
00073 monoid_elt_t empty = tmp_auto.series().monoid().VCSN_EMPTY_;
00074 standard_of(tmp_auto, exp.get(empty).value());
00075 partial_1(tmp_auto, t, state_exp_pair_set);
00076 }
00077
00078
00079
00080 template<typename S1, typename T1,
00081 typename S2, typename T2,
00082 typename M>
00083 void
00084 partial_evaluation(const Element<S1, T1>& exp,
00085 const Element<S2, T2>& trans,
00086 M& state_exp_pair_set)
00087 {
00088 do_partial_evaluation(exp, trans.structure(), trans, state_exp_pair_set);
00089 }
00090
00091 template<typename S, typename Auto_t, typename M, typename Chooser_t>
00092 void
00093 do_partial_elimination(const AutomataBase<S>&,
00094 const Auto_t& a,
00095 Chooser_t chooser,
00096 M& state_exp_pair_set)
00097 {
00098 AUTOMATON_TYPES(Auto_t);
00099 typedef typename std::set<htransition_t> htransition_set_t;
00100 typedef typename Auto_t::hstate_t hstate_t;
00101 typedef std::map<hstate_t, series_set_elt_t> sums_t;
00102
00103 typename htransition_set_t::const_iterator i, j;
00104 hstate_t q;
00105 htransition_set_t transitions;
00106
00107 Auto_t b = a;
00108
00109
00110
00111 std::map<hstate_t, hstate_t> states_map;
00112 for (state_iterator i = a.states().begin(), j = b.states().begin(),
00113 end_ = a.states().end();
00114 i != end_;
00115 ++i, ++j)
00116 states_map.insert(std::make_pair(*j, *i));
00117
00118
00119
00120
00121 int num = b.final().size() + b.initial().size();
00122
00123 while (int(b.states().size()) != num)
00124 {
00125 series_set_elt_t loop_sum(b.series());
00126 sums_t in_sums, out_sums;
00127 typename sums_t::iterator f;
00128 q = chooser(b);
00129 if (b.is_initial(q) || b.is_final(q))
00130 continue;
00131
00132 transitions.clear();
00133
00134 b.deltac(transitions, q, delta_kind::transitions());
00135 for (i = transitions.begin(); i != transitions.end(); i = j)
00136 {
00137 j = i; ++j;
00138
00139 if (b.dst_of(*i) == q)
00140 loop_sum += b.series_of(*i);
00141 else if ((f = out_sums.find(b.dst_of(*i))) !=
00142 out_sums.end())
00143 f->second += b.series_of(*i);
00144 else
00145 out_sums.insert(std::make_pair(b.dst_of(*i), b.series_of(*i)));
00146 b.del_transition(*i);
00147 }
00148 transitions.clear();
00149
00150 b.rdeltac(transitions, q, delta_kind::transitions());
00151 for (i = transitions.begin(); i != transitions.end(); i = j)
00152 {
00153 j = i; ++j;
00154
00155 if ((f = in_sums.find(b.src_of(*i))) !=
00156 in_sums.end())
00157 f->second += b.series_of(*i);
00158 else
00159 in_sums.insert(std::make_pair(b.src_of(*i), b.series_of(*i)));
00160 b.del_transition(*i);
00161 }
00162 loop_sum.star();
00163 for_all_const_(sums_t, in, in_sums)
00164 for_all_const_(sums_t, out, out_sums)
00165 {
00166 series_set_elt_t res = in->second * loop_sum * out->second;
00167 b.add_series_transition(in->first, out->first, res);
00168 }
00169 b.del_state(q);
00170 }
00171
00172 typedef std::map<hstate_t, series_set_elt_t> se_map_t;
00173 typedef std::pair<hstate_t, series_set_elt_t> state_exp_pair_t;
00174 se_map_t se_m;
00175
00176
00177
00178 for_all_transitions(e, b)
00179 {
00180 hstate_t dst = b.dst_of(*e);
00181 typename se_map_t::iterator i = se_m.find(dst);
00182 if (i == se_m.end())
00183 se_m.insert(std::make_pair(dst,
00184 series_set_elt_t (b.structure().series(),
00185 b.label_of(*e))));
00186 else
00187 i->second += b.label_of(*e);
00188 }
00189
00190 for_all_final_states(p, b)
00191 {
00192 typename se_map_t::iterator i = se_m.find(*p);
00193 if (i != se_m.end())
00194 state_exp_pair_set.insert(std::make_pair(states_map[*p], i->second));
00195 }
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 template<typename A, typename T, typename M>
00207 void
00208 partial_elimination(const Element<A, T>& a,
00209 M& state_exp_pair_set)
00210 {
00211 do_partial_elimination(a.structure(),
00212 a,
00213 DefaultChooser(),
00214 state_exp_pair_set);
00215 }
00216
00218
00219 template<typename SA, typename ST,
00220 typename Auto_t, typename Trans_t,
00221 typename M>
00222 void
00223 do_partial_1(const AutomataBase<SA>&,
00224 const TransducerBase<ST>&,
00225 const Auto_t& a,
00226 const Trans_t& t,
00227 M& state_exp_pair_set)
00228 {
00229 typedef typename Trans_t::value_t T;
00230 typedef typename output_projection_helper<ST, T>::ret Auto_ret_t;
00231 typedef typename Auto_ret_t::set_t::series_set_t Auto_ret_series_set_t;
00232 typename Auto_ret_t::set_t
00233 auto_structure(Auto_ret_series_set_t(t.structure().series().semiring()));
00234
00235 AUTOMATON_TYPES_(Auto_t, a_);
00236 AUTOMATON_TYPES_(Trans_t, t_);
00237 AUTOMATON_TYPES_(Auto_ret_t, ret_);
00238
00239 typedef std::map<t_hstate_t, std::pair<t_hstate_t, t_hstate_t> >
00240 state_pair_map_t;
00241 typedef std::map<t_hstate_t, ret_hstate_t> state_state_map_t;
00242 typedef std::pair<t_hstate_t, ret_series_set_elt_t> se_pair_t;
00243
00244 Trans_t tmp_trans(t.structure());
00245 tmp_trans = extension(a, t);
00246
00247 Trans_t pro(t.structure());
00248 state_pair_map_t sp_m;
00249 pro = product(tmp_trans, t, sp_m);
00250 std::map<typename Trans_t::hstate_t, typename Trans_t::hstate_t> states_map_for_sp_m;
00251 for_all_iterator (typename state_pair_map_t::iterator, i, sp_m)
00252 states_map_for_sp_m.insert(std::make_pair(pro.get_state(size_t(i->first)), i->first));
00253
00254 Auto_ret_t auto_p(auto_structure);
00255 state_state_map_t proj_m;
00256 auto_p = image(pro, proj_m);
00257
00258 std::map<typename Trans_t::hstate_t, typename Trans_t::hstate_t> states_map_for_proj_m;
00259 for_all_iterator (typename state_state_map_t::iterator, i, proj_m)
00260 states_map_for_proj_m.insert(std::make_pair(auto_p.get_state(size_t(i->first)), i->first));
00261
00262
00263 auto_p.clear_final();
00264
00265
00266 state_state_map_t final_of, is_final_of;
00267 for (t_state_iterator u = t.states().begin(); u != t.states().end(); ++u)
00268 {
00269 ret_hstate_t new_state = auto_p.add_state();
00270 final_of[*u] = new_state;
00271 is_final_of[new_state] = *u;
00272 auto_p.set_final(new_state);
00273 }
00274
00275 for (a_state_iterator u = auto_p.states().begin();
00276 u != auto_p.states().end(); ++u)
00277 {
00278 if (!auto_p.is_final(*u))
00279 {
00280 t_hstate_t p = sp_m[states_map_for_sp_m[proj_m[states_map_for_proj_m[*u]]]].first;
00281 t_hstate_t q = sp_m[states_map_for_sp_m[proj_m[states_map_for_proj_m[*u]]]].second;
00282
00283 if (tmp_trans.is_final(p))
00284 auto_p.add_spontaneous(*u, final_of[q]);
00285 }
00286 }
00287
00288 M se;
00289 partial_elimination(auto_p, se);
00290
00291 for_all_(M, p, se)
00292 {
00293 se_pair_t my_pair = std::make_pair(is_final_of[(*p).first],
00294 p->second);
00295 state_exp_pair_set.insert(my_pair);
00296 }
00297 }
00298
00299 template<typename SA, typename TA,
00300 typename ST, typename TT,
00301 typename M>
00302 void
00303 partial_1(const Element<SA, TA>& a,
00304 const Element<ST, TT>& t,
00305 M& state_exp_pair_set)
00306 {
00307 do_partial_1(a.structure(), t.structure(), a, t, state_exp_pair_set);
00308 }
00309
00311
00312 template<typename SA, typename ST,
00313 typename Auto_t, typename Trans_t,
00314 typename Exp>
00315 void
00316 do_partial_2(const AutomataBase<SA>&,
00317 const TransducerBase<ST>&,
00318 const Auto_t& a,
00319 const Trans_t& t,
00320 const typename Trans_t::hstate_t p,
00321 Exp& exp)
00322 {
00323 typedef typename Trans_t::value_t T;
00324 typedef typename output_projection_helper<ST, T>::ret Auto_ret_t;
00325 typedef typename Auto_ret_t::set_t::series_set_t Auto_ret_series_set_t;
00326
00327 typename Auto_ret_t::set_t
00328 auto_structure(Auto_ret_series_set_t(t.structure().series().semiring()));
00329
00330 Trans_t tt = t;
00331 tt.clear_initial();
00332
00333
00334
00335 tt.set_initial(tt.get_state(size_t(p)));
00336
00337 Trans_t tmp_trans(tt.structure());
00338 tmp_trans = extension(a, tt);
00339
00340 Trans_t pro(t.structure());
00341 pro = trim(product(tmp_trans, tt));
00342 Auto_ret_t auto_p(auto_structure);
00343 auto_p = image(pro);
00344
00345 exp = aut_to_exp(auto_p);
00346 }
00347
00348 template<typename SA, typename TA,
00349 typename ST, typename TT,
00350 typename Exp>
00351 void
00352 partial_2(const Element<SA, TA>& a,
00353 const Element<ST, TT>& t,
00354 const typename TT::hstate_t p, Exp& exp)
00355 {
00356 do_partial_2(a.structure(), t.structure(), a, t, p, exp);
00357 }
00358
00360
00361 template<typename SA, typename ST,
00362 typename Auto_t, typename Trans_t,
00363 typename M>
00364 void
00365 do_partial_3(const AutomataBase<SA>&,
00366 const TransducerBase<ST>&,
00367 const Auto_t& a,
00368 const Trans_t& t,
00369 const typename Trans_t::hstate_t p,
00370 M& state_exp_pair_set)
00371 {
00372 Trans_t tt = t;
00373 tt.clear_initial();
00374
00375
00376
00377 std::map<typename Trans_t::hstate_t, typename Trans_t::hstate_t> states_map;
00378 for (typename Trans_t::state_iterator i = t.states().begin(), j = tt.states().begin(),
00379 end_ = t.states().end();
00380 i != end_;
00381 ++i, ++j)
00382 states_map.insert(std::make_pair(*j, *i));
00383
00384
00385
00386 tt.set_initial(tt.get_state(size_t(p)));
00387
00388 trim_here(tt);
00389
00390 M temp_state_exp_pair_set;
00391 partial_1(a, tt, temp_state_exp_pair_set);
00392
00393 for_all_iterator(typename M::iterator, i, temp_state_exp_pair_set)
00394 state_exp_pair_set.insert(std::make_pair(states_map[i->first], i->second));
00395 }
00396
00397 template<typename SA, typename TA,
00398 typename ST, typename TT,
00399 typename M>
00400 void
00401 partial_3(const Element<SA, TA>& a,
00402 const Element<ST, TT>& t,
00403 const typename TT::hstate_t p,
00404 M& state_exp_pair_set)
00405 {
00406 do_partial_3(a.structure(), t.structure(), a, t, p, state_exp_pair_set);
00407 }
00408
00409 }
00410
00411 #endif // ! VCSN_ALGORITHMS_EVALUATION_HXX