实现基于谓词逻辑得归结原理.doc

上传人:李司机 文档编号:1091225 上传时间:2022-06-23 格式:DOC 页数:23 大小:161KB
返回 下载 相关 举报
实现基于谓词逻辑得归结原理.doc_第1页
第1页 / 共23页
实现基于谓词逻辑得归结原理.doc_第2页
第2页 / 共23页
实现基于谓词逻辑得归结原理.doc_第3页
第3页 / 共23页
实现基于谓词逻辑得归结原理.doc_第4页
第4页 / 共23页
实现基于谓词逻辑得归结原理.doc_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《实现基于谓词逻辑得归结原理.doc》由会员分享,可在线阅读,更多相关《实现基于谓词逻辑得归结原理.doc(23页珍藏版)》请在三一办公上搜索。

1、某某城建学院人工智能实验报告实验名称:实现基于谓词逻辑的归结原理成绩:_专业班级:学 号:某某:实验日期 :实验器材: 一台装PC机。1、 实验目的熟练掌握使用归结原理进展定理证明的过程,掌握基于谓词逻辑的归结过程中,子句变换过程、替换与合一算法、归结过程与简单归结策略等重要环节,进一步了解机器自动定理证明的实现过程。2、 实验要求对于任意给定的一阶谓词逻辑所描述的定理,要某某现如下过程:(1) 谓词公式到子句集变换;(2) 替换与合一算法;(3) 在某简单归结策略下的归结。3、 实验步骤步1 设计谓词公式与自居的存储结构,即内部表示。注意对全称量词x和存在量词$x可采用其他符号代替;步2 实

2、现谓词公式到子句集变换过程;步3 实现替换与合一算法;步4 实现某简单归结策略;步5 设计输出,动态演示归结过程,可以以归结树的形式给出;步6 实现谓词逻辑中的归结过程,其中要调用替换与合一算法和归结策略。4、 代码谓词公式到子句集变换的源代码:#include#include#include#includeusing namespace std;/一些函数的定义void initString(string &ini);/初始化string del_inlclue(string temp);/消去蕴涵符号string dec_neg_rand(string temp);/减少否认符号的辖域st

3、ring standard_var(string temp);/对变量标准化string del_exists(string temp);/消去存在量词string convert_to_front(string temp);/化为前束形string convert_to_and(string temp);/把母式化为合取X式string del_all(string temp);/消去全称量词string del_and(string temp);/消去连接符号合取%string change_name(string temp);/更换变量名称/辅助函数定义bool isAlbum(char

4、 temp);/是字母string del_null_bracket(string temp);/删除多余的括号string del_blank(string temp);/删除多余的空格void checkLegal(string temp);/检查合法性char numAfectChar(int temp);/数字显示为字符/主函数void main()cout-求子句集九步法演示-P);/orign = (#x)y(x);/orign = (x)x!b(x);/orign = (x!y);/orign = (a(b);string orign,temp;char mand,mand0,m

5、and1,mand2,mand3,mand4,mand5,mand6,mand7,mand8,mand9,mand10;/=cout请输入(Y/y)初始化谓词演算公式mand;if(mand = y | mand = Y)initString(orign);elseexit(0);/=cout请输入(Y/y)消除空格mand0;if(mand0 = y | mand0 = Y)/del_blank(orign);/undonecout消除空格后是endlorignendl;elseexit(0);/=cout请输入(Y/y)消去蕴涵项mand1;if(mand1 = y | mand1 = Y

6、)orign =del_inlclue(orign);cout消去蕴涵项后是endlorignendl;elseexit(0);/=cout请输入(Y/y)减少否认符号的辖域mand2;if(mand2 = y | mand2 = Y)dotemp = orign;orign = dec_neg_rand(orign);while(temp != orign);cout减少否认符号的辖域后是endlorignendl;elseexit(0);/=cout请输入(Y/y)对变量进展标准化mand3;if(mand3 = y | mand3 = Y)orign = standard_var(ori

7、gn);cout对变量进展标准化后是endlorignendl;elseexit(0);/=cout请输入(Y/y)消去存在量词mand4;if(mand4 = y | mand4 = Y)orign = del_exists(orign);cout消去存在量词后是(w = g(x)是一个Skolem函数)endlorignendl;elseexit(0);/=cout请输入(Y/y)化为前束形mand5;if(mand5 = y | mand5= Y)orign = convert_to_front(orign);cout化为前束形后是endlorignendl;elseexit(0);/=

8、cout请输入(Y/y)把母式化为合取方式mand6;if(mand6 = y | mand6 = Y)orign = convert_to_and(orign);cout把母式化为合取方式后是endlorignendl;elseexit(0);/=cout请输入(Y/y)消去全称量词mand7;if(mand7 = y | mand7 = Y)orign= del_all(orign);cout消去全称量词后是endlorignendl;elseexit(0);/=cout请输入(Y/y)消去连接符号mand8;if(mand8 = y | mand8 = Y)orign = del_and

9、(orign);cout消去连接符号后是endlorignendl;elseexit(0);/=cout请输入(Y/y)变量别离标准化mand9;if(mand9 = y | mand9 = Y)orign = change_name(orign);cout变量别离标准化后是(x1,x2,x3代替变量x)endlorignendl;elseexit(0);/=cout-完毕-endl;cout(请输入Y/y)完毕endl;dowhile(y = getchar() | Y=getchar();exit(0);void initString(string &ini)char manda,mand

10、b;cout请输入您所需要转换的谓词公式endl;cout需要查看输入帮助(Y/N)? manda;if(manda = Y | manda = y)cout,全称量词为,存在量词为#,endl取反为,吸取为!,合取为%,左右括号分别为( 、 ),函数名请用一个字母endl;cout请输入(y/n)选择是否用户自定义mandb;if(mandb =Y| mandb=y)cinini;elseini = (x)(P(x)(y)(P(y)P(f(x, y)%(y)(Q(x,y)P(y);cout原始命题是endlini蕴涵项/ab变为a!bchar ctemp100=;string output;

11、int length = temp.length();int i = 0,right_bracket = 0,falg= 0;stack stack1,stack2,stack3;strcpy(ctemp,temp.c_str();while(ctempi != 0 & i = ctempi+1)/如果是ab如此用a!b替代falg = 1;if(isAlbum(ctempi)/如果是字母如此把ctempi弹出stack1.pop();stack1.push();stack1.push(ctempi);stack1.push(!);i = i + 1;else if() = ctempi)ri

12、ght_bracket+;doif( = 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();sta

13、ck1.pop();while(!stack2.empty()output += stack2.top();stack2.pop();if(falg = 1)return output;elsereturn temp;string dec_neg_rand(string temp)/减少否认符号的辖域char ctemp100,tempc;string output;int flag2 = 0;int i = 0,left_bracket = 0,length = temp.length();stack stack1,stack2;queue queue1;strcpy(ctemp,temp.

14、c_str();/复制到字符数组中while(ctempi != 0 & i =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;/*/char ctemp1100;st

15、ring output1;stack stack11,stack22;int falg1 = 0;int times = 0;int length1 = temp.length(),inleftbackets = 1,j = 0;strcpy(ctemp1,temp.c_str();while(ctemp1j != 0 & j =0 & times = 0)stack11.push(ctemp1j);if(ctemp1j = ()inleftbackets +;else if(ctemp1j = )inleftbackets -;if(inleftbackets = 1 & ctemp1j+1

16、 = ! & ctemp1j+2 != & ctemp1j+2 != #)falg1 =1;stack11.push();/stack11.push(%);stack11.push();stack11.push();/j = j+1;if(inleftbackets = 1 & ctemp1j+1 = % & ctemp1j+2 != & ctemp1j+2 != #)falg1 =1;stack11.push();/stack11.push(!);stack11.push();stack11.push();/j = j+1;j = j +1;if(falg1 = 1)stack11.push

17、();stack11.pop();stack11.push();/此处有bugstack11.push();/此处有bugj +;while(!stack11.empty()stack22.push(stack11.top();stack11.pop();while(!stack22.empty()output1 += stack22.top();stack22.pop();if(falg1 = 1)temp = output1;/*/char ctemp3100;string output3;int k = 0,left_bracket3 = 1,length3 = temp.length(

18、);stack stack13,stack23;int flag = 0,bflag = 0;strcpy(ctemp3,temp.c_str();/复制到字符数组中while(ctemp3k != 0 & k =0)stack13.push(ctemp3k+1);if(ctemp3k+1 = ()left_bracket3 +;if(ctemp3k+1 = )left_bracket3 -;if(ctemp3k+1 = ! | ctemp3k+1 = %)bflag = 1;k +;stack13.pop();k +;while(!stack13.empty()stack23.push(st

19、ack13.top();stack13.pop();while(!stack23.empty()output3 += stack23.top();stack23.pop();if(flag = 1 & bflag = 0)temp = output3;return temp;string standard_var(string temp)/对变量标准化,简化,不考虑多层嵌套char ctemp100,des10= ;strcpy(ctemp,temp.c_str();stack stack1,stack2;int l_bracket = 1,falg = 0,bracket = 1;int i

20、 = 0,j = 0;string output;while(ctempi != 0 & i temp.length()stack1.push(ctempi);if(ctempi = | ctempi = #)stack1.push(ctempi+1);desj = ctempi+1;j+;stack1.push(ctempi+2);i = i + 3;stack1.push(ctempi);i+;if(ctempi-1 = ()while(ctempi != 0 & l_bracket != 0)if(ctempi = ()l_bracket +;if(ctempi = )l_bracket

21、 -;if(ctempi = ( & ctempi+1 = )desj = ctempi+2;j+;if(ctempi+1 = ( & ctempi+2 = # )falg = 1;int kk = 1;stack1.push(ctempi);stack1.push();stack1.push(ctempi+2);i = i+3;if(ctempi = y)ctempi =w;stack1.push(ctempi);stack1.push();stack1.push();i = i+3;while(kk != 0)if(ctempi=()kk+;if(ctempi =)kk-;if(ctemp

22、i = y)ctempi =w;stack1.push(ctempi);i+;stack1.push(ctempi);i +;i +;while(!stack1.empty()stack2.push(stack1.top();stack1.pop();while(!stack2.empty()output += stack2.top();stack2.pop();if(falg = 1)return output;elsereturn temp;string del_exists(string temp)/消去存在量词char ctemp100,unknow;strcpy(ctemp,temp

23、.c_str();int left_brackets = 0,i = 0,falg = 0;queue queue1;string output;while(ctempi != 0 & i temp.length()if(ctempi =( & ctempi+1 =#)falg = 1;unknow = ctempi+2;i = i+4;doif(ctempi = ()left_brackets +;if(ctempi = )left_brackets -;if(ctempi = unknow)queue1.push(g);queue1.push();queue1.push(x);queue1

24、.push();if(ctempi != unknow)queue1.push(ctempi);i+;while(left_brackets != 0);queue1.push(ctempi);i+;while(!queue1.empty()output+= queue1.front();queue1.pop();if(falg = 1)return output;elsereturn temp;string convert_to_front(string temp)/化为前束形char ctemp100;strcpy(ctemp,temp.c_str();int i = 0;string o

25、ut_var = ,output = ;while(ctempi != 0 & i temp.length()if(ctempi = ( & ctempi+1 = )out_var = out_var + ctempi ;/()out_var = out_var + ctempi+1 ;out_var = out_var +ctempi+2;out_var = out_var +ctempi+3;i = i + 4;output = output + ctempi;i+;output = out_var + output;return output;string convert_to_and(

26、string temp)/把母式化为合取X式 ,Q/A?temp = (x)(y)(P(x)!(P(y)!P(f(x,y)%(P(x)!Q(x,g(x)%(P(x)!(P(g(x);return temp;string del_all(string temp)/消去全称量词char ctemp100;strcpy(ctemp,temp.c_str();int i = 0,flag = 0;string output = ;while(ctempi != 0 & i temp.length()if(ctempi = ( & ctempi+1 = )i = i + 4;flag = 1;elseo

27、utput = output + ctempi;i +;return output;string del_and(string temp)/消去连接符号合取%char ctemp100;strcpy(ctemp,temp.c_str();int i = 0,flag = 0;string output = ;while(ctempi != 0 & i temp.length()if(ctempi = % )ctempi = n;output = output +ctempi;i+;return output;string change_name(string temp)/更换变量名称char

28、ctemp100;strcpy(ctemp,temp.c_str();string output = ;int i = 0,j = 0,falg = 0;while(ctempi != 0 & i temp.length()falg+;while(n != ctempi & i temp.length()if(x = ctempi)output = output + ctempi ;output = output + numAfectChar(falg);elseoutput = output + ctempi ;i+;output = output + ctempi ;i +;return

29、output;bool isAlbum(char temp)if(temp = A | temp = a)return true;return false;char numAfectChar(int temp)/数字显示为字符int t;switch (temp)case 1:t = 1;break;case 2:t = 2;break;case 3:t = 3;break;case 4:t = 4;break;default:t = 89;break;return t;5、 结果分析(1) 非用户自定义时:6、 心得体会通过这次实验,熟悉了谓词公式转换成子句集的步骤,理解消解谓词公式化为子句集规如此,能把任意谓词公式转换成子句集。对子句集进展消解推理,得到相应的结论。为了对含有变量的字句使用消解规如此,必须找到一个置换,作用于父辈字句使其含有互补文字。消解两个字句时可能有一个以上的消解式,不过,在任何情况下最多有有限个消解式。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号