yyzsamplerecognise2.cpp
来自「某个实验事编写粗糙集智能信息处理的程序」· C++ 代码 · 共 913 行 · 第 1/2 页
CPP
913 行
delete[] temp;
delete[] d;
}
void CLieSampleRecogniseYyz2::SampleRecogniseMany()
{
TRACE("sample recognise few\n");
int i,j,k;
int* temp;
int temp1 = -1;
float reliability; //reliability
int totalnum;
int coveragenum; //覆盖数
int n = 0;
int pos;
char* d;
try
{
pRecogniseResultTable = new char**[iTestNum]; //result table
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory11!",MB_OK|MB_ICONSTOP);
e->Delete();
return;
}
for(i = 0;i < iTestNum;i++)
pRecogniseResultTable[i] = new char*[2];//识别结果
for(i = 0;i < iTestNum;i++)
{
try
{
pRecogniseResultTable[i][0] = new char[MAX];
pRecogniseResultTable[i][1] = new char[MAX];
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return;
}
}
try
{
d = new char[MAX];
temp = new int[iRuleNum];
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return;
}
CString tempstring;
CString trimstring;
CString compstring;
for(i = 0;i < iTestNum;i++)
{//对每个测试样本
n = 0;
int MaxMatch=0;
bool bcollision=false;
for(j = 0;j < iRuleNum;j++)
temp[j] = 0; //initialize temp[] to zero
for(j = 0;j < iRuleNum;j++)
{//遍历每条规则,找出匹配规则
//------added by li e ,2001.6.27---------------
if ( IsRuleAttAllNull(j) )
{
/* if( IsTestAttAllNull(i) )
{
for(int xxx=0;xxx<j;xxx++)
temp[xxx]=0;
temp[j]=1;
temp1=j;
n=1;
// 此处continue已经没有意义
//因为规则集中已经不再有全为空的情况.
//而其余的规则肯定和该测试(识别)数据不匹配
}
break;
*/
temp[j]=-1;
n++;
continue;
}
/// end li e 2001.6.27------------
for(k = 0;k < iAttNum;k++)
{//每个条件属性
if(strcmp(pRuleTable[j][k],"-") == 0
|| strcmp(pStringTable[i][k],"-") == 0)
continue;//规则为‘-且长度为1,继续该循环
tempstring = pRuleTable[j][k];//不为空的情况
if(tempstring[0] == '[' && strcmp(pDataType[k],"String")!=0 )//为连续区间
{//比较范围情况[XXX,XXX),如果在这个范围,go on,否则跳出
pos = tempstring.Find(',');
trimstring = tempstring.Left(pos);
trimstring.Remove('[');
if(strcmp(trimstring,"*") != 0)
{
if(atof(trimstring) > atof(pStringTable[i][k]))
{
temp[j]=0;
break; //l比下限小,不匹配,退出该循环
}
}//end if
trimstring = tempstring;
trimstring.Delete(0,pos+1);
trimstring.Remove(')');
trimstring.Remove(']');
if(strcmp(trimstring,"*") != 0)
if(atof(trimstring) <= atof(pStringTable[i][k]))
{
temp[j]=0;
break; //比上限大,退出该循环
}
//应该加 continue
temp[j]++;//denny 加
continue; //added by li e
}//end if
else if(strcmp(pDataType[k],"String")==0 )
{//属性类型为字符串型
if(strcmp(tempstring,pStringTable[i][k])==0)
{
temp[j]++;
continue;
}
else
{
temp[j]=0;
break;
}
}
else
{//为整数浮点数的情况,如果不等,跳出。如果等,go on
if(strcmp(pDataType[k],"Integer")==0)
if(atoi(pRuleTable[j][k]) != atoi(pStringTable[i][k]))
{
temp[j]=0;
break;
}
if(strcmp(pDataType[k],"Float")==0)
{
float a1=(float)atof(pRuleTable[j][k]);
float a2=(float)atof(pStringTable[i][k]);
if(a1!=a2)
{
temp[j]=0;
break;
}
}
temp[j]++;
}//end else
}//end for(k)
if(temp[j]>0)
n++;
if(temp[j]> MaxMatch)
{
MaxMatch=temp[j];//得到最大匹配的属性数目
temp1=j;
}
}//end for(j)
int x;
if(n>1)
{
if(MaxMatch>0)
{
for(x=0;x<iRuleNum;x++)
{
if(temp[x]!=0 && temp[x]<MaxMatch )
{
temp[x]=0;//考虑最长匹配
n--;
}
if(temp[x]!=0)
temp[x]=1;
}//end for
} //end if(maxMatch>0)
else{//当只有全部遗失属性值规则匹配样列时
for(x=0;x<iRuleNum;x++)
{
if(temp[x]==-1)
temp[x]=1;
}
}
} //end if(n>1)
else
{
for(x=0;x<iRuleNum;x++){
if(temp[x]!=0)
temp[x]=1;
}
}
if(n != 0 && bBlock) //存在匹配的规则,而且有 Blocks
{
for(j = 0;j < iBlockNum;j++)
{
for(k = 0;k < iAttNum;k++)
{
if((strcmp(pBlockTable[j][k],"-")== 0 )
|| (strcmp(pStringTable[i][k],"-")== 0 ))
continue;//封锁事实或样例属性为'-'
tempstring = pBlockTable[j][k];//tempstring为CString类
if(tempstring[0] == '[')
{
pos = tempstring.Find(',');
trimstring = tempstring.Left(pos);
trimstring.Remove('[');
if(strcmp(trimstring,"*") != 0)
{
if(atof(trimstring) > atof(pStringTable[i][k]))
break;
}//end if
trimstring = tempstring;
trimstring.Delete(0,pos+1);
trimstring.Remove(')');
trimstring.Remove(']');
if(strcmp(trimstring,"*") != 0)
if(atof(trimstring) <= atof(pStringTable[i][k]))
break;
}//end if
else if(strcmp(pDataType[k],"String")==0 )
{//属性类型为字符串型
if(strcmp(tempstring,pStringTable[i][k])==0)
continue;
else break;
}
else
if(atoi(pBlockTable[j][k]) != atoi(pStringTable[i][k]))
break;
}//end for
if(k == iAttNum) //如果块匹配
{//删除对应封锁规则
trimstring = pBlockTable[j][iAttNum];
trimstring.Delete(0,5);
if(temp[atoi(trimstring)] == 1)
{
temp[atoi(trimstring)] = 0;
n--;
}//end if
}//end if
}//end for(j)
}//end if(n != 0 && bBlock)
if(n == 0)
{//无规则匹配
strcpy(pRecogniseResultTable[i][0],"未识别");
strcpy(pRecogniseResultTable[i][1],"-");
if(bTest)
iDentifyNone++;//未识别数加一
continue;
}//end if
if(n == 1)
{//规则匹配数为1
for(j = 0;j < iRuleNum;j++)
{
if(temp[j] == 1)
break;
}//end for
if(bTest)
{
if(strcmp(pRuleTable[j][iAttNum],pStringTable[i][iAttNum]) == 0)
iDentifyRight++;//决策相等时,决策正确数加一
else
iDentifyError++;//决策不等时,决策错误数加一
}
strcpy(pRecogniseResultTable[i][0],pRuleTable[j][iAttNum]);//统计识别值
_itoa(temp1+1,pRecogniseResultTable[i][1],10);
//在pRecogniseResultTable[i][1]中以十进制字符串形式存储temp1+1,即规则序号
continue;
}//end if
//以下处理有多条规则匹配时
temp1 = -1;
bcollision=false;
if(bTest)
for(j = 0;j < iRuleNum;j++)
{//统计冲突样例数目
for(int j1 = 0;j1 < iRuleNum;j1++)
if(j1!=j && temp[j1]==1 && temp[j]==1)
if(atoi(pRuleTable[j][iAttNum])!=atoi(pRuleTable[j1][iAttNum]))
{//决策不等,冲突
bcollision=true;
break;
}
if(bcollision)
{
iTestConflict++;//冲突数目加一
break;
}
}
for(j = 0;j < iRuleNum;j++)
{//多数优先策略
if(temp[j] == 1)
{
if(temp1 == -1)
{
temp1 = j;
reliability = (float)atof(pRuleTable[j][iAttNum+1]);
coveragenum = atoi(pRuleTable[j][iAttNum+2]);
totalnum = atoi(pRuleTable[j][iAttNum+3]);
strcpy(d,pRuleTable[j][iAttNum]);
}//end if
else
if(atof(d)==atof(pRuleTable[j][iAttNum])) //一致规则
{//一致的就选择覆盖度最大的
if(coveragenum< atoi(pRuleTable[j][iAttNum+2]))
{
temp1 = j;
reliability = (float)atof(pRuleTable[j][iAttNum+1]);
coveragenum = atoi(pRuleTable[j][iAttNum+2]);
totalnum = atoi(pRuleTable[j][iAttNum+3]);
strcpy(d,pRuleTable[j][iAttNum]);
}//end if
}
else
{
if(reliability == atof(pRuleTable[j][iAttNum+1]))
{
if(totalnum < atoi(pRuleTable[j][iAttNum+3]))
{
temp1 = j;
reliability = (float)atof(pRuleTable[j][iAttNum+1]);
coveragenum = atoi(pRuleTable[j][iAttNum+2]);
totalnum = atoi(pRuleTable[j][iAttNum+3]);
strcpy(d,pRuleTable[j][iAttNum]);
}//end if
}//end if
else
{//信任度不等
if(totalnum == atoi(pRuleTable[j][iAttNum+3]))
{//频度一样时选取可信度较大的规则
if(reliability < atof(pRuleTable[j][iAttNum+1]))
{
temp1 = j;
reliability = (float)atof(pRuleTable[j][iAttNum+1]);
coveragenum = atoi(pRuleTable[j][iAttNum+2]);
totalnum = atoi(pRuleTable[j][iAttNum+3]);
strcpy(d,pRuleTable[j][iAttNum]);
}//end if
}//end if
else
{//频度不等
if(totalnum < atoi(pRuleTable[j][iAttNum+3]))
{
if(reliability < atof(pRuleTable[j][iAttNum+1]))
{
temp1 = j;
reliability = (float)atof(pRuleTable[j][iAttNum+1]);
coveragenum = atoi(pRuleTable[j][iAttNum+2]);
totalnum = atoi(pRuleTable[j][iAttNum+3]);
strcpy(d,pRuleTable[j][iAttNum]);
}//end if
else
{
// if(coveragenum/(float)(totalnum*totalnum) >
// atoi(pRuleTable[j][iAttNum+2])/(float)(atoi(pRuleTable[j][iAttNum+3])*atoi(pRuleTable[j][iAttNum+3])))
if((float)(coveragenum*coveragenum)/totalnum <
(float)(atoi(pRuleTable[j][iAttNum+2])*atoi(pRuleTable[j][iAttNum+2]))/atoi(pRuleTable[j][iAttNum+3]))
{
temp1 = j;
reliability = (float)atof(pRuleTable[j][iAttNum+1]);
coveragenum = atoi(pRuleTable[j][iAttNum+2]);
totalnum = atoi(pRuleTable[j][iAttNum+3]);
strcpy(d,pRuleTable[j][iAttNum]);
}//end if
}//end else
}//end if
else
{
if(reliability < atof(pRuleTable[j][iAttNum+1]))
{
if((float)(coveragenum*coveragenum)/totalnum <
(float)(atoi(pRuleTable[j][iAttNum+2])*atoi(pRuleTable[j][iAttNum+2]))/atoi(pRuleTable[j][iAttNum+3]))
{
temp1 = j;
reliability = (float)atof(pRuleTable[j][iAttNum+1]);
coveragenum = atoi(pRuleTable[j][iAttNum+2]);
totalnum = atoi(pRuleTable[j][iAttNum+3]);
strcpy(d,pRuleTable[j][iAttNum]);
}//end if
}//end if
}//end else
}//end else
}//end else
}//end else
}//end if
}//end for
if(bTest)
{
if(strcmp(d,pStringTable[i][iAttNum]) == 0)
{
if(bcollision)
iTestRight++;
iDentifyRight++;
}//end if
else
{
if(bcollision)
iTestError++;
iDentifyError++;
}//end else
}
strcpy(pRecogniseResultTable[i][0],d);
_itoa(temp1+1,pRecogniseResultTable[i][1],10);
}//end for
delete[] temp;
delete[] d;
}
int CLieSampleRecogniseYyz2::Match(int test,int rule )
{//比较序号为test的实例和序号为rule的规则,看是否匹配,返回不为0时,匹配
int n=0,i;
CString tempstring,trimstring;
for(i=0;i<iAttNum;i++)
{
if( pStringTable[test][i][0]=='-' || pRuleTable[rule][i][0]=='-')//任意遗失,匹配
continue;
tempstring = pRuleTable[rule][i];
if(tempstring[0] == '[')
{
int pos = tempstring.Find(',');
trimstring = tempstring.Left(pos);
trimstring.Remove('[');
if(strcmp(trimstring,"*") != 0)
{
if(atof(trimstring) > atof(pStringTable[test][i]))
return 0; //l比下限小,退出该循环,不匹配
}//end if
trimstring = tempstring;
trimstring.Delete(0,pos+1);
trimstring.Remove(')');
if(strcmp(trimstring,"*") != 0)
if(atof(trimstring) <= atof(pStringTable[test][i]))
return 0; //比上限大,退出该循环
n++;
continue;
}//end if
if( atoi(pStringTable[test][i]) == atoi(pRuleTable[rule][i]))
n++;//都不为遗失时,如果相等,累加
else
return 0;
}
return n;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?