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

📄 kmeandoc.cpp

📁 神经网络中的K-MEAN聚类算法
💻 CPP
字号:
// KmeanDoc.cpp : implementation of the CKmeanDoc class
//

#include "stdafx.h"
#include "Kmean.h"

#include "KmeanDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CKmeanDoc

IMPLEMENT_DYNCREATE(CKmeanDoc, CDocument)

BEGIN_MESSAGE_MAP(CKmeanDoc, CDocument)
	//{{AFX_MSG_MAP(CKmeanDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKmeanDoc construction/destruction

CKmeanDoc::CKmeanDoc()
{
	// TODO: add one-time construction code here
}

void CKmeanDoc::ResetCenters(int n)
{
    m_vecCenters.clear();
    m_vecCenters.resize(n);
    srand(time(NULL));
    for (int i = 0; i < n; i++)
    {
        m_vecCenters[i].x = rand() % 61 / 10.0 - 3;
        m_vecCenters[i].y = rand() % 21 / 10.0 - 1;

    }
    UpdateAllViews(NULL);
}

double CKmeanDoc::Kmeans(int nIndex, double dbRate)
{
    if (nIndex >= m_vecPoints.size() || m_vecCenters.size() == 0)
        return 0.0;

    int k;
    double minval = 500000.0;
    double dx, dy, d;
    for (int i = 0; i < m_vecCenters.size(); i++)
    {        
        dx = m_vecPoints[nIndex].x - m_vecCenters[i].x;
        dy = m_vecPoints[nIndex].y - m_vecCenters[i].y;
        d = dx * dx + dy * dy;
        if (minval > d)
        {
            minval = d;
            k = i;
        }
    }

    m_vecCenters[k].x += dbRate * (m_vecPoints[nIndex].x - m_vecCenters[k].x);
    m_vecCenters[k].y += dbRate * (m_vecPoints[nIndex].y - m_vecCenters[k].y);

    UpdateAllViews(NULL);
    return sqrt(minval);
}

CKmeanDoc::~CKmeanDoc()
{
}

BOOL CKmeanDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
    ResetCenters(0);  

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CKmeanDoc serialization

void CKmeanDoc::Serialize(CArchive& ar)
{
	if (ar.IsLoading())
	{
        int iSize;
        ifstream fin(ar.GetFile()->GetFilePath());

        fin >> iSize;
        
        ResetCenters(0);

        m_vecPoints.clear();
        m_vecPoints.resize(iSize);

        for (int i = 0; i< iSize; i++)
        {   
            fin >> m_vecPoints[i].x >> m_vecPoints[i].y;
        }
        
        UpdateAllViews(NULL);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CKmeanDoc diagnostics

#ifdef _DEBUG
void CKmeanDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CKmeanDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CKmeanDoc commands

⌨️ 快捷键说明

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