1、华 中 科 技 大 学人工智能与模式识别实验报告 院 系: 电子与信息工程系 班 级: 姓 名: 同 组 人: 指导老师: 电 话: 邮 箱: 日 期: 2013 年 12 月 24 日 1、 实验内容利用一阶谓词逻辑求解猴子摘香蕉问题:房内有一个猴子,一个箱子,天花板上挂了一串香蕉,其位置如图所示,猴子为了拿到香蕉,它必须把箱子搬到香蕉下面,然后再爬到箱子上。请定义必要的谓词,列出问题的初始化状态(即下图所示状态) ,目标状态(猴子拿到了香蕉,站在箱子上,箱子位于位置 b) 。 (附加:从初始状态到目标状态的谓词演算过程。 )2、 实验平台VC6.03、 实验分析1. 定义描述环境状态的谓词
2、。AT(x,w):x 在 t 处,个体域:xmonkey,wa,b,c,box;HOLD(x,t):x 手中拿着 t,个体域: tbox,banana;EMPTY(x):x 手中是空的;ON(t,y):t 在 y 处,个体域:yb,c,ceiling;CLEAR(y):y 上是空的;BOX(u):u 是箱子,个体域:ubox;BANANA(v):v 是香蕉,个体域:vbanana;2. 使用谓词、连结词、量词来表示环境状态。问题的初始状态可表示为:So:AT(monkey,a)EMPTY(monkey)ON(box,c)ON(banana,ceiling)CLEAR(b)BOX(box)BAN
3、ANA(banana)要达到的目标状态为:Sg:AT(monkey,box)HOLD(monkey,banana)ON(box,b)CLEAR(ceiling)CLEAR(c)BOX(box)BANANA(banana)3. 从初始状态到目标状态的转化, 猴子需要完成一系列操作, 定义操作类谓词表示其动作。WALK(m,n):猴子从 m 走到 n 处,个体域:m,na,b,c;CARRY(s,r):猴子在 r 处拿到 s,个体域:rc,ceiling, sbox,banana;CLIMB(u,b):猴子在 b 处爬上 u;这 3 个操作也可分别用条件和动作来表示。条件直接用谓词公式表示,是为完
4、成相应操作所必须具备的条件;当条件中的事实使其均为真时,则可激活操作规则,于是可执行该规则中的动作部分。动作通过前后状态的变化表示,即通过从动作前删除或增加谓词公式来描述动作后的状态。WALK(m,n):猴子从 m 走到 n 处条件:AT(monkey,m)动作: ),(nokeyAT增 加 :删 除 :CARRY(s,r):猴子在 r 处拿到 s条件:AT(monkey,r)EMPTY(monkey)ON(s,r)BOX(box)BANANA(banana)动作: )(),(,rCLEARsmonkeyHOLDNEMPTY增 加 :删 除 :CLIMB(u,b):猴子在 b 处爬上 u条件:
5、AT(monkey,b)HOLD(monkey,u)CLEAR(b)BOX(box)BANANA(banana)动作: ),()(),( ,cuONmonkeyEMPTYumonkeyATLEARL增 加 :删 除 :4. 按照行动计划, 一步步进行状态替换 , 直至目标状态。AT(monkey,a)EMPTY(monkey)ON(box,c)ON(banana,ceiling)CLEAR(b)BOX(box)BANANA(banana) ncmacWALK代 换用代 换用 ,),(AT(monkey,c)EMPTY(monkey)ON(box,c)ON(banana,ceiling)CLEA
6、R(b)BOX(box)BANANA(banana) rboxscboxCARY代 换用代 换用 ,),(AT(monkey,c)HOLD(monkey,box)ON(banana,ceiling)CLEAR(b)CLEAR(c)BOX(box)BANANA(banana) nbmcbWALK代 换用代 换用 ,),(AT(monkey,b)HOLD(monkey,box)ON(banana,ceiling)CLEAR(b)CLEAR(c)BOX(box)BANANA(banana) uboxCLIMB代 换用),(AT(monkey,box)EMPTY(monkey)ON(box,b)ON(
7、banana,ceiling)CLEAR(c)BOX(box)BANANA(banana) rceilngsbanceilgbanCARY 代 换用代 换用 ,),(AT(monkey,box)HOLD(monkey,banana)ON(box,b)CLEAR(ceiling)CLEAR(c)BOX(box)BANANA(banana)(目标得解)猴子行动的规则序列是:WALK(a,c)CARRY(c,box)WALK(c,b)CLIMB(box,b)CARRY(banana,ceiling)在上述过程中,我们应该注意,当猴子执行某一个操作之前,需要检查当前状态是否可使所要求的条件得到满足,即
8、证明当前状态是否蕴涵操作所要求的状态的过程。在行动过程中, 检查条件的满足性后才进行变量的代换。代入新条件后的新状态如果是目标状态,则问题解决;否则看是否满足下面的操作,如果不满足或即使满足却又回到了原来的状态,那么代入无效。4、 源代码#include struct Stateint monkey; /*-1:Monkey at A;0: Monkey at B;1:Monkey at C;*/int box; /*-1:box at A;0:box at B;1:box at C;*/int banana; /*Banana at B,Banana=0*/int monbox; /*-1:
9、 monkey on the box;1: monkey the box;*/;struct State States 150;char* routesave150;/*function monkeygoto,it makes the monkey goto the other place*/void monkeygoto(int b,int i) int a;a=b;if (a=-1)routesavei=“Monkey go to A“;Statesi+1=Statesi;Statesi+1.monkey=-1;else if(a=0)routesavei=“Monkey go to B“
10、;Statesi+1=Statesi;Statesi+1.monkey=0;else if(a=1)routesavei=“Monkey go to C“;Statesi+1=Statesi;Statesi+1.monkey=1;elseprintf(“parameter is wrong“);/*end function monkeyygoto*/*function movebox,the monkey move the box to the other place*/void movebox(int a,int i) int B;B=a;if(B=-1)routesavei=“monkey
11、 move box to A“;Statesi+1=Statesi;Statesi+1.monkey=-1;Statesi+1.box=-1;else if(B=0)routesavei = “monkey move box to B“;Statesi+1=Statesi;Statesi+1.monkey=0;Statesi+1.box=0;else if(B=1)routesavei = “monkey move box to C“;Statesi+1=Statesi;Statesi+1.monkey=1;Statesi+1.box=1;elseprintf(“parameter is wr
12、ong“);/*end function movebox*/*function climbonto,the monkey climb onto the box*/void climbonto(int i)routesavei=“Monkey climb onto the box“;Statesi+1=Statesi;Statesi+1.monbox=1;/*function climbdown,monkey climb down from the box*/void climbdown(int i) routesavei=“Monkey climb down from the box“;Sta
13、tesi+1=Statesi;Statesi+1.monbox=-1;/*function reach,if the monkey,box,and banana are at the same place,the monkey reach banana*/void reach(int i) routesavei=“Monkey reach the banana“;/*output the solution to the problem*/void showSolution(int i)int c;printf (“%s n“, “Result to problem:“);for(c=0; c=
14、150)printf(“%s n“, “steplength reached 150,have problem “);return;for (c=0; ci; c+) /*if the current state is same to previous,retrospect*/if(Statesc.monkey=Statesi.monkeyif(Statesi.monbox=1printf(“Press any key to continue n“);getchar();/*to save screen for user,press any key to continue*/return; j
15、=i+1; if(Statesi.monkey=0) if(Statesi.box=0)if(Statesi.monbox=-1)climbonto(i);reach(i+1);nextStep(j);/*monkeygoto(-1,i);nextStep(j);monkeygoto(0,i);nextStep(j);movebox(-1,i);nextStep(j);movebox(0,i);nextStep(j);*/elsereach(i+1);nextStep(j);/*climbdown(i);nextStep(j);*/else if(Statesi.box=1)/*monkeyg
16、oto(-1,i);nextStep(j);*/monkeygoto(1,i);nextStep(j);movebox(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);else /*box=-1*/monkeygoto(-1,i);nextStep(j);movebox(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);/*end if*/if(Statesi.monkey=-1) if(Statesi.box=-1)if(Statesi.monbox=-1) movebox(0,
17、i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);elseclimbdown(i);nextStep(j);movebox(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);else if(Statesi.box=0) monkeygoto(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);elsemonkeygoto(1,i);nextStep(j);movebox(0,i);nextStep(j);climbonto(i);re
18、ach(i+1);nextStep(j);/*end if*/if(Statesi.monkey=1)if (Statesi.box=1)if(Statesi.monbox=-1) movebox(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);elseclimbdown(i);nextStep(j);movebox(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j); else if(Statesi.box=-1) monkeygoto(-1,i);nextStep(j);movebox(0,i);nextStep(j);movebox(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);elsemonkeygoto(0,i);nextStep(j);movebox(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);/*end if*/*end nextStep*/int main()States0.monkey=-1;States0.box=1;States0.banana=0;States0.monbox=-1;nextStep(0);5、 实验截图