[Path planning] A* algorithm robot path planning [Matlab 469]

1. Introduction

A algorithm
A algorithm is a typical heuristic search algorithm, based on the Dijkstra algorithm, and is widely used in game maps and the real world to find the shortest path between two points. The main thing of the A algorithm is to maintain a heuristic evaluation function, as shown in equation (1).
f(n)=g(n)+h(n)(1)
Among them, f(n) is the corresponding heuristic function when the algorithm searches for each node. It consists of two parts. The first part g(n) is the actual travel cost from the starting node to the current node, and the second part h(n) is the estimated value of the travel cost from the current node to the end point. Each time the algorithm expands, the node with the smallest value of f(n) is selected as the next node on the optimal path.
In practical applications, if the shortest distance is the optimization goal, h(n) is often taken as the Euclidean Distance or Manhattan Distance from the current point to the end point. If h(n)=0, it means that no information about the current node and end point is used, and the A
algorithm degenerates to the non-heuristic Dijkstra algorithm. The algorithm search space becomes larger and the search time becomes longer.
The steps of the A* algorithm are as follows. The algorithm maintains two sets: P list and Q list. The P table stores those nodes that have been searched but have not been added to the optimal path tree; the Q table maintains those nodes that have been added to the optimal path tree.
(1) Set the P and Q tables to be empty, add the starting point S to the P table, set its g value to 0, the parent node is empty, and set the g value of other nodes in the road network to infinity.
(2) If the P list is empty, the algorithm fails. Otherwise, select the node with the smallest value of f in the P list, record it as BT, and add it to the Q list. Judge whether BT is the end point T, if yes, go to step (3); otherwise, find each adjacent node NT of BT according to the topological attributes of the road network and traffic rules, and perform the following steps:

①Calculate the heuristic value of
NT f(NT)=g(NT)+h(NT)(2)
g(NT)=g(BT)+cost(BT, NT)(3)
where cost(BT, NT) It is the cost of passing BT to NT.
② If NT is in the P table, and the g value calculated by formula (3) is smaller than the original g value of NT, then the g value of NT is updated to the result of formula (3), and the parent node of NT is set to BT.
③If NT is in the Q table, and the g value calculated by formula (3) is smaller than the original g value of NT, then the g value of NT is updated to the result of formula (3), and the parent node of NT is set to BT, and Move NT to the P list.
④ If NT is neither in the P list nor in the Q list, set the parent node of NT to BT and move NT to the P list.
⑤Go to step (2) to continue execution.
(3) Backtrack from the end point T, find the parent node in turn, and add it to the optimized path until the starting point S, then the optimized path can be obtained.

Second, the source code

close all; clear all;
%initial the map size
size_x = 10;
size_y = 10;
size_t = 100;
%1 - white - clear cell
%2 - black - obstacle
%3 - Grayish blue   - robot 1
%4 - Reddish blue - robot 2
%5 - purple - robot 3
%6 - vivid green - robot 4
%7 - Plum red - robot 5
cmap = [1 1 1;            
              0 0 0;
              0.69 0.87 0.90;
              0.25 0.41 0.88;
              0.63 0.13 0.94;
              0 1 0.5;
              0.87 0.63 0.87;
              ];
colormap(cmap);    %将颜色进行映射
%initial the space_time_map and reservation table
space_map = ones(size_x, size_y);
space_time_map = ones(size_x, size_y, size_t);
reservation_table = zeros(size_x, size_y, size_t);
%add the static obstacle
space_time_map(1:5, 5, :) = 2*ones(5,1, size_t);
reservation_table(1:5, 5, :) = 2*ones(5,1, size_t);
%the start point array and end point array
start_points= [2 4; 5 1];%[2 4; 5 1; 1 1; 9 6;7 3 ];
end_points = [8 7; 8 10];%[8 7; 10 10; 7 3; 3 2; 1 1];
len_route_max = 0;
route_all = [];
for index = 1:length(start_points(:,1))
%initial the start point and the end point
function [route] = Time_Astar_For_Cooperative_Path_Finding(start_coords, end_coords, space_time_map, reservation_table)
%1 - white - clear cell
%2 - black - obstacle
%3 - red - visited
%4 - blue - on list
%5 - green - start
%6 - yellow - destination
cmap = [1 1 1;
              0 0 0;
              1 0 0;
              0 0 1;
              0 1 0;
              1 1 0;
              0.5 0.5 0.5];
colormap(cmap);
[size_x, size_y, size_t] = size(space_time_map);
%add the start point and the end point 
space_time_map(start_coords(1), start_coords(2), :) = 5;
space_time_map(end_coords(1), end_coords(2), :) = 6;
%initial parent array and Heuristic array
parent = zeros(size_x, size_y, size_t);
[X, Y] = meshgrid(1:size_y, 1:size_x);
xd = end_coords(1);
yd = end_coords(2);
H =abs(X-xd) + abs(Y-yd);
H = H';
%initial cost arrays
g = inf(size_x, size_y, size_t);
f = inf(size_x, size_y, size_t);
g(start_coords(1), start_coords(2), 1) = 0;
f(start_coords(1), start_coords(2), 1) = H(start_coords(1), start_coords(2));
end_time = 1;
while true
    %find the node with the minimum f values
    [min_f, current] = min(f(:));
    [current_x, current_y, current_t] = ind2sub(size(space_time_map), current);
    if((current_x == end_coords(1) && current_y == end_coords(2))  || isinf(min_f))
        end_time = current_t;
        disp('hheheh');
        break;
   end
    %add the current node to close list
    space_time_map(current) = 3;
    f(current) = Inf;
    [i, j, k] = ind2sub(size(space_time_map), current);
    neighbours = [i-1, j, k+1;
                           i+1, j, k+1;
                           i, j-1, k+1;
                           i, j+1, k+1;
                           i, j, k+1];
    for index = 1:size(neighbours, 1)
            px = neighbours(index, 1);
            py = neighbours(index, 2);
            pt = neighbours(index, 3);
             % judge whether out of bound or not
            if (px >0 && px<=size_x && py >0 && py<= size_y )
                sub_neighbours = sub2ind(size(space_time_map), px, py, pt);
                %judge whether the node is obstacle, start point, end
                %point, or not
                if(space_time_map(sub_neighbours) ~= 2 && space_time_map(sub_neighbours) ~= 5 && space_time_map(sub_neighbours) ~= 3)
                    %judge whether the node has less f 
                    if(g(current) +1+ H(px, py) < f(sub_neighbours))
                        %judge whether the node is in reservation table or not
                        if (~reservation_table(sub_neighbours ))
                            %judge whether the special action
                             [special_action] = Special_action(current, sub_neighbours,reservation_table);
                            if (~special_action)
                                g(sub_neighbours) = g(current) + 1;
                                f(sub_neighbours) = g(sub_neighbours) +  H(px, py);
                                parent(sub_neighbours) = current;
                                space_time_map(sub_neighbours) = 4;
                            end
                       end
                    end
                end
            end
    end
    

Three, running results

Insert picture description here
Insert picture description here

Four, remarks

Complete code or writing to add QQ912100926 past review
>>>>>>
[Path planning] Particle swarm optimization algorithm for three-dimensional UAV path planning [Matlab 012]
[Path planning] Genetic algorithm for open vehicles in multiple logistics centers Path planning [Matlab 013]
[Path planning] Particle swarm algorithm for robot grid path planning [Matlab 014]
[Path planning] Ant colony algorithm for solving the shortest path [Matlab 015]
[Path planning] Immune algorithm for logistics center Site selection [Matlab 016]
[Path planning] Three-dimensional path planning of drones using artificial bee colony [Matlab 017]
[Path planning] Genetic algorithm based on grid map robot path planning [Matlab 018]
[Path planning] Ant Multi-UAV attack scheduling
based on swarm algorithm [Matlab 019] [Path planning] Genetic algorithm for optimal path planning of robots based on grid map [Matlab 020]
[Path planning] Genetic algorithm with multiple unmanned systems considering the order of distribution [Matlab 021 Issue]
[Path Planning]
Multi-center VRP Problem of Ant Colony Algorithm [Matlab 022] [Path Planning] Ant Colony Algorithm Solving Multi-center VRP with Time Window [Matlab 023 Issue]
[ Path planning] Multi-center VRP solution based on genetic algorithm [Matlab 024]
[Path planning] Simulated annealing to solve VRP problem [Matlab 025]
[Path planning] A star grid path planning [Matlab 026]
[Path planning] Based on A two-way optimization particle swarm grid map path planning with cross factor [Matlab 027]
[Path Planning] [TSP] Ant Colony Algorithm for Solving TSP Problem with GUI [Matlab 028]
[Path Planning] Ant Colony Algorithm for Raster Map Path Planning [Matlab 029]
[Path Planning] Genetic Algorithm for Traveling Salesman TSP [ Matlab Issue 030]
[Path Planning] Traveling Salesman TSP Problem of Simulated Annealing Algorithm [Matlab 031]
[Path Planning] Intelligent Car Path Planning of Ant Colony Algorithm [Matlab 032]
[Path Planning] Huawei Cup: UAV based on matlab Optimized application in emergency rescue and disaster relief [Matlab 033]
[Path planning] MATLAB's minimum cost and maximum flow calculation problem [Matlab 034]
[Path planning] A algorithm to solve the three-dimensional path planning problem [Matlab 035]
[Path planning] Artificial bee Path planning of group algorithm [Matlab036 period]
[Path planning] Path planning of artificial bee colony algorithm [Matlab 037]
[Path planning] Ant colony algorithm to solve multi-traveling salesman MTSP problem [Matlab 038]
[Path planning] Ant colony Algorithm for UAV path planning [Matlab 039]
[Path planning] Genetic algorithm to solve multiple VRP problems [Matlab 040]
[VRP] Genetic algorithm for vehicle routing problem with time windows [Matlab 041]
[Path planning] Three-dimensional path planning of ant colony algorithm [Matlab 042]
[Path planning] Particle swarm optimization ant colony to solve the shortest path [Matlab 043]
[TSP problem] Differential evolution to solve the TSP problem [Matlab 044]
[Path planning] RRT Three-dimensional path planning【Matlab 144 period】
[Path planning] UAV formation path planning based on artificial potential field algorithm [Matlab 145]
[VRP problem] Saving algorithm to solve TWVRP problem [Matlab 146]
[VRP problem] Saving algorithm to solve CVRP problem [Matalb 147]
[VRP problem] Taboo search algorithm to solve VRP problem [Matalb 148]
[VRP problem] Simulated annealing algorithm to solve CVRP problem [Matlab 149]
[VRP problem] Simulated annealing to solve the TWVRP problem with time window [Matlab 150]
[VRP problem] Artificial fish swarm algorithm to solve VRP problem with time window [Matlab 151]
[VRP problem] Genetic algorithm to solve VRP problem with capacity [Matlab 152]
[Path planning] Three-dimensional path planning of wolf swarm algorithm[ Matlab Issue 153]
[Path Planning] Artificial Potential Field Algorithm for UAV 3D Path Planning [Matlab 154]
[Path Planning] Improved Difference Algorithm for 3D Multi-UAV Cooperative Track Planning [Matlab Issue 155]
[Path Planning] Artificial bee colony algorithm for multi-UAV three-dimensional path planning [Matlab 156]
[Path planning] Sparrow search algorithm for UAV three-dimensional path planning [Matlab 157]
[Path planning] Ant colony algorithm for three-dimensional path planning [Matlab 158 period]
[path] planning the shortest path planning algorithm of immune Matlab 159 [of]
[TSP] algorithm to solve the traveling salesman problem of immune Matlab 160 [period]
[path] bus scheduling system planning analysis of genetic algorithms in Matlab 161 [ 】
【TSP】TSP solution of Hopfield particle swarm algorithm 【Matlab 162】
[Path planning] A and improved A* path planning [Matlab 163]
[TSP] Improved tabu search algorithm for solving traveling salesman problem [Matlab 170]
[TSP] Improved ant colony algorithm for solving traveling salesman problem [Matlab Issue 171]
[Path planning] Simulated annealing algorithm for solving the shortest path of fire patrol [Matlab 193]
[Three-dimensional path planning] ant colony algorithm to optimize the three-dimensional path of the submersible [Matlab 194]
[Three-dimensional path planning] Matlab ant colony algorithm UAV inspection path [Matlab 195]
[3D path planning] 3D dynamic simulation of UAV [Matlab 196]
[3D path planning] UAV three-dimensional space trajectory planning [Matlab 228]
[Path planning] Distribution Multi-UAV for target detection and tracking [Matlab 229]
[Path planning] Particle swarm algorithm to solve the shortest path of drones [Matlab 277]
[UAV] Multi-unmanned cooperative task allocation program platform [Matlab278]
[Path planning] Multi-UAV collaborative mission planning [Matlab 279]
[Path planning] Arbitrary plant protection drone operation path planning [Matlab 280]
[Path planning] Particle swarm genetic algorithm for multi-UAV three-dimensional path planning [ Matlab 281 issue]
[VRP problem] particle swarm to solve the VRPTW model [Matlab 282]
[Path planning] Improved ant colony algorithm path planning [Matlab 283]
[VRP] Improved simulated annealing and genetic algorithm to solve the VRP problem [Matlab 284 Issue]
[VRP Problem] Grey Wolf Algorithm to Solve VRPTW Problem [Matlab Issue 285]
[VRP problem] Genetic algorithm and simulated annealing to solve the bicycle scheduling problem with time windows [Matlab 286]
[Path planning] Improved artificial potential field method for dynamic and static obstacle avoidance of robots [Matlab 287]
[TSP] Hybrid particle swarm to solve TSP Problem [Matlab Issue 288]
[TSP] Ant Colony Algorithm to Solve Traveling Salesman Problem [Matlab Issue 289]
[TSP] Hopfield Neural Network to Solve TSP Problem [Matlab Issue 290]
[TSP] Ant Colony Algorithm to Solve 76 City TSP Problem [Matlab Issue 291] 】
【Path planning】Particle swarm
algorithm to solve the logistics location 【Matlab 292】【TSP】Artificial fish swarm algorithm to solve the TSP problem 【Matlab 293】
【TSP】Basic particle swarm algorithm to solve the traveling salesman problem 【Matlab 308】
【Tracks Planning] Astar algorithm three-dimensional trajectory planning [Matlab 361 issue]
[TSP] Tabu search algorithm to solve TSP problem [Matlab 465]
[[Path planning] Dynamic multi-swarm particle swarm algorithm local search [Matlab 466]]
[Path planning] Shortest path planning in two-dimensional and three-dimensional space with dynamic programming [Matlab 467]
[Path planning] Improved particle swarm algorithm path planning [Matlab 468]

Guess you like

Origin blog.csdn.net/m0_54742769/article/details/114956340