AprioriView.cpp
资源名称:Apriori.zip [点击查看]
上传用户:zscyanite
上传日期:2022-07-25
资源大小:4947k
文件大小:12k
源码类别:
数值算法/人工智能
开发平台:
Visual C++
- // AprioriView.cpp : implementation of the CAprioriView class
- //
- #include "stdafx.h"
- #include "Apriori.h"
- #include "AprioriSet.h"
- #include "AprioriDoc.h"
- #include "AprioriView.h"
- #include "GetData.h"
- #include "time.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_BN_CLICKED(IDC_FREQBN, OnFreqBn)
- ON_COMMAND(ID_GETDATA, OnGetData)
- ON_BN_CLICKED(IDC_RULEBN, OnRuleBn)
- //}}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
- CAprioriView::CAprioriView()
- : CRecordView(CAprioriView::IDD)
- {
- //{{AFX_DATA_INIT(CAprioriView)
- // NOTE: the ClassWizard will add member initialization here
- 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_RULELIST, m_rlist);
- DDX_Control(pDX, IDC_LIST1, m_list);
- //}}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::OnFreqBn()
- {
- // TODO: Add your control notification handler code here
- CString strValue[MaxSize];
- clock_t start,stop;
- double timeused;
- int flag=0;
- if (ItemCount<=0)
- {
- MessageBox("请先进行参数设置",NULL,MB_OK);
- return;
- }
- start=clock();
- FindLargeItem();
- for(int k=1;LargeItemCount[k-1]!=0;k++)
- {
- AprioriGen(k);
- for(int mm=0;mm<CandLargeItemCount[k];mm++)
- {
- nCountCand[mm]=0;
- }
- //统计计数
- for(int j=0;j<CandLargeItemCount[k];j++)
- {
- for(int j1=0;j1<nDbItemCount;j1++)
- {
- for(int j2=0;j2<DbItemCount[j1];j2++)
- {
- if(CandLargeItem[k][j].Find(DbItem[j1][j2])!=-1)
- { flag++; }
- }
- if(flag==k+1)
- {
- nCountCand[j]++;
- flag=0;
- }
- flag=0;
- }
- }
- ShowFreqItem(k);
- }
- ncount=k-1;
- //CString msg1;
- //msg1.Format("%d",ncount);
- //AfxMessageBox(msg1);
- m_list.InsertString(0,"频繁项目集:n");
- stop=clock();
- timeused=(double)(stop-start)/CLK_TCK;
- CString msg;
- msg.Format("运行程序所用时间为:%5.7f秒",timeused);
- AfxMessageBox(msg);
- }
- void CAprioriView::FindLargeItem()
- {
- int nFieldCount=m_pSet->GetODBCFieldCount();
- int nCount=0;
- int nFreqItem[100];
- int nDbCount=0;
- CString str;
- CString strValue;
- CString strIntToString;
- // 初始化候选项目集
- for(int i=0;i<ItemCount;i++)
- {
- str="";
- str.Format("%s%d",str,i+1);
- CandLargeItem[0][i]='I'+str;
- }
- //初始化数组
- for(int ii=0;ii<ItemCount;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<ItemCount;i++)
- if(strValue.Find(CandLargeItem[0][i])!=-1)
- nFreqItem[i]++;
- }
- m_pSet->MoveNext();
- }
- nDbItemCount=nDbCount;
- for(int i1=0;i1<ItemCount;i1++)
- {
- if(double(nFreqItem[i1])/double(nDbItemCount)>=ItemSupp)
- {
- CString str;
- LargeItem[0][nCount]=CandLargeItem[0][i1];
- str.Format("%s%20d",LargeItem[0][nCount],nFreqItem[i1]);
- m_list.InsertString(nCount,str);
- NLargeItemCount[0][nCount]=nFreqItem[i1];
- nCount++;
- }
- }
- LargeItemCount[0]=nCount;
- }
- void CAprioriView::OnGetData()
- {
- // TODO: Add your command handler code here
- CGetData dlg;
- dlg.m_itemcount=10;
- dlg.m_itemsupp=0.2;
- dlg.m_itemconf=0.7;
- int ren=dlg.DoModal();
- ItemCount=dlg.m_itemcount;
- ItemSupp=dlg.m_itemsupp;
- ItemConf=dlg.m_itemconf;
- }
- void CAprioriView::SubItemGen(int strSubItemCount/*表示第几个事物*/,CString strSubItem
- /*表示第几个事物的具体内容如:I1,I2*/)
- {//对每个事务分解成单个项目,并存储到数组 DbItem[][]中去
- CString strTemp;
- int nSubItemCount;
- int nstrTemp;
- strTemp=strSubItem;
- nSubItemCount=0;//用于统计所有的单个项目的个数
- while((nstrTemp=strTemp.Find(','))!=-1)
- {
- DbItem[strSubItemCount][nSubItemCount++]=strTemp.Left(nstrTemp);
- strTemp=strTemp.Mid(nstrTemp+1);
- }
- DbItem[strSubItemCount][nSubItemCount++]=strTemp;
- DbItemCount[strSubItemCount]=nSubItemCount;
- }
- bool CAprioriView::Prune( int level, CString strCandFreqItem)
- {
- CString strTemp,str;
- int flag=0,nstrTemp;
- int nSubItemCount=0,nFreqCount=0;//存放单个项目的个数
- strTemp=strCandFreqItem;
- CString strSubTemp[MaxSize];//存放分解出来的单个项目
- CString strFreqItem[MaxSize];//存放k阶的子集
- while((nstrTemp=strTemp.Find(','))!=-1)
- {
- strSubTemp[nSubItemCount++]=strTemp.Left(nstrTemp);
- strTemp=strTemp.Mid(nstrTemp+1);
- }
- strSubTemp[nSubItemCount++]=strTemp;
- for(int i=0;i<nSubItemCount;i++)
- { str="";
- if(nSubItemCount<=2)
- {
- strFreqItem[nFreqCount++]=strSubTemp[i];
- }
- else{
- for(int j=i+1;j<nSubItemCount;j++)
- { str=strSubTemp[i]+",";
- if(level>1){
- for(int k=j;k<j+level-1&&k<nSubItemCount;k++)
- str=str+strSubTemp[k]+",";}
- for(int m=j+level-1;m<nSubItemCount;m++)
- {
- str=str+strSubTemp[m]+",";
- }
- strFreqItem[nFreqCount++]=str.Left(str.GetLength()-1);
- }
- }
- }
- for(int j=0;j<nFreqCount;j++)
- {
- for( int i = 0; i < LargeItemCount[level-1]; i++ )
- if (LargeItem[level-1][i].Find(strFreqItem[nFreqCount])!=-1 )
- flag++;
- }
- if(flag>0)return true;
- else return false;
- }
- void CAprioriView::AprioriGen( int level )
- {
- CString strTemp,strTemp1,strTemp2,strTemp11,strTemp22;
- int levelCount = 0;
- int n;
- // 如果两个L(k-1)项,前k-2都相同,只是最后一项不同,且前一项小于后一项
- // 则生成C(k),并剪枝
- if(level==1)
- {
- for(int i = 0; i < LargeItemCount[level-1]; i++)
- {
- strTemp1 = LargeItem[ level-1 ][i];
- for(int j = i + 1; j < LargeItemCount[level-1]; j++)
- {
- strTemp2 = LargeItem[ level-1 ][j];
- if ( strTemp1 < strTemp2)
- {
- strTemp = strTemp1 + "," + strTemp2;
- for(n=0;n<j;n++)
- if(strTemp==CandLargeItem[level][n]) continue;
- if(Prune(level,strTemp))
- CandLargeItem[level][levelCount++] = strTemp;
- }
- }
- }
- }
- else{
- for(int i = 0; i < LargeItemCount[level-1]; i++)
- {
- strTemp1 = LargeItem[ level-1 ][i];
- strTemp11=strTemp1.Left(strTemp1.GetLength()-3);
- for(int j = i + 1; j < LargeItemCount[level-1]; j++)
- {
- strTemp2 = LargeItem[ level-1 ][j];
- strTemp22=strTemp2.Left(strTemp2.GetLength()-3);
- if ( strTemp11 == strTemp22 &&strTemp1.Mid(strTemp1.GetLength()-2)
- < strTemp2.Mid(strTemp2.GetLength()-2))
- {
- strTemp = strTemp1 + "," + strTemp2.Mid(strTemp2.GetLength()-2);
- for(n=0;n<j;n++)
- if(strTemp==CandLargeItem[level][n]) continue;
- if(Prune(level,strTemp))
- CandLargeItem[level][levelCount++] = strTemp;
- }
- }
- }
- }
- CandLargeItemCount[level]=levelCount++;
- }
- void CAprioriView::ShowFreqItem(int nScanCount)
- {
- CString strIntToString="";
- CString strValue;
- CString strjj3[2];
- CString str;
- int nLargeCount=-1;
- int nLargeItemCount=0;
- //以下为求频繁项目集
- int k;
- k=nScanCount;
- m_list.InsertString(0,"-----------------------");
- for(int jj2=0;jj2<CandLargeItemCount[k];jj2++)
- {
- if(double(nCountCand[jj2])/double(nDbItemCount)>=ItemSupp)
- {
- LargeItem[k][nLargeItemCount++]=CandLargeItem[k][jj2];
- nLargeCount++;
- //nLargeItemCount[0][0]=4;
- NLargeItemCount[k][nLargeItemCount-1]=nCountCand[jj2];
- str.Format("%s%20d",LargeItem[k][nLargeItemCount-1],nCountCand[jj2]);
- m_list.InsertString(nLargeCount,str);
- }
- }
- //复制频繁项目个数
- LargeItemCount[k]=nLargeItemCount;
- }
- void CAprioriView::OnRuleBn()
- {
- // TODO: Add your control notification handler code here
- CString strSubTemp[MaxSize][MaxSize],strTemp,Rule[MaxSize][MaxSize]/*用于存放规则的左边部分*/;
- CString str,msg;
- int nstrTemp,nSubCount=0,count=-1,rcount=0,rrcount;
- clock_t start,stop;
- double timeused;
- start=clock();
- for(int i=0;i<LargeItemCount[ncount-1];i++)
- { strTemp=LargeItem[ncount-1][i];
- while((nstrTemp=strTemp.Find(','))!=-1)
- {
- strSubTemp[i][nSubCount++]=strTemp.Left(nstrTemp);
- strTemp=strTemp.Mid(nstrTemp+1);
- //AfxMessageBox(strSubTemp[i][nSubCount-1]);
- }
- strSubTemp[i][nSubCount++]=strTemp;
- // AfxMessageBox(strSubTemp[i][nSubCount-1]);
- nSubCount=0;
- }
- strTemp="";
- for(int n=0;n<LargeItemCount[ncount-1];n++)
- {
- for(int k=1;k<ncount;k++)
- {
- for(int ii=0;ii<ncount;ii++)
- {
- if(k==1){Rule[n][rcount++]=strSubTemp[n][ii];}
- else
- {
- strTemp=strSubTemp[n][ii];
- if(k>2){
- for(int kk=ii+1;kk<ii+k-1&&kk<ncount;kk++)
- {strTemp=strTemp+","+strSubTemp[n][kk]+",";
- //AfxMessageBox(strSubTemp[n][kk]);
- }
- for(int jj=kk;jj<ncount;jj++)
- Rule[n][rcount++]=strTemp+","+strSubTemp[n][jj];
- }
- else{
- for(int jj=ii+1;jj<ncount;jj++)
- Rule[n][rcount++]=strTemp+","+strSubTemp[n][jj];}
- }
- }
- }
- rrcount=rcount;
- rcount=0;
- }
- for(int i1=0;i1<LargeItemCount[ncount-1];i1++)
- {
- for(int j1=0;j1<rrcount;j1++)
- {
- for(int i2=0;i2<ncount;i2++)
- for(int j2=0;j2<LargeItemCount[i2];j2++)
- if(Rule[i1][j1]==LargeItem[i2][j2])
- {
- if(double(NLargeItemCount[ncount-1][i1])/double(NLargeItemCount[i2][j2])>ItemConf)
- { str="";
- for(int i3=0;i3<ncount;i3++)
- {
- if(Rule[i1][j1].Find(strSubTemp[i1][i3])==-1)
- {
- str=str+strSubTemp[i1][i3]+",";
- }
- }
- count++;
- msg.Format("%s→%s",Rule[i1][j1],str.Left(str.GetLength()-1));
- m_rlist.InsertString(count,msg);
- }
- }
- }
- }
- stop=clock();
- timeused=(double)(stop-start)/CLK_TCK;
- msg.Format("生成关联规则所用时间为:%5.7f秒",timeused);
- AfxMessageBox(msg);
- }
English
