人工智能算法实现.docx

上传人:牧羊曲112 文档编号:5005853 上传时间:2023-05-29 格式:DOCX 页数:18 大小:425.61KB
返回 下载 相关 举报
人工智能算法实现.docx_第1页
第1页 / 共18页
人工智能算法实现.docx_第2页
第2页 / 共18页
人工智能算法实现.docx_第3页
第3页 / 共18页
人工智能算法实现.docx_第4页
第4页 / 共18页
人工智能算法实现.docx_第5页
第5页 / 共18页
亲,该文档总共18页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《人工智能算法实现.docx》由会员分享,可在线阅读,更多相关《人工智能算法实现.docx(18页珍藏版)》请在三一办公上搜索。

1、人工智能算法实现:1A*算法c语言分步阅读A*算法,A* (A-Star)算法是一种静态路网中求解最短路最有效的方法。估价值 与实际值越接近,估价函数取得就越好。A*1(A-Star)算法是一种静态路网中求解最短路最有效的方法。公式表示为:f(n)=g(n)+h(n),其中f(n)是从初始点经由节点n到目标点的估价函数,g(n)是在状态空间中从初始节点到n节点的实际代价,h(n)是从n到目标节点最佳路径的估计代价。保证找到最短路径(最优解的)条件,关键在于估价函数h(n)的选取:估价值h(n)-J - T-J - T-J - nDr-DCDCDD 匚口匚 口匚口匚匚?口?口 RAAAnnnnn

2、nnnRscnfiDa 口.Brln型口曰口却苔留口cloDrr口卷片毫 RRRR-nn-nnnnnn-nM.PRRR g.pp.QPi.0*lclslel局ICIEI剑?zcowsq疤留.0pwrlQd3ol!.Ja藉 JlulrElulrlDlnln* 片片苗InEHlrirlmlclr-l2.A star算法在静态路网中的应用,以在地图中找从开始点s到e点的最短行走路径为例:首先定义数据结构#define MAPMAXSIZE 100 地图面积最大为 100x100#define MAXINT 8192 /定义一个最大整数,地图上任意两点距离不会超过 它#define STACKSIZE

3、 65536 /保存搜索节点的堆栈大小#define tile_num(x,y) (y)*map_w+(x)/ 将 x,y 坐标转换为地图上块的编 号#define tile_x(n) (n)%map_w) 由块编号得出 x,y 坐标#define tile_y(n) (n)/map_w)/树结构,比较特殊,是从叶节点向根节点反向链接typedef struct node *TREE;struct node int h;int tile;TREE father;typedef struct node2 *LINK;struct node2 TREE node;int f;LINK next;L

4、INK queue; /保存没有处理的行走方法的节点TREE stackSTACKSIZE; /保存已经处理过的节点(搜索完后释放)int stacktop;char map6=x,x,x,x,x,x,x,e, , ,x,乂乂 ,x, ,x,乂乂 , ,x,x,x,x,x,s,x,x,x,x,x,x,x ;地图数据int dis_mapMAPMAXSIZEMAPMAXSIZE;/ 保存搜索路径时,中间目标地最优解int map_w,map_h;/ 地图宽和高int start_x,start_y,end_x,end_y; 地点,终点坐标/路径寻找主函数void findpath(int *pa

5、th)/printf(%d,%d,%d,%d,start_x,start_y,end_x,end_y);TREE root;int i,j;stacktop=0;for (i=0;imap_h;i+)for (j=0;jtile=tile_num(start_x,start_y);root-h=0;root-father=NULL;enter_queue(root,judge(start_x,start_y);for (;) int x,y,child;TREE p;root=get_from_queue();if (root= = NULL) *path=-1;return;x=tile_x

6、(root-tile);y=tile_y(root-tile);if (x=end_x & y=end_y) break;/ 达到目的地成功返回child=trytile(x,y-1,root);/ 尝试向上移动child&=trytile(x,y+1,root);/ 尝试向下移动child&=trytile(x+1,y,root);/ 尝试向右移动if (child!=0)pop_stack();/如果四个方向均不能移动,释放这个死节点/回溯树,将求出的最佳路径保存在path中for (i=0;root;i+) pathi = root-tile;root=root-father;/prin

7、tf(pathis %d,pathi);pathi = -1;freetree();/估价函数,估价x,y到目的地的距离,估计值必须保证比实际值小int judge(int x,int y)int distance;distance=abs(end_x-x)+abs(end_y-y);return distance;/尝试下一步移动到x,y可行否int trytile(int x,int y,TREE father)TREE p=father;int h;if (mapyx =x) return 1; / 如果(x,y)处是障碍,失败while (p) if (x=tile_x(p-tile)

8、 & y=tile_y(p-tile) return 1; / 如果(x,y)曾经经过,失败p=p-father;h=father-h + 1;if (h=dis_mapyx) return 1;/如果曾经有更好的方案移动到(x,y)失败dis_mapyx = h;/记录这次到(x,y)的距离为历史最佳距离将这步方案记入待处理队列p=(TREE)malloc(sizeof(*p);p-father=father;p-h=father-h + 1;p-tile=tile_num(x,y);enter_queue(p,p-h+judge(x,y);return 0;YTi-ifi d t15以m吟

9、it di -vaa .I? Itf f- h H*iPi.-c ; i=W Id LB.t TJEX i* 心, hf w 寸件- =s wees i . j - ygaLQsy ie&3 -.皂口刖 p ri nL paUi :心加:irEJXM5 ItiLLI :B IJLnTUEt I : GLE:X |1 ; 1*11 3par ! r-4!=iB点任意键进行运行找静态路网4.3. *是如始点int mj n()g-ezch-axO j ysEfeHi (pause retnrn 0;LXMKW LTHint irjja-ysEejn (c=lw| ;for (i0; i=0;i+)

10、 gotoxy(tile_x(pathi) + 1,tile_y(pathi) + 1);printf(-);Sleep(2000); printf(n);printf(n);printf(走迷宫完成”);6.整个程序的代码如下:#include #includestdio.h” #include #includeassert.h” #includestdlib.h” #define MAPMAXSIZE 100 地图面积最大为 100x100 #define MAXINT 8192 /定义一个最大整数,地图上任意两点距离不会超过#define STACKSIZE 65536 /保存搜索节点的

11、堆栈大小 #define tile_num(x,y) (y)*map_w+(x)/ 将 x,y 坐标转换为地图上块的编#define tile_x(n) (n)%map_w) 由块编号得出 x,y 坐标#define tile_y(n) (n)/map_w) /树结构,比较特殊,是从叶节点向根节点反向链接 typedef struct node *TREE;struct/*/node int h;int tile;TREE father;typedefstruct/*/node2*LINK;struct node2 TREE node;intf;/*/LINK next;LINK queue;

12、 /保存没有处理的行走方法的节点TREE stackSTACKSIZE; /保存已经处理过的节点(搜索完后释放)int stacktop;char map6=x,x,x,x,x,x,x,e, , ,x,乂乂 ,x, ,x,乂乂 , ,x,x,x,x,x,s,x,x,x,x,x,x,x ;地图数据intdis_map/*designdeby*/MapmaXSIZEMAPMAXSIZE;/ 保存搜索路径时,中间目标地最优解int map_w,map_h;/ 地图宽和高int start_x,start_y,end_x,end_y; 地点,终点坐标void gotoxy(int x ,int y)H

13、ANDLE a;COORD zb;zb.X =x-1;zb.Y =y-1;a= GetStdHandle(STD_OUTPUT_HANDLE/*designde by*枫SetConsoleCursorPosition(a,zb);/初始化队列void init_queue()queue=(LINK)malloc(sizeof(*queue);queue-node=NULL;queue-f=-1;queue-next=(LINK)/*designdeby*/malloc(sizeof(*queue);queue-next-f=MAXINT;queue-next-node=NULL;queue-

14、next-next=NULL; /待处理节点入队列,依靠对目的地估价距离插入排序void enter_queue(TREE node,int f)LINK p=queue,father,q;while(fp-f) father=p;p=p-next/*/;assert(p);q = (LINK)malloc(sizeof(*q);assert(queue);q-f=f,q-node=node,q-next=p;father-next=q;/将离目的地估计最近的方案出队列TREE get_from_queue()TREE bestchoice=queue-next-node;LINK next=

15、queue-next-next;/*/free(queue-next);queue-next=next;stackstacktop+ = bestchoice;assert(stacktopSTACKSIZE);*/释放栈顶节点void pop_stack()free(stack-stacktop);/释放申请过的所有节点void freetree()int i;LINK p;for (i=0;inode);queue=queue-next;free(p);/估价函数,估价x,y到目的地的距离,估计值必须保证比实际值小int judge(int x,int y) int distance;di

16、stance=abs(end_x-x)+abs(end_y-y);return distance;/尝试下一步移动到x,y可行否int trytile(int/*designde by */ x,int y,TREE father)TREE p=father;int h;if (mapyx =x) return 1; / 如果(x,y)处是障碍,失败while (p) /*designde by */if (x=tile_x(p-tile) &y=tile_y(p-tile) return 1; /如果(x,y)曾经经过,失败p=p-father;h=father-h + 1;if (h=dis_mapyx) return 1;/如果曾经有更好的方案移动到(x,y)失败dis_mapyx = h;/记录这次到(x,y)的距离为历史最佳距离

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

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


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号