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

📄 attrreduce.cpp

📁 一种基于启发式算法(互信息熵)的粗糙集约简源代码
💻 CPP
字号:
// AttrReduce.cpp: implementation of the CAttrReduce class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Reduce.h"
#include "AttrReduce.h"


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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAttrReduce::CAttrReduce()
{

}

CAttrReduce::~CAttrReduce()
{

}
BOOL CAttrReduce::ConfigODBC()
{
	if (!m_db.IsOpen())
	{
		CMylib mylib;
		CString m_strPath=mylib.GetHomeDirectory();
		
		m_strPath+="reduce.mdb";

		mylib.ConfigAccess("reduce",m_strPath);
	
		m_db.OpenEx("ODBC;host=local;DSN=reduce;");
	}

	return true;
}
void CAttrReduce::InsertObject(vector<CString> m_object)
{
	TrainingSet.push_back(m_object);

}
	//构造区分矩阵
void CAttrReduce::ConstructMatrix()
{
	int iSize;
	iSize=TrainingSet.size();

	int i,j;
	for(i=0;i<iSize-1;i++)
	{
		vector<CString> ob1;
		ob1=TrainingSet[i];

		for(j=i+1;j<iSize;j++)
		{
			vector<CString> ob2;
			ob2=TrainingSet[j];

			//比较ob1、ob2,找出能够区分出来的属性
			list<CString> Matrix_Cell;
			int iNum;
			iNum=ob1.size();
			for (int z=0;z<iNum;z++)
			{
				if (ob1[z]!=ob2[z])
					Matrix_Cell.push_back(v_attr[z]);
			}

			int tmp=Matrix_Cell.size();
			if (tmp>0)
				Matrix.push_back(Matrix_Cell);
		}
	}

}

	//提取约简
void CAttrReduce::ExtractReduce(list<CString> &reduce)
{
	//提取核,直接把核属性加入到候选约简中
	list<list<CString> >::iterator p;
	for(p=Matrix.begin();p!=Matrix.end();p++)
	{
		int m_iCount;
		m_iCount=p->size();

		if (m_iCount==1)
		{
			list<CString>::iterator p2;
			p2=p->begin();
						
			//防止相同的核被插入两次
			bool m_bAdd=true;
			list<CString>::iterator p3;
			for(p3=reduce.begin();p3!=reduce.end();p3++)
			{
				if(*p3==*p2)
				{
					m_bAdd=false;
					break;
				}
			}
			
			if(m_bAdd)
				reduce.push_back(*p2);
		}
	}
	

	//将包含候选约简(此时完全为核属性)中任意属性的区分矩阵的单元删除

	for(p=Matrix.begin();p!=Matrix.end();)
	{
		if (IsIn(reduce,*p))
		{
			Matrix.erase(p);
			p=Matrix.begin();
		}
		else
			p++;
	}

	////启发式计算约简
	map<CString,int> m_FieldCount;
	while (1)
	{
		//循环遍历整个矩阵,计算每一个条件属性出现的次数
		m_FieldCount.clear();
		list<list<CString> >::iterator p1;
		for(p1=Matrix.begin();p1!=Matrix.end();p1++)
		{
			//对于每一个矩阵单元
			list<CString>::iterator p2;
			for(p2=p1->begin();p2!=p1->end();p2++)
			{
				//对于每一个矩阵单元中的每一个可区分两个对象的条件属性
				map<CString,int>::iterator mp;
				mp=m_FieldCount.find(*p2);
				if (mp==m_FieldCount.end())
					m_FieldCount.insert(pair<CString,int>(*p2,1));
				else
					mp->second++;
			}
		}


		//从m_FieldCount中找出出现次数最高的那个条件属性,加入到候选约简中
		//如果m_FieldCount为空,退出循环
		int iCount;
		iCount=m_FieldCount.size();

		CString strTargetAttr;
		strTargetAttr="ILOVEYOU";
		
		if (iCount==0)
			break;
		else
		{
			map<CString,int>::iterator  m_p;
			m_p=m_FieldCount.begin();

			int m_iMax;

			strTargetAttr=m_p->first;
			m_iMax=m_p->second;

			m_p++;
			for(;m_p!=m_FieldCount.end();m_p++)
			{
				if (m_p->second>m_iMax)
				{
					strTargetAttr=m_p->first;
					m_iMax=m_p->second;
				}
			}

		}

		if (strTargetAttr=="ILOVEYOU")
			break;
		else
			reduce.push_back(strTargetAttr);
		
		//
		PRINTF("new reduce cell:%s",strTargetAttr);
		
		//将区分矩阵中含有这个条件属性的单元删除,开始下一轮循环
		
		list<list<CString> >::iterator p_1;
		for(p_1=Matrix.begin();p_1!=Matrix.end();)
		{
		
			if (IsIn(strTargetAttr,*p_1))
			{
				Matrix.erase(p_1);
				p_1=Matrix.begin();
			}
			else
				p_1++;

		}
	}

}

void CAttrReduce::InsertAttr(CString strAttr)
{
	v_attr.push_back(strAttr);
}

void CAttrReduce::InsertObjects()
{
	CRecordset m_rs(&m_db);
	m_rs.Open(CRecordset::snapshot,"select * from tb_rs");
	//首先获得各个字段名
	int m_iFieldCount=m_rs.GetODBCFieldCount();
	
	for(int j=1;j<m_iFieldCount;j++)
	{
		CODBCFieldInfo FInfo;
		m_rs.GetODBCFieldInfo(short(j),FInfo);

		CString m_strField=FInfo.m_strName;
		InsertAttr(m_strField);
	}

	//将每一个对象加载到训练集中
	for(;!m_rs.IsEOF();m_rs.MoveNext())
	{
		vector<CString> vFieldValue;

		int m_iFieldCount=m_rs.GetODBCFieldCount();

		for(int i=1;i<m_iFieldCount;i++)
		{
			CString strTmp;
			m_rs.GetFieldValue(short(i),strTmp);
			vFieldValue.push_back(strTmp);
		}

		 InsertObject(vFieldValue);
	}
	
}

void CAttrReduce::PrintTrainingSet()
{

	int nSize1=TrainingSet.size();

	for (int i=0;i<nSize1;i++)
	{
		PRINTF("this is a object");
		vector<CString> ob;
		ob=TrainingSet[i];

		int nSize2=ob.size();
		for (int j=0;j<nSize2;j++)
		{
			PRINTF(ob[j]);
		}
	}
}

void CAttrReduce::PrintMatrix()
{
	list<list<CString> >::iterator p1;
	for(p1=Matrix.begin();p1!=Matrix.end();p1++)
	{
		PRINTF("this is a Matrix Cell");
		list<CString>::iterator p2;
		for(p2=p1->begin();p2!=p1->end();p2++)
			PRINTF("%s",*p2);
	}
}


	//判断第一个参数是否在第二个参数lst中
BOOL CAttrReduce::IsIn(CString str,list<CString> lst)
{

	list<CString>::iterator p;
	for(p=lst.begin();p!=lst.end();p++)
	{
		if(*p==str)
			return true;
	}
	return false;
}

	//判断第一个参数中是否存在某一个CString在第二个参数中
BOOL CAttrReduce::IsIn(list<CString> lst1,list<CString> lst2)
{
	list<CString>::iterator p1;
	for(p1=lst1.begin();p1!=lst1.end();p1++)
	{
		if (IsIn(*p1,lst2))
			return true;
	}
	return false;
}

⌨️ 快捷键说明

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