实现基于谓词逻辑的归结原理.docx
《实现基于谓词逻辑的归结原理.docx》由会员分享,可在线阅读,更多相关《实现基于谓词逻辑的归结原理.docx(26页珍藏版)》请在冰豆网上搜索。
![实现基于谓词逻辑的归结原理.docx](https://file1.bdocx.com/fileroot1/2023-5/23/7cd95f68-d8fc-426b-adea-de2d55978e7c/7cd95f68-d8fc-426b-adea-de2d55978e7c1.gif)
实现基于谓词逻辑的归结原理
河南城建学院
《人工智能》实验报告
实验名称:
实现基于谓词逻辑的归结原理成绩:
—
专业班级:
—学号:
姓名:
实验日期:
2014年05月13日
实验器材:
•台装pc机°
一、实验目的
熟练掌握使用归结原理进行定理证明的过程,掌握基于谓词逻辑的归结过程中,子句变换过程、替换与合一算法、归结过程及简单归结策略等重要环节,进一步了解机器自动定理证明的实现过程。
二、实验要求
对于任意给定的一阶谓词逻辑所描述的定理,要求实现如下过程:
(1)谓词公式到子句集变换;
(2)替换与合一算法;
(3)在某简单归结策略下的归结。
三、实验步骤
步1设汁谓词公式及自居的存储结构,即内部表示。
注意对全称量词臥和
存在量词"X可采用其他符号代替;
步2实现谓词公式到子句集变换过程;
步3实现替换与合一算法;
实现某简单归结策略;4步.
步5设计输出,动态演示归结过程,可以以归结树的形式给出;
步6实现谓词逻辑中的归结过程,其中要调用替换与合一算法和归结策略。
四、代码
谓词公式到子句集变换的源代码:
#iiiclude
#iiiclude
#iiiclude
#iiiclude
usingnamespacestd:
//•些函数的定义
voidimtStrmg(strmg&im);〃初始化
stringdel_iiilchie(stringtenq))y/消去蕴涵符号
stringdec_neg_rand(strmgtemp);//减少否定符号的辖域
stringstandard-var(stringtemp);〃对变晟标准化
stringdel_exist$(stringtenip)^/消去存在量词
stringcon\rert_to_front(stringtemp);//化为前束形
stringcon\rert_to_and(stringtemp);//把母式化为合取范式
stringdel_all(striiigtemp);//消去全称量词
stringdel_aiid(striiigtemp);//消去连接符号合取%
stringchange_name(stnngtemp);〃更换变ht名称
//辅助函数定义
boolisAlbum(chartenip)^/是字母
stringdel_null_bracket(stringtemp)y/删除多余的括号
stringdel_blank(strmgtemp);//删除多余的空格
voidcheckLegal(stringtemp);〃检查合法性
chariiuniAfectChai(iiittemp);//数字显示为字符〃主函数
voidniain()
{
cout«求子句集九步法演示«endl;
system(colorOA);
//ongn=Q(x,y)%〜(P(y);
〃ongn=(@x)(P(y)>P);
//ongn=Y#x)y(x);
//ongn=〜((@x)x!
b(x));
//ongn=Yx!
y);
//ongn=p〜2(b));
stringorignjemp;
charconimand.conimand0,cominandl,command2.conimand3,comiiiand4,conimand5,cominand6.conimand7,comiiiand8,cominand9,conimaiidl0;
憫璟?
请输入(Y/y)初始化谓词演算公式vv已ndl:
cin»conimand;
if(conmiaiid=y||command=Y')lmtString(ongn);
else
exit(O);
憫瑾?
请输入(Y/y)消除空格«endl;cin»conimandO:
if(commaiidO=y||conimandO=Y){
//del_blank(origii);//undone
澗璜?
消除空格后是«endl
«orign«endl:
}
else
exit(O);
憫璜?
请输入(Y/y)消去蕴涵项«endl:
cin»conimandl;
if(commandl=y||command1=Y){
orign=del_mlclue(ongn);
憫璜?
消去蕴涵项后是«endl
«orign«endl:
}
else
exit(O);
憫瑾?
请输入(Y/y)减少否定符号的辖域vv诅dl:
cin»conimand2;
if(comiiiand2=*y*||command?
=Y)
{
do
{
temp=orign;
orign=dec_neg_rand(orign);
}while(temp!
=orign);
澗璜?
减少否定符号的辖域后是vvzdl
«ongn«endl;
}
else
exit(O);
制璟?
请输入(Y/y)对变量进行标准化vv已ndl;cin»conimand3;
if(comiiiand3=y||conunaiid3=Y)
{
ongii=standard_var(ongii);
憫璜?
对变量进行标准化后是«endl
«orign«endl:
}
else
exit(O);
憫璟?
请输入(Y/y)消去存在量词«endl;
cin»conimand4;
if(coiiuiiaiid4=y||command4=fY*)
{
ongii=del_exists(ongn);
憫璜?
消去存在量词后是(w=g(x)是•个Skolem函数)《endl«orign«endl;
}
else
exit(O);
憫瑾?
请输入(Y/y)化为前束形«endl:
cin»conimand5;
if(commaiid5=y||conmiand5==Y')
orign=com-ert_to_front(orign);憫璜?
化为前束形后是vvendl
«orign«endl;
}
else
exit(O);
n
憫璜?
请输入(Y/y)把母式化为合取方式《endl:
cin»conimand6;
if(conmiand6=y||coniniand6=
:
Y)
{
orign=com*ert_to_and(orign):
憫璜?
把母式化为合取方式后是
«endl
«orign«endl:
}
else
exit(O);
〃—
«endl;
消去全称量词(Y/y)请输入制璜?
cin»cominand7;
if(conmiand7=y||coniniand7=
:
Y)
orign=del_all(ongn);
憫璜?
消去全称量词后是«endl
«orign«endl:
}
else
exit(O);
JJ
憫瑾?
请输入(Y/y)消去连接符号《旳dl;cin»conimand8;
if(comiiiand8=y||commands=Y)
ongii=del_and(ongii);
憫璜?
消去连接符号后是«endl
«orign«endl;
}
else
exit(O);
JJ
力二=============================
憫璟?
请输入(Y/y)变量分离标准化«endl;
cin»conimand9;
if(comiiiand9=||conmiand9=Y)
{
orign=change_name(orign);
憫璜?
变量分离标准化后是(xl,x2,x3代替变虽x)«endl
«orign«endl:
}
else
exit(O);
cout«完毕«endl;
cout«(请输入Y/y)结束«endl:
do
{
}while(y=getchar()||T^getcharO);
exit(O);
}
voidimtStnng(strmg&im)
{
charconimanda,commandb;
«endl;涸璜?
请输入您所需要转换的谓词公式(Y/N)?
«eiidl;涸瑾?
需要查看输入帮助cin»conimanda;
if(comiiianda=T*||cominanda='y')
#,«endl全称量词为涸璜?
本例程规定输入时蕴涵符号为>,@,存在量词为,函数名请用•个字母)左右括号分别为吸取为?
取反为~,!
,合取为%,(、
«endl;
憫璜?
请输入(y/n)选择是否用户H定义<cin»conimandb;
if(coiimiandb='丫・conimandb=,y,)
cin»mi;
else
mi=(@x)(P(X)>((@y)(P(y)>P(f(x,y)))%^@y)(Q(x,y)>P(y))));
澗璜?
原始命题是wendl
«ini«endl:
}
>消去stringdel_inlclue(striiigtemp)//蕴涵项{
〜a!
b变为〃a>bcharc2inp[100]={\};
stringoutput;
intlength=temp.lengthO:
iiiti=O.right^bracket=O.falg=0:
stackstackl?
stack2,stack3:
strcpy(ctemp,temp.c_str());
wlule(ctemp[i]!
=*\0*&&i<=length-1)
{
stacklpush(cten^[i]);
ifC>'=ctemp[i十1])〃如果是a>b则用〜a!
b替代
{
falg=l;
if(isAlbum(ctemp[i]))//如果是字母则把ctenq)[i]弹出
{
stackl.pop();
stacklpush(^);
stacklpush(ctemp[i]);
stackl.pushC!
*);
i=i+1;
elseif(7=ctemp[i])
{
right_bracket++;
do
{
right_bracket—;
if(f=stackl.topO)
stack3.push(stackltop());
stackl.popO;
}while((right_bracket!
=0));stack3.push(stackltop());
stackl.popO;
st2ckl.push('〜‘);
wlule(!
stack3.einptyX))
{
stacklpush(stack3.topO);
stack3.pop();
}
stackl.pushC!
1);
}
i++;
}
wlule(!
stackleinpt^X))
{
stack?
pnsh(stackltopO);
stackl.popO;
}
wlule(!
stack2.einptyX))
{
output十=stack2.top();
stack2.pop();
lf(felg=1)returnoutput;elsereturntenq?
;
}
stringdec_neg_rand(stringtemp)//减少否定符号的辖域
{
charctemp[100],tempc:
stringoutput;
mtflag?
=0;
inti=O,left_bracket=Ojength=temp.leiigthO;stackstackl,stack?
:
queuequeue1;
strcpyXctemp,temp.c__str());//复制到字符数组中wlule(ctemp[i]!
=‘\0‘&&i<=length-1)
{
stacklpnsh(ctemp[i]);
否则什么都不做~如果是if(cteinp[i]=•-•)//{
charfb=ctemp[i+2];
if(ctemp[i+l]=(C)//如果是(,否则什么都不做{
if(fo=IIfo=wy/如果是全称量词
{
£lag2=l;
i卄;
stackl.popO;
stackl.push(ctemp[i]);
lf(fo=@)
stackl.pushC#*);
else
stackl.pushC@*);
stackl.push(ctemp[i+2]);
stacklpush(ctemp[i+3]);
stackl.pushCX1);
stackl.push(‘〜');
if(isAlbum(ctemp[i+4]))
stackl.push(ctemp[i+4]);
i=i+5;
}
else
i=i+4;
do
{
queue1.pnsh(temp[i]);
if(tenq)[i]='(')
left_bracket卄;
elseif(temp[i]=丁)
left_bracket—;
i卄;
}while(left_bracket!
=0&&left_bracket>=0);
queuelpushC)*);
while(!
queue1.emptyO)
{
tempc=queuel.frontO:
queuel.pop();
stacklpush(tempc);
}
}
}
}
i卄;
}
wlule(!
stackleinpty^))
{
stack?
push(stackl.topO);
stackl.pop();
wlule(!
stack2.已inpty())output十=stack2.top();stack2.pop();
}if(flag2=1)temp=output;
/車***************4(寒**4(拿*****尊*尊*草**"寒**********************專
charctemplflOO];stringoutput1;
stackstack11,stock?
2;
intfalgl=0;
inttimes=0:
intlength1=tenq).length(),inleftbackets=l,j=0;strcpy(ctempl.teinp.c_strO)ixvlule(ctempl[j]!
=r\0f&&j<=(lengthl-1)){
stackl1,push(ctempl[j]);
if(ctempl[j]==z)
if(ctempl[j-l]='(•严&&ctemplfj十2]!
='〜叫){
J=j+2;
stackl1.push('C);////〃//////////
while(mleftbackets!
=0&&mleftbackets>=0&×<=(length1・j)&&
times>=0)
{
stackl1push(cteinpl[j]);if(ctempl[j]=,C)mleftbackets十十;
elseif(ctempl[j]=')')mleftbackets—;
if(inleftbackets=1&&ctempl[j+l]=T&&ctempl[j+2]!
='@&&
Ctemplti+2〕M3
Egl£
stackl1.push(.y)yjws§ssckll.pusM%)stackll.push(■〜>?
stack!
1.Push(・(・)w//、AW
iRinleRbackasHH一ctempl=+l〕==・%•ctemplfj+2〕-ir・®&,&.
Ctemplti+2〕M3mgl4s£ll・push(w=§
stack!
1push(J八stackll.push(・〜)stack!
1.Push(・(・)w//O7
J2
JHJ+常
lf(£dglhh1)ssrc片11pushQx
stack11.Popo八smdcl1.pusho.)寺期b-lgsrackl1・push(・y)*、llt^^b=g
while(一srackl1cmptyo)
stack22.push(stackl1topO);stackll.pop();
wlule(!
stack?
2.emptyO)
{
output1+=stack22.topO;
stack22.pop0:
temp=output1;
charctemp3[100];
stringontput3;
intk=O,left_bracket3=1Jength3=teinp.lengthO;stackstack13,stack?
3;
intflag=O,bflag=0;
strcpy(ctemp3.temp.c_strO);//复制到字符数组中wliile(ctemp3[k]!
=‘\0‘&&k<=length3-l)
stackl3.push(ctemp3[k]);if(ctemp3[k]='〜)
if(ctemp3[k+l]=T)
if(ctemp3[k+2]=^)
flag=l;
stackl3.popO:
k=k+2;
wliile(left_bracket3!
=0&&left__bracket3>=0)
stackl3.pnsh(ctenq)3[k+1]);
if(ctemp3[k十1]=V)leftbracket3十+;
leftbracket3if(ctemp3[k+l]=T||ctemp3[k+l]=*%*)
bflag=1;
k++;
}
stackl3.pop();
}
}
}
k卄;
}
xvlule(!
stackl3.emptyO)
{
stack?
3.push(stack13.topO);
stackl3.pop0:
}
wlule(!
stack23.emptyO)
{
output3+=stack23.topO;
stack23.popO:
}
if(flag=1&&bflag=0)
temp=output3;
retiimtemp;
}
stringstandard^-ar(stringtemp)//对变晟标准化,简化,不考虑多层嵌套{
charctemp[100],des[10]={};
strcpyXctemp,tenip.c_str());
stackstackl,stack?
:
intl_bracket=l.folg=O.bracket=1;
inti=Oj=0;
stringoutput:
wlnle(ctemp[i]!
=*\0'&&ivtemp.length。
)
{
stackl.pnsh(ctenq)[i]);
if(ctemp[i]=||ctemp[i]=*#*)
{
stackl,push(ctemp[i+l]);
des[j]=ctempfi+1];
J卄;
stacklpush(ctemp[i+2]);
i=i+3;
stacklpush(ctemp[i]);
i++;
if(ctemp[i-l]==*(*)
{
wlule(ctemp[i]!
='\0'&&l_bracket!
=0){
if(ctemp[i]='(')
l__bracket++;
if(ctemp[i]=*(*&&ctemp[i+l]=9@')
{
des[j]=ctemp[i+2];
J卄;
if(cteinp[i+l]==•(*&&ctemp[i+2]=)
falg=l;
mtkk=1;
stackl.push(f);
stacklpush(ctemp[i+2]);
i=i+3;
if(ctemp[i]=y)ctemp[i]-w\stacklpush(ctemp[i]);st3ckl.push(')');stackl.push('C);
i=i+3;
wlule(kk!
=0)
{
if(ctemp[i]='(')
kk+-:
if(ctemp[i]=y)
kk-;
if(ctemp[i]=y)ctempfi]=*;
stacklpush(ctemp[i]);
i++;
}
}
stackl.push(ctemp[i]);
i++;
}
}
}
i++;
}wlule(!
stacklenipty^))
stack2.push(stackltop());
stackl.pop();
}
wlule(!
stack2.已inpty())
{
outputh-=stack?
.top();
stack2.pop();
}
lf(folg==1)
returnoutput;
else
retiirntenip;
}
stringdel_exists(stringtemp)//消去存在虽词
{
charctemp[100]Ainknow;
strcpy(ctemp,temp.c_str());
mtleft_brackets=0,i=O,falg=0;
queuequeue1;
stringoutput;
wlule(ctemp[i]!
='\0‘&&ivteinp.lengthO)
{
if(ctemp[i]='('&&ctemp[i+l]='拌)
{
falg=l;
unknow=ctemp[i+2];
i=i+4;
do
{
leftbrackets-H-;
if(ctemp[i]='(•)if(ctemp[i]=')')
left_brackets—;
queue1push('g');queuel.push('C);queuel.push('x');
queuel.pushC)*);
if(ctemp[i]!
=unknow)queue1.pu$h(ct