📄 ghmm_swdiscretemodel.cpp
字号:
/* created: 2. April 2003 by Wasinee authors: Wasinee Rungsarityotin (rungsari@molgen.mpg.de) file : $Source: /cvsroot/ghmm/ghmm/ghmm++/GHMM_SWDiscreteModel.cpp,v $ $Id: GHMM_SWDiscreteModel.cpp,v 1.8 2004/03/01 14:59:57 wasinee 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 "ghmm/ghmm.h"#include "ghmm/viterbi.h"#include "ghmm/mes.h"#include "ghmm/foba.h"#include "ghmm/matrix.h"#include "ghmm/model.h"#include "ghmm/vector.h"#include <xmlio/XMLIO_Definitions.h>#include <xmlio/XMLIO_Document.h>#include "ghmm++/GHMM_IntVector.h"#include "ghmm++/GHMM_DoubleVector.h"#include "ghmm++/GHMM_Sequences.h"#include "ghmm++/GHMM_DoubleMatrix.h"#include "ghmm++/GHMM_Alphabet.h"#include "ghmm++/GHMM_GMLEmission.h"#include "ghmm++/GHMM_GMLAlphabet.h"#include <ghmm++/GHMM_AbstractModelT.hh> // Template#include "ghmm++/GHMM_SWDiscreteModel.h"#include <iostream>#include <queue>#include <vector>// #include "ghmm/reestimate.h"#ifdef HAVE_NAMESPACESusing namespace std;#endifGHMM_SWDiscreteModel::GHMM_SWDiscreteModel(GHMM_Alphabet* my_alphabet) { init(my_alphabet); alphabet = my_alphabet;}GHMM_SWDiscreteModel::GHMM_SWDiscreteModel(GHMM_Alphabet* my_alphabet, int no_klass){ init(my_alphabet); alphabet = my_alphabet; no_classes = no_klass; c_model->cos = no_klass;}/** Constructor. */GHMM_SWDiscreteModel::GHMM_SWDiscreteModel( sdmodel* my_model) { init(); c_model = my_model; buildCppData();}GHMM_SWDiscreteModel::~GHMM_SWDiscreteModel() { /* frees model. */ sdmodel_free(&c_model); if (own_alphabet) SAFE_DELETE(alphabet); cleanCPP();}void GHMM_SWDiscreteModel::setNodeTag(const string& tag){ GHMM_AbstractModelT<GHMM_GMLState, GHMM_GMLTransition>::setNodeTag( tag ); }void GHMM_SWDiscreteModel::setTransitionTag(const string& tag){ GHMM_AbstractModelT<GHMM_GMLState, GHMM_GMLTransition>::setTransitionTag( tag ); }const char* GHMM_SWDiscreteModel::toString() const { return "GHMM_SWDiscreteModel";}/*int GHMM_SWDiscreteModel::check() const { return model_check(c_model);}*//** Produces a model, which generates the given sequence with probability 1. The model is a strict left-right model with one state for each element in the sequence and the output in state i is the i-th value in the sequence with probability 1. The model also has a final state, a state with no output. @param seq: sequence @param seq_len: length of the sequence @param anz_symb: number of symbols in the sequence*///GHMM_SWDiscreteModel::GHMM_SWDiscreteModel(const int *seq, int seq_len, int anz_symb) {// c_model = model_generate_from_sequence(seq,seq_len,anz_symb);//}GHMM_Sequences* GHMM_SWDiscreteModel::generate_sequences(int seed, int global_len, long seq_number, int Tmax) const { return new GHMM_Sequences(sdmodel_generate_sequences(c_model,seed,global_len,seq_number, Tmax));}/** Calculates the sum log( P( O | lambda ) ). Sequences, that can not be generated from the given model, are neglected. @return log(P) @param mo model @param sq sequences *///double GHMM_SWDiscreteModel::likelihood(sequence_t *sq) {// return model_likelihood(c_model,sq);//}/** Writes the model in matrix format. @param file: output file*/void GHMM_SWDiscreteModel::print(FILE *file){ fprintf(stderr, "GHMM_SWDiscreteModel::print method not implemented\n");}void GHMM_SWDiscreteModel::A_print(FILE *file, char *tab, char *separator, char *ending) const { for(int k=0; k < c_model->cos; k++) sdmodel_Ak_print(file,c_model,k,tab,separator,ending);}void GHMM_SWDiscreteModel::B_print(FILE *file, char *tab, char *separator, char *ending) const { sdmodel_B_print(file,c_model,tab,separator,ending);}void GHMM_SWDiscreteModel::Pi_print(FILE *file, char *tab, char *separator, char *ending) const { sdmodel_Pi_print(file,c_model,tab,separator,ending);}//void GHMM_SWDiscreteModel::states_print(FILE *file) {// sdmodel_states_print(file,c_model);//}//double GHMM_SWDiscreteModel::prob_distance(GHMM_SWDiscreteModel* m, int maxT, int symmetric, int verbose) {// return model_prob_distance(c_model,m->c_model,maxT,symmetric,verbose);//}/*GHMM_DoubleMatrix* GHMM_SWDiscreteModel::foba_forward(GHMM_Sequences* seq, int index, GHMM_DoubleVector *scale, double *log_p) const { int len = seq->getLength(index); GHMM_DoubleMatrix *alpha = new GHMM_DoubleMatrix(len,c_model->N); bool delete_scale = false; if (! scale) { scale = new GHMM_DoubleVector(); delete_scale = true; } scale->resize(len); int result = ::foba_forward(c_model,seq->getIntSequence(index),len,alpha->c_matrix,scale->c_vector,log_p); if (result == -1) SAFE_DELETE(alpha); if (delete_scale) SAFE_DELETE(scale); return alpha; }*//*int GHMM_SWDiscreteModel::foba_backward(GHMM_Sequences* seq, int index, double **beta, const double *scale) const { return ::foba_backward(c_model,seq->getIntSequence(index),seq->getLength(index),beta,scale); }*//*int GHMM_SWDiscreteModel::foba_logp(GHMM_Sequences* seq, int index, double *log_p) const { return ::foba_logp(c_model,seq->getIntSequence(index),seq->getLength(index),log_p); }*//*sdstate* GHMM_SWDiscreteModel::getCState(int index) const { if (index >= c_model->N) { fprintf(stderr,"GHMM_SWDiscreteModel::getCState(int):\n"); fprintf(stderr,"State no. %d does not exist. Model has %d states.\n",index,c_model->N); exit(1); } return &c_model->s[index]; }*/int GHMM_SWDiscreteModel::getNumberOfTransitionMatrices() const { return c_model->cos;}//int GHMM_SWDiscreteModel::reestimate_baum_welch(GHMM_Sequences* seq) {// return ::reestimate_baum_welch(c_model,seq->c_i_sequences);//}void GHMM_SWDiscreteModel::buildCppData(){ /* Create C++ wrapper for all states and fill C states with usefull information. */ int i; for (i = 0; i < c_model->N; ++i) { GHMM_GMLState* state = new GHMM_GMLState(this,i,&c_model->s[i]); states.push_back(state); }}void GHMM_SWDiscreteModel::cleanCPP(){ unsigned int i; for (i = 0; i < states.size(); ++i) { SAFE_DELETE(states[i]); if (edge_classes) delete edge_classes[i]; } if (edge_classes) delete edge_classes; states.clear();}void GHMM_SWDiscreteModel::init() { attributes.clear(); tag = "graph"; setNodeTag("node"); setTransitionTag("edge"); alphabet = NULL; c_model = NULL; own_alphabet = false; no_classes = 1;}void GHMM_SWDiscreteModel::init(GHMM_Alphabet *my_alphabet) { attributes.clear(); tag = "graph"; setNodeTag("node"); setTransitionTag("edge"); alphabet = my_alphabet; c_model = NULL; own_alphabet = true; no_classes = 1; c_model = (sdmodel*) calloc(1,sizeof(sdmodel)); if (!c_model) { fprintf(stderr,"GHMM_SWDiscreteModel::GHMM_SWDiscreteModel() could not allocate c_model\n"); exit(1); } c_model->N = 0; c_model->M = alphabet->size(); c_model->cos = no_classes; c_model->prior = -1; c_model->s = NULL; c_model->model_type = 0; c_model->silent = NULL;}void GHMM_SWDiscreteModel::init(int number_of_states, int my_M, double my_prior) { init(); int i; int j; c_model = (sdmodel*) calloc(1,sizeof(sdmodel)); if (!c_model) { fprintf(stderr,"GHMM_SWDiscreteModel::GHMM_SWDiscreteModel() could not allocate c_model\n"); exit(1); } c_model->model_type = 0; c_model->N = number_of_states; c_model->M = my_M; c_model->prior = my_prior; c_model->cos = no_classes; c_model->s = (sdstate*) malloc(sizeof(sdstate) * max(c_model->N,1)); /* initialize all states. */ c_model->silent = NULL; for (i = 0; i < number_of_states; ++i) { c_model->s[i].pi = 0; c_model->s[i].b = (double*) malloc(sizeof(double) * my_M); /* output probabilities are initialized with 0. */ for (j = 0; j < my_M; ++j) c_model->s[i].b[j] = 0; c_model->s[i].out_id = NULL; c_model->s[i].in_id = NULL; c_model->s[i].out_a = NULL; c_model->s[i].in_a = NULL; c_model->s[i].out_states = 0; c_model->s[i].in_states = 0; c_model->s[i].fix = 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -