📄 ghmm_state.cpp
字号:
/* * created: 21 Jan 2002 by Peter Pipenbacher * authors: Peter Pipenbacher (pipenb@zpr.uni-koeln.de) * file : $Source: /cvsroot/ghmm/ghmm/ghmm++/GHMM_State.cpp,v $ * $Id: GHMM_State.cpp,v 1.7 2003/09/18 10:04:49 cic99 Exp $ * * Copyright (C) 1998-2001, ZAIK/ZPR, Universit鋞 zu K鰈n * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -