多语言展示
当前在线:1959今日阅读:27今日分享:41

人工智能算法实现:[1]A*算法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)<= n到目标节点的距离实际值,这种情况下,搜索的点数多,搜索范围大,效率低。但能得到最优解。如果 估价值>实际值,搜索的点数少,搜索范围小,效率高,但不能保证得到最优解。
工具/原料

DEVC++或 VC 6.0

方法/步骤
1

估价值与实际值越接近,估价函数取得就越好例如对于几何路网来说,可以取两节点间欧几理德距离(直线距离)做为估价值,即f=g(n)+sqrt((dx-nx)*(dx-nx)+(dy-ny)*(dy-ny));这样估价函数f在g值一定的情况下,会或多或少的受估价值h的制约,节点距目标点近,h值小,f值相对就小,能保证最短路的搜索向终点的方向进行。明显优于Dijkstra算法的毫无方向的向四周搜索。conditions of heuristicOptimistic (must be less than or equal to the real cost)As close to the real cost as possible详细内容:创建两个表,OPEN表保存所有已生成而未考察的节点,CLOSED表中记录已访问过的节点。算起点的估价值;将起点放入OPEN表;

2

A star算法在静态路网中的应用,以在地图中找从开始点 s 到e点的最短行走路径为例:首先定义数据结构#define MAPMAXSIZE 100 //地图面积最大为 100x100 #define MAXINT 8192 //定义一个最大整数, 地图上任意两点距离不会超过它 #define STACKSIZE 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;};LINK queue; // 保存没有处理的行走方法的节点 TREE stack[STACKSIZE]; // 保存已经处理过的节点 (搜索完后释放) int stacktop;char map[][6]={{'x','x','x','x','x','x'},               {'x','e',' ',' ',' ','x'},               {'x','x',' ','x',' ','x'},               {'x','x',' ',' ',' ','x'},               {'x','x','x','x','s','x'},               {'x','x','x','x','x','x'} };//地图数据 int dis_map[MAPMAXSIZE][MAPMAXSIZE];//保存搜索路径时,中间目标地最优解 int map_w,map_h;//地图宽和高 int start_x,start_y,end_x,end_y; //地点,终点坐标 // 路径寻找主函数 void findpath(int *path){ //printf('%d,%d,%d,%d',start_x,start_y,end_x,end_y);  TREE root;int i,j;stacktop=0;for (i=0;itile=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(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);//尝试向左移动 child&=trytile(x+1,y,root);//尝试向右移动 if (child!=0)pop_stack();// 如果四个方向均不能移动,释放这个死节点 }// 回溯树,将求出的最佳路径保存在 path[] 中 for (i=0;root;i++) {path[i]=root->tile;root=root->father;//printf('pathis %d',path[i]);}path[i]=-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 (map[y][x]=='x') return 1; // 如果 (x,y) 处是障碍,失败 while (p) {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_map[y][x]) return 1;// 如果曾经有更好的方案移动到 (x,y) 失败 dis_map[y][x]=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;}

3

打开c语言编译器,输入我们的运行代码,编译,运行如下,打印出地图如下图:

4

点任意键进行运行找静态路网

5

说明:找到路后会存到一个数组中去,我们为了显示这个过程可以运用打印函数打印出来代码如下void printpath(int *path){int i;//printf('-44444444444444');for (i=0;path[i]>=0;i++) { gotoxy(tile_x(path[i])+1,tile_y(path[i])+1);printf('-');Sleep(2000);         }printf('\n');printf('\n');printf('走迷宫完成');}

6

整个程序的代码如下:#include#include'stdio.h'#include#include'assert.h' #include'stdlib.h'#define MAPMAXSIZE 100 //地图面积最大为 100x100 #define MAXINT 8192 //定义一个最大整数, 地图上任意两点距离不会超过它 #define STACKSIZE 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/*designde by 1wangxiaobo@163.com*/ node {int h;int tile;TREE father;};typedef struct /*designde by 1wangxiaobo@163.com*/node2 *LINK;struct node2 {TREE node;int f;/*designde by 1wangxiaobo@163.com*/LINK next;};LINK queue; // 保存没有处理的行走方法的节点 TREE stack[STACKSIZE]; // 保存已经处理过的节点 (搜索完后释放) int stacktop;char map[][6]={{'x','x','x','x','x','x'},               {'x','e',' ',' ',' ','x'},               {'x','x',' ','x',' ','x'},               {'x','x',' ',' ',' ','x'},               {'x','x','x','x','s','x'},               {'x','x','x','x','x','x'} };//地图数据 int dis_map/*designde by 1wangxiaobo@163.com*/[MAPMAXSIZE][MAPMAXSIZE];//保存搜索路径时,中间目标地最优解 int map_w,map_h;//地图宽和高 int start_x,start_y,end_x,end_y; //地点,终点坐标 void gotoxy(int x ,int y){HANDLE a;COORD zb;zb.X =x-1;zb.Y =y-1;a= GetStdHandle(STD_OUTPUT_HANDLE/*designde by 1wangxiaobo@163.com*/);SetConsoleCursorPosition(a,zb);}// 初始化队列 void init_queue(){ queue=(LINK)malloc(sizeof(*queue));queue->node=NULL;queue->f=-1;queue->next=(LINK)/*designde by 1wangxiaobo@163.com*/malloc(sizeof(*queue));queue->next->f=MAXINT;queue->next->node=NULL;queue->next->next=NULL;} // 待处理节点入队列, 依靠对目的地估价距离插入排序 void enter_queue(TREE node,int f){ LINK p=queue,father,q;while(f>p->f) {father=p;p=p->next/*designde by 1wangxiaobo@163.com*/;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=queue->next->next;/*designde by 1wangxiaobo@163.com*/free(queue->next);queue->next=next;stack[stacktop++]=bestchoice;assert(stacktopnode);queue=queue->next;free(p);} } // 估价函数,估价 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/*designde by 1wangxiaobo@163.com*/ x,int y,TREE father){TREE p=father;int h;if (map[y][x]=='x') return 1; // 如果 (x,y) 处是障碍,失败 while (p) {/*designde by 1wangxiaobo@163.com*/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_map[y][x]) return 1;// 如果曾经有更好的方案移动到 (x,y) 失败 dis_map[y][x]=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;}// 路径寻找主函数 void findpath(int *path){ //printf('%d,%d,%d,%d',start_x,start_y,end_x,end_y);  TREE root;int i,j;stacktop=0;for (i=0;itile=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(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);//尝试向左移动 child&=trytile(x+1,y,root);//尝试向右移动 if (child!=0)pop_stack();// 如果四个方向均不能移动,释放这个死节点 }// 回溯树,将求出的最佳路径保存在 path[] 中 for (i=0;root;i++) {path[i]=root->tile;root=root->father;//printf('pathis %d',path[i]/*designde by 1wangxiaobo@163.com*/);}path[i]=-1;freetree();}void printpath(int *path){int i;//printf('-44444444444444');for (i=0;path[i]>=0;i++) { gotoxy(tile_x(path[i])+1,tile_y(path[i])+1);printf('-');Sleep(2000);         }printf('\n');printf('\n');printf('走迷宫完成');}void readmap(){ printf('走迷宫,s是起始点 e是终点  按任意键开始');getchar();//FILE *f;int i,j;//f=fopen('2.c','r');//assert(f);//scanf('%d%d',&map_w,&map_h);map_w=map_h=6;for (i=0;i=0 && end_x>=0);//printf('%d,%d,%d,%d',start_x,start_y,end_x,end_y);}void showmap(){int i,j;system('cls');for (i=0;i

注意事项

为了使的程序可以慢慢的打印出来动态的运行效果 加了Sleep(2000);

推荐信息