猴子摘香蕉实验报告(含代码)

更新时间:2024-05-30 00:30:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

华 中 科 技 大 学

《人工智能与模式识别》

院 系:班 级:姓 名:同 组 人:指导老师:电 话:邮 箱:实 验 报 告

电子与信息工程系

日 期: 2013年12月24日

一、实验内容

利用一阶谓词逻辑求解猴子摘香蕉问题:房内有一个猴子,一个箱子,天花板上挂了一串香蕉,其位置如图所示,猴子为了拿到香蕉,它必须把箱子搬到香蕉下面,然后再爬到箱子上。请定义必要的谓词,列出问题的初始化状态(即下图所示状态),目标状态(猴子拿到了香蕉,站在箱子上,箱子位于位置b)。(附加:从初始状态到目标状态的谓词演算过程。)

二、实验平台

VC6.0

三、实验分析

1. 定义描述环境状态的谓词。

AT(x,w):x在t处,个体域:x?{monkey},w?{a,b,c,box}; HOLD(x,t):x手中拿着t,个体域:t?{box,banana}; EMPTY(x):x手中是空的;

ON(t,y):t在y处,个体域:y?{b,c,ceiling}; CLEAR(y):y上是空的;

BOX(u):u是箱子,个体域:u?{box}; BANANA(v):v是香蕉,个体域:v?{banana}; 2. 使用谓词、连结词、量词来表示环境状态。 问题的初始状态可表示为:

So:AT(monkey,a)?EMPTY(monkey)?ON(box,c)?ON(banana,ceiling)?CLEAR(b)?BOX(box)? BANANA(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,n?{a,b,c};

CARRY(s,r):猴子在r处拿到s,个体域:r?{c,ceiling},s?{box,banana}; CLIMB(u,b):猴子在b处爬上u;

这3个操作也可分别用条件和动作来表示。条件直接用谓词公式表示,是为完成相应操作所必须具备的条件;当条件中的事实使其均为真时,则可激活操作规则,于是可执行该规则中的动作部分。动作通过前后状态的变化表示,即通过从动作前删除或增加谓词公式来描述动作后的状态。

WALK(m,n):猴子从m走到n处 条件:AT(monkey,m)

动作:??删除:AT(monkey,m)?增加:AT(monkey,n)

CARRY(s,r):猴子在r处拿到s

条件:AT(monkey,r)?EMPTY(monkey)?ON(s,r)?BOX(box)?BANANA(banana)

动作:??删除:EMPTY(monkey)?ON(s,r)?增加:HOLD(monkey,s)?CLEAR(r)

CLIMB(u,b):猴子在b处爬上u

条件:AT(monkey,b)?HOLD(monkey,u)?CLEAR(b)?BOX(box)?BANANA(banana)

动作:??删除:AT(monkey,b)?HOLD(monkey,u)?CLEAR(c)?增加:AT(monkey,u)EMPTY(monkey)?ON(u,c)

4. 按照行动计划, 一步步进行状态替换, 直至目标状态。

AT(monkey,a)?EMPTY(monkey)?ON(box,c)?ON(banana,ceiling)?CLEAR(b)?BOX(box)? BANANA(banana)

?WALK(a,c)用a代换m,用c代换n

AT(monkey,c)?EMPTY(monkey)?ON(box,c)?ON(banana,ceiling)?CLEAR(b)?BOX(box)? BANANA(banana)

?CARRY(c,box)用c代换s,用box代换r

AT(monkey,c)?HOLD(monkey,box)?ON(banana,ceiling)?CLEAR(b)?CLEAR(c)?BOX(box)?

BANANA(banana)

?WALK(c,b)用c代换m,用b代换n

AT(monkey,b)?HOLD(monkey,box)?ON(banana,ceiling)?CLEAR(b)?CLEAR(c)?BOX(box)?

BANANA(banana)

?CLIMB(box,b)用box代换u

AT(monkey,box)?EMPTY(monkey)?ON(box,b)?ON(banana,ceiling)?CLEAR(c)?BOX(box)?

BANANA(banana)

?CARRY(banana,ceiling)用banana代换s,用ceiling代换r

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)

在上述过程中,我们应该注意,当猴子执行某一个操作之前,需要检查当前状态是否可使所要求的条件得到满足,即证明当前状态是否蕴涵操作所要求的状态的过程。在行动过程中, 检查条件的满足性后才进行变量的代换。代入新条件后的新状态如果是目标状态,则问题解决;否则看是否满足下面的操作,如果不满足或即使满足却又回到了原来的状态,那么代入无效。

四、源代码

#include struct State {

int 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: monkey on the box;1: monkey the box;*/

};

struct State States [150]; char* routesave[150];

/*function monkeygoto,it makes the monkey goto the other place*/ void monkeygoto(int b,int i) {

int a; a=b; if (a==-1) { }

else if(a==0) { }

else if(a==1) { }

routesave[i]=\States[i+1]=States[i]; States[i+1].monkey=-1;

routesave[i]=\States[i+1]=States[i]; States[i+1].monkey=0;

routesave[i]=\States[i+1]=States[i]; States[i+1].monkey=1;

else

{ }

/*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) { }

else if(B==0) { }

else if(B==1)

printf(\

}

routesave[i]=\States[i+1]=States[i]; States[i+1].monkey=-1; States[i+1].box=-1;

routesave[i] = \States[i+1]=States[i]; States[i+1].monkey=0; States[i+1].box=0;

}

{ } else { }

printf(\

routesave[i] = \States[i+1]=States[i]; States[i+1].monkey=1; States[i+1].box=1;

/*end function movebox*/

/*function climbonto,the monkey climb onto the box*/ void climbonto(int i) { }

routesave[i]=\States[i+1]=States[i]; States[i+1].monbox=1;

/*function climbdown,monkey climb down from the box*/ void climbdown(int i) {

routesave[i]=\States[i+1]=States[i];

}

States[i+1].monbox=-1;

/*function reach,if the monkey,box,and banana are at the same place,the monkey reach banana*/ void reach(int i) { }

routesave[i]=\

/*output the solution to the problem*/ void showSolution(int i) { int c;

printf (\ for(c=0; c

printf (\

}

printf(\}

/*perform next step*/ void nextStep(int i) {

int c;

int j; if(i>=150) {

printf(\ \\n\

return; }

for (c=0; c

{

if(States[c].monkey==States[i].monkey&&States[c].box==States[i].box&&States[c].banana==States[i].banana&&States[c].monbox==States[i].monbox) {

return;

} }

if(States[i].monbox==1&&States[i].monkey==0&&States[i].banana==0&&States[i].box==0) {

} j=i+1;

if(States[i].monkey==0) {

if(States[i].box==0) {

if(States[i].monbox==-1) {

showSolution(i);

printf(\

getchar();/*to save screen for user,press any key to continue*/ return;

} else {

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);*/

reach(i+1); nextStep(j); /*climbdown(i); nextStep(j);*/

}

else if(States[i].box==1) {

/*monkeygoto(-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(States[i].monkey==-1)

{

if(States[i].box==-1) {

if(States[i].monbox==-1) { } else {

climbdown(i);

movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j);

}

}

}

nextStep(j); movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j);

else if(States[i].box==0) { } else { }

monkeygoto(1,i); nextStep(j); movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j); monkeygoto(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j);

/*end if*/

if(States[i].monkey==1)

{

if (States[i].box==1) { }

else if(States[i].box==-1) {

monkeygoto(-1,i); nextStep(j); movebox(0,i); nextStep(j); movebox(0,i); nextStep(j); climbonto(i);

if(States[i].monbox==-1) { } else { }

climbdown(i); nextStep(j); movebox(0,i); nextStep(j); climbonto(i); reach(i+1);

nextStep(j); movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j);

} else { }

reach(i+1); nextStep(j);

monkeygoto(0,i); nextStep(j); movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j);

}

/*end if*/

}/*end nextStep*/ int main() {

States[0].monkey=-1; States[0].box=1; States[0].banana=0; States[0].monbox=-1; nextStep(0); }

五、实验截图

本文来源:https://www.bwwdw.com/article/wec6.html

Top