a星算法详解_matlab优化算法

a星算法详解_matlab优化算法概述基于上一篇文章提到的DFS算法和BFS算法A星算法属于图这种数据结构的搜索算法,对比于树的遍历搜索,需要考虑到的问题是:同一个节点的重复访问,所以需要对于已经访问过的节点进行标记。曼哈顿距离:在几何度量空间中,用以标明两个点在标准坐标系上的绝对轴距总和。图1中绿色代表欧氏距离(直线距离),蓝色和黄色代表等价的曼哈顿距离。d(i,j)=|Xi-Xj|+|Yi-…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

概述

基于上一篇文章提到的DFS算法和BFS算法
A星算法属于图这种数据结构的搜索算法,对比于树的遍历搜索,需要考虑到的问题是:同一个节点的重复访问,所以需要对于已经访问过的节点进行标记。

曼哈顿距离:
在几何度量空间中,用以标明两个点在标准坐标系上的绝对轴距总和。
图 1
图1中绿色代表欧氏距离(直线距离),蓝色和黄色代表等价的曼哈顿距离。
d( i , j ) = |Xi – Xj| + |Yi – Yj|
优势:计算机图形学中,欧氏距离需要进行浮点运算,曼哈顿距离只涉及到加减法,运算速度大大提高。

我们假设无人车需要从A点(左下侧浅绿色)移动到B点(右上侧浅黄色),但是两点之间被障碍物隔开,我们使用矩阵的形式来构建地图,其中元素为0的矩阵坐标视为可走的,值为1的视为不可走的。


clear;
clc;
clf;
figure(1);
map =[
0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0
0	0	0	.3	0	0	0	1	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	1	0	0	.7	0	0	0	0	0
0	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	1	1	1	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	1	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
];
pcolor(map)
colormap summer
[row,col] = size(map);
[start_px,start_py] = find(map == .3);
[end_px,end_py] = find(map == .7);

close = struct([]); 
closelen = 0;
open = struct([]); 
openlen = 0;

%% 将起点添加到open列表
open(1).row = start_px;
open(1).col = start_py;
open(1).g = 0;
open(1).h = abs(end_py - start_py) + abs(end_px - start_px);
openlen = openlen + 1;
%% 四种运动格式
sport = [0,1;0,-1;-1,0;1,0];
backNum = 0;
prev = [];
while openlen > 0
    %% 获取代价最小的值
    for i = 1:openlen
        f(i,:) = [i,open(i).g + open(i).h];       
    end
    f1 = sortrows(f,2);
    current = open(f1(1));
    choose = 0;
    chooseArr = [];
    %% 回溯将走过的点标记出来
     if current.row == end_px && current.col == end_py
         i = 1;
         while(i<=size(prev,1))
             if prev(i,3) == current.row && prev(i,4) == current.col
                 choose = choose +1;
                 chooseArr(choose,1) = prev(i,1);
                 chooseArr(choose,2) = prev(i,2);
                 current.row =  prev(i,1);
                 current.col =  prev(i,2);
                 i = 1;
             else
                 i = i + 1;
             end
         end      
         for j = 1: size(chooseArr,1)
                map(chooseArr(j,1),chooseArr(j,2)) = 0.5;
         end
         figure(2);
         pcolor(map);
         colormap winter;
         return;         
     end
     closelen = closelen + 1;
     close(closelen).row = open(f1(1)).row;
     close(closelen).col = open(f1(1)).col;
     close(closelen).g = open(f1(1)).g;
     close(closelen).h = open(f1(1)).h;   
     open(f1(1)) = [];
     openlen = openlen -1;     
    for i = 1:4
        dimNormal = all([current.row,current.col]+sport(i,:)>0) ...
            && current.row+sport(i,1)<=row && current.col+sport(i,2)<=col;
        neighbor.row = current.row + sport(i,1);
        neighbor.col = current.col + sport(i,2);
        neighbor.g = abs(start_px - neighbor.row) + abs(start_py - neighbor.col);
        neighbor.h = abs(end_px - neighbor.row) + abs(end_py - neighbor.col);
    
    
        if dimNormal
            inCloseFlag = 0; 
            if closelen ==0
            else
                for j = 1:closelen
                    if close(j).row == neighbor.row && close(j).col ==neighbor.col
                        inCloseFlag = 1;
                        break;
                    end
                end
            end
        
            if inCloseFlag
                continue;
            end
            
            temp_g = current.g + abs(current.row - neighbor.row) + abs(current.col - neighbor.col);
            inOpenFlag = 0;
            for j =1:openlen
                if open(j).row == neighbor.row && open(j).col ==neighbor.col
                    inOpenFlag = 1;
                    break;
                end
            end        
            if ~inOpenFlag && map(neighbor.row,neighbor.col) ~= 1
                openlen = openlen + 1;
                open(openlen).row = neighbor.row;
                open(openlen).col = neighbor.col;
                open(openlen).g = abs(start_px - neighbor.row) + abs(start_py - neighbor.col);
                open(openlen).h = abs(end_px - neighbor.row) + abs(end_py - neighbor.col);               
            elseif temp_g >= neighbor.g
                continue;
            end
                backNum = backNum +1;
                prev(backNum,:) = [current.row ,current.col,neighbor.row ,neighbor.col];
                neighbor.g = temp_g;            
        else
            continue;
        end
    end     
end

生成初始图:
在这里插入图片描述

开始搜索

我们已经完成地图搜寻区域的准备工作,下面我们基于可以移动的四个动作(上、下、左、右)来进行搜索,我们使用openList来维护需要展开的待搜索的节点,在最开始的时候,只有起点这一项。之后,openList里面的元素可能是会经过的,也有可能是不经过的,但是经过的点都应该在openList中存在或者曾经存在。另外,对于我们已经访问过的点,我们使用closeList来进行记录,避免多次访问。

代价函数:
f(v) = g(v) + h(v)
g(v)表示由起始点到当前节点的最小cost;
h(v)表示由当前结点到目标节点的最小cost的估计值;
这里,为方便计算,笔者统一选取了曼哈顿距离用来计算g(v)和h(v)的cost值。

算法伪码:

function AStar_Routing(Gragh(V,E),src,dst)
	create vertex List openList
	create vertex List closeList
	create prev_map
	insert src into openList
	while(openList.isNotEmpty)
		current = the node v in openList s.t.  min(f[v]) in openList
		if current = dst
			return  reconstruction_route(prev_map,current)
		endif
		remove current from openList
		insert current into closeList
		for each neighbor u of current
			if u in closeList
				continue;
			endif
			temp_u = g[current] + h(current,u)
			if u not in openList
				insert u into openList
			elseif temp_u >= g[u]
				continue;
			endif
			prev_map[u] = current			
			g[u] = temp_cost
			f[u] = g[u] + h(current,dst)
	endwhile

回溯后,整体PATH效果如下:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/188079.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 旁路由设置的正确方式

    旁路由设置的正确方式最近在玩旁路由,踩了一些坑,也学习了点相关知识,特整理记录下。一、旁路由的配置上图是旁路由的连接方式,一般作为旁路由的只有一个LAN口,可以把它想成一个普通的连接路由器的电脑。让他们ip在一个网段即可,比如主路由网关192.168.3.1,旁路由配置成192.168.3.21.主路由配置:DHCP配置,把网关和DNS改成旁路由ip地址,如192.168.3.2。2.旁路由配置:关闭DHCP,把网关改成主路由地址,如192.168.3.1,关闭桥接模式。这样配置后,网络流量如下图:.

    2022年6月14日
    55
  • petshop java_petshop.java[通俗易懂]

    petshop java_petshop.java[通俗易懂]importjava.util.Scanner;classPet{privateintnumber;privateStringvariety;privateStringcolor;privateintage;privateintprice;publicPet(intnumber,Stringvariety,Stringcolor,intage,intprice)…

    2022年10月17日
    2
  • 物联网开发实战:手把手教你开发一款久坐提醒小助手

    物联网开发实战:手把手教你开发一款久坐提醒小助手过几天就是跟女朋友的恋爱纪念日了,作为一名程序员,送什么礼物才能既有创意又有诚意,既实用又能让女朋友感受到我满满的爱呢?我突然想到,女朋友平时工作忙,有时候一坐就是一整天,连水都会忘记喝。于是我想,何不利用我的专业技能,开发一个”久坐提醒“和”喝水提醒“小助手送给女朋友呢。

    2022年10月1日
    3
  • HDOJ1078 记忆化搜索入门题 有详细的记忆化搜索模板程序

    HDOJ1078 记忆化搜索入门题 有详细的记忆化搜索模板程序FatMouseandCheeseTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):10863    AcceptedSubmission(s):4625ProblemDescriptionFatMo

    2022年7月26日
    8
  • 主、外键约束_创建主键约束

    主、外键约束_创建主键约束主、外键约束点关注不迷路,欢迎再来!主键和外键是两种类型的约束;1.主键是能唯一的标识表中的每一行,就是说这一列非空且值不重复,可以指定为主键;作用是用来强制约束表中的每一行数据的唯一性;2.外键是b表中的某一列引用的值来源于a表中的主键列。也是约束b表中的外键列的值必须取致a表中的主键列值,不是其中的值就不能插入b表中。可以形成a表b表的联系,保持数据的约束和关联性。创建主表主键…

    2022年10月20日
    2
  • 60.0.1(64位)windows版 uploadify使用有问题

    60.0.1(64位)windows版 uploadify使用有问题

    2021年10月26日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号