简体   繁体   中英

How to find a shortest path in a graph that while travelling it, you can "see" all the nodes within a radius

I have a 2d grid with some of the tiles being obstacles (walls), I want to be able to find the shortest path that allows you to go around the grid being able to see all of the other grids in the map with a radius of view. Here is a pixel art example (Blacks are the obstacles, gray is an arbitrary path). 在此处输入图像描述

  • Set R to be the "radius of view"
  • Create orthogonal point grid with separation 2 * R
  • Remove grid points that collide with obstacles
  • Add connections from each point to the 9 or less closest points. Do not connect across obstacles
  • Calculate minimum spanning tree of remaining points.

Specify an "obstacle course" with a 20 by 20 gris, a view radius of 2 and three groups of abstacles

20 20 2
4 4
5 5
6 6
14 9
14 10
14 11
14 12
14 13
14 14
14 15
14 16
10 16
11 16
12 16
13 16
4 14
4 15
5 15

It looks like this

在此处输入图像描述

Adjacency list ( link id, node1 id, node2 id, distance )

l 42 47 5
l 42 142 5
l 47 52 5
l 47 152 7.07107
l 52 57 5
l 52 147 7.07107
l 52 152 5
l 52 157 7.07107
l 57 152 7.07107
l 57 157 5
l 142 242 5
l 142 247 7.07107
l 147 152 5
l 147 242 7.07107
l 147 247 5
l 147 252 7.07107
l 152 247 7.07107
l 152 252 5
l 157 257 5
l 242 247 5
l 242 342 5
l 247 252 5
l 247 347 5
l 257 357 5
l 342 347 5
l 347 352 5
l 352 357 5

Pass this graph into your favorite graph theory library to get the minimum spanning tree.

Here is the result when I use the PathFinder application

在此处输入图像描述

The spanning tree is useful if, for example, the robot needs to return to a base frequently to recharge. However, if your robot does not need to do this, then it involves a lot of unnecessary backtracking.

Calculation of a tour of the nodes that visits all of them with minimal backtracking can be done in various ways ( see travelling salesman problem ). I use depth first searching of the spanning tree plus using the Dijsktra algorithm to find the nearest unvisited node when the robot gets trapped at a leaf of the spanning tree. This quickly gives a reasonably effective result.

在此处输入图像描述

Application coded in C++, available at https://github.com/JamesBremner/obstacles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM