实现基于谓词逻辑的归结原理.docx
- 文档编号:4750723
- 上传时间:2022-12-08
- 格式:DOCX
- 页数:33
- 大小:61.09KB
实现基于谓词逻辑的归结原理.docx
《实现基于谓词逻辑的归结原理.docx》由会员分享,可在线阅读,更多相关《实现基于谓词逻辑的归结原理.docx(33页珍藏版)》请在冰豆网上搜索。
实现基于谓词逻辑的归结原理
河南城建学院
《人工智能》实验报告
实验名称:
实现基于谓词逻辑的归结原理
成绩:
____
专业班级:
学号:
姓名:
实验日期:
2014年05月13日
实验器材:
一台装PC机。
一、实验目的
熟练掌握使用归结原理进行定理证明的过程,掌握基于谓词逻辑的归结过程中,子句变换过程、替换与合一算法、归结过程及简单归结策略等重要环节,进一步了解机器自动定理证明的实现过程。
二、实验要求
对于任意给定的一阶谓词逻辑所描述的定理,要求实现如下过程:
(1)谓词公式到子句集变换;
(2)替换与合一算法;
(3)在某简单归结策略下的归结。
三、实验步骤
步1设计谓词公式及自居的存储结构,即内部表示。
注意对全称量词?
x和存在量词?
x可采用其他符号代替;
步2实现谓词公式到子句集变换过程;
步3实现替换与合一算法;
实现某简单归结策略;4步.
步5设计输出,动态演示归结过程,可以以归结树的形式给出;
步6实现谓词逻辑中的归结过程,其中要调用替换与合一算法和归结策略。
四、代码
谓词公式到子句集变换的源代码:
#include
#include
#include
#include
usingnamespacestd;
//一些函数的定义
voidinitString(string&ini);//初始化
stringdel_inlclue(stringtemp);//消去蕴涵符号
stringdec_neg_rand(stringtemp);//减少否定符号的辖域
stringstandard_var(stringtemp);//对变量标准化
stringdel_exists(stringtemp);//消去存在量词
stringconvert_to_front(stringtemp);//化为前束形
stringconvert_to_and(stringtemp);//把母式化为合取范式
stringdel_all(stringtemp);//消去全称量词
stringdel_and(stringtemp);//消去连接符号合取%
stringchange_name(stringtemp);//更换变量名称
//辅助函数定义
boolisAlbum(chartemp);//是字母
stringdel_null_bracket(stringtemp);//删除多余的括号
stringdel_blank(stringtemp);//删除多余的空格
voidcheckLegal(stringtemp);//检查合法性
charnumAfectChar(inttemp);//数字显示为字符
//主函数
voidmain()
{
cout<<------------------求子句集九步法演示-----------------------< system(color0A); //orign=Q(x,y)%~(P(y); //orign=(@x)(P(y)>P); //orign=~(#x)y(x); //orign=~((@x)x! b(x)); //orign=~(x! y); //orign=~(~a(b)); stringorign,temp; charcommand,command0,command1,command2,command3,command4,command5, command6,command7,command8,command9,command10; //================================================================= ============ 潣瑵? 请输入(Y/y)初始化谓词演算公式< cin>>command; if(command=='y'||command=='Y') initString(orign); else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)消除空格< cin>>command0; if(command0=='y'||command0=='Y') { //del_blank(orign);//undone 潣瑵? 消除空格后是< < } else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)消去蕴涵项< cin>>command1; if(command1=='y'||command1=='Y') { orign=del_inlclue(orign); 潣瑵? 消去蕴涵项后是< < } else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)减少否定符号的辖域< cin>>command2; if(command2=='y'||command2=='Y') { do { temp=orign; orign=dec_neg_rand(orign); }while(temp! =orign); 潣瑵? 减少否定符号的辖域后是< < } else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)对变量进行标准化< cin>>command3; if(command3=='y'||command3=='Y') { orign=standard_var(orign); 潣瑵? 对变量进行标准化后是< < } else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)消去存在量词< cin>>command4; if(command4=='y'||command4=='Y') { orign=del_exists(orign); 潣瑵? 消去存在量词后是(w=g(x)是一个Skolem函数)< < } else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)化为前束形< cin>>command5; if(command5=='y'||command5=='Y') { orign=convert_to_front(orign); 潣瑵? 化为前束形后是< < } else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)把母式化为合取方式< cin>>command6; if(command6=='y'||command6=='Y') { orign=convert_to_and(orign); 潣瑵? 把母式化为合取方式后是< < } else exit(0); //================================================================= ============ < 消去全称量词(Y/y)请输入潣瑵? cin>>command7; if(command7=='y'||command7=='Y') { orign=del_all(orign); 潣瑵? 消去全称量词后是< < } else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)消去连接符号< cin>>command8; if(command8=='y'||command8=='Y') { orign=del_and(orign); 潣瑵? 消去连接符号后是< < } else exit(0); //================================================================= ============ 潣瑵? 请输入(Y/y)变量分离标准化< cin>>command9; if(command9=='y'||command9=='Y') { orign=change_name(orign); 潣瑵? 变量分离标准化后是(x1,x2,x3代替变量x)< < } else exit(0); //============================================================================ cout<<-------------------------完毕-----------------------------------< cout<<(请输入Y/y)结束< do { }while('y'==getchar()||'Y'==getchar()); exit(0); } voidinitString(string&ini) { charcommanda,commandb; < 请输入您所需要转换的谓词公式(Y/N)? < 需要查看输入帮助cin>>commanda; if(commanda=='Y'||commanda=='y') #,< 本例程规定输入时蕴涵符号为>,@,存在量词为,函数名请用一个字母)左右括号分别为吸取为? 取反为~,! 合取为%,(、 < 潣瑵? 请输入(y/n)选择是否用户自定义< cin>>commandb; if(commandb=='Y'||commandb=='y') cin>>ini; else ini=(@x)(P(x)>((@y)(P(y)>P(f(x,y)))%~(@y)(Q(x,y)>P(y)))); 潣瑵? 原始命题是< < } >消去stringdel_inlclue(stringtemp)//蕴涵项{ ~a! b变为//a>bcharctemp[100]={\}; stringoutput; intlength=temp.length(); inti=0,right_bracket=0,falg=0; stack strcpy(ctemp,temp.c_str()); while(ctemp[i]! ='\0'&&i<=length-1) { stack1.push(ctemp[i]); if('>'==ctemp[i+1])//如果是a>b则用~a! b替代 { falg=1; if(isAlbum(ctemp[i]))//如果是字母则把ctemp[i]弹出 { stack1.pop(); stack1.push('~'); stack1.push(ctemp[i]); stack1.push('! '); i=i+1; } elseif(')'==ctemp[i]) { right_bracket++; do { if('('==stack1.top()) right_bracket--; stack3.push(stack1.top()); stack1.pop(); }while((right_bracket! =0)); stack3.push(stack1.top()); stack1.pop(); stack1.push('~'); while(! stack3.empty()) { stack1.push(stack3.top()); stack3.pop(); } stack1.push('! '); i=i+1; } } i++; } while(! stack1.empty()) { stack2.push(stack1.top()); stack1.pop(); } while(! stack2.empty()) { output+=stack2.top(); stack2.pop(); } if(falg==1) returnoutput; else returntemp; } stringdec_neg_rand(stringtemp)//减少否定符号的辖域 { charctemp[100],tempc; stringoutput; intflag2=0; inti=0,left_bracket=0,length=temp.length(); stack queue strcpy(ctemp,temp.c_str());//复制到字符数组中 while(ctemp[i]! ='\0'&&i<=length-1) { stack1.push(ctemp[i]); 否则什么都不做~如果是if(ctemp[i]=='~')// { charfo=ctemp[i+2]; if(ctemp[i+1]=='(')//如果是(,否则什么都不做 { if(fo=='@'||fo=='#')//如果是全称量词 { flag2=1; i++; stack1.pop(); stack1.push(ctemp[i]); if(fo=='@') stack1.push('#'); else stack1.push('@'); stack1.push(ctemp[i+2]); stack1.push(ctemp[i+3]); stack1.push('('); stack1.push('~'); if(isAlbum(ctemp[i+4])) { stack1.push(ctemp[i+4]); i=i+5; } else i=i+4; do { queue1.push(temp[i]); if(temp[i]=='(') left_bracket++; elseif(temp[i]==')') left_bracket--; i++; }while(left_bracket! =0&&left_bracket>=0); queue1.push(')'); while(! queue1.empty()) { tempc=queue1.front(); queue1.pop(); stack1.push(tempc); } } } } i++; } while(! stack1.empty()) { stack2.push(stack1.top()); stack1.pop(); } while(! stack2.empty()) { output+=stack2.top(); stack2.pop(); } if(flag2==1) temp=output; /************************************************************/ charctemp1[100]; stringoutput1; stack intfalg1=0; inttimes=0; intlength1=temp.length(),inleftbackets=1,j=0; strcpy(ctemp1,temp.c_str()); while(ctemp1[j]! ='\0'&&j<=(length1-1)) { stack11.push(ctemp1[j]); if(ctemp1[j]=='~') { if(ctemp1[j+1]=='('/*&&ctemp1[j+2]! ='~'*/) { j=j+2; stack11.push('(');//////////////// while(inleftbackets! =0&&inleftbackets>=0&×<=(length1-j)&& times>=0) { stack11.push(ctemp1[j]); if(ctemp1[j]=='(') inleftbackets++; elseif(ctemp1[j]==')') inleftbackets--; if(inleftbackets==1&&ctemp1[j+1]=='! '&&ctemp1[j+2]! ='@'&& ctemp1[j+2]! ='#') { falg1=1; stack11.push(')');////////// stack11.push('%'); stack11.push('~'); stack11.push('(');////////// j=j+1; } if(inleftbackets==1&&ctemp1[j+1]=='%'&&ctemp1[j+2]! ='@'&& ctemp1[j+2]! ='#') { falg1=1; stack11.push(')');////////// stack11.push('! '); stack11.push('~'); stack11.push('(');////////// j=j+1; } j=j+1; } if(falg1==1) stack11.push(')'); stack11.pop(); stack11.push(')');//此处有bug stack11.push(')');//此处有bug } } j++; } while(! stack11.empty()) { stack22.push(stack11.top()); stack11.pop(); } while(! stack22.empty()) { output1+=stack22.top(); stack22.pop(); } if(falg1==1) temp=output1; /************************************************************/ charctemp3[100]; stringoutput3; intk=0,left_bracket3=1,length3=temp.length(); stack intflag=0,bflag=0; strcpy(ctemp3,temp.c_str());//复制到字符数组中 while(ctemp3[k]! ='\0'&&k<=length3-1) { stack13.push(ctemp3[k]); if(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实现 基于 谓词 逻辑 归结 原理