ToPS
GHMMStates.hpp
00001 /*
00002  *       GHMMStates.hpp
00003  *
00004  *       Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br>
00005  *                      Ígor Bonádio <ibonadio@ime.usp.br>
00006  *                      Vitor Onuchic <vitoronuchic@gmail.com>
00007  *                      Alan Mitchell Durham <aland@usp.br>
00008  *
00009  *       This program is free software; you can redistribute it and/or modify
00010  *       it under the terms of the GNU  General Public License as published by
00011  *       the Free Software Foundation; either version 3 of the License, or
00012  *       (at your option) any later version.
00013  *
00014  *       This program is distributed in the hope that it will be useful,
00015  *       but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *       GNU General Public License for more details.
00018  *
00019  *       You should have received a copy of the GNU General Public License
00020  *       along with this program; if not, write to the Free Software
00021  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022  *       MA 02110-1301, USA.
00023  */
00024 
00025 #ifndef GHMM_STATES_HPP
00026 #define GHMM_STATES_HPP
00027 
00028 #include "crossplatform.hpp"
00029 #include "util.hpp"
00030 
00031 #include <vector>
00032 #include <boost/shared_ptr.hpp>
00033 #include <string>
00034 #include <list>
00035 #include "ProbabilisticModelParameter.hpp"
00036 
00037 
00038 namespace tops {
00039 
00040   class DLLEXPORT ProbabilisticModel;
00041   typedef boost::shared_ptr <ProbabilisticModel> ProbabilisticModelPtr;
00042   class DLLEXPORT DiscreteIIDModel;
00043   typedef boost::shared_ptr <DiscreteIIDModel> DiscreteIIDModelPtr;
00044   class DLLEXPORT Symbol;
00045   typedef boost::shared_ptr<Symbol> SymbolPtr;
00046 
00047     class DLLEXPORT GHMMState;
00048     typedef boost::shared_ptr<GHMMState> GHMMStatePtr;
00049     typedef std::vector<GHMMStatePtr> GHMMStates;
00051   class DLLEXPORT GHMMState {
00052   public:
00053     GHMMState();
00054     GHMMState(ProbabilisticModelPtr observation,
00055               DiscreteIIDModelPtr transition, SymbolPtr name) :
00056       _observation(observation), _transition(transition), _name(name){};
00057     virtual ~GHMMState();
00058     virtual void setObservation(ProbabilisticModelPtr obs) ;
00059     virtual ProbabilisticModelPtr observation() const ;
00060     virtual void setTransition(DiscreteIIDModelPtr trans) ;
00061     virtual DiscreteIIDModelPtr transition() const ;
00062     virtual int chooseDuration() const ;
00063     virtual std::string name() const ;
00064     virtual int id() const ;
00065     virtual void addPredecessor(int id) ;
00066     virtual std::vector<int> & predecessors() ;
00067     virtual void addSuccessor(int id) ;
00068     virtual void clearPredecessorSuccessor() {
00069       _successors.clear();
00070       _predecessors.clear();
00071     }
00072     virtual std::vector<int> & successors() ;
00073     virtual std::vector<int> & classes() ;
00074     virtual void setClasses(std::vector<int> &classes);
00075     virtual double duration_probability(int l) const ;
00076     virtual bool isGeometricDuration() const ;
00077     virtual std::string str() const ;
00078     virtual int getInputPhase() const ;
00079     virtual void setInputPhase(int _inputPhase) ;
00080     virtual int getOutputPhase() const ;
00081     virtual void setOutputPhase(int _outputPhase) ;
00082 
00083     virtual int getStart() const ;
00084     virtual void setStart(int start) ;
00085     virtual int getStop() const ;
00086     virtual void setStop(int stop) ;
00087 
00088     virtual void isLeftJoinable(int joinable);
00089     virtual int isLeftJoinable() const;
00090 
00091     virtual void isRightJoinable(int joinable);
00092     virtual int isRightJoinable() const;
00093 
00094 
00095     virtual void observationModelName(std::string name) ;
00096     virtual void durationModelName(std::string name) ;
00097     virtual std::string observationModelName() const;
00098     virtual std::string durationModelName() const;
00099     virtual void fixTransitionDistribution () const {} ;
00100     virtual ProbabilisticModelParameters parameters() const;
00101     virtual void findBestPredecessor (Matrix & gamma, Matrix &psi, Matrix &psilen, const Sequence & s, int base, const GHMMStates & all_states, std::map < int, std::list<int> >  & valid_positions );
00102     virtual void forwardSum (Matrix & alpha, const Sequence & s, int base, const GHMMStates & all_states, std::vector< std::list<int> > &valid_positions);
00103     virtual double backwardSum(Matrix &beta, const Sequence &s, int base, std::vector< std::list<int> > &valid_positions);
00104     virtual void posteriorSum (Matrix & alpha, Matrix &beta, fMatrix &postProbs, const Sequence & s, int base, const GHMMStates & all_states, std::vector< std::list<int> > &valid_positions, double prob, int stateNumber);
00105 
00106     virtual void choosePredecessor (Matrix & alpha, int base, int & state, int & position , const GHMMStates & all_states);
00107 
00108   protected:
00109     ProbabilisticModelPtr _observation;
00110     DiscreteIIDModelPtr _transition;
00111     SymbolPtr _name;
00112     std::vector<int> _predecessors;
00113     std::vector<int> _successors;
00114     std::vector<int> _classes;
00115     int _inputPhase;
00116     int _outputPhase;
00117     int _start;
00118     int _stop;
00119     bool _left_joinable;
00120     bool _right_joinable;
00121     std::string _observationModelName;
00122   };
00124   class DLLEXPORT GHMMSignalState: public GHMMState {
00125   public:
00126     GHMMSignalState() ;
00127     GHMMSignalState(ProbabilisticModelPtr observation,
00128                     DiscreteIIDModelPtr transition, SymbolPtr name) :
00129       GHMMState(observation, transition, name) {};
00130 
00131     virtual int size() const ;
00132     virtual void setSize(int s);
00133     virtual int chooseDuration() const ;
00134     virtual double getThreshold() const ;
00135     virtual void setThreshold(double threshold) ;
00136     virtual double duration_probability(int l) const ;
00137     virtual std::string str() const ;
00138 
00139     virtual ProbabilisticModelParameters parameters() const;
00140     virtual void fixTransitionDistribution () const ;
00141       virtual void findBestPredecessor (Matrix & gamma, Matrix &psi, Matrix &psilen, const Sequence & s, int base, const GHMMStates & all_states, std::map < int, std::list<int> >  & valid_positions );
00142       virtual void forwardSum (Matrix & alpha, const Sequence & s, int base, const GHMMStates & all_states, std::vector< std::list<int> > &valid_positions);
00143     virtual double backwardSum(Matrix &beta, const Sequence &s, int base, std::vector< std::list<int> > &valid_positions);
00144     virtual void posteriorSum (Matrix & alpha, Matrix &beta, fMatrix &postProbs, const Sequence & s, int base, const GHMMStates & all_states, std::vector< std::list<int> > &valid_positions, double prob, int stateNumber);
00145 
00146       virtual void choosePredecessor (Matrix & alpha, int base, int & state, int & position, const GHMMStates & all_states);
00147 
00148   private:
00149     int _size;
00150     double _threshold;
00151 
00152   };
00154   class DLLEXPORT GHMMExplicitDurationState: public GHMMState {
00155   public:
00156     virtual ~GHMMExplicitDurationState() ;
00157 
00158     GHMMExplicitDurationState() ;
00159 
00160     GHMMExplicitDurationState(ProbabilisticModelPtr observation,
00161                               DiscreteIIDModelPtr transition, SymbolPtr name) :
00162       GHMMState(observation, transition, name) {};
00163       virtual void findBestPredecessor (Matrix & gamma, Matrix &psi, Matrix &psilen, const Sequence & s, int base, const GHMMStates & all_states, std::map < int, std::list<int> >  & valid_positions );
00164       virtual void forwardSum (Matrix & alpha, const Sequence & s, int base, const GHMMStates & all_states, std::vector< std::list<int> > &valid_positions);
00165     virtual double backwardSum(Matrix &beta, const Sequence &s, int base, std::vector< std::list<int> > &valid_positions);
00166     virtual void posteriorSum (Matrix & alpha, Matrix &beta, fMatrix &postProbs, const Sequence & s, int base, const GHMMStates & all_states, std::vector< std::list<int> > &valid_positions, double prob, int stateNumber);
00167 
00168       virtual void choosePredecessor (Matrix & alpha, int base, int & state, int & position, const GHMMStates & all_states);
00169 
00170 
00171     virtual void setDuration(ProbabilisticModelPtr d) ;
00172     virtual ProbabilisticModelPtr duration() const ;
00173     virtual int chooseDuration() const ;
00174     virtual bool isGeometricDuration() const ;
00175     virtual double duration_probability(int l) const ;
00176     virtual std::string str() const ;
00177 
00178     virtual void durationModelName(std::string name);
00179     virtual std::string durationModelName() const ;
00180     virtual void fixTransitionDistribution () const;
00181 
00182     virtual ProbabilisticModelParameters parameters() const;
00183 
00184   private:
00185     ProbabilisticModelPtr _duration;
00186     std::string _durationModelName;
00187     int _number_of_phases;
00188   };
00189 
00190 
00191   typedef boost::shared_ptr<GHMMSignalState> GHMMSignalStatePtr;
00192   typedef boost::shared_ptr<GHMMExplicitDurationState>
00193   GHMMExplicitDurationStatePtr;
00194 
00195 
00196   typedef std::vector<GHMMSignalStatePtr> GHMMSignalStates;
00197   typedef std::vector<GHMMExplicitDurationStatePtr> GHMMExplicitDurationStates;
00198 
00199 }
00200 
00201 #endif