[Path planning] The optimal path planning of the robot based on grid map based on genetic algorithm [Matlab 020]

1. Introduction

Using grids to divide the robot's workspace and then using optimization algorithms to optimize the robot path is a classic problem of using intelligent algorithms to find the optimal path. At present, the use of ant colony algorithm to optimize the path on the grid map has achieved relatively good results, and the use of genetic algorithm to optimize the path on the grid map is more difficult to achieve in the algorithm.
The difficulty of using genetic algorithm to process the robot path planning of the grid map mainly includes: 1 to ensure that the path is uninterrupted, and 2 to ensure that the path does not pass through obstacles.
When using genetic algorithms to solve optimization problems, the steps are fixed, that is, population initialization, selection, crossover, mutation, and fitness calculation. Then I will talk about the problem of genetic algorithm for robot path planning in raster maps at each step. , Difficulties and solutions.
1. Population initialization
First of all, we must know the definition of genetic algorithm population initialization. The genetic algorithm population initialization is to generate a certain population of individuals, and each individual is a feasible solution. It should be noted here that it is a feasible solution. For this problem, it is a feasible path, that is to say, it should be a path from the beginning of the path to the end of the path without passing obstacles. How to initialize the population to obtain a feasible path is the first and most difficult point of the genetic algorithm to find the grid path.
method
Path initialization can be divided into two steps: the first step is to generate a path that must pass through nodes. What is a path that must pass through nodes? For example, for a 10*10 grid, the path from number 1 in the lower left corner to number 100 in the upper right corner must pass through a point in row 2, a point in row 3... a point in row 9, this is Obviously, then we randomly select a node in the free grid in the second row, and a node in the free grid in the third row... Take a node in the free grid in the 9th row, then it will be a must The path through points, of course, this path is also intermittent. So the second step is needed. The second step is to connect these discontinuous nodes, and this problem is the most difficult problem in the algorithm, because in the connection path, it is too difficult to bypass obstacles, and if you bypass the obstacles, you will I found that I lost my direction and couldn't even return. Therefore, the midpoint connection method is used in the connection nodes, but the midpoint must be skillfully obtained. You can take 4 or 3 grids at the midpoint, and then find the free grids in these grids, and choose with equal probability. In the worst case, the grids at these midpoints are all obstacles, and one of these grids is selected as the path point with a moderate probability. Therefore, this method ensures the continuity of the path, but there may also be paths passing through obstacles. The path of this obstacle can be penalized in the fitness function.
After the two steps of the above initialization path are completed, the simplified path operation is performed, that is, if there are two identical nodes in the path, the section between the same nodes is removed. This is easy to understand.
At this point the initialization path is over, and you have made a big step!
2. Selection
The selection is the same as the selection of genetic algorithm, no special changes are required, and the selection is based on fitness.
3.
Crossover uses repeated point crossover. This is also easier to understand. For example, one path is [1 12 13 24 25 36 45 56], and the other path is [1 14 25 35 46 56]. The two paths have a common At node 25, crossover becomes [1 12 13 24 25 35 46 56] and [1 14 25 36 45 56].
4. Mutation
The mutation method uses random generation to select two nodes, and remove the path between these two nodes, and then use the above-mentioned interpolation method to generate a continuous path.
5. Fitness calculation The
fitness calculates the length of the path and the number of obstacles that pass through, and the sum is enough. This is more intermittent, but the effect of the program I wrote is shown below.

Second, the source code

% 基于遗传算法的栅格法机器人路径规划
clc;
clear all
close all;
% 输入数据,即栅格地图
G=  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
   0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0;
   0 1 1 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0;
   0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 1 0 0 0 0;
   0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0;
   0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
   0 1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0;
   0 0 0 0 0 0 1 1 1 0 1 0 1 1 0 0 0 0 0 0;
   0 1 1 1 0 0 1 1 0 0 1 0 1 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 1 0 1 1 1 1 0 0 0 0 0 0;
   0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0;
   0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1 0;
   0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0;
   0 0 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0;
   0 0 1 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0;
   0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 1 0;
   0 0 1 0 1 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0;
   0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 1 1 0;
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
​
p_start = 0;    % 起始序号
p_end =399;    % 终止序号
NP = 200;      % 种群数量
max_gen = 2000;  % 最大进化代数
pc = 0.5;      % 交叉概率
pm = 0.5;      % 变异概率
a = 1;         % 路径长度比重
b = 7;         % 路径顺滑度比重
%init_path = [];
z = 1;
new_pop1 = {
    
    }; % 元包类型路径
[y, x] = size(G);
% 起点所在列(从左到右编号1.2.3...)
xs = mod(p_start, x) + 1;
% 起点所在行(从上到下编号行1.2.3...)
ys = fix(p_start / x) + 1;
% 终点所在列、行
xe = mod(p_end, x) + 1;
ye = fix(p_end / x) + 1;% 种群初始化step1,必经节点,从起始点所在行开始往上,在每行中挑选一个自由栅格,构成必经节点
pass_num = ye - ys + 1;
pop = zeros(NP, pass_num);
min_value=1000;
for i = 1 : NP
    pop(i, 1) = p_start;
    j = 1;
    % 除去起点和终点
    for yk = ys+1 : ye-1
        j = j + 1;
        % 每一行的可行点
        can = [];
        for xk = 1 : x
            % 栅格序号
            no = (xk - 1) + (yk - 1) * x;
            if G(yk, xk) == 0
                % 把点加入can矩阵中
                can = [can no];
            end
        end
        can_num = length(can);
        % 产生随机整数
        index = randi(can_num);
        % 为每一行加一个可行点
        pop(i, j) = can(index);
    end
    pop(i, end) = p_end;
    %pop
    % 种群初始化step2将上述必经节点联结成无间断路径
    single_new_pop = generate_continuous_path(pop(i, :), G, x);
    %init_path = [init_path, single_new_pop];
    if ~isempty(single_new_pop)
        new_pop1(z, 1) = {
    
    single_new_pop};
        z = z + 1;
    end
end
​
% 计算初始化种群的适应度
% 计算路径长度
path_value = cal_path_value(new_pop1, x);
% 计算路径平滑度
path_smooth = cal_path_smooth(new_pop1, x);
fit_value = a .* path_value .^ -1 + b .* path_smooth .^ -1;
​
mean_path_value = zeros(1, max_gen);
min_path_value = zeros(1, max_gen);
% 循环迭代操作
for i = 1 : max_gen
    % 选择操作
    new_pop2 = selection(new_pop1, fit_value);
    % 交叉操作
    new_pop2 = crossover(new_pop2, pc);
    % 变异操作
    new_pop2 = mutation(new_pop2, pm, G, x);
    % 更新种群
    new_pop1 = new_pop2;
    % 计算适应度值
    % 计算路径长度
    path_value = cal_path_value(new_pop1, x);
    % 计算路径平滑度
    path_smooth = cal_path_smooth(new_pop1, x);
    fit_value = a .* path_value .^ -1 + b .* path_smooth .^ -1;
    mean_path_value(1, i) = mean(path_value);
    [~, m] = max(fit_value);
    if  min(path_value)<min_value
        min_value=min(path_value);
    min_path_value(1, i) = min(path_value);
    else 
        min_path_value(1, i) = min_value;
    end
end
% 画每次迭代平均路径长度和最优路径长度图
figure(1)
plot(1:max_gen,  mean_path_value, 'r')
hold on;
title(['a = ', num2str(a)', ',b = ',num2str(b)','的优化曲线图']);
xlabel('迭代次数');
ylabel('路径长度');
plot(1:max_gen, min_path_value, 'b')
legend('平均路径长度', '最优路径长度');
min_path_value(1, end)
% 在地图上画路径
[~, min_index] = max(fit_value);
min_path = new_pop1{
    
    min_index, 1};
figure(2)
hold on;
title(['a = ', num2str(a)', ',b = ',num2str(b)','遗传算法机器人运动轨迹']);
xlabel('坐标x');
ylabel('坐标y');
DrawMap(G);
[~, min_path_num] = size(min_path);
for i = 1:min_path_num
    % 路径点所在列(从左到右编号1.2.3...x_min_path(1, i) = mod(min_path(1, i), x) + 1;
    % 路径点所在行(从上到下编号行1.2.3...y_min_path(1, i) = fix(min_path(1, i) / x) + 1;
end
hold on;
plot(x_min_path, y_min_path, 'r')

Three, running results

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 with artificial bee colony [Matlab 017]
[Path planning] Genetic algorithm based on grid map robot path planning [Matlab 018]
[Path planning] Ant Swarm algorithm for multiple drone attack scheduling [Matlab 019]

Guess you like

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