1、华 中 科 技 大 学人工智能与模式识别实验报告 院 系: 电子与信息工程系 班 级: 电信中英班 1101 姓 名: 何宇坤 学 号: U201115729 指导老师: 刘澍 电 话: 13058153519 邮 箱: 日 期: 2015 年 1 月 17 日 实验一1、实验内容利用一阶谓词逻辑求解猴子摘香蕉问题:房内有一个猴子,一个箱子,天花板上挂了一串香蕉,其位置如图所示,猴子为了拿到香蕉,它必须把箱子搬到香蕉下面,然后再爬到箱子上。请定义必要的谓词,列出问题的初始化状态(即下图所示状态),目标状态(猴子拿到了香蕉,站在箱子上,箱子位于位置 b)。(附加:从初始状态到目标状态的谓词演算
2、过程。)2、实验平台VC6.03、实验分析1. 定义描述环境状态的谓词。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
3、,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,na,b,c ;CARRY(s,r):猴子在 r 处拿到 s,个体域:rc,ceiling ,sbox,banana ;CLIMB(u,b):猴子在 b
4、 处爬上 u;这 3 个操作也可分别用条件和动作来表示。条件直接用谓词公式表示,是为完成相应操作所必须具备的条件;当条件中的事实使其均为真时,则可激活操作规则,于是可执行该规则中的动作部分。动作通过前后状态的变化表示,即通过从动作前删除或增加谓词公式来描述动作后的状态。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)动作: )(),(,rCLEARsmonkeyH
5、OLDNEMPTY增 加 :删 除 :CLIMB(u,b):猴子在 b 处爬上 u条件:AT(monkey,b)HOLD(monkey,u)CLEAR(b) BOX(box)BANANA(banana)动作: ),()(),( ,cuONmonkeyEMPTYumonkeyATCLEARHLDb增 加 :删 除 :4. 按照行动计划, 一步步进行状态替换, 直至目标状态。AT(monkey,a)EMPTY(monkey)ON(box,c)ON(banana,ceiling)CLEAR(b)BOX(box)BANANA(banana) ncmacWALK代 换用代 换用 ,),(AT(monke
6、y,c)EMPTY(monkey)ON(box,c)ON(banana,ceiling)CLEAR(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) uboxCLI
7、MB代 换用),(AT(monkey,box)EMPTY(monkey)ON(box,b)ON(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 ba
9、nana; /*Banana at B,Banana=0*/int monbox; /*-1: 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.mo
10、nkey=-1;else if(a=0)routesavei=“Monkey go to B“;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(in
11、t a,int i) int B;B=a;if(B=-1)routesavei=“monkey 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.monke
12、y=1;Statesi+1.box=1;elseprintf(“parameter is wrong“);/*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)
13、routesavei=“Monkey climb down from the box“;Statesi+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;pr
14、intf (“%s n“, “Result to problem:“);for(c=0; c=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 scree
15、n for user,press any key to continue*/return; j=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
16、);nextStep(j);*/else if(Statesi.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(Statesi.monkey=-1) if(
17、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=0) monkeygoto(0,i);nextStep(j);climbonto(i);reach(i+1);nextStep(j);elsemonkeygoto(1,i);nextS
18、tep(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,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(St
19、atesi.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.ban
20、ana=0;States0.monbox=-1;nextStep(0);5、实验截图6、实验感想主要输算法的中心思想要搞明白,根据设计的流程图转换成程序,加上互联网资源丰富,得以顺利完成实验二求解函数逼近问题1、实验内容有 21 组单输入矢量和相对应的目标矢量 T,试采用 Matlab (7.0 以上版本)设 计神经网络来实现这对数组的函数关系P=-1:0.1:1T=-0.96 0.577 -0.0729 0.377 0.641 0.66 0.461 0.1336 -0.201 -0.434 -0.5 -0.393 -0.1647 0.0988 0.3072 0.396 0.3449 0.18
21、16 -0.0312 -0.2183 -0.3201测试集P2=-1:0.025:12、实验解看到期望输出的范围是 ,所以利用双极性 Sigmoid 函数作为转移函数。1,程序如下:clear;clc;X=-1:0.1:1;D=-0.9602 -0.5770 -0.0729 0.3771 0.6405 0.6600 0.4609.0.1336 -0.2013 -0.4344 -0.5000 -0.3930 -0.1647 -.0988.0.3072 0.3960 0.3449 0.1816 -0.312 -0.2189 -0.3201;figure;plot(X,D,*); %绘制原始数据分布
22、图(附录:1-1)net = newff(-1 1,5 1,tansig,tansig);net.trainParam.epochs = 100; %训练的最大次数net.trainParam.goal = 0.005; %全局最小误差net = train(net,X,D); O = sim(net,X); figure; plot(X,D,*,X,O); %绘制训练后得到的结果和误差曲线(附录:1-2、1-3)V = net.iw1,1%输入层到中间层权值theta1 = net.b1%中间层各神经元阈值W = net.lw2,1%中间层到输出层权值theta2 = net.b2%输出层各神经元阈值3、实验结果输入层到中间层的权值: -9.16 7.348 .61 4.89 3.50TV中间层各神经元的阈值: .5 -2.09 -.2 . .271中间层到输出层的权值: .347 .1 .8 0.4 .93W输出层各神经元的阈值: -1.52T图一 原始数据分布图图二 仿真结果