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

📄 globe2.cpp

📁 某个实验事编写粗糙集智能信息处理的程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//globe2.cpp
#include "StdAfx.h"
#include "Globe2.h"
#include "stdio.h"
#include "fstream.h"
#include "stdlib.h"
#include "time.h"
#include "setclass.h"
#include "Utility.h"
#include "iomanip.h"
#include <string.h>
///////////////////////read record number and conditon attribut number////////////////

bool globe2::ReadRecConNum(char* FileName,int* pNum)
{//读入条件属性和记录数目,分别存储在pNum[0]和pNum[1]中,读入文件头前4行
	fstream ReadFile;
	ReadFile.open(FileName,ios::in);
	if(!ReadFile)
	{
      // cout<<" can not open  file"<<endl;
		///李鄂////修正
		return false;       
	}
	char buf[256];	
	ReadFile.getline(buf,256);
	ReadFile.getline(buf,256);
	ReadFile.getline(buf,256,':');
    ReadFile>>pNum[0]; //读入条件属性数目
	ReadFile.getline(buf,256,':');
    ReadFile>>pNum[1];//读入记录数目
    ReadFile.close();
	return true;
}
////////////////////////////////////input information system///////////////////////
void  globe2::InputInfSys(char* FileName,float** &pIS,char ***&ISTable,int* &pDecis,char **&ConName,char** &ConType)
{//读入信息表
    int i,j;
    int ConAttrNum;
	int RecNum;
	fstream ReadFile;
	ReadFile.open(FileName,ios::in);
	if(!ReadFile)
	{
//       cout<<" can not open  file"<<endl;
	   return;
	}
	char buf[256];
	ReadFile.getline(buf,256);
	ReadFile.getline(buf,256);
	ReadFile.getline(buf,256,':');
    ReadFile>>ConAttrNum;// condition attribut sets
	ReadFile.getline(buf,256,':');
    ReadFile>>RecNum;
	for(i=0;i<ConAttrNum+1;i++)
		ReadFile>>ConName[i];//读入属性名称
	for(i=0;i<ConAttrNum+1;i++)
		ReadFile>>ConType[i];//读入属性类型
	char Decis[32];
	for(i=0;i<RecNum;i++)
	{
		for(j=0;j<ConAttrNum;j++)
		{
		    ReadFile>>ISTable[i][j];
			if(strcmp(ISTable[i][j],"-")==0)
			{
				pIS[i][j]=-1;//表示为遗失属性值
				continue;
			}
			if(strcmp(ConType[j],"Float")==0)
				pIS[i][j]=float(atof(ISTable[i][j]));//转换为浮点数  修改
			else if(strcmp(ConType[j],"Integer")==0)
				pIS[i][j]=atoi(ISTable[i][j]);//转换为整数  修改
		}//end for(j)
		ReadFile>>Decis;//读入决策值
		pDecis[i]=atoi(Decis);
	}
    ReadFile.close();
}


/////////////////////////////////////Integer transform flaot////////////////////
bool globe2::IntTransFloat(float** pRule,int rule_number, Set& set,
				   int* dec,int* pCon,int* pClass,
				   char* cut_file_name,char* rule_file_name,int con_attr_num)
{//写规则文件,输出结果,将规则中的整数还原为原先的浮点类型
//	const char* cut_file_name;
//	cut_file_name=Cut_file_name.operator LPCTSTR ( ) ;
	//begining
	//pointer of file stream
	FILE* pIn;
	FILE* pOut;
	//end

    //storage for value in file
	int* sorb=new int[10];
	char** s;
	char*** pCut;//断点
	char*  cut=new char[100];
	//end
    
	WORD* attr;
	int i,j,k;//counter for loop
	CUtility u;
	int* counter;
	int* order;
	int* position;
	bool found;
    
	//sort data in set
	attr=new WORD[set.GetCard()];
	order=new int[set.GetCard()];
	for(i=0;i<set.GetCard();i++)
		attr[i]=set.GetElement(i);//返回集合中的第i个元素,属性赋值
    u.QuickSort(attr,0,set.GetCard()-1);//快速排序
    //end    
	//prepare the order of display;
	
	k=set.GetCard();
	for(i=0;i<k;i++)
	{//得到排序后元素序号order
		for(j=0;j<k;j++)
		{
			if(attr[i]==set.GetElement(j))
			{
				order[i]=j;
				break;
			}
		}
	}
	//end 

	//prepare the position of deleted attributes
	position=new int[con_attr_num];
	k=0;
	for(i=0;i<con_attr_num;i++)
	{///////////////得到删除的属性的位置
		found=false;
		for(j=0;j<set.GetCard();j++)
		{
			if(i==set.GetElement(j))//当序号等于元素值时,设为true
			{
				found=true;
			}
		}
		if(!found)
		{
			position[k]=i;
			k++;
		}
	}

	pIn=fopen(cut_file_name,"r");//打开训练文件,准备读
	pOut=fopen(rule_file_name,"w");//打开规则文件,准备写
	fprintf(pOut,"Style:rule\nStage:0\nCondition attributes number:");
	fprintf(pOut,"%d\n",set.GetCard());
	fprintf(pOut,"The Number of Condition attributes deleted: %d\n",con_attr_num-set.GetCard());
    fprintf(pOut,"The position of Condition attributes deleted: ");
	for(i=0;i<k;i++)
	{
		fprintf(pOut,"%d ",position[i]);
	}
	fprintf(pOut,"\n");
	fprintf(pOut,"Rules number:");
	fprintf(pOut,"%d\n",rule_number);
	fprintf(pOut,"Blocks number:0\n");    	
    fscanf(pIn,"%s",cut);
/*	if(strcmp(cut,"Style:train")!=0)//!!!!!!!!!!!!!!!!!!!!!!
	{
     return false;
	}*/
	fscanf(pIn,"%s\n",cut);
	fscanf(pIn,"Condition attributes number:%d\nRecords number:%d",sorb+1,sorb+2);
    //sorb[1]=number of conattributes
	//sorb[2]=number of record
	i=sorb[1]+1;//条件属性数目+1
	//i=number of attributes
    s=new char*[i];
	for(k=0;k<2;k++)//2行,名称和类型
	{
	  for(j=0;j<i;j++)
	  {//读入一行
		s[j]=new char[10];//分配10个字符
		fscanf(pIn,"%s",s[j]);
	  }
	  for(j=0;j<set.GetCard();j++)
	  {//输出一行
		fprintf(pOut,"%s ",s[attr[j]]);
	  }
	  fprintf(pOut,"%s\n",s[sorb[1]]);//输出决策属性名称和类型
	}

    for(k=0;k<sorb[2];k++)
		for(j=0;j<i;j++)
			fscanf(pIn,"%d",sorb);

	fscanf(pIn,"%s",cut);
   if(strcmp(cut,"[Cuts]")!=0)//!!!!!!!!!!!!!!!!!!!!!!
	{
	//	AfxMessageBox("不是断点文件",MB_OK);
		return false;
	}
	pCut=new char**[sorb[1]];
	counter=new int[sorb[1]];
	for(i=0;i<sorb[1];i++)
	{
	    fscanf(pIn,"%d",sorb);////属性序号
		fscanf(pIn,"%d",counter+i);//属性断点数目
		pCut[i]=new char*[counter[i]];
		for(j=0;j<counter[i];j++)
		{
			pCut[i][j]=new char[30];
			fscanf(pIn,"%s",pCut[i][j]);//断点区间
			fscanf(pIn,"%d",sorb+4);//离散化的值
		}
	}

	for(i=0;i<rule_number;i++)
	{
		for(j=0;j<set.GetCard();j++)
		{
			if(pRule[i][order[j]]==-1)
			{
				fprintf(pOut,"%c\t",'-');
			}
			else
			{
				k=pRule[i][order[j]];
				fprintf(pOut,"%s\t",pCut[attr[j]][k]);
			}
		}
		fprintf(pOut,"%d\t",dec[i]);//决策
		fprintf(pOut,"%1.3f\t",(pCon[i]*1.0)/pClass[i]);//信任度
		fprintf(pOut,"%d\t",pCon[i]);
		fprintf(pOut,"%d\n",pClass[i]);
	}
    
	
	for(i=0;i<sorb[1]+1;i++)
		delete[] s[i];
	delete[] s;
	for(i=0;i<sorb[1];i++)
	{
		for(j=0;j<counter[i];j++)
		{
			delete[] pCut[i][j];
		}
		delete[] pCut[i];
	}
	delete[] pCut;
	delete[] sorb;
	delete[] attr;
	delete[] counter;
	delete[] order;
	delete[] position;
    fclose(pOut);
	fclose(pIn);
	return true;
 
}
///////////////Save result/////////////////////////////////////////////////////////////////////////////////////////
//void  globe2::Save(char* FileName,char** pResult,int RecNum,int ConAttrNum,int* pDecisR,int* pSupportR,int* pCredi,char)
void  globe2::Save(char* In_File,Set& Rset,char* FileName,float** pResult,int RecNum,int ConAttrNum,int* pDecisR,int* pCov,int* pClass,char**ConName,char**ConType)
{ //存储
	int i,j;
	fstream WriteFile;
	WriteFile.open(FileName,ios::out);
	if(!WriteFile)
	{
//       cout<<" can not open  file"<<endl;
		return;
	}
//	int Transform;
//	CString CutFileName;
//	Transform=AfxMessageBox("  需要还原段点吗?\n\n若需要,下一步请选择段点文件。",MB_YESNO|MB_ICONQUESTION);
//	CFileDialog dlg(true,".txt",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
//			"RS files(*.txt)|*.txt|all files(*.*)|*.*||",NULL);
//BEGIN:	if(Transform==IDYES&&dlg.DoModal()==IDOK){
 //   	CutFileName=dlg.GetPathName(); 
		 ///////////////liao:IntegerToFloat////////////////////////////////////////////
	if(!IntTransFloat(pResult,RecNum, Rset,pDecisR,pCov,pClass,In_File,FileName,ConAttrNum)){//如果不是含有断点的文件
//			goto  BEGIN;
//		}

     ////////////////////////////////////////////////////////////////////////////////
//	}else{
        int Del_Attr_Num=ConAttrNum-Rset.GetCard ();//删除了的属性数目
		bool bAdd;
		int m=0;int k;
		int* pPosition=new int[Del_Attr_Num];
        for(j=0;j<ConAttrNum;j++){//得到删除了的属性的序号
			bAdd=true;
			for(i=0;i<Rset.GetCard ();i++){
				if(j==Rset.GetElement(i)){
				     bAdd=false;
					 break;
				}
			}
			if(bAdd){
				pPosition[m]=j;
				m++;
			}
		}
    	WriteFile<<"Style: rule"<<endl;
     	WriteFile<<"Stage: 0"<<endl;
    	WriteFile<<"Condition attributes number: ";
        WriteFile<<Rset.GetCard ()<<endl;
		WriteFile<<"The Number of Condition attributes deleted: ";
		WriteFile<<Del_Attr_Num<<endl;
		WriteFile<<"The position of Condition attributes deleted: ";
		for(i=0;i<Del_Attr_Num;i++){
			WriteFile<<pPosition[i]<<" ";
		}
		WriteFile<<endl;
		delete [] pPosition;
    	WriteFile<<"Rules number: ";
      	WriteFile<<RecNum<<endl;
	    WriteFile<<"Blocks number: 0"<<endl;
		for(i=0;i<ConAttrNum;i++){
			for(k=0;k<Rset.GetCard ();k++){
				if(i==Rset.GetElement (k)){
					WriteFile<<ConName[i]<<' ';
				}
			}
		}
		WriteFile<<ConName[ConAttrNum]<<' ';
    	WriteFile<<endl;
		for(i=0;i<ConAttrNum;i++){
			for(k=0;k<Rset.GetCard ();k++){
				if(i==Rset.GetElement (k)){
					WriteFile<<ConType[i]<<' ';
				}
			}
		}
		WriteFile<<ConType[ConAttrNum];
    	WriteFile<<endl;
    	//end 李鄂
    	float Credi;
		int* pCon=new int[Rset.GetCard ()];
		for(i=0;i<Rset.GetCard ();i++){

		}
    	for(i=0;i<RecNum;i++){
	         for(j=0;j<ConAttrNum;j++){
	    		for(k=0;k<Rset.GetCard ();k++){
			    	if(j==Rset.GetElement (k)){
						if(pResult[i][k]==-1){
                            WriteFile<<"-\t";
						}else{
							WriteFile<<pResult[i][k]<<"\t";
						}
			        	break;
					}
				}
			}
        	WriteFile<<pDecisR[i]<<"\t";
	    	Credi=float((pCov[i]*1.0)/pClass[i]);
			
	    	WriteFile<<setprecision(8)<<Credi<<"\t"<<pCov[i]<<"\t"<<pClass[i];
	    	WriteFile<<endl;
		}
        WriteFile.close();
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////tolerance relation with limit //////////////////////////////////////////////////
bool globe2::ToleRwL(float** pIS,char ***ISTable,Set& ConAttrSet,int m,int n)
{//比较m,n实例是否属于限制容差关系,true,属于; false,不属于
    int i;
	int Num1=0;//
	int Num2=0;
	int Num3=0;
	float a1,a2;
	bool lable=false;
	for(i=0;i<ConAttrSet.GetCard ();i++){
		if(strcmp(ISTable[m][ConAttrSet.GetElement(i)],"-")==0 &&strcmp(ISTable[n][ConAttrSet.GetElement(i)],"-")==0)
		{
			Num1++;//统计m,n实例对应属性值都遗失的属性数目
		}
		else if(strcmp(ISTable[m][ConAttrSet.GetElement(i)],"-")!=0 
			&& strcmp(ISTable[n][ConAttrSet.GetElement(i)],"-")!=0){
     		a1=pIS[m][ConAttrSet.GetElement(i)];
			a2=pIS[n][ConAttrSet.GetElement(i)];
			if(a1==a2){			
			 Num1++;//统计相等的数目
			 }
		} 
	}
	if(Num1==ConAttrSet.GetCard ())
	{
		return true;//所有属性值都遗失,属于限制容差关系,返回true
	}
	else return false;
}
////////////////////////////tolerance class  with  limit ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void globe2::ToleClasswL(Set& TClasswL,float** pIS,char ***ISTable,int RecNum , Set& ConAttrSet,int m)
{//得到m实例的限制容差类, RecNum:实例数目
	int i;
	int* pelement;
	pelement=new int[RecNum];
	int card=0;
	for(i=0;i<RecNum;i++){
		if(ToleRwL(pIS,ISTable,ConAttrSet,i,m)){
			pelement[card]=i;
		    card++;//统计限制容差类的元素数目
		}
	}
	TClasswL.SetParameter (pelement,card);//实现拷贝
	delete pelement;
}
///////////////////////////////equivialence class of decision //////////////////////////////////////////////////////////////////////////////////////
void globe2::EqClassofDecis(Set& EClassD,int* pDecis,int RecNum,int m)
{//m实例决策的等价类
	int i;
	int card=0;
	int* pelement;
	pelement =new int[RecNum];
	for(i=0;i<RecNum;i++){
		if(pDecis[i]==pDecis[m]){
			pelement[card]=i;
		    card++;
		}
	}
	EClassD.SetParameter (pelement,card);
	delete pelement;
}
////////////////////////EqClassNumofDecis/////////////////////////////////////////////////////////////////////////////////////
void globe2::EqClassNumofDecis(Set& EClassDNum,int* pDecis,int RecNum)
{ //划分决策的等价类
	int  i;
	int  Card=0;
	int* pElement=new int[RecNum];	
	Set* pSTem;
	int* pTem=new int[RecNum];
	int  TemCard;
	Set  TemDecis;
	for(i=0;i<RecNum;i++){
		pElement[i]=i;
	}
    Set  Decis(pElement,RecNum);
	int  k=0;
	do{
		pElement[Card]=k;
		Card++;
		pSTem=new Set[1];
		TemCard=0;
    	for(i=0;i<RecNum;i++){//得到k实例的决策等价类
    		if(pDecis[i]==pDecis[k]){
    			pTem[TemCard]=i;
    			TemCard++;
			}
		}
		pSTem[0].SetParameter (pTem,TemCard);
		TemDecis=Decis-pSTem[0];//集合相减
		delete[] pSTem;
		Decis=TemDecis;
		if(Decis.GetCard ()>0){
			k=Decis.GetElement(0);
		}
	}while(Decis.GetCard ()>0 );
	EClassDNum.SetParameter (pElement,Card);
	delete[] pElement;
	delete[] pTem;
}
/////////////////////Card of Positive Region/////////////////////////////////////////////////////////////////////
int globe2::CardofPosRegion(float** pIS,char ***ISTable,int* pDecis,int RecNum,Set& ConAttrSet) 
{  //返回正域中的元素数目
	if(ConAttrSet.GetCard ()==0){
		return 0;
	}
	int i,j;
	int Tem;
	int ClassDNum;
	Set* pEClassofDecis;
	int* pTem=new int[RecNum];
    Set EClassDNum; 
    EqClassNumofDecis(EClassDNum,pDecis,RecNum);
	ClassDNum=EClassDNum.GetCard ();
	pEClassofDecis=new Set[ClassDNum];
	for(i=0;i<ClassDNum;i++){
		Tem=0;
		for(j=0;j<RecNum;j++){
			if(pDecis[j]==pDecis[EClassDNum.GetElement (i)]){
				pTem[Tem]=j;//标注i类决策对应的所有实例
	    		Tem++;//统计数目
			}
		}
		pEClassofDecis[i].SetParameter (pTem,Tem);//得到每一类对应的所有实例
	}
    ////////////all equivialences class of decision of IS ///////////////
    Set* pTClasswL=new Set[RecNum];
	for(i=0;i<RecNum;i++){//得到每个实例的限制容差类pTClasswL[i]
		ToleClasswL(pTClasswL[i],pIS,ISTable,RecNum,ConAttrSet,i);
	}
	/////////////////All tolerances class with limit of IS///////////////////////
   //	cout<<ClassDNum<<endl;
 	Set*  pPosSet=new Set[ClassDNum];/////////////positive Set 
	Set TemSet,TemSet1;
	int* pElement=new int[1];
    for(i=0;i<ClassDNum;i++){//得到每个决策类的正域pPosSet[i]
		for(j=0;j<RecNum;j++){
			if(Set::SetLessSet(pTClasswL[j],pEClassofDecis[i])){
                pElement[0]=j;
				TemSet1.SetParameter (pElement,1);
			    TemSet=pPosSet[i]+TemSet1;
				pPosSet[i]=TemSet;
			}
		}
	}
	int CardofPosRegion=0;
	for(i=0;i<ClassDNum;i++){
		CardofPosRegion+=pPosSet[i].GetCard ();//得到正域元素数目
	}

⌨️ 快捷键说明

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