启发式搜索实验Word文档下载推荐.docx

上传人:b****5 文档编号:19375954 上传时间:2023-01-05 格式:DOCX 页数:13 大小:103.74KB
下载 相关 举报
启发式搜索实验Word文档下载推荐.docx_第1页
第1页 / 共13页
启发式搜索实验Word文档下载推荐.docx_第2页
第2页 / 共13页
启发式搜索实验Word文档下载推荐.docx_第3页
第3页 / 共13页
启发式搜索实验Word文档下载推荐.docx_第4页
第4页 / 共13页
启发式搜索实验Word文档下载推荐.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

启发式搜索实验Word文档下载推荐.docx

《启发式搜索实验Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《启发式搜索实验Word文档下载推荐.docx(13页珍藏版)》请在冰豆网上搜索。

启发式搜索实验Word文档下载推荐.docx

4、计算出最短路径

如果明白了上面的场景描述,下面就可以进展分析了。

在A*算法中,核心思想是一个公式,上面已经提到过:

f(n)=g(n)+h(n)

〔2〕源程序清单:

package.itxxz.ui.suanfa.astar;

importjava.util.Iterator;

importjava.util.LinkedList;

importjava.util.Queue;

import.itxxz.ui.suanfa.astar.Point;

publicclassItxxzAstar{

//开场节点

privatePointstartPoint=null;

//当前节点

privatePointendPoint=null;

//完毕节点

privatePointcurrentPoint=null;

//最短距离坐标节点

privatePointshortestFPoint=null;

//迷宫数组地图

privatestaticfinalint[][]mazeArray={

{1,0,0,0,0},

{1,0,2,0,0},

{1,0,0,0,1},

{1,1,1,1,0},

{3,0,1,1,1}};

//迷宫坐标对象

privatePoint[][]mazePoint=null;

//开启队列,用于存放待处理的节点

Queue<

Point>

openQueue=null;

//关闭队列,用于存放已经处理过的节点

closedQueue=null;

//起始节点到某个节点的距离

int[][]FList=null;

//某个节点到目的节点的距离

int[][]GList=null;

//起始节点经过某个节点到目的节点的距离

int[][]HList=null;

/**

*构造函数

*

*parammaze

*迷宫图

*paramstartPoint

*起始节点

*paramendPoint

*完毕节点

*/

publicItxxzAstar(Point[][]mazePoint,PointstartPoint,PointendPoint){

this.mazePoint=mazePoint;

this.startPoint=startPoint;

this.endPoint=endPoint;

openQueue=newLinkedList<

();

openQueue.offer(startPoint);

closedQueue=newLinkedList<

FList=newint[mazePoint.length][mazePoint[0].length];

GList=newint[mazePoint.length][mazePoint[0].length];

HList=newint[mazePoint.length][mazePoint[0].length];

for(inti=0;

i<

mazePoint.length;

i++){

for(intj=0;

j<

mazePoint[0].length;

j++){

FList[i][j]=Integer.MAX_VALUE;

GList[i][j]=Integer.MAX_VALUE;

HList[i][j]=Integer.MAX_VALUE;

}

//起始节点到当前节点的距离

GList[startPoint.getX()][startPoint.getY()]=0;

//当前节点到目的节点的距离

HList[startPoint.getX()][startPoint.getY()]=getPointDistance(

startPoint.getX(),startPoint.getY(),endPoint.getX(),

endPoint.getY());

//f(x)=g(x)+h(x)

FList[startPoint.getX()][startPoint.getY()]=GList[startPoint.getX()][startPoint

.getY()]+HList[startPoint.getX()][startPoint.getY()];

*计算当前坐标与完毕坐标之间的距离

*计算方法为每向相信坐标移动一次算作一个距离单位

privateintgetPointDistance(intcurrent_x,intcurrent_y,intend_x,

intend_y){

returnMath.abs(current_x-end_x)+Math.abs(current_y-end_y);

*数组迷宫地图

*0、可通行1、障碍2、开场节点3、完毕节点

publicstaticvoidmain(String[]args){

//创立节点迷宫图

Point[][]mazePoint=newPoint[mazeArray.length][mazeArray[0].length];

mazePoint[i][j]=newPoint(i,j,mazeArray[i][j]);

Pointstart=mazePoint[1][2];

Pointend=mazePoint[6][0];

ItxxzAstarstar=newItxxzAstar(mazePoint,start,end);

star.start();

System.out.println(mazeArray.length+"

"

+mazeArray[0].length);

star.printPath();

*开场迷宫搜索

publicvoidstart(){

while((currentPoint=findShortestFPoint())!

=null){

if(currentPoint.getX()==endPoint.getX()

&

&

currentPoint.getY()==endPoint.getY())

return;

updateNeighborPoints(currentPoint);

*获取距离最短的坐标点

publicPointfindShortestFPoint(){

currentPoint=null;

shortestFPoint=null;

intshortestFValue=Integer.MAX_VALUE;

Iterator<

it=openQueue.iterator();

while(it.hasNext()){

currentPoint=it.next();

if(FList[currentPoint.getX()][currentPoint.getY()]<

=shortestFValue){

shortestFPoint=currentPoint;

shortestFValue=FList[currentPoint.getX()][currentPoint.getY()];

if(shortestFValue!

=Integer.MAX_VALUE){

System.out

.println("

【移除节点】:

"

+shortestFPoint.getValue()+"

["

+shortestFPoint.getX()+"

+shortestFPoint.getY()+"

]"

);

openQueue.remove(shortestFPoint);

closedQueue.offer(shortestFPoint);

returnshortestFPoint;

*更新临近节点

privatevoidupdateNeighborPoints(PointcurrentPoint){

intcurrent_x=currentPoint.getX();

intcurrent_y=currentPoint.getY();

System.out.println("

当前节点:

+current_x+"

+current_y+"

//上

if(checkPosValid(current_x-1,current_y)){

System.out.print("

上"

updatePoint(mazePoint[current_x][current_y],

mazePoint[current_x-1][current_y]);

//下

if(checkPosValid(current_x+1,current_y)){

下"

mazePoint[current_x+1][current_y]);

//左

if(checkPosValid(current_x,current_y-1)){

左"

mazePoint[current_x][current_y-1]);

//右

if(checkPosValid(current_x,current_y+1)){

右"

mazePoint[current_x][current_y+1]);

---------------"

*检查该节点是否有效

privatebooleancheckPosValid(intx,inty){

//检查x,y是否越界,并且当前节点不是墙

if((x>

=0&

x<

mazePoint.length)

(y>

y<

mazePoint[0].length)

(mazePoint[x][y].getValue()!

=1)){

//检查当前节点是否已在关闭队列中,假设存在,那么返回"

false"

it=closedQueue.iterator();

Pointpoint=null;

if((point=it.next())!

if(point.getX()==x&

point.getY()==y)

returnfalse;

returntrue;

*更新当前节点

privatevoidupdatePoint(PointlastPoint,PointcurrentPoint){

intlast_x=lastPoint.getX();

intlast_y=lastPoint.getY();

inttemp_g=GList[last_x][last_y]+1;

//当前节点到目的位置的距离

["

+mazePoint[current_x][current_y].getValue());

inttemp_h=getPointDistance(current_x,current_y,endPoint.getX(),

到目的位置的距离:

+temp_h);

inttemp_f=temp_g+temp_h;

f(x)=g(x)+h(x):

+temp_f+"

="

+temp_g+"

+"

//如果当前节点在开启列表中不存在,那么:

置入开启列表,并且“设置〞

//1)起始节点到当前节点距离

//2)当前节点到目的节点的距离

//3)起始节点到目的节点距离

if(!

openQueue.contains(currentPoint)){

openQueue.offer(currentPoint);

currentPoint.setFather(lastPoint);

添加到开启列表:

+currentPoint.getValue()+"

+currentPoint.getX()+"

+currentPoint.getY()+"

GList[current_x][current_y]=temp_g;

HList[current_x][current_y]=temp_h;

FList[current_x][current_y]=temp_f;

}else{

//如果当前节点在开启列表中存在,并且,

//从起始节点、经过上一节点到当前节点、至目的地的距离<

上一次记录的从起始节点、到当前节点、至目的地的距离,

//那么:

“更新〞

if(temp_f<

FList[current_x][current_y]){

//更新当前节点的父节点

currentPoint:

currentPoint.father:

+currentPoint.getFather().getValue()+"

+currentPoint.getFather().getX()+"

+currentPoint.getFather().getY()+"

*打印行走路径

publicvoidprintPath(){

================开场打印行走路径【用8表示】================"

Pointfather_point=null;

int[][]result=newint[mazeArray.length][mazeArray[0].length];

mazeArray.length;

mazeArray[0].length;

result[i][j]=0;

intstep=0;

father_point=mazePoint[endPoint.getX()][endPoint.getY()];

while(father_point!

【father_point】"

+father_point.getValue()+"

+father_point.getX()+"

+father_point.getY()+"

if(father_point.equals(startPoint))

result[father_point.getX()][father_point.getY()]=2;

elseif(father_point.equals(endPoint)){

result[father_point.getX()][father_point.getY()]=3;

step++;

result[father_point.getX()][father_point.getY()]=8;

father_point=father_point.getFather();

//打印行走步数

stepis:

"

+step);

System.out.print(result[i][j]+"

System.out.println();

}

〔3〕实验结果及分析。

实验结果:

很好的完成了实验目的,并且成功给出了最短路径!

A*算法作为解决最优路径的一种高效算法,自从1968年诞生以来,得到了广泛的应用,而其的多种改良算法也在许多领域发挥着作用。

可以预见,在更优的算法发现以前,A*算法将会得到更广泛的应用,并会由于图论、人工智能、机器人技术、自动控制等多学科的融合而得到更大的开展。

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

当前位置:首页 > 工程科技 > 兵器核科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1