hmm_model.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_HMM_HMM_MODEL_HPP
13 #define MLPACK_METHODS_HMM_HMM_MODEL_HPP
14 
15 #include "hmm.hpp"
17 
18 namespace mlpack {
19 namespace hmm {
20 
21 enum HMMType : char
22 {
26 };
27 
31 class HMMModel
32 {
33  private:
35  HMMType type;
41  HMM<gmm::GMM>* gmmHMM;
42 
43  public:
46  type(HMMType::DiscreteHMM),
47  discreteHMM(new HMM<distribution::DiscreteDistribution>()),
48  gaussianHMM(NULL),
49  gmmHMM(NULL)
50  {
51  // Nothing to do.
52  }
53 
55  HMMModel(const HMMType type) :
56  type(type),
57  discreteHMM(NULL),
58  gaussianHMM(NULL),
59  gmmHMM(NULL)
60  {
61  if (type == HMMType::DiscreteHMM)
62  discreteHMM = new HMM<distribution::DiscreteDistribution>();
63  else if (type == HMMType::GaussianHMM)
64  gaussianHMM = new HMM<distribution::GaussianDistribution>();
65  else if (type == HMMType::GaussianMixtureModelHMM)
66  gmmHMM = new HMM<gmm::GMM>();
67  }
68 
70  HMMModel(const HMMModel& other) :
71  type(other.type),
72  discreteHMM(NULL),
73  gaussianHMM(NULL),
74  gmmHMM(NULL)
75  {
76  if (type == HMMType::DiscreteHMM)
77  discreteHMM =
78  new HMM<distribution::DiscreteDistribution>(*other.discreteHMM);
79  else if (type == HMMType::GaussianHMM)
80  gaussianHMM =
81  new HMM<distribution::GaussianDistribution>(*other.gaussianHMM);
82  else if (type == HMMType::GaussianMixtureModelHMM)
83  gmmHMM = new HMM<gmm::GMM>(*other.gmmHMM);
84  }
85 
87  HMMModel(HMMModel&& other) :
88  type(other.type),
89  discreteHMM(other.discreteHMM),
90  gaussianHMM(other.gaussianHMM),
91  gmmHMM(other.gmmHMM)
92  {
93  other.type = HMMType::DiscreteHMM;
94  other.discreteHMM = new HMM<distribution::DiscreteDistribution>();
95  other.gaussianHMM = NULL;
96  other.gmmHMM = NULL;
97  }
98 
100  HMMModel& operator=(const HMMModel& other)
101  {
102  if (this == &other)
103  return *this;
104 
105  delete discreteHMM;
106  delete gaussianHMM;
107  delete gmmHMM;
108 
109  discreteHMM = NULL;
110  gaussianHMM = NULL;
111  gmmHMM = NULL;
112 
113  type = other.type;
114  if (type == HMMType::DiscreteHMM)
115  discreteHMM =
116  new HMM<distribution::DiscreteDistribution>(*other.discreteHMM);
117  else if (type == HMMType::GaussianHMM)
118  gaussianHMM =
119  new HMM<distribution::GaussianDistribution>(*other.gaussianHMM);
120  else if (type == HMMType::GaussianMixtureModelHMM)
121  gmmHMM = new HMM<gmm::GMM>(*other.gmmHMM);
122 
123  return *this;
124  }
125 
128  {
129  delete discreteHMM;
130  delete gaussianHMM;
131  delete gmmHMM;
132  }
133 
138  template<typename ActionType,
139  typename ExtraInfoType>
140  void PerformAction(ExtraInfoType* x)
141  {
142  if (type == HMMType::DiscreteHMM)
143  ActionType::Apply(*discreteHMM, x);
144  else if (type == HMMType::GaussianHMM)
145  ActionType::Apply(*gaussianHMM, x);
146  else if (type == HMMType::GaussianMixtureModelHMM)
147  ActionType::Apply(*gmmHMM, x);
148  }
149 
151  template<typename Archive>
152  void serialize(Archive& ar, const unsigned int /* version */)
153  {
154  ar & BOOST_SERIALIZATION_NVP(type);
155 
156  // If necessary, clean memory.
157  if (Archive::is_loading::value)
158  {
159  delete discreteHMM;
160  delete gaussianHMM;
161  delete gmmHMM;
162 
163  discreteHMM = NULL;
164  gaussianHMM = NULL;
165  gmmHMM = NULL;
166  }
167 
168  if (type == HMMType::DiscreteHMM)
169  ar & BOOST_SERIALIZATION_NVP(discreteHMM);
170  else if (type == HMMType::GaussianHMM)
171  ar & BOOST_SERIALIZATION_NVP(gaussianHMM);
172  else if (type == HMMType::GaussianMixtureModelHMM)
173  ar & BOOST_SERIALIZATION_NVP(gmmHMM);
174  }
175 
176  // Accessor method for type of HMM
177  HMMType Type() { return type; }
178 
199  HMM<gmm::GMM>* GMMHMM() { return gmmHMM; }
200 };
201 
202 } // namespace hmm
203 } // namespace mlpack
204 
205 #endif
HMM< distribution::DiscreteDistribution > * DiscreteHMM()
Accessor methods for discreteHMM, gaussianHMM and gmmHMM.
Definition: hmm_model.hpp:197
.hpp
Definition: add_to_po.hpp:21
void PerformAction(ExtraInfoType *x)
Given a functor type, perform that functor with the optional extra info on the HMM.
Definition: hmm_model.hpp:140
~HMMModel()
Clean memory.
Definition: hmm_model.hpp:127
HMM< gmm::GMM > * GMMHMM()
Definition: hmm_model.hpp:199
HMMModel(HMMModel &&other)
Take ownership of another model.
Definition: hmm_model.hpp:87
A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
Definition: hmm.hpp:85
HMMModel(const HMMType type)
Construct a model of the given type.
Definition: hmm_model.hpp:55
A serializable HMM model that also stores the type.
Definition: hmm_model.hpp:31
void serialize(Archive &ar, const unsigned int)
Serialize the model.
Definition: hmm_model.hpp:152
HMMModel()
Construct an uninitialized model.
Definition: hmm_model.hpp:45
HMMModel(const HMMModel &other)
Copy another model.
Definition: hmm_model.hpp:70
HMM< distribution::GaussianDistribution > * GaussianHMM()
Definition: hmm_model.hpp:198
HMMModel & operator=(const HMMModel &other)
Copy assignment operator.
Definition: hmm_model.hpp:100