研究生( 人工智能 )报告
题目:人工智能实验报告
学 号 姓 名 专 业 电磁场与微波技术 指 导 教 师 院(系、所)
华中科技大学研究生院制
----精品
精品--
----精品
精品--
1 问题二
利用一阶谓词逻辑求解猴子摘香蕉问题:房内有一个猴子,一个箱子,天花板上挂了一串香蕉,其位置如图所示,猴子为了拿到香蕉,它必须把箱子搬到香蕉下面,然后再爬到箱子上。请定义必要的谓词,列出问题的初始化状态(即下图所示状态),目标状态(猴子拿到了香蕉,站在箱子上,箱子位于位置b)。
图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};
使用谓词、连结词、量词来表示环境状态。
问题的初始状态可表示为: 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)
----精品
精品--
从初始状态到目标状态的转化, 猴子需要完成一系列操作, 定义操作类谓词
表示其动作。
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)
按照行动计划, 一步步进行状态替换, 直至目标状态。
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 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) { routesave[i]=\"Monkey go to A\"; States[i+1]=States[i]; States[i+1].monkey=-1; } else if(a==0) { routesave[i]=\"Monkey go to B\"; States[i+1]=States[i]; States[i+1].monkey=0; } else if(a==1) { routesave[i]=\"Monkey go to C\"; States[i+1]=States[i]; States[i+1].monkey=1; } else { printf(\"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) { routesave[i]=\"monkey move box to ----精品 精品-- A\"; States[i+1]=States[i]; States[i+1].monkey=-1; States[i+1].box=-1; } else if(B==0) { routesave[i] = \"monkey move box to B\"; States[i+1]=States[i]; States[i+1].monkey=0; States[i+1].box=0; } else if(B==1) { routesave[i] = \"monkey move box to C\"; States[i+1]=States[i]; States[i+1].monkey=1; States[i+1].box=1; } else { printf(\"parameter is wrong\"); } } /*end function movebox*/ /*function climbonto,the monkey climb onto the box*/ void climbonto(int i) { routesave[i]=\"Monkey climb onto the box\"; States[i+1]=States[i]; States[i+1].monbox=1; } /*function climbdown,monkey climb down from the box*/ void climbdown(int i) { routesave[i]=\"Monkey climb down from the box\"; 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]=\"Monkey reach the banana\"; } /*output the solution to the problem*/ void showSolution(int i) { int c; printf (\"%s \\n\ for(c=0; cprintf(\"\\n\"); } /*perform next step*/ void nextStep(int i) { int c; int j; if(i>=150) { printf(\"%s \\n\150,have problem \"); return; } for (c=0; c----精品 精品-- ==States[i].banana&&States[c].monbox==State s[i].monbox) { return; } } if(States[i].monbox==1&&States[i].monkey==0 &&States[i].banana==0&&States[i].box==0) { showSolution(i); printf(\"Press any key to continue \\n\"); getchar();/*to save screen for user,press any key to continue*/ return; } j=i+1; if(States[i].monkey==0) { if(States[i].box==0) { if(States[i].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);*/ } else { 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) { movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j); } else { climbdown(i); nextStep(j); movebox(0,i); nextStep(j); climbonto(i); 精品-- reach(i+1); nextStep(j); } } else if(States[i].box==0) { monkeygoto(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j); } else { 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) { movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j); } else { climbdown(i); nextStep(j); movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j); } } else if(States[i].box==-1) { monkeygoto(-1,i); nextStep(j); movebox(0,i); nextStep(j); movebox(0,i); nextStep(j); climbonto(i); reach(i+1); nextStep(j); } else { 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); } ----精品 精品-- 2 问题三 求解函数逼近问题 有21组单输入矢量P和相对应的目标矢量T,试采用Matlab设计神经网络来实现这对数组的函数关系。 P=-1:0.1:1; T=[-0.96 -0.577 -0.0729 0.377 0.1 0.66 0.461 0.1336 -0.201 -0.434 -0.5 -0.393 -0.17 0.0988 0.3072 0.396 0.3449 0.1816 -0.0312 -0.2183 -0.3201]; 测试集 P2=-1:0.25:1 解: BP神经网络模型 BP 神经网络作为人工神经网络中应用最广的算法模型,具有完备的理论体系和学习机制。它模仿人脑神经元对外部激励信号的反应过程,建立多层感知器模型,利用信号正向传播和误差反向调节的学习机制,通过多次迭代学习,成功地搭建出处理非线性信息的智能化网络模型。 ----精品 精品-- 图2 具有一个隐含层的BP网络模型示意图 BP 算法的主要思想是把学习过程分为信号的正向传播与误差的反向传播两个阶段。在正向传播阶段,输入信息从输入层经隐含层传向输出层,在输出端产生输出信号。在信号的向前传递过程中网络的权值固定不变,每一层神经元的状态只影响下一层神经元的状态。如果在输出层不能得到期望的输出,则转入误差信号反向传播。在反向传播阶段,未能满足精度要求的误差信号由输出端开始,以某种方式逐层向前传播,并将误差分摊给各层的所有单元,依据误差信号动态的调整各单元层的连接权重。通过周而复始的正向传播与反向调节,神经元间的权值得到不断的修正。当输出信号的误差满足精度要求时,停止学习。 BP神经网络的MATLAB实现 进行BP神经网络设计时,需要考虑以下问题:网络的拓扑结构(隐层的层数及各层的神经元的数目);神经元的变换函数选取;网络的初始化(连接权值和阈值的初始化);训练参数设置;训练样本的归一化处理;样本数据导入方式等。 根据以上分析可知,对于网络的实现有四个基本的步骤: 1) 网络建立:通过函数newff实现,它根据样本数据自动确定输入层、输出层的神经元数目;隐层神经元数目以及隐层的层数、隐层和输出层的变换函数、训练算法函数需由用户确定。 2) 初始化:通过函数init实现,当newff在创建网络对象的同时,自动调动初始化函数init,根据缺省的参数对网络进行连接权值和阈值初始化。 3) 网络训练:通过函数train实现,它根据样本的输入矢量P、目标矢量T;和预先已设置好的训练函数的参数;对网络进行训练。 4) 网络仿真:通过函数sim实现,它根据已训练好的网络,对测试数据进行仿真计算。 仿真结果 仿真面板如下图: ----精品 精品-- 图3 仿真面板 输入矢量P与相对应的目标矢量T之间的函数关系仿真图如下,训练次数达到6855时,即结束,红色线代表训练后,蓝色线代表原数据。 BP神经网络逼近非线性函数的Matlab实现10.50-0.5-1 -1BP神经网络逼近非线性函数原数据曲线 -0.8-0.6-0.4-0.200.20.40.60.81 TrainBestGoalMean Squared Error (mse)100Best Training Performance is 0.0099994 at epoch 7432 10-2 010002000300040005000600070007432 Epochs----精品 精品-- 10gradient0Gradient = 0.01336, at epoch 6855Output ~= 0.94*Target + 0.00071010-1Training: R=0.971220.60.40.20-0.2-0.4-0.6-0.8 -0.500.5 DataFitY = T-2Validation Checks = 0, at epoch 68551val fail0-10200040006855 Epochs6000Target图4 BP神经网络逼近非线性函数图(红)与原数据(蓝) ----精品 精品-- 1. 训练次数为10000次时,测试集P2时,仿真如下 BP神经网络逼近非线性函数的Matlab实现1BP神经网络逼近非线性函数原数据曲线 0.50-0.5: Mean Squared Error (mse)-1 -1-0.8-0.6-0.4-0.200.20.40.60.81 100Best Training Performance is 0.0099983 at epoch 6050TrainBestGoal10-2 01000200030004000500060006050 Epochs0 Training: R=0.971230.60.40.20-0.2-0.4-0.6-0.8 -0.500.5 DataFitY = TGradient = 0.013405, at epoch 60501010-1-2Validation Checks = 0, at epoch 605010-10200040006050 Epochs6000Output ~= 0.94*Target + 0.0006710val failgradientTarget 图5 测试集P2的仿真结果 MATLAB仿真程序源码 clear;clc; P=-1:0.1:1; ----精品 精品-- P2=-1:0.25:1;% 测试集P2 T=[-0.96 -0.577 -0.0729 0.377 0.1 0.66 0.461 0.1336 -0.201 -0.434 -0.5 -0.393 -0.17 0.0988 0.3072 0.396 0.3449 0.1816 -0.0312 -0.2183 -0.3201]; net=newff([-1,1],[5,1],{'tansig','tansig'},'traingd'); %可以改变训练不熟来查看网络的训练结果 net.trainParam.epochs=10000; net.trainParam.goal=0.01; %目标误差 LP.lr=0.1; %设置学习速率 net=train(net,P,T); y=sim(net,P); y1=sim(net,P2); figure hndl1=plot(P,y); set(hndl1,'linewidth',2); set(hndl1,'color','red'); hold on hndl2=plot(P,T); set(hndl2,'linewidth',2); %测试集P2 hndl3=plot(P2,y1); set(hndl3,'linewidth',2); set(hndl3,'color','black'); title('BP神经网络逼近非线性函数的Matlab实现');%标题 legend('BP神经网络逼近非线性函数','原数据曲线'); ----精品 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- dfix.cn 版权所有 湘ICP备2024080961号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务