evaluation.hxx

00001 // evaluation.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_ALGORITHMS_EVALUATION_HXX
00018 # define VCSN_ALGORITHMS_EVALUATION_HXX
00019 
00020 # include <vaucanson/algorithms/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/tools/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     Trans_t tt(t.structure());
00047     tt = extension(a, t);
00048     Trans_t pro(t.structure());
00049     pro = product(tt, t);
00050     pro = trim(pro);
00051     output_projection(pro, ret);
00052   }
00053 
00054   template<typename SA, typename TA, typename ST,
00055            typename TT, typename SRET, typename TRET>
00056   void
00057   evaluation(const Element<SA, TA>& a, const Element<ST, TT>& t,
00058              Element<SRET, TRET>& ret)
00059   {
00060     do_evaluation(a.structure(), t.structure(), ret.structure(), a, t, ret);
00061   }
00062 
00063   template<typename E, typename S, typename Trans_t, typename M>
00064   void
00065   do_partial_evaluation(const E& exp,
00066                         const TransducerBase<S>&,
00067                         const Trans_t& t,
00068                         M& state_exp_pair_set)
00069   {
00070     typedef typename Trans_t::value_t T;
00071     typedef typename output_projection_helper<S, T>::ret Auto_t;
00072     typedef typename Auto_t::set_t::series_set_t      Auto_series_set_t;
00073     typename Auto_t::set_t
00074       auto_structure(Auto_series_set_t(t.structure().series().semiring()));
00075     Auto_t tmp_auto(auto_structure);
00076 
00077     AUTOMATON_TYPES(Auto_t);
00078     monoid_elt_t empty = tmp_auto.series().monoid().empty_;
00079     standard_of(tmp_auto, exp.get(empty).value());
00080     partial_1(tmp_auto, t, state_exp_pair_set);
00081   }
00082 
00083   /* Input : an expression, a transducer.
00084    Output : a set of pair (hstate_t, expression)*/
00085   template<typename S1, typename T1,
00086            typename S2, typename T2,
00087            typename M>
00088   void
00089   partial_evaluation(const Element<S1, T1>& exp,
00090                      const Element<S2, T2>& trans,
00091                      M& state_exp_pair_set)
00092   {
00093     do_partial_evaluation(exp, trans.structure(), trans, state_exp_pair_set);
00094   }
00095 
00096   template<typename S, typename Auto_t, typename M, typename Chooser_t>
00097   void
00098   do_partial_elimination(const AutomataBase<S>&,
00099                          const Auto_t& a,
00100                          Chooser_t chooser,
00101                          M& state_exp_pair_set)
00102   {
00103     AUTOMATON_TYPES(Auto_t);
00104     typedef typename std::set<htransition_t>            htransition_set_t;
00105     typedef std::map<hstate_t, series_set_elt_t>        sums_t;
00106 
00107     typename htransition_set_t::const_iterator          i, j;
00108     hstate_t                                            q;
00109     htransition_set_t                                   transitions;
00110 
00111     Auto_t b = a;
00112     standardize(b);
00113 
00114     int num = b.final().size() + 1; // all final states and the initial state.
00115 
00116     while (int(b.states().size()) != num)
00117     {
00118       series_set_elt_t loop_sum(b.series());
00119       sums_t       in_sums, out_sums;
00120       typename sums_t::iterator f;
00121       q = chooser(b);
00122       if (b.is_initial(q) || b.is_final(q))
00123         continue;
00124 
00125       transitions.clear();
00126       // FIXME: use a new version of delta !
00127       b.deltac(transitions, q, delta_kind::transitions());
00128       for (i = transitions.begin(); i != transitions.end(); i = j)
00129       {
00130         j = i; ++j;
00131 
00132         if (b.dst_of(*i) == q)
00133           loop_sum += b.series_of(*i);
00134         else if ((f = out_sums.find(b.dst_of(*i))) !=
00135                  out_sums.end())
00136           f->second += b.series_of(*i);
00137         else
00138           out_sums.insert(std::make_pair(b.dst_of(*i), b.series_of(*i)));
00139         b.del_transition(*i);
00140       }
00141       transitions.clear();
00142       // FIXME: use a new version of delta !
00143       b.rdeltac(transitions, q, delta_kind::transitions());
00144       for (i = transitions.begin(); i != transitions.end(); i = j)
00145       {
00146         j = i; ++j;
00147         // here all loops have already been removed
00148         if ((f = in_sums.find(b.src_of(*i))) !=
00149             in_sums.end())
00150           f->second += b.series_of(*i);
00151         else
00152           in_sums.insert(std::make_pair(b.src_of(*i), b.series_of(*i)));
00153         b.del_transition(*i);
00154       }
00155       loop_sum.star();
00156       for_each_const_(sums_t, in, in_sums)
00157         for_each_const_(sums_t, out, out_sums)
00158       {
00159         series_set_elt_t res = in->second * loop_sum * out->second;
00160         b.add_series_transition(in->first, out->first, res);
00161       }
00162       b.del_state(q);
00163     }
00164 
00165     typedef std::map<hstate_t, series_set_elt_t>   se_map_t;
00166     typedef std::pair<hstate_t, series_set_elt_t>  state_exp_pair_t;
00167     se_map_t se_m;
00168 
00169     // maybe there are more than one transition comming to one final state
00170     // we must sum the labels
00171     for_each_transition(e, b)
00172     {
00173       hstate_t aim = b.dst_of(*e);
00174       typename se_map_t::iterator i = se_m.find(aim);
00175       if (i == se_m.end())
00176         se_m.insert(std::make_pair(aim,
00177                                    series_set_elt_t (b.structure().series(),
00178                                                      b.label_of(*e))));
00179       else
00180         i->second += b.label_of(*e);
00181     }
00182 
00183     for_each_final_state(p, b)
00184     {
00185       typename se_map_t::iterator i = se_m.find(*p);
00186       if (i != se_m.end())
00187         state_exp_pair_set.insert(std::make_pair(*p, i->second));
00188     }
00189   }
00190 
00191   /*------------.
00192   | elimination |
00193   `------------*/
00194   // preconditions :
00195   //   - hope that automaton's labels are sufficient to support "star"
00196   //     => in fact, generalized automaton are generally expected here.
00197   //
00198 
00199   template<typename A, typename T, typename M>
00200   void
00201   partial_elimination(const Element<A, T>& a,
00202                       M& state_exp_pair_set)
00203   {
00204     do_partial_elimination(a.structure(),
00205                            a,
00206                            DefaultChooser(),
00207                            state_exp_pair_set);
00208   }
00209 
00211   /* partial_1 */
00212   template<typename SA, typename ST,
00213            typename Auto_t, typename Trans_t,
00214            typename M>
00215   void
00216   do_partial_1(const AutomataBase<SA>&,
00217                const TransducerBase<ST>&,
00218                const Auto_t& a,
00219                const Trans_t& t,
00220                M& state_exp_pair_set)
00221   {
00222     typedef typename Trans_t::value_t T;
00223     typedef typename output_projection_helper<ST, T>::ret Auto_ret_t;
00224     typedef typename Auto_ret_t::set_t::series_set_t      Auto_ret_series_set_t;
00225     typename Auto_ret_t::set_t
00226       auto_structure(Auto_ret_series_set_t(t.structure().series().semiring()));
00227 
00228     AUTOMATON_TYPES_(Auto_t, a_);
00229     AUTOMATON_TYPES_(Trans_t, t_);
00230     AUTOMATON_TYPES_(Auto_ret_t, ret_);
00231 
00232     typedef std::map<hstate_t, std::pair<hstate_t, hstate_t> >
00233       state_pair_map_t;
00234     typedef std::map<hstate_t, hstate_t> state_state_map_t;
00235     typedef std::pair<hstate_t, ret_series_set_elt_t> se_pair_t;
00236 
00237     Trans_t tmp_trans(t.structure());
00238     tmp_trans = extension(a, t);
00239 
00240     Trans_t pro(t.structure());
00241     state_pair_map_t sp_m;
00242     pro = product(tmp_trans, t, sp_m);
00243     Auto_ret_t auto_p(auto_structure);
00244     std::map<hstate_t, hstate_t> proj_m;
00245     auto_p = output_projection(pro, proj_m);
00246 
00247     /* unset final all the final states of auto_p */
00248     auto_p.clear_final();
00249 
00250     /* for each state u of t, add one final state to 'auto_p' */
00251     state_state_map_t final_of, is_final_of;
00252     for(t_state_iterator u = t.states().begin(); u != t.states().end(); ++u)
00253     {
00254       hstate_t new_state = auto_p.add_state();
00255       final_of[*u] = new_state;
00256       is_final_of[new_state] = *u;
00257       auto_p.set_final(new_state);
00258     }
00259 
00260     for(a_state_iterator u = auto_p.states().begin();
00261         u != auto_p.states().end(); ++u)
00262     {
00263       if (!auto_p.is_final(*u))
00264       {
00265         hstate_t p = sp_m[proj_m[*u]].first;
00266         hstate_t q = sp_m[proj_m[*u]].second;
00267 
00268         if (tmp_trans.is_final(p))
00269           auto_p.add_spontaneous(*u, final_of[q]);
00270       }
00271     }
00272 
00273     M se;
00274     partial_elimination(auto_p, se);
00275 
00276     state_exp_pair_set.clear();
00277     for_each_(M, p, se)
00278     {
00279       se_pair_t my_pair = std::make_pair(is_final_of[(*p).first],
00280                                          p->second); // checking type compatibility
00281       state_exp_pair_set.insert(my_pair);
00282     }
00283   }
00284 
00285   template<typename SA, typename TA,
00286            typename ST, typename TT,
00287            typename M>
00288   void
00289   partial_1(const Element<SA, TA>& a,
00290             const Element<ST, TT>& t,
00291             M& state_exp_pair_set)
00292   {
00293     do_partial_1(a.structure(), t.structure(), a, t, state_exp_pair_set);
00294   }
00295 
00297   /* partial_2 */
00298   template<typename SA, typename ST,
00299            typename Auto_t, typename Trans_t,
00300            typename Exp>
00301   void
00302   do_partial_2(const AutomataBase<SA>&,
00303                const TransducerBase<ST>&,
00304                const Auto_t& a,
00305                const Trans_t& t,
00306                const hstate_t p,
00307                Exp& exp)
00308   {
00309     typedef typename Trans_t::value_t T;
00310     typedef typename output_projection_helper<ST, T>::ret    Auto_ret_t;
00311     typedef typename Auto_ret_t::set_t::series_set_t      Auto_ret_series_set_t;
00312 
00313     typename Auto_ret_t::set_t
00314       auto_structure(Auto_ret_series_set_t(t.structure().series().semiring()));
00315 
00316     Trans_t tt = t;
00317     tt.clear_initial();
00318     tt.set_initial(p);
00319 
00320     Trans_t tmp_trans(tt.structure());
00321     tmp_trans = extension(a, tt);
00322 
00323     Trans_t pro(t.structure());
00324     pro = trim(product(tmp_trans, tt));
00325     Auto_ret_t auto_p(auto_structure);
00326     auto_p = output_projection(pro);
00327 
00328     exp = aut_to_exp(auto_p);
00329   }
00330 
00331   template<typename SA, typename TA,
00332            typename ST, typename TT,
00333            typename Exp>
00334   void
00335   partial_2(const Element<SA, TA>& a,
00336             const Element<ST, TT>& t,
00337             const hstate_t p, Exp& exp)
00338   {
00339     do_partial_2(a.structure(), t.structure(), a, t, p, exp);
00340   }
00341 
00343 
00344   template<typename SA, typename ST,
00345            typename Auto_t, typename Trans_t,
00346            typename M>
00347   void
00348   do_partial_3(const AutomataBase<SA>&,
00349                const TransducerBase<ST>&,
00350                const Auto_t& a,
00351                const Trans_t& t,
00352                const hstate_t p,
00353                M& state_exp_pair_set)
00354   {
00355     Trans_t tt = t;
00356     tt.clear_initial();
00357     tt.set_initial(p);
00358     tt=trim(tt);
00359     partial_1(a, tt, state_exp_pair_set);
00360   }
00361 
00362   template<typename SA, typename TA,
00363            typename ST, typename TT,
00364            typename M>
00365   void
00366   partial_3(const Element<SA, TA>& a,
00367             const Element<ST, TT>& t,
00368             const hstate_t p,
00369             M& state_exp_pair_set)
00370   {
00371     do_partial_3(a.structure(), t.structure(), a, t, p, state_exp_pair_set);
00372   }
00373 
00374 }
00375 
00376 #endif // ! VCSN_ALGORITHMS_EVALUATION_HXX

Generated on Fri Jul 28 12:18:31 2006 for Vaucanson by  doxygen 1.4.6