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

📄 aprioriview.cpp

📁 aprior算法 数据仓库关联分析经典算法
💻 CPP
字号:
// AprioriView.cpp : implementation of the CAprioriView class
//

#include "stdafx.h"
#include "Apriori.h"

#include "AprioriSet.h"
#include "AprioriDoc.h"
#include "AprioriView.h"
#include "time.h"
#include "SetPara.h"


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

/////////////////////////////////////////////////////////////////////////////
// CAprioriView

IMPLEMENT_DYNCREATE(CAprioriView, CRecordView)

BEGIN_MESSAGE_MAP(CAprioriView, CRecordView)
	//{{AFX_MSG_MAP(CAprioriView)
	ON_WM_CANCELMODE()
	ON_BN_CLICKED(IDC_BUTTON1, OnBnFreqItem)
	ON_COMMAND(ID_Parameter, OnParameter)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAprioriView construction/destruction

void CAprioriView::OnParameter() 
{
	// TODO: Add your command handler code here
CSetPara dlg;
dlg.m_ItemCount =10;
dlg.m_Item_Supp=0.2; 
int ren=dlg.DoModal(); //显示参数对话框
nItemCount=dlg.m_ItemCount;  //保存对话框设置参数到view中对应的成员变量中
dItemSupp=dlg.m_Item_Supp ;
}


CAprioriView::CAprioriView()
	: CRecordView(CAprioriView::IDD)
{
	
	//{{AFX_DATA_INIT(CAprioriView)
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CAprioriView::~CAprioriView()
{
}

void CAprioriView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAprioriView)
	DDX_Control(pDX, IDC_List_FreqItem, m_List_FreqItem);
	//}}AFX_DATA_MAP
}

BOOL CAprioriView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRecordView::PreCreateWindow(cs);
}

void CAprioriView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_aprioriSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CAprioriView printing

BOOL CAprioriView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAprioriView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CAprioriView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CAprioriView diagnostics

#ifdef _DEBUG
void CAprioriView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CAprioriView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

CAprioriDoc* CAprioriView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAprioriDoc)));
	return (CAprioriDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAprioriView database support
CRecordset* CAprioriView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CAprioriView message handlers

void CAprioriView::OnCancelMode() 
{
	CRecordView::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}

void CAprioriView::OnBnFreqItem() 
{
    int nDbCount;
    CString strValue;
    CString strIntToString="";

    clock_t start,stop,tick;
    double timeused;
    int nLargeCount=0;
    int nListFreqItemCount=0;
    start=clock();
    ClearItem();  // CLEAR LIST CONTROL

    m_List_FreqItem.InsertColumn(0,"Item",LVCFMT_LEFT,100);
    m_List_FreqItem.InsertColumn(1,"Count",LVCFMT_LEFT,100);

    if (nItemCount<=0)
    {
        MessageBox("请先进行参数设置",NULL,MB_OK);
        return;
    }

// 根据数据源找出0级频繁项目列表;
    FindLargeItem();

    for(int k=1;LargeItemCount[k-1]!=0;k++)
    {// 如果当前级别大项目数量不为0,则继续处理
        AprioriGen(k,1);

        //初始化数组
        for(int mm=0;mm<CandLargeItemCount[k];mm++)
        {
            nCountCand[mm]=0;
        }
        for(nDbCount = 0; nDbCount < nDbItemCount; nDbCount++)
        {
        // 将nDbCount指向的条目分解成K级候选组合;
            TransGenCand(k,nDbCount);
            //统计计数; 分解个数nTransCandCount
            for(int jj=0;jj<nTransCandCount;jj++)
                for(int jj1=0;jj1<CandLargeItemCount[k];jj1++)
                {
                    if(TransGenCandFreq[jj].Find(CandLargeItem[k][jj1])!=-1)
                    {
                        nCountCand[jj1]++;
                        break;
                    }
                }
        }
       // 计算并显示频繁集合
        ShowFreqItem(k);
    }
// 显示消耗时间
    stop=clock();
    tick=stop - start;
    timeused=(double)tick/CLK_TCK;
    strIntToString="";
    strIntToString.Format("time used: %s%f second",strIntToString,timeused);
    MessageBox(strIntToString,NULL,MB_OK);
}
void CAprioriView::ClearItem()
{
    m_List_FreqItem.DeleteAllItems ();
    while(m_List_FreqItem.DeleteColumn (0));
    UpdateWindow();
    for(int kk=0;kk<nMaxSize;kk++)
        LargeItemCount[kk]=0;
    for(int kk1=0;kk1<nMaxSize;kk1++)
        for(int kk2=0;kk2<nMaxSize;kk2++)
            LargeItem[kk1][kk2]="";
}


void CAprioriView::FindLargeItem()
{
    //显示1-频繁项目集
    int nFieldCount=m_pSet->GetODBCFieldCount();
    int nCount=0;
    int nFreqItem[100];
    int nDbCount=0;
    CString strInit;
    CString strValue;
    CString strIntToString;
    // 初始化level 0 候选项目集
    for(int nInitCount=0;nInitCount<nItemCount;nInitCount++)
    {
        strInit="";
        strInit.Format("%s%d",strInit,nInitCount+1);
        CandLargeItem[0][nInitCount]='I'+strInit;
    }
    //初始化数组
    for(int ii=0;ii<nItemCount;ii++)
        nFreqItem[ii]=0;

//根据数据源初始化业务记录的项目集合,
    m_pSet->MoveFirst ();
    while(!m_pSet->IsEOF())
    {
        for(int j=1;j<nFieldCount;j++)
        {
            m_pSet->GetFieldValue(j,strValue);
            SubItemGen(nDbCount++ ,strValue);//每项业务包含的项目初始化
            for(int i=0;i<nItemCount;i++)
                if(strValue.Find(CandLargeItem[0][i])!=-1)
                    nFreqItem[i]++;
        }
        m_pSet->MoveNext();
    }
    nDbItemCount=nDbCount;

//找出频度高于设定值的项目,
//并将其加入列表控件中;
    for(int i1=0;i1<nItemCount;i1++)
    {
        strIntToString="";
        if(double(nFreqItem[i1])/double(nDbItemCount)>=dItemSupp)
        {
            LargeItem[0][nCount]=CandLargeItem[0][i1];
            m_List_FreqItem.InsertItem(nCount,strIntToString);
            m_List_FreqItem.SetItemText(nCount,0,LargeItem[0][nCount]);
            strIntToString="";
            strIntToString.Format("%s%d",strIntToString,nFreqItem[i1]);
            m_List_FreqItem.SetItemText(nCount,1,strIntToString);
            nCount++;
        }
    }
    LargeItemCount[0]=nCount;
}

void CAprioriView::SubItemGen(int strSubItemCount,CString strSubItem)
{//对每个事务分解成单个项目,保存到DbItem[],DbItemCount[]中
    CString strTemp1;
    CString strTempSubItem[10];
    CString strReverse;

    int nSubItemCount;
    int nstrRightTemp1;
    int nTempCount;

    strTemp1=strSubItem;
    nSubItemCount=0;
    while((nstrRightTemp1=strTemp1.ReverseFind(','))!=-1)
    {
        nTempCount=strTemp1.GetLength() - nstrRightTemp1 - 1;
        strTempSubItem[nSubItemCount++]=strTemp1.Right( nTempCount);
        strTemp1=strTemp1.Left(nstrRightTemp1);
    }
    strTempSubItem[nSubItemCount++]=strTemp1;
    for(int i2=0;i2<nSubItemCount/2;i2++)
    {
        strReverse=strTempSubItem[nSubItemCount-i2-1];
        strTempSubItem[nSubItemCount- i2-1]=strTempSubItem[i2];
        strTempSubItem[i2]=strReverse;
    }
    for(int i1=0;i1<nSubItemCount;i1++)
    {
        DbItem[strSubItemCount][i1]=strTempSubItem[i1];
    }
    DbItemCount[strSubItemCount]=nSubItemCount;

}
//从k-1级大项集生成k级候选集;
void CAprioriView::AprioriGen(int k, int nMinSupp)
{    //由L(k-1)生成C(k)
    CString strTemp1,strTemp2,strRightTemp1,strRightTemp2;
    CString strTemp,strLeftTemp1,strLeftTemp2;
    int nstrTemp;
    int nCandFreqItemCount=0;
    strTemp1="";    strTemp2="";
    strRightTemp1="";    strRightTemp2="";
    strTemp="";
    strLeftTemp1="";    strLeftTemp2="";
    nstrTemp=0;  

    for(int i1=0;i1<LargeItemCount[k-1];i1++)
    {
        strTemp1=LargeItem[k -1][i1];
        nstrTemp=strTemp1.ReverseFind(',');
        strRightTemp1=strTemp1.Right(strTemp1.GetLength ()-nstrTemp-1);
        strLeftTemp1=strTemp1.Left(nstrTemp);
        for(int j1=i1+1;j1<LargeItemCount[k-1];)
        {
            strTemp2=LargeItem[k-1][j1];
            nstrTemp=strTemp2.ReverseFind (',');
            strRightTemp2=strTemp2.Right(strTemp2.GetLength ()-nstrTemp-1);
            strLeftTemp2=strTemp2.Left(nstrTemp) ;
            if((strLeftTemp1==strLeftTemp2)&&(strRightTemp1<strRightTemp2))
            {
                strTemp=strTemp1+','+strRightTemp2;
                CandLargeItem[k][nCandFreqItemCount++]=strTemp;
                j1++;
            }
            else
                break;
        }
}
CandLargeItemCount[k]=nCandFreqItemCount;
}
// Cmn:将nCurrentItem所指业务分解成k级组合
void CAprioriView::TransGenCand(int k,int nCurrentItem )
{
    CString strTransTemp;
    CString strTransTemp1;
    nTransCandCount=0;
    int a[nMaxSize];
    int nCount=0;
    a[nCount]=0;
    //初始化数组
    for(int nTransCand=0;nTransCand<nMaxSize;nTransCand++)
    {
        TransGenCandFreq[nTransCand]="";
    }
    do
    {
        if((a[nCount]-nCount) <= (DbItemCount[nCurrentItem]- k -1))
        {
            if(nCount==k)
            {
                strTransTemp="";
                for(int jj=0;jj<k;jj++)
                    strTransTemp=strTransTemp+DbItem[nCurrentItem][a[jj]]+',';

                strTransTemp1="";
                strTransTemp1=strTransTemp+DbItem[nCurrentItem][a[k]];
                TransGenCandFreq[nTransCandCount++]=strTransTemp1;
                // MessageBox(strTransTemp1);
                a[nCount]++;
                continue;
            }
            nCount++;
            a[nCount]=a[nCount-1]+1;
        }
        else
        {
            if(nCount==0)
                return;
            a[--nCount]++;
        }
    }while(1);
}

//将k级分解统计结果按支持度输出;
void CAprioriView::ShowFreqItem(int nScanCount)
{
    CString strIntToString="";
    CString strValue;
    CString strjj3[2];
    int nLargeCount=-1;
    int nLargeItemCount=0;

    //以下为求频繁项目集
    int k,nListFreqItemCount;
    k=nScanCount;
    nListFreqItemCount=LargeItemCount[k-1];
    m_List_FreqItem.InsertItem(0,strValue);
    m_List_FreqItem.SetItemText(0,0,"-----------");
    m_List_FreqItem.SetItemText(0,1,"-----------");
    for(int jj2=0;jj2<CandLargeItemCount[k];jj2++)
        if(double(nCountCand[jj2])/double(nDbItemCount)>=dItemSupp)
        {
            LargeItem[k][nLargeItemCount++]=CandLargeItem[k][jj2];
            nLargeCount++;

            strjj3[1]=strIntToString;
            strjj3[0]=CandLargeItem[k][jj2];
            strIntToString="";
            strIntToString.Format("%s%d",strIntToString,nCountCand[jj2]);
            strjj3[1]=strIntToString;

            m_List_FreqItem.InsertItem(nLargeCount,strValue);
            m_List_FreqItem.SetItemText(nLargeCount,0,LargeItem[k][nLargeItemCount-1]);
            m_List_FreqItem.SetItemText(nLargeCount,1,strIntToString);

            UpdateWindow();
        }
    //复制频繁项目个数
    LargeItemCount[k]=nLargeItemCount;
}

BOOL CAprioriView::Prune(int nCandFreqItemCount, CString strCandFreqItem)
{
	CString strTemp1;
    CString strTempSubItem[nMaxSize];
    CString strReverse;
    CString strSubCandFreqItem[nMaxSize];//分解候选项目
    CString strSubTemp="";
    CString strSubTemp1;
    int nSubFreqItemCount=0;//统计分解候选项目的个数
    int nSubItemCount;
    int nstrRightTemp1;
    int nTempCount;
    int nPruneCount=0;

    strTemp1=strCandFreqItem;
    nSubItemCount=0;
    while((nstrRightTemp1=strTemp1.ReverseFind(','))!=-1)
    {
        nTempCount=strTemp1.GetLength() - nstrRightTemp1 - 1;
        strTempSubItem[nSubItemCount++]=strTemp1.Right( nTempCount);
        strTemp1=strTemp1.Left(nstrRightTemp1);
    }
    strTempSubItem[nSubItemCount++]=strTemp1;
    for(int i2=0;i2<nSubItemCount/2;i2++)
    {
        strReverse=strTempSubItem[nSubItemCount-i2-1];
        strTempSubItem[nSubItemCount- i2-1]=strTempSubItem[i2];
        strTempSubItem[i2]=strReverse;
    }


    for(int i3=nSubItemCount-1;i3>=0;i3--)
    {
        strSubTemp1=strTempSubItem[i3];
        strSubTemp.Empty();
        for(int i4=0;i4<nSubItemCount;i4++)
            if(strSubTemp1!=strTempSubItem[i4])
            {
                strSubTemp=strSubTemp+strTempSubItem[i4]+',';

            }
        strSubCandFreqItem[nSubFreqItemCount++] = strSubTemp.Left(strSubTemp.GetLength()-1);
    }

    for(int i5=0;i5<nSubFreqItemCount;i5++)
    {
        for(int i6=0;i6<LargeItemCount[nCandFreqItemCount-1];i6++)
            if(strSubCandFreqItem[i5].Find(LargeItem[nCandFreqItemCount-1][i6])>=0)
                nPruneCount++;
    }
    if(nPruneCount==nSubItemCount)
    {

        return TRUE;
    }

    return FALSE;
}

	// TODO: Add your control notification handler code here
	




⌨️ 快捷键说明

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