a星算法详解_matlab优化算法

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


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)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • Docker 离线安装_安装下载

    Docker 离线安装_安装下载Docker是在Linux容器里运行应用的开源工具,是一种轻量级的”虚拟机”。Docker的Logo设计为蓝色鲸鱼,拖着许多集装箱。如下图,鲸鱼可以看作宿主机,而集装箱可以理解为相互隔离的容器。每个集装箱中都包含自己的应用程序

    2022年9月26日
  • 宽度学习(BLS)网络的研究和应用[通俗易懂]

    宽度学习(BLS)网络的研究和应用[通俗易懂]宽度学习(BLS)网络的研究和应用  除了上述这几大AI学术研究要点之外,还有宽度学习(BLS)网络也值得重点关注。宽度学习(BLS)自2018年由我们(陈俊龙教授及其团队)首次在学术界提出,便迅速在科研机构(中科院)、国内知名高校及企业展开了较为广泛的研究与应用。  虽然深度学习网络非常强大,但大多数网络都被极度耗时的训练过程所困扰。首先深度网络的结构复杂并且涉及到大量的超参数。另…

    2022年5月11日
  • 常见的路由协议「建议收藏」

    常见的路由协议「建议收藏」常见的路由协议可以分为两种类型一、内部网关协议内部网关协议(IGP:InteriorGatewayProtocol),适用于单个ISP的统一路由协议的运行,一般由一个ISP运营的网络位于一个AS(自治系统)内,有统一的ASnumber(自治系统号),用来处理内部路由。RIP、IGRP(Cisco私有协议)、EIGRP(Cisco私有协议)、OSPF、IS-IS等都是内部网关协议。1、…

    2022年10月22日
  • calendar类的方法_unsafe类常用方法

    calendar类的方法_unsafe类常用方法概念java.util.Claendar日历类,抽象类,在Date类后出现的,替换掉了很多Date类中的方法,该类将所有的可能用到的时间信息封装为静态成员变量,通过类名.静态成员变量获取时间字段值获取方式由于Calendar类是一个抽象类,语言敏感性,Calendar类并不是直接创建对象来获取时间属性值,而是通过静态方法创建,返回子类对象。静态方法如下:publicstaticCalendargetInstance();使用默认时区和默认的语言环境获取一个对象…

    2022年9月23日
  • java常量池和字符串常量池_java声明常量

    java常量池和字符串常量池_java声明常量本篇文章带大家认识Java基础知识——字符串类,在前面我们已经知道如何在Java中定义字符串,本文将介绍Java字符串中的字符串常量池,探究字符串相等问题。

    2022年7月28日
  • SQLite的使用——图片存储

    SQLite的使用——图片存储在上篇有说到SQLite的文本存储,文本的增删改查,在本篇中主要说SQLite对单张图片的存储。简单介绍:SQLiteOpenHelper是一个抽象类。SQLiteOpenHelper中有两个抽象方法,分别是onCreate()和onUpgrade()。SQLiteOpenHelper中还有两个非常重要的实例方法,getReadableDatabase()和getWritableD…

    2022年7月12日

发表回复

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

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