ghmm_state.cpp

来自「General Hidden Markov Model Library 一个通用」· C++ 代码 · 共 602 行 · 第 1/2 页

CPP
602
字号
/* * created: 21 Jan 2002 by Peter Pipenbacher * authors: Peter Pipenbacher (pipenb@zpr.uni-koeln.de) * file   : $Source$ * $Id: GHMM_State.cpp 275 2003-09-18 10:04:49Z cic99 $ * * __copyright__ */#include <xmlio/XMLIO_Definitions.h>#include <xmlio/XMLIO_Document.h>#include "ghmm/matrix.h"#include "ghmm++/GHMM_Types.h"#include "ghmm++/GHMM_State.h"#include "ghmm++/GHMM_Transition.h"#include "ghmm++/GHMM_ContinuousModel.h"#include "ghmm++/GHMM_DiscreteModel.h"#include "ghmm++/GHMM_Emission.h"#include "ghmm++/GHMM_Toolkit.h"#include "ghmm++/GHMM_Alphabet.h"#include <iostream>#ifdef HAVE_NAMESPACESusing namespace std;#endifGHMM_State::GHMM_State(GHMM_AbstractModel* my_model, int my_index, XMLIO_Attributes& attrs) {  index             = my_index;  c_state           = NULL;   c_sstate          = NULL;  reading           = GHMM_STATE_NONE;  cemission          = NULL;  demission          = NULL;  parent_model      = my_model;  tag               = "state";  xmlio_indent_type = XMLIO_INDENT_BOTH;  /* by default take index as id. */  id = attrs["id"];  if (id == "")    id = GHMM_Toolkit::toString(my_index);  my_model->addStateID(id,index);  attributes = attrs;}GHMM_State::GHMM_State(GHMM_AbstractModel* my_model, int my_index, sstate* my_state) {  index             = my_index;  c_sstate          = my_state;  c_state           = NULL;  c_sdstate          = NULL;  reading           = GHMM_STATE_NONE;  cemission          = NULL;  demission          = NULL;  parent_model      = my_model;  tag               = "state";  xmlio_indent_type = XMLIO_INDENT_BOTH;    /* take index as id. */  id = GHMM_Toolkit::toString(my_index);  my_model->addStateID(id,index);}GHMM_State::GHMM_State(GHMM_AbstractModel* my_model, int my_index, state* my_state) {  index             = my_index;  c_sstate          = NULL;  c_state           = my_state;  c_sdstate         = NULL;  reading           = GHMM_STATE_NONE;  cemission          = NULL;  demission          = NULL;  parent_model      = my_model;  tag               = "state";  xmlio_indent_type = XMLIO_INDENT_BOTH;    /* take index as id. */  id = GHMM_Toolkit::toString(my_index);  my_model->addStateID(id,index);}GHMM_State::GHMM_State(GHMM_AbstractModel* my_model, int my_index, sdstate* my_state) {  index             = my_index;  c_sstate          = NULL;  c_state           = NULL;  c_sdstate         = my_state;  reading           = GHMM_STATE_NONE;  demission          = NULL;  cemission          = NULL;  parent_model      = my_model;  tag               = "state";  xmlio_indent_type = XMLIO_INDENT_BOTH;    /* take index as id. */  id = GHMM_Toolkit::toString(my_index);  my_model->addStateID(id,index);}GHMM_State::~GHMM_State() {  SAFE_DELETE(cemission);  SAFE_DELETE(demission);}const char* GHMM_State::toString() const {  return "GHMM_State";}XMLIO_Element* GHMM_State::XMLIO_startTag(const string& tag, XMLIO_Attributes &attrs) {    if (tag == "initial") {    reading = GHMM_STATE_INITIAL;        return this;  }  if (tag == "emission") {    if (getModelType() == GHMM_CONTINOUS) {      SAFE_DELETE(cemission);      return (cemission = new GHMM_CEmission(this));    }    if (getModelType() == GHMM_DISCRETE) {      SAFE_DELETE(demission);      return (demission = new GHMM_DEmission(this));    }  }  fprintf(stderr,"tag '%s' not recognized in state element\n",tag.c_str());  exit(1);    return NULL;}void GHMM_State::XMLIO_endTag(const string& tag) {  reading = GHMM_STATE_NONE;}void GHMM_State::XMLIO_getCharacters(const string& characters) {  switch (reading) {  case GHMM_STATE_INITIAL:    initial = atof(characters.c_str());    break;      case GHMM_STATE_NONE:    break;  }}void GHMM_State::fillState(sstate* s) {  GHMM_ContinuousModel* model = (GHMM_ContinuousModel*) parent_model;  smodel* c_model             = model->c_model;  /* store current c representation of state. */  c_sstate = s;  s->c   = (double*) malloc(sizeof(double) * c_model->M);  s->mue = (double*) malloc(sizeof(double) * c_model->M);  s->u   = (double*) malloc(sizeof(double) * c_model->M);  vector<GHMM_Transition*> out_edges;  vector<GHMM_Transition*> in_edges;  unsigned int i;  for (i = 0; i < model->transitions.size(); ++i) {    if (model->transitions[i]->source == id)      out_edges.push_back(model->transitions[i]);    if (model->transitions[i]->target == id)      in_edges.push_back(model->transitions[i]);  }  if (out_edges.size() > 0) {    s->out_id = (int*) malloc(sizeof(int) * out_edges.size());    s->out_a  = matrix_d_alloc(c_model->cos, out_edges.size());  }  if (in_edges.size() > 0) {    s->in_id = (int*) malloc(sizeof(int) * in_edges.size());    s->in_a  = matrix_d_alloc(c_model->cos, in_edges.size());  }  /* now fill with useful data. */  s->pi = initial;  for (i = 0; i < out_edges.size(); ++i)    s->out_id[i] = model->getStateIndex(out_edges[i]->target);  for (i = 0; i < in_edges.size(); ++i)    s->in_id[i] = model->getStateIndex(in_edges[i]->source);  int cos;  for (cos = 0; cos < c_model->cos; ++cos) {    for (i = 0; i < out_edges.size(); ++i)      s->out_a[cos][i] = out_edges[i]->prob;    for (i = 0; i < in_edges.size(); ++i)      s->in_a[cos][i] = in_edges[i]->prob;  }  s->out_states = out_edges.size();  s->in_states  = in_edges.size();  if (c_model->M != 1) {    fprintf(stderr,"M != 1 not yet supported in GHMM_State.cpp\n");    exit(1);  }    if (c_model->M != (int) cemission->weights.size()) {    fprintf(stderr,"M == %d, but just %d weights found in GHMM_State.cpp\n",c_model->M,(int) cemission->weights.size());    exit(1);  }  for (i = 0; (int) i < c_model->M; ++i) {    s->c[i]   = cemission->weights[i];    s->mue[i] = cemission->mue[i];    s->u[i]   = cemission->variance[i];  }  s->fix = 0;}void GHMM_State::fillState(state* s) {  GHMM_DiscreteModel* m = (GHMM_DiscreteModel*) parent_model;  model* c_model        = m->c_model;  /* store current c representation of state. */  c_state = s;  s->b   = (double*) malloc(sizeof(double) * c_model->M);  vector<GHMM_Transition*> out_edges;  vector<GHMM_Transition*> in_edges;  unsigned int i;  for (i = 0; i < m->transitions.size(); ++i) {    if (m->transitions[i]->source == id)      out_edges.push_back(m->transitions[i]);    if (m->transitions[i]->target == id)      in_edges.push_back(m->transitions[i]);  }  if (out_edges.size() > 0) {    s->out_id = (int*) malloc(sizeof(int) * out_edges.size());    s->out_a  = (double*) malloc(sizeof(double) * out_edges.size());  }  if (in_edges.size() > 0) {    s->in_id = (int*) malloc(sizeof(int) * in_edges.size());    s->in_a  = (double*) malloc(sizeof(double) * in_edges.size());  }  /* now fill with useful data. */  s->pi = initial;  for (i = 0; i < out_edges.size(); ++i)    s->out_id[i] = m->getStateIndex(out_edges[i]->target);  for (i = 0; i < in_edges.size(); ++i)    s->in_id[i] = m->getStateIndex(in_edges[i]->source);  for (i = 0; i < out_edges.size(); ++i)    {      s->out_a[i] = out_edges[i]->prob;      // printf( "\t State %d, out %g\n", i, s->out_a[i]);    }  for (i = 0; i < in_edges.size(); ++i)    {      s->in_a[i] = in_edges[i]->prob;      // printf( "\t State %d, in %g\n", i, s->out_a[i]);    }  s->out_states = out_edges.size();  s->in_states  = in_edges.size();  if (c_model->M != (int) demission->weights.size()) {    fprintf(stderr,"M == %d, but just %d weights found in GHMM_State.cpp\n",c_model->M,(int) demission->weights.size());    exit(1);  }  for (i = 0; (int) i < c_model->M; ++i)    s->b[i] = demission->weights[i];  s->fix = 0;}void GHMM_State::fillState(sdstate* s) {  fprintf(stderr, "GHMM_State::fillState. You should not be here\n");  exit(1);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?