路径规划——Jump Point Search算法

路径规划——Jump Point Search算法

算法原理

跳点搜索算法(Jump Point Search),简称JPS,是由澳大利亚两位教授于2011年提出的基于Grid格子的寻路算法。JPS算法在保留A Star算法的框架的同时,进一步优化了A Star算法寻找后继节点的操作。
A Star算法可见:A Star

JPS与A Star算法的主要区别在后继节点的拓展策略上,不同于A Star算法中直接获取当前节点所有非已访问过的可达邻居节点来进行拓展的策略,JPS根据当前节点的方向,并基于搜索跳点的策略来拓展后继节点,遵循“两个定义、三个规则”。

定义一,强迫邻居(forced neighbor):
如果节点n是x的邻居,并且节点n的邻居有障碍物,并且从parent(x)经过x再到n的路径长度比其他任何从parent(x)不经过x到n的路径短,其中parent(x)为路径中x的前一个点,则n为x的强迫邻居,x为n的跳点。

举例来具体说,如上图
先看直线方向(straight),x的父节点是4,如果从4经过x到达节点node与从4不经过到达节点node相比,前者路径较长则没必要加入到x的邻居节点(如节点1、2、3、6、7、8),其中节点3与8两者路径是相等的,这时路径具有对称性,而JPS的核心便是打破这种对称,所以节点3和8也是不需要加入的,这些节点称为inferior neighbors劣性节点;前者路径较短则需要将节点node加入到x的邻居节点(如节点5),这种节点称为natural neighbors自然节点;
再来看对角线方向(diagonal),x的父节点是6,同理,劣性节点有1、2、7、8,自然节点有2、3、5;

如果有障碍物,如下图

在这里插入图片描述

当原先的节点2处有障碍物时,从父节点4经过x到达节点3比不经过x到达节点3路径更短,这时节点3便不是inferior neighbor了,需要作为x的有效邻居节点进一步考虑,这种节点就是forced neighbor强迫邻居了。同样,对角线方向移动时,原先的节点4处有障碍物时,从父节点6经过x到达节点1比不经过节点x到达节点1路径更短,这时节点1便是forced neighbor加入到openlist了。

定义二,跳点(Jump Point):
1.如果点y是起点或者目标点,则y是跳点;
2.如果y有强迫邻居则y是跳点;
3.如果parent(y)到y是对角移动,并且y经过水平或垂直方向移动可以到达跳点,则y是跳点。

在这里插入图片描述

先看Jumping Straight,从p(x)到x,向左一直推进直到y,节点y有一个forced neigbor节点z,那么节点y便作为跳点加入到openlist,此处对应于定义2。
再看Jumping Diagonally,从p(x)沿着对角方向到x,然后节点x沿着水平方向和垂直方向搜索,均没有发现感兴趣的节点,于是继续沿着对角方向推进,一直到节点y,节点y沿着水平方向搜索时发现了感兴趣节点z,节点z有forced neighbor节点,那么节点y便作为跳点成为了节点x的后继节点,此处对应于定义3,被加入到openlist中,节点z被标记为特殊点,之后返回到y,并沿着垂直方向搜索,垂直方向上无特殊点,那么节点x的这个方向上的工作便结束了,之后沿着另一个对角方向搜索,有forced neighbor节点w,那么节点w也作为跳点被加入到openlist中,此处对应于定义2。

**规则一:**JPS搜索跳点的过程中,如果直线方向(为了和对角线区分,直线方向代表水平方向和垂直方向,且不包括对角线等斜线方向)和对角线方向都可以移动,则首先在直线方向搜索跳点,再在对角线方向搜索跳点。

**规则二:**1.如果从parent(x)到x是直线移动,n是x的邻居,若有从parent(x)到n的路径不经过x且路径长度小于或等于从parent(x)经过x到n的路径,则走到x后下一个点不会走到n;
2.如果从parent(x)到x是对角线移动,n是x的邻居,若有从parent(x)到n的路径不经过x且路径长度小于从parent(x)经过x到n的路径,则走到x后下一个点不会走到n。

**规则三:**只有跳点才会加入openlist,因为跳点会改变行走方向,而非跳点不会改变行走方向,最后寻找出来的路径点也都是跳点。

算法流程:

首先检查当前节点是否有强迫邻居,如果有,那么此节点便可以作为跳点加入到openlist中,如果没有,那么遵循三个规则沿着水平方向、垂直方向和对角方向寻找跳点。

算法实现

"""
    Filename: jps.py
    Description: Plan path using Jump-Point-Search Algorithm
    Author: Benxiaogu:https://github.com/Benxiaogu
    Date: 2024-08-18
"""
import heapq
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation


class JPS:
    def __init__(self,grid,start,goal,board_size):
        self.grid = grid
        self.start = self.Node(start,0,0)
        self.goal = self.Node(goal,0,0)
        self.board_size = board_size
        self.path = []

    class Node:
        def __init__(self, position, g, h, parent=None):
            self.position = position    # position of node
            self.g = g                  # distance from node to start
            self.h = h                  # heuristic value from node to goal
            self.parent = parent        # parent node

        def __eq__(self, other
Jump Point Search算法是一种近年来提出的新型寻路算法,能够在路网数据中快速过大量的不可行区域,极大地提高了寻路效率。以下是一个简单的C++实现示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <cmath> using namespace std; const int MAXN = 100; const int INF = 0x3f3f3f3f; int n, m; // 路网数据的行列数 int sx, sy, ex, ey; // 起和终的坐标 int map[MAXN][MAXN]; // 路网数据 int dis[MAXN][MAXN]; // 距离起的距离 bool vis[MAXN][MAXN]; // 是否已经访问过 int dir[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; // 方向数组 struct Node { int x, y, f, g, h; // 坐标、f值、g值、h值 bool operator<(const Node& a) const { return f > a.f; // 优先队列为小根堆,f值小的优先级高 } }; bool is_valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m && map[x][y] == 0; } vector<pair<int, int>> get_successors(int x, int y, int dx, int dy) { vector<pair<int, int>> successors; if (dx != 0 && dy != 0) { if (is_valid(x + dx, y + dy) && (!is_valid(x + dx, y) || !is_valid(x, y + dy))) { successors.push_back({x + dx, y + dy}); } if (is_valid(x + dx, y) && !is_valid(x, y + dy)) { successors.push_back({x + dx, y}); successors.push_back({x + dx, y + dy}); } if (is_valid(x, y + dy) && !is_valid(x + dx, y)) { successors.push_back({x, y + dy}); successors.push_back({x + dx, y + dy}); } } else { if (dx == 0) { if (is_valid(x, y + dy)) { successors.push_back({x, y + dy}); if (!is_valid(x + 1, y)) successors.push_back({x + 1, y + dy}); if (!is_valid(x - 1, y)) successors.push_back({x - 1, y + dy}); } } else { if (is_valid(x + dx, y)) { successors.push_back({x + dx, y}); if (!is_valid(x, y + 1)) successors.push_back({x + dx, y + 1}); if (!is_valid(x, y - 1)) successors.push_back({x + dx, y - 1}); } } } return successors; } void jump(int x, int y, int dx, int dy) { int nx = x + dx, ny = y + dy; if (!is_valid(nx, ny)) return; if (nx == ex && ny == ey) { dis[nx][ny] = dis[x][y] + 1; return; } if (vis[nx][ny]) return; vis[nx][ny] = true; int g = dis[x][y] + 1; int h = abs(nx - ex) + abs(ny - ey); int f = g + h; priority_queue<Node> pq; pq.push({nx, ny, f, g, h}); while (!pq.empty()) { Node u = pq.top(); pq.pop(); int x = u.x, y = u.y; if (vis[x][y]) continue; vis[x][y] = true; dis[x][y] = u.g; vector<pair<int, int>> successors = get_successors(x, y, dx, dy); for (auto& s : successors) { int nx = s.first, ny = s.second; int g = dis[x][y] + 1; int h = abs(nx - ex) + abs(ny - ey); int f = g + h; pq.push({nx, ny, f, g, h}); } } } void JPS() { memset(dis, INF, sizeof(dis)); memset(vis, false, sizeof(vis)); dis[sx][sy] = 0; vis[sx][sy] = true; priority_queue<Node> pq; pq.push({sx, sy, abs(ex - sx) + abs(ey - sy), 0, abs(ex - sx) + abs(ey - sy)}); while (!pq.empty()) { Node u = pq.top(); pq.pop(); int x = u.x, y = u.y; if (x == ex && y == ey) return; for (int i = 0; i < 8; i++) { int dx = dir[i][0], dy = dir[i][1]; if ((dx == 0 || dy == 0) || (is_valid(x + dx, y) && is_valid(x, y + dy))) { jump(x, y, dx, dy); } } } } int main() { cin >> n >> m >> sx >> sy >> ex >> ey; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> map[i][j]; } } JPS(); cout << dis[ex][ey] << endl; return 0; } ``` 这段代码实现了Jump Point Search算法的基本框架,包括预处理、递归跃、状态转移和优先队列的使用。需要注意的是,Jump Point Search算法的预处理过程比较耗时,因此它更适用于需要多次寻路的场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笨小古

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值