📄 globe2.cpp
字号:
delete[] pCMiss;
return true;
}
/////////////////////Decision Matrix///////////////////////////////////////////////////////////////////////
//void globe2::DecisMatrix(Set** pDMatrix,char** pIS,int RecNum,Set& ConAttrSet,int* pDecis)
void globe2::DecisMatrix(Set* pDMatrix,int n,float** pIS,char*** ISTable,int RecNum,int ConAttrNum,int* pDecis)
{//构建实例n的广义差异矩阵行, denny 修改
int j,k,m;
int* pTem=new int[ConAttrNum];//ConAttrSet.GetCard ()
int Tem;
m=0;
int Same;
for(j=0;j<RecNum;j++){
if(pDecis[j]!=pDecis[n]){
Tem=0;
for(k=0;k<ConAttrNum;k++){
if((strcmp(ISTable[n][k],"-")!=0)&& (strcmp(ISTable[j][k],"-")!=0 ) &&
(pIS[j][k]!=pIS[n][k])){//ConAttrSet.GetElement (k)
pTem[Tem]=k;
Tem++;
}
}
if(Tem!=0){
//pDMatrix[m]=EmptySet;
pDMatrix[m].SetParameter (pTem,Tem);
m++;
}
}
}
delete []pTem;
}
////////////////////////////////////Computer Parameters ///////////////////
void globe2::ComputerPara(float** pRule,int* pRdec,int RuleNum,Set& Rset,float** pIS,char ***ISTable,int* pDec,int RecNum,int* pCov,int* pClass)
{//
//计算规则的覆盖度和限制容差类的元素个数
int i,j;
int Cov,Class,k;
int SameNum1,SameNum2;
int ConAttrNum=Rset.GetCard ();
for(i=0;i<RuleNum;i++){//i为规则序号
Cov=0;Class=0;
for(j=0;j<RecNum;j++){//j为实例序号
SameNum1=0;SameNum2=0;
for(k=0;k<ConAttrNum;k++){
if((pRule[i][k]!=-1)&&(strcmp(ISTable[j][Rset.GetElement (k)],"-")!=0)){
SameNum1++;
}
}
if(SameNum1==0){
continue;
}else{
for(k=0;k<ConAttrNum;k++){
if((pRule[i][k]!=-1)&&(strcmp(ISTable[j][Rset.GetElement (k)],"-")!=0)
&&(pRule[i][k]==pIS[j][Rset.GetElement (k)])){
SameNum2++;//规则匹配实例
}
}//k
}//else
if(SameNum1==SameNum2){
Class++;
if(pRdec[i]==pDec[j]){//决策相等
Cov++;
}
}
}//j
pClass[i]=Class;
pCov[i]=Cov;
}//i
}
/////////////Selminset///////////////////////////////////////////////////////
void globe2::SetMinSet(Set* pDM, int n, Set& RSet)
{//设置最小的与或式集合
int i;
bool bAllEmpty=false;
for(i=0;i<n;i++){
if(pDM[i].GetCard ()>0){
bAllEmpty=true;
break;
}
}
if(bAllEmpty){
int k,newsize,minNum,counter=0;//nsize,
Set* temp;
CUtility u;
SETSLOT* set_of_set=new SETSLOT[n];
SETSLOT* result;
CIntSet minSet;
for(i=0;i<n;i++)
{
if(pDM[i].GetCard()!=0)
{
temp=&(pDM[i]);
set_of_set[i].theSet.From_Set_To_IntSet(temp);//从set到本集合
set_of_set[i].fValid=true;
counter++;
}
else
{
set_of_set[i].fValid=false;
}
}
SETSLOT* setnew=u.ChangeMemory(set_of_set,n,counter);//返回消除了冗余的新的集合newSet
result=u.SimpLogic(setnew,counter,newsize);//或与式到与或式转化
minNum=result[0].theSet.NumOfEle();//初始化最小集合中的元素个数
minSet=result[0].theSet;//初始化最小集合
for(i=1;i<newsize;i++)
{//循环,找到真正的最小集合
k=result[i].theSet.NumOfEle();
if(k<minNum)
{
minNum=k;
minSet=result[i].theSet;
}
}
minSet.From_IntSet_To_Set(RSet);//拷贝到RSet
}
else{//pDM为空
RSet.SetParameter (NULL,0);
}
}
////////////////////algorithm2: generate rule////////////////////////////////////////////////////
//void globe2::GenerateRules(char* FileName,char** pIS,int* pDecis,Set& Rset,int RecNum,int ConAttrNum)
void globe2::GenerateRules(char* In_File,char* FileName,float** pIS,char ***ISTable,int* pDecis,Set& Rset,int RecNum,int ConAttrNum,char** ConName,char** ConType)
{//导出规则集 GenerateRules(In_File,filename,pIS,ISTable,pDecis,Rset,Rec_Num,ConAttrNum,ConName,ConType);
int i,j,k;
int NewRecNum;
int NewConAttrNum;
int AsteriskNum;
int SameNum;
NewConAttrNum=Rset.GetCard ();//约简后的属性集
float** pIS1;// step2: generate empty table
try{
pIS1=new float*[RecNum];
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory11!",MB_OK|MB_ICONSTOP);
e->Delete();
return;
}
for(i=0;i<RecNum;i++){
pIS1[i]=new float[NewConAttrNum];
}
int* pSupport;
int* pDecis1;
try{
pSupport=new int[RecNum];//支持度
pDecis1=new int[RecNum];//决策
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory11!",MB_OK|MB_ICONSTOP);
e->Delete();
return;
}
int m=0;
bool add;
for(i=0;i<RecNum;i++){////step1到step3:process the reapted record
add=true;
AsteriskNum=0;
for(j=0;j<NewConAttrNum;j++){
if(strcmp(ISTable[i][Rset.GetElement (j)],"-")==0)
{
AsteriskNum++;//统计遗失数目
}
}
if(AsteriskNum==NewConAttrNum){//如果约简后该样例上的属性值全为遗失
add=false;
continue;
}
for(k=0;k<m;k++){
SameNum=0;
for(j=0;j<NewConAttrNum;j++){
if( pIS1[k][j]==-1 && (strcmp(ISTable[i][Rset.GetElement (j)],"-")==0) ||
pIS1[k][j]!=-1 && (strcmp(ISTable[i][Rset.GetElement (j)],"-")!=0) &&
// if((strcmp(ISTable[k][Rset.GetElement (j)],"-")==0) && (strcmp(ISTable[i][Rset.GetElement (j)],"-")==0) ||
// (strcmp(ISTable[k][Rset.GetElement (j)],"-")!=0) && (strcmp(ISTable[i][Rset.GetElement (j)],"-")!=0) &&
pIS1[k][j]==pIS[i][Rset.GetElement (j)]){//基于限制容差关系,统计相等值的个数,看是否重复
SameNum++;
}
}
if((SameNum==NewConAttrNum)&&(pDecis1[k]==pDecis[i])){//重复
add=false;
pSupport[k]++;
break;
}
}
if(add){//初始化,加入
for(j=0;j<NewConAttrNum;j++){
pIS1[m][j]=pIS[i][Rset.GetElement (j)];
pDecis1[m]=pDecis[i];
pSupport[m]=1;
}
m++;
}
}//end for(i)
NewRecNum=m;//得到新的规则的条数
// for(i=m;i<RecNum;i++){
// delete[] pIS1[i];
// }
int RuleNum;
float** pRule;///step4:generate empty Rule table
pRule=new float*[NewRecNum];
for(i=0;i<NewRecNum;i++){
pRule[i]=new float[NewConAttrNum];
}
int* pDecisR=new int[NewRecNum];
int* pSupportR=new int[NewRecNum];
m=0;
for(i=0;i<NewRecNum;i++){////process the rules with conllision
add=true;
for(k=0;k<m;k++){
SameNum=0;
for(j=0;j<NewConAttrNum;j++){
if(pRule[k][j]==-1 && pIS1[i][j]==-1 || pRule[k][j]!=-1 && pIS1[i][j]!=-1
// if((strcmp(ISTable[k][Rset.GetElement (j)],"-")==0) &&
// (strcmp(ISTable[i][Rset.GetElement (j)],"-")==0) ||
// (strcmp(ISTable[k][Rset.GetElement (j)],"-")!=0) && (strcmp(ISTable[i][Rset.GetElement (j)],"-")!=0)
&& pRule[k][j]==pIS1[i][j]){//修改,基于限制容差关系
SameNum++;
}
}
if((SameNum==NewConAttrNum)&&(pDecisR[k]!=pDecis1[i])){//冲突规则
add=false;//按照少数服从多数的原则进行
if(pSupportR[k]<pSupport[i]){//删除kn
pDecisR[k]=pDecis1[i];
pSupportR[k]=pSupport[i];
break;
}else{
break;
}
}
}
if(add){
for(j=0;j<NewConAttrNum;j++){
pRule[m][j]=pIS1[i][j];
}
pDecisR[m]=pDecis1[i];
pSupportR[m]=pSupport[i];//////////
m++;
}
}
RuleNum=m;
for(i=m;i<NewRecNum;i++){
delete[] pRule[i];
}
for(i=0;i<NewRecNum;i++){
delete[] pIS1[i];
}
delete[] pIS1;
delete[] pDecis1;
delete[] pSupport;
delete[] pSupportR;
//////////////////////////////////////////////Modify //////////////
// int* pSpecRule=new int[NewConAttrNum];
bool bSpecRule=false;
int SpecDec;
for(i=0;i<RuleNum;i++){//step 1
AsteriskNum=0;
for(j=0;j<NewConAttrNum;j++){
if(pRule[i][j]==-1){
// if(strcmp(ISTable[i][Rset.GetElement (j)],"-")==0){
AsteriskNum++;
}
}
if(AsteriskNum==NewConAttrNum){//全为遗失属性值的情况,删除
bSpecRule=true;
for(k=0;k<NewConAttrNum;k++){
// pSpedRule[k]=pRule[i][k];
pRule[i][k]=pRule[RuleNum-1][k];//将最后一条规则拷贝到当前位置
// pRule[RuleNum-1][k]=pTemRule[k];
}
SpecDec=pDecisR[i];//拷贝出来,有何意义?
pDecisR[i]=pDecisR[RuleNum-1];//决策值
delete[] pRule[RuleNum-1];
i--; //denny修改,因为不能判断是否最后一条规则是否为全为遗失属性值
RuleNum--;
break;
}
}
///////////////////////value reduct////////////////////////////////////////////////////
if(RuleNum>1){//规则数目大于一
Set* pDM;
Set* pR=new Set [RuleNum];
int DecClassNum;
for(i=0;i<RuleNum;i++){
DecClassNum=0;
for(j=0;j<RuleNum;j++){//统计不同决策的个数
if(pDecisR[j]!=pDecisR[i]){//决策不等
DecClassNum++;//类别加一
}
}
pDM=new Set [DecClassNum];
DecisMatrix(pDM,i,pRule,ISTable,RuleNum,NewConAttrNum, pDecisR);//构建决策差异矩阵
SetMinSet(pDM,DecClassNum,pR[i]);//化简,得到最小的字集合
delete[] pDM;
}
bool bWrite;//根据最小集合化简规则
for(i=0;i<RuleNum;i++){
if(pR[i].GetCard ()){
for(j=0;j<NewConAttrNum;j++){
bWrite=true;
for(k=0;k<pR[i].GetCard ();k++){
if(j==pR[i].GetElement (k)){
bWrite=false;
break;
}
}
if(bWrite){
pRule[i][j]=-1;
}
}
}
}
}
////////////////////////////////////combine reapted rules/合并重复的规则
int NewRuleNum;
float** pRule2;
int* pDecisR2;
try{
pRule2=new float*[RuleNum];//临时规则
pDecisR2=new int[RuleNum];//规则决策
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory11!",MB_OK|MB_ICONSTOP);
e->Delete();
return;
}
for(i=0;i<RuleNum;i++){
pRule2[i]=new float[NewConAttrNum];
}
m=0;
bool bExchange;
int change;
int SameNum1;
int SameNum2;
for(i=0;i<RuleNum;i++){
add=true;
bExchange=false;
AsteriskNum=0;
for(k=0;k<NewConAttrNum;k++){
if(pRule[i][k]==-1){
AsteriskNum++;
}
}//end for(k)
if(AsteriskNum==NewConAttrNum){//全为遗失属性值,淘汰本规则
add=false;
continue;
}
for(j=0;j<m;j++){//判断是否和已有规则重复
if(pDecisR[i]==pDecisR2[j]){//决策相等
SameNum1=0;SameNum2=0;
for(k=0;k<NewConAttrNum;k++){
if(pRule2[j][k]!=-1){//不为遗失值
// if(strcmp(ISTable[j][Rset.GetElement (k)],"-")!=0){
SameNum1++;
if(pRule2[j][k]==pRule[i][k]){
SameNum2++;
}
}
}
if(SameNum1==SameNum2){//说明重复,i不加入
add=false;
break;
}
SameNum1=0;SameNum2=0;
for(k=0;k<NewConAttrNum;k++){
// if(pRule[i][k]!=-1){
if(strcmp(ISTable[i][Rset.GetElement (k)],"-")!=0){
SameNum1++;
if(pRule[i][k]==pRule2[j][k]){
SameNum2++;
}
}
}
if(SameNum1==SameNum2){//i加入
bExchange=true;
change=j;
break;
}
}
}//end for(j)
if(add){
if(bExchange){//拷贝规则pRule到pRule2, 替换
for(k=0;k<NewConAttrNum;k++){
pRule2[change][k]=pRule[i][k];
}
pDecisR2[change]=pDecisR[i];
}else{//添加规则
for(k=0;k<NewConAttrNum;k++){
pRule2[m][k]=pRule[i][k];
}
pDecisR2[m]=pDecisR[i];
m++;
}
}
}//end for(i)
NewRuleNum=m;//规则数目
for(i=m;i<RuleNum;i++){
delete[] pRule2[i];
}
for(i=0;i<RuleNum;i++){
delete[] pRule[i];
}
delete[] pRule;
delete[] pDecisR;
///////////////////////////////////////
int* pDel_Con=new int[NewConAttrNum];
for(i=0;i<NewConAttrNum;i++){
pDel_Con[i]=i;
}
Set NewConAttrSet;
Set Old_Rset;
Old_Rset=Rset;
NewConAttrSet.SetParameter (pDel_Con,NewConAttrNum);
Set Del_ConAttr,Del_ConAttr1;
Set Tem,Tem1;
int* pTem=new int[1];
int* pTem1=new int[1];
int Del_Num;
for(j=0;j<NewConAttrNum;j++){
Del_Num=0;
for(i=0;i<NewRuleNum;i++){
if(pRule2[i][j]==-1){
Del_Num++;
}
}
/* if(Del_Num==NewRuleNum){//如果该属性上所有属性值都遗失,则删除该属性
pTem1[0]=j;
Del_ConAttr1.SetParameter (pTem1,1);
Tem=NewConAttrSet-Del_ConAttr1;
NewConAttrSet=Tem;//////
pTem[0]=Old_Rset.GetElement (j);
Del_ConAttr.SetParameter (pTem,1);
Tem=Rset-Del_ConAttr;
Rset=Tem;///////得到新的属性约简结果
}
*/
}
delete[] pTem;
delete[] pTem1;
float** pRule3= new float*[NewRuleNum];
for(i=0;i<NewRuleNum;i++){
pRule3[i]= new float[NewConAttrSet.GetCard ()];
}
for(i=0;i<NewRuleNum;i++){
for(j=0;j<NewConAttrSet.GetCard ();j++){
pRule3[i][j]=pRule2[i][NewConAttrSet.GetElement (j)];//拷贝规则到pRule3
}
}
for(i=0;i<NewRuleNum;i++){
delete[] pRule2[i];
}
delete[] pRule2;
////////////////////////////////////////////
int NewConAttrNum4=NewConAttrSet.GetCard ();//新的属性数目
int* pCov;
int* pClass;
if(bSpecRule){///////////////////存在全是遗失条件属性的规则
pCov=new int[NewRuleNum+1];
pClass=new int[NewRuleNum+1];
}else{
pCov=new int[NewRuleNum];
pClass=new int[NewRuleNum];
}
ComputerPara(pRule3,pDecisR2,NewRuleNum,Rset,pIS,ISTable,pDecis,RecNum, pCov,pClass);//计算覆盖度等参数
if(bSpecRule){
pCov[NewRuleNum]=1;
pClass[NewRuleNum]=1;
int* pDecisR4=new int[NewRuleNum+1];
float**pRule4=new float*[NewRuleNum+1];
for(i=0;i<NewRuleNum+1;i++){
pRule4[i]=new float[NewConAttrNum4];
}
for(i=0;i<NewRuleNum;i++){
for(j=0;j<NewConAttrNum4;j++){
pRule4[i][j]=pRule3[i][j];
}
pDecisR4[i]=pDecisR2[i];
}
for(j=0;j<NewConAttrNum4;j++){
pRule4[NewRuleNum][j]=-1;
}
pDecisR4[NewRuleNum]=SpecDec;//默认匹配,当所有前面的规则都匹配不上时也可以不需要条件属性而匹配
Save(In_File,Rset,FileName,pRule4,NewRuleNum+1,ConAttrNum,pDecisR4,pCov,pClass,ConName,ConType);
for(i=0;i<NewRuleNum+1;i++){
delete[] pRule4[i];
}
delete[] pRule4;
delete[] pDecisR4;
}else{
Save(In_File,Rset,FileName,pRule3,NewRuleNum,ConAttrNum,pDecisR2,pCov,pClass,ConName,ConType);
}
for(i=0;i<NewRuleNum;i++){
delete[] pRule3[i];
}
delete[] pRule3;
delete[] pDecisR2;
}
////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -