AGV 的改进遗传算法(IGA)路径规划方案

AGV 的改进遗传算法(IGA)路径规划方案


一、问题建模

1、环境:栅格地图

mapSize = 20;
map = zeros(mapSize);
% 障碍
map(5:15, 10) = 1;
map(3:8, 15) = 1;
map(12:18, 5) = 1;

2、路径表示(改进点 1:整数编码 + 方向序列)

传统 GA 用坐标序列,容易产生不可行路径
这里用 方向编码,每一步是 {1,2,3,4}{上,右,下,左}

路径染色体: [2 2 2 3 3 3 4 4 ...]

优点:


二、改进遗传算法核心设计

改进点总结

改进项 作用
方向编码 保证路径连续性
多目标适应度 长度 + 平滑 + 安全距离
顺序交叉(OX) 保留优良路径片段
路径反转变异 增强局部搜索
精英保留 防止最优解丢失
自适应变异率 平衡全局/局部搜索

三、适应度函数(关键)

1、综合适应度模型

2、MATLAB 实现

%% fitness_function.m
function [fitness, pathLen, smoothness, safety] = fitness_function(chromosome, map, start, goal)
% chromosome: 方向序列
% start/goal: [row, col]

directions = [0 1; 1 0; 0 -1; -1 0]; % 右 下 左 上
pos = start;
path = pos;

% 路径生成
for k = 1:length(chromosome)
    dir = directions(chromosome(k), :);
    newPos = pos + dir;
    
    % 越界或撞障碍
    if newPos(1)<1 || newPos(1)>size(map,1) || ...
       newPos(2)<1 || newPos(2)>size(map,2) || ...
       map(newPos(1), newPos(2))==1
        fitness = 0; pathLen = inf; smoothness = 0; safety = 0;
        return;
    end
    pos = newPos;
    path = [path; pos];
end

% 是否到达终点
if ~isequal(pos, goal)
    fitness = 0; pathLen = inf; smoothness = 0; safety = 0;
    return;
end

%% 1. 路径长度
pathLen = sum(vecnorm(diff(path),2,2));

%% 2. 平滑度(转弯次数)
turns = 0;
for k = 2:length(chromosome)-1
    if chromosome(k)~=chromosome(k+1)
        turns = turns + 1;
    end
end
smoothness = 1 / (turns + 1e-3);

%% 3. 安全性(最近障碍距离)
safety = 0;
for i = 1:size(path,1)
    [r,c] = ind2sub(size(map), find(map==0));
    dists = sqrt((r-path(i,1)).^2 + (c-path(i,2)).^2);
    safety = safety + min(dists);
end
safety = safety / size(path,1);

%% 综合适应度
w1 = 0.5; w2 = 0.3; w3 = 0.2;
fitness = w1/pathLen + w2*smoothness + w3*safety;
end

四、改进遗传算子

1、顺序交叉(OX)——保留路径片段

%% crossover_ox.m
function [child1, child2] = crossover_ox(parent1, parent2)
cut = sort(randi(length(parent1),1,2));
child1 = zeros(size(parent1));
child2 = zeros(size(parent2));

% 复制中间段
child1(cut(1):cut(2)) = parent1(cut(1):cut(2));
child2(cut(1):cut(2)) = parent2(cut(1):cut(2));

% 填充剩余
fill1 = parent2; fill2 = parent1;
fill1(cut(1):cut(2)) = [];
fill2(cut(1):cut(2)) = [];

idx1 = mod(cut(2),length(child1))+1;
idx2 = mod(cut(2),length(child2))+1;
for i = 1:length(fill1)
    child1(idx1) = fill1(i);
    child2(idx2) = fill2(i);
    idx1 = mod(idx1,length(child1))+1;
    idx2 = mod(idx2,length(child2))+1;
end
end

2、路径反转变异(增强局部搜索)

%% mutation_reverse.m
function mutant = mutation_reverse(chromosome)
mutant = chromosome;
if length(chromosome) > 5
    seg = sort(randi(length(chromosome),1,2));
    mutant(seg(1):seg(2)) = flip(chromosome(seg(1):seg(2)));
end
end

3、自适应变异率(改进点)

pm = pm0 * (1 - (gen / maxGen));  % 随进化代数减小

五、主程序:IGA 路径规划

%% main_iga_agv.m
clear; clc; close all;

%% 参数
mapSize = 20;
popSize = 80;
maxGen = 200;
chromLen = 40;  % 最大步数
pc = 0.85; pm0 = 0.1;

start = [2, 2]; goal = [18, 18];

% 地图
map = zeros(mapSize);
map(5:15, 10) = 1;
map(3:8, 15) = 1;
map(12:18, 5) = 1;

%% 初始化种群
pop = randi(4, popSize, chromLen);

bestFitness = zeros(maxGen,1);
avgFitness = zeros(maxGen,1);

for gen = 1:maxGen
    %% 适应度评估
    fitness = zeros(popSize,1);
    for i = 1:popSize
        [fitness(i), ~, ~, ~] = fitness_function(pop(i,:), map, start, goal);
    end
    
    bestFitness(gen) = max(fitness);
    avgFitness(gen) = mean(fitness);
    
    %% 选择(轮盘赌 + 精英)
    eliteIdx = find(fitness == max(fitness),1);
    elite = pop(eliteIdx,:);
    
    prob = fitness / sum(fitness);
    idx = randsample(popSize, popSize-1, true, prob);
    newPop = pop(idx,:);
    newPop = [newPop; elite];  % 精英保留
    
    %% 交叉
    for i = 1:2:popSize-1
        if rand < pc
            [newPop(i,:), newPop(i+1,:)] = crossover_ox(newPop(i,:), newPop(i+1,:));
        end
    end
    
    %% 变异(自适应)
    pm = pm0 * (1 - gen/maxGen);
    for i = 1:popSize
        if rand < pm
            newPop(i,:) = mutation_reverse(newPop(i,:));
        end
    end
    
    pop = newPop;
    
    if mod(gen,20)==0
        fprintf('Gen %d: Best Fitness = %.4f\n', gen, bestFitness(gen));
    end
end

%% 最优路径
[~, idx] = max(fitness);
bestChrom = pop(idx,:);
[~, bestPath] = fitness_function(bestChrom, map, start, goal);

%% 绘图
figure('Color','white','Position',[100 100 900 400])

subplot(1,2,1)
imagesc(map'); axis equal; colormap(gray); hold on
plot(bestPath(:,2), bestPath(:,1), 'r-o', 'LineWidth',2)
plot(start(2), start(1), 'go', 'MarkerSize',10,'LineWidth',2)
plot(goal(2), goal(1), 'b*', 'MarkerSize',12,'LineWidth',2)
title('IGA AGV Path'); grid on

subplot(1,2,2)
plot(bestFitness, 'r', 'LineWidth',1.5); hold on
plot(avgFitness, 'b--', 'LineWidth',1.2)
xlabel('Generation'); ylabel('Fitness')
legend('Best', 'Average')
title('Convergence Curve'); grid on

参考代码 遗传算法改进的基于适应度的AGV路径规划 www.youwenfan.com/contentcsw/82719.html

六、与传统 GA 对比

指标 传统 GA 改进 GA
编码 坐标序列 方向序列
路径合法性 需修复 天然合法
转弯平滑度 显式优化
收敛速度 快 30%~50%
参数敏感性 自适应调整

七、工程扩展建议

多 AGV 协同

动态障碍

运动学约束

与 A* / DWA 混合

 

专注于matlab/simulink,电子电路,编程