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

📄 rtr.cpp

📁 某个实验事编写粗糙集智能信息处理的程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// Rtr.cpp: implementation of the Rtr class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "RSet.h"
#include "Rtr.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

const int  MAX=30;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Rtr::Rtr()
{

}

Rtr::~Rtr()
{
    //释放可辨识矩阵
	/*for(int i=0;i<NumOfRecord;i++)
		for(int j=0;j<NumOfRecord;j++)
		{
			delete []DiffMatrix[i][j];
		}

	for(i=0;i<NumOfRecord;i++)
		delete []DiffMatrix[i];

	delete []DiffMatrix;
	DiffMatrix=NULL;*/
}

void Rtr::DeleteMemory()
{ 
	//释放决策表
	for(int i=0;i<NumOfRecord;i++)
		delete []InfTable[i];
	delete []InfTable;
	InfTable=NULL;

	//释放决策类
	delete []DecClass;
	DecClass=NULL;

    //释放每个属性的遗失属性值的个数
	delete []NumOfMissAttribute;
	NumOfMissAttribute=NULL;
	

	//释放属性约简的结果
	delete []AttributeReductResult;

	//释放规则的覆盖度
	delete []RuleCover;

	//释放规则的支持度
	delete []RuleSupport;

	//释放规则的状态
	delete []RuleCondition;

	//释放规则集
    for(i=0;i<NumOfRecord;i++)
		delete []Rule[i];
	delete []Rule;
	Rule=NULL;

	//释放属性的类型
	for(i=0;i<NumOfAttribute;i++)
		delete TypeOfAttribute[i];

	//释放属性的名称
    for(i=0;i<NumOfAttribute;i++)
		delete NameOfAttribute[i];
}

BOOL Rtr::ReadTable(CString filename)
{
    FILE* fp;
    if((fp = fopen(filename,"r"))==NULL)
	{		
		AfxMessageBox("无法打开文件!");
		return FALSE; 
	}
    
	try
	{
		DataStyle=new char[MAX];
	}
    catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.24)!");
		e->Delete(); 
		return FALSE;
	}
	fscanf(fp,"Style:%s\n",DataStyle);
	fscanf(fp,"Stage:%d\n",&DataState);


    if ((strcmp(DataStyle,"train")!=0) && (DataState!=2 || DataState!=0))
	{
		AfxMessageBox("数据集类型不符!");
		return FALSE;
	}

	fscanf(fp,"Condition attributes number:%d\n",&NumOfAttribute);
	NumOfAttribute++;
	fscanf(fp,"Records number:%d\n",&NumOfRecord);

	//读取属性名称
	try
	{
         NameOfAttribute=new char*[NumOfAttribute];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.24)!");
		e->Delete(); 
		return FALSE;
	}

	for(int i=0;i<NumOfAttribute;i++)
	{
		try
		{
			NameOfAttribute[i]=new char[MAX];
		}
        catch(CMemoryException *e)
		{
		    AfxMessageBox("超出内存(NO.25)!");
		    e->Delete(); 
		    return FALSE;
		}
	}

	for(i=0;i<NumOfAttribute;i++)
	{
		fscanf(fp,"%s",NameOfAttribute[i]);
	}
	fscanf(fp,"\n");
    
    //读取属性类型
    try
	{
         TypeOfAttribute=new char*[NumOfAttribute];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.27)!");
		e->Delete(); 
		return FALSE;
	}
    
	for(i=0;i<NumOfAttribute;i++)
	{
		try
		{
			TypeOfAttribute[i]=new char[MAX];
		}
        catch(CMemoryException *e)
		{
		    AfxMessageBox("超出内存(NO.28)!");
		    e->Delete(); 
		    return FALSE;
		}
	}
    
	for(i=0;i<NumOfAttribute;i++)
	{
		fscanf(fp,"%s",TypeOfAttribute[i]);
	}
	fscanf(fp,"\n");

	
   
    
	//读取决策表值
	try
	{
		InfTable = new int*[NumOfRecord];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.1)!");
		e->Delete(); 
		return FALSE;
	}
	for(i = 0;i<NumOfRecord;i++)
	{
		
		try
		{
			InfTable[i] = new int[NumOfAttribute];
		}
        catch(CMemoryException *e)
		{
		    AfxMessageBox("超出内存(NO.2)!");
		    e->Delete(); 
			return FALSE;
		}
	}

	for(i=0;i<NumOfRecord;i++)
	{
		for(int j=0;j<NumOfAttribute;j++)
		{
			CString strTemp;
			int intTemp;
			fscanf(fp,"%s",strTemp);
			if((strcmp(strTemp,"*")==0) || (strcmp(strTemp,"-"))||
				(strcmp(strTemp,"?")  == 0))
			{
                intTemp=-1;
			}
			else
			{
				intTemp=atoi(strTemp);
			}
			InfTable[i][j]=intTemp;
		}
	}
   return TRUE;
}

BOOL Rtr::ComputerMissNum()
{
	//初始化遗失属性值数组
	try
	{
	 	NumOfMissAttribute=new int[NumOfAttribute-1];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.3)!");
		e->Delete();
		return FALSE;
	}

	for(int i=0;i<NumOfAttribute-1;i++)
		NumOfMissAttribute[i]=0;

	//统计遗失信息
	for(i=0;i<NumOfAttribute-1;i++)
	{
		for(int j=0;j<NumOfRecord;j++)
		{
			if(InfTable[j][i]==-1)
                 NumOfMissAttribute[i]++;
		}
	}
	return TRUE;
}



BOOL Rtr::JudgeRelation(int i,int j,int *a,int Number)
{
	int k;
	for(k=0;k<Number;k++)
	{
		if(InfTable[i][a[k]]==-1 && InfTable[j][a[k]]==-1)
			continue;
		break;
	}
	if(k==Number)
		return TRUE;

	CArray<int,int&> att;
	for(k=0;k<Number;k++)
	{
		if(InfTable[i][a[k]]!=-1 && InfTable[j][a[k]]!=-1)
			att.Add(a[k]);
	}
	if(!att.GetSize())
		return FALSE;

	for(k=0;k<att.GetSize();k++)
	{
		if(InfTable[i][att.GetAt(k)]!=InfTable[j][att.GetAt(k)])
			return FALSE;
	}
	return TRUE;
}

BOOL Rtr::ComputerDecClass()
{
	CArray<int,int&> temp;
	temp.Add(InfTable[0][NumOfAttribute-1]); 
	for(int i=1;i<NumOfRecord;i++)
	{
		BOOL b=TRUE;
		for(int j=0;j<temp.GetSize();j++)
		{
			if(InfTable[i][NumOfAttribute-1]==temp.GetAt(j))
			{
				b=FALSE;
				break;
			}
		}
		if(b) temp.Add(InfTable[i][NumOfAttribute-1]);
	}

    NumOfDecClass=temp.GetSize(); 
	try
	{
       DecClass=new CArray<int,int&>[NumOfDecClass];
	}   
    catch(CMemoryException *e)
	{
        AfxMessageBox("超出内存(NO.7)!");
		e->Delete();
		return FALSE;
	}
    
	for(i=0;i<NumOfRecord;i++)
	{
       for(int j=0;j<temp.GetSize();j++)
		   if(InfTable[i][NumOfAttribute-1]==temp.GetAt(j))
		   {
			   DecClass[j].Add(i);
			   break;
		   }
	}
	return TRUE;
}

BOOL Rtr::ComputerClassifyPre(int *a,int Number,double& result)
{	
  
   CArray<int,int&> *ConClass;
   try
   {
	   ConClass=new CArray<int,int&> [NumOfRecord];
   }
   catch(CMemoryException *e)
   {
        AfxMessageBox("超出内存(NO.8)!");
		e->Delete();
		return FALSE;
   }

   //计算每条记录的限制容差类
   for(int i=0;i<NumOfRecord;i++)
   {
	   ConClass[i].Add(i);
	   for(int j=0;j<NumOfRecord;j++)
	   {
		   if(j==i) continue;
           if(JudgeRelation(i,j,a,Number))
			   ConClass[i].Add(j);
	   }
   }

    
   int Precision=0;
   for(i=0;i<NumOfRecord;i++)
   {
	   for(int j=0;j<NumOfDecClass;j++)
	   {
		   if(Contain(ConClass[i],DecClass[j]))
		   {
			   Precision++;
			   break;
		   }
	   }
   }
   result=(double)Precision/(double)NumOfRecord;
   //释放占用的内存
   for(i=0;i<NumOfRecord;i++)
	   ConClass[i].RemoveAll();
   delete []ConClass;
   ConClass=NULL;
   

   return TRUE;
}

BOOL Rtr::Contain(CArray<int,int&>& a,CArray<int,int&>& b)
{
	for(int i=0;i<a.GetSize();i++)
	{
		BOOL bl=FALSE;
		int ele1=a.GetAt(i);
		for(int j=0;j<b.GetSize();j++)
		{
			int ele2=b.GetAt(j); 
			if(ele1==ele2) bl=TRUE;
		}
		if(!bl) return FALSE;
	}
	return TRUE;
}



BOOL Rtr::IsSame(CArray<int,int&>& a,int *b)
{
	if(a.GetSize()!=b[0])
		return FALSE;
	for(int i=0;i<a.GetSize();i++)
	{
		BOOL bl=FALSE;
		int Ele1=a.GetAt(i); 
		for(int j=0;j<b[0];j++)
		{
			if(Ele1==b[j+1])
			{
				bl=TRUE;
				break;
			}
		}
		if(!bl) return FALSE;
	}
	return TRUE;
}




BOOL Rtr::AttributeReduct()
{

	int* Reduct,CountOfReduct=0;
	int* Candi,CountOfCandi=0;
	try
	{
		Reduct=new int[NumOfAttribute-1];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.14)!");
	    e->Delete();
	    return FALSE;
	}

    try
	{
		Candi=new int[NumOfAttribute-1];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.15)!");
	    e->Delete();
	    return FALSE;
	}

	//给约简集和候选集赋初值
	for(int j=0;j<NumOfAttribute-1;j++)
	{
			Candi[CountOfCandi++]=j;
	}

	
    double Pre;
	ComputerClassifyPre(Reduct,CountOfReduct,Pre);

	
    while(Pre<ExpClassifyPre)
	{
		int Result;
		FindImp(Candi,CountOfCandi,Reduct,CountOfReduct,Result);
		Reduct[CountOfReduct++]=Result;
		for(int i=0;i<CountOfCandi;i++)
			if(Reduct[CountOfReduct-1]==Candi[i])
			{
				Candi[i]=-1;
				break;
			}

		Pre=0;
        ComputerClassifyPre(Reduct,CountOfReduct,Pre);
	}

	try
	{
		AttributeReductResult=new int[NumOfAttribute-1];
	}
    catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.16)!");
	    e->Delete();
	    return FALSE;
	}

	
	//保存属性约简的结果
    NumOfReduct=CountOfReduct;
	for(int i=0;i<NumOfAttribute-1;i++)
	{
		BOOL bl=FALSE;
		for(int j=0;j<CountOfReduct;j++)
			if(Reduct[j]==i)
			{
				bl=TRUE;
				break;
			}
		if(bl)
            AttributeReductResult[i]=1;
		else
			AttributeReductResult[i]=0;
	}
    return TRUE;
		

}

BOOL Rtr::FindImp(int* Candi,int CountOfCandi,int* Reduct,int CountOfReduct,int& Result)
{
	int* Test;
	double* Pre;
	try
	{
		Test=new int[CountOfReduct+1];
	}
	catch(CMemoryException *e)
    {
        AfxMessageBox("超出内存(NO.28)!");
		e->Delete();
		return FALSE;
    }

	try
	{
		Pre=new double[CountOfCandi];
	}
	catch(CMemoryException *e)
    {
        AfxMessageBox("超出内存(NO.29)!");
		e->Delete();
		return FALSE;
    }
	for(int i=0;i<CountOfReduct;i++)
		Test[i]=Reduct[i];

    for(i=0;i<CountOfCandi;i++)
		Pre[i]=-1;

	for(int j=0;j<CountOfCandi;j++)
	{
		if(Candi[j]==-1)
			continue;
		Test[CountOfReduct]=Candi[j];
        ComputerClassifyPre(Test,CountOfReduct+1,Pre[j]);
	}

	double Max=-100;
	for(i=0;i<CountOfCandi;i++)
	{
        if(Pre[i]>Max)
		{
			Max=Pre[i];
		}
	}

	CArray<int,int&> Same;
	for(i=0;i<CountOfCandi;i++)
		if(Pre[i]==Max)
			Same.Add(i);
   
    int MinMiss=NumOfMissAttribute[Same.GetAt(0)];
	int position=Same.GetAt(0);
	for(j=1;j<Same.GetSize();j++)
	{
		if(NumOfMissAttribute[Same.GetAt(j)]<MinMiss)
		{
			MinMiss=NumOfMissAttribute[Same.GetAt(j)];
			position=Same.GetAt(j);
		}
	}
    
	Result=Candi[position];
	delete []Pre;
	delete []Test;
	Pre=NULL;
	Test=NULL;
	return TRUE;
}

	





BOOL Rtr::IsEqual(CArray<int,int&>& PosOfAttr,CArray<int,int&>& Pos)
{
	if(PosOfAttr.GetSize()!=Pos.GetSize())
		return FALSE;
	for(int i=0;i<Pos.GetSize();i++)
	{
		BOOL bl=FALSE;
	    for(int j=0;j<PosOfAttr.GetSize();j++)
			if(Pos.GetAt(i)==PosOfAttr.GetAt(j))
			{
				bl=TRUE;
				break;
			}
		if(!bl)
			return FALSE;
	}
	return TRUE;
}

BOOL Rtr::ComputerPos(int* Set,int NumOfSet,CArray<int,int&>& Pos)
{

⌨️ 快捷键说明

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