⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hmm_1dlg.cpp

📁 隐马尔可夫模型演示,給出向前矩阵和向后矩阵
💻 CPP
字号:
// hmm_1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "hmm_1.h"
#include "hmm_1Dlg.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHmm_1Dlg dialog

CHmm_1Dlg::CHmm_1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHmm_1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHmm_1Dlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CHmm_1Dlg::~CHmm_1Dlg()
{
	delete [] m_pA[0];
	delete [] m_pA[1];
	delete [] m_pA[2];
	delete [] m_pA;

	delete [] m_pB[0];
	delete [] m_pB[1];
	delete [] m_pB[2];
	delete [] m_pB;

	if(m_pRltMatrix!=NULL)
	{
		for(int i=0; i<m_iDimA; i++)
		{
			if(m_pRltMatrix[i]!=NULL)
			{
				delete [] m_pRltMatrix[i];
				m_pRltMatrix[i] = NULL;
			}
		}

		delete [] m_pRltMatrix;
		m_pRltMatrix = NULL;
	}

	if(m_plamda!=NULL)
	{
		for(int i=0; i<m_iDimA; i++)
		{
			if(m_plamda[i]!=NULL)
			{
				delete [] m_plamda[i];
				m_plamda[i] = NULL; 
			}
		}

		delete [] m_plamda;
		m_plamda = NULL;
	}
}

void CHmm_1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHmm_1Dlg)
	DDX_Control(pDX, IDC_STATIC_INFO2, m_cInfo2);
	DDX_Control(pDX, IDC_STATIC_INFO1, m_cInfo1);
	DDX_Control(pDX, IDC_STATIC_INFO, m_cInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHmm_1Dlg, CDialog)
	//{{AFX_MSG_MAP(CHmm_1Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHmm_1Dlg message handlers

BOOL CHmm_1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	m_iDimA = 3;
	m_iDimB = 2;

	m_pA = new double*[m_iDimA];
	m_pA[0] = new double[m_iDimA];	m_pA[1] = new double[m_iDimA];	m_pA[2] = new double[m_iDimA];

	m_pA[0][0] = 0.9;	m_pA[0][1] = 0.05;		m_pA[0][2] = 0.05;
	m_pA[1][0] = 0.45;	m_pA[1][1] = 0.1;		m_pA[1][2] = 0.45;
	m_pA[2][0] = 0.45;	m_pA[2][1] = 0.45;		m_pA[2][2] = 0.1;
	
	m_pB = new double*[m_iDimA];
	m_pB[0] = new double[m_iDimB];	m_pB[1] = new double[m_iDimB];	m_pB[2] = new double[m_iDimB];
	m_pB[0][0] = 0.5;			m_pB[0][1] = 0.5;
	m_pB[1][0] = 0.75;			m_pB[1][1] = 0.25;
	m_pB[2][0] = 0.25;			m_pB[2][1] = 0.75;

	m_pPI = new double[m_iDimA];
	m_pPI[0] = double(1.0/3.0);
	m_pPI[1] = double(1.0/3.0);
	m_pPI[2] = double(1.0/3.0);


	m_hmm.SetHmmParam(m_iDimA, m_iDimB, m_pA, m_pB, m_pPI);


	m_iInputLen = 3;

	m_pRltMatrix = new double*[m_iDimA];
	for(int i=0; i<m_iDimA; i++)
	{
		m_pRltMatrix[i] = new double[m_iInputLen+1];
	}

	m_plamda = new int*[m_iDimA];
	for(i=0; i<m_iDimA; i++)
	{
		m_plamda[i] = new int[m_iInputLen];
	}
	

	m_plamda[0][0] = 0;
	m_plamda[1][0] = 0;
	m_plamda[2][0] = 0;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CHmm_1Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CHmm_1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void CHmm_1Dlg::OnButton1() 
{
	int *Input;
	Input = new int[m_iInputLen];

	Input[0] = 0;
	Input[1] = 0;
	Input[2] = 1;

	//get result
	double result = m_hmm.Forward(Input, 3, m_pRltMatrix);
	

	CString s;
	s.Format("%f", result);
	m_cInfo.SetWindowText(s);

	s.Format("%f %f %f | %f %f %f | %f %f %f", 
		m_pRltMatrix[0][0],	m_pRltMatrix[1][0], m_pRltMatrix[2][0],
		m_pRltMatrix[0][1], m_pRltMatrix[1][1], m_pRltMatrix[2][1],
		m_pRltMatrix[0][2],	m_pRltMatrix[1][2], m_pRltMatrix[2][2]);
	m_cInfo1.SetWindowText(s);
	m_cInfo2.SetWindowText("");

	delete [] Input;
}


void CHmm_1Dlg::OnButton2() 
{

	int *Input;
	Input = new int[m_iInputLen];

	Input[0] = 0;
	Input[1] = 0;
	Input[2] = 1;
	
	//get result
	double result = m_hmm.Backward(Input, 3, m_pRltMatrix);

	CString s;
	s.Format("%f", result);
	m_cInfo.SetWindowText(s);

	s.Format("%f %f %f | %f %f %f | %f %f %f", 
		m_pRltMatrix[0][0],	m_pRltMatrix[1][0], m_pRltMatrix[2][0],
		m_pRltMatrix[0][1], m_pRltMatrix[1][1], m_pRltMatrix[2][1],
		m_pRltMatrix[0][2],	m_pRltMatrix[1][2], m_pRltMatrix[2][2]);
	m_cInfo1.SetWindowText(s);
	m_cInfo2.SetWindowText("");

	delete [] Input;
}


void CHmm_1Dlg::OnButton3() 
{

	//输入 序列
	int *Input;
	Input = new int[m_iInputLen];
	Input[0] = 0;
	Input[1] = 0;
	Input[2] = 1;

	int *bestpath;	//最佳路径
	bestpath = new int[m_iInputLen];

	//
	double result = m_hmm.Viterbi(Input, 3, bestpath, m_pRltMatrix, m_plamda);
	

	//显示结果
	CString s;
	s.Format("%f 最佳路径: %d %d %d", result, bestpath[0], bestpath[1], bestpath[2]);
	m_cInfo.SetWindowText(s);

	s.Format("%f %f %f | %f %f %f | %f %f %f", 
		m_pRltMatrix[0][0],	m_pRltMatrix[1][0], m_pRltMatrix[2][0],
		m_pRltMatrix[0][1], m_pRltMatrix[1][1], m_pRltMatrix[2][1],
		m_pRltMatrix[0][2],	m_pRltMatrix[1][2], m_pRltMatrix[2][2]);
	m_cInfo1.SetWindowText(s);

	s.Format("%d %d %d | %d %d %d | %d %d %d", 
		m_plamda[0][0],	m_plamda[1][0], m_plamda[2][0],
		m_plamda[0][1], m_plamda[1][1], m_plamda[2][1],
		m_plamda[0][2],	m_plamda[1][2], m_plamda[2][2]);
	m_cInfo2.SetWindowText(s);

	delete [] bestpath;
	delete [] Input;

}


⌨️ 快捷键说明

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