基于MATLAB的栅格环境建模与遗传算法路径规划完整实现

一、环境建模(栅格地图)

%% 环境参数设置
mapSize = [100,100]; % 地图尺寸(100x100)
obsDensity = 0.3;    % 障碍物密度30%
safeRadius = 2;      % 机器人半径

% 创建栅格地图
envMap = zeros(mapSize);
obsCount = round(obsDensity * numel(envMap)/4); % 障碍物数量

% 生成随机障碍物
obsPositions = randi([1,mapSize(1)],obsCount,2);
for i = 1:obsCount
    x = obsPositions(i,1);
    y = obsPositions(i,2);
    envMap(max(1,x-2):min(mapSize(1),x+2), max(1,y-2):min(mapSize(2),y+2)) = 1;
end

% 可视化
figure;
imagesc(envMap);
colormap([1 1 1; 0 0 0]); % 白色-自由,黑色-障碍
hold on;
plot(1,1,'go','MarkerSize',10,'LineWidth',2); % 起点
plot(mapSize(1),mapSize(2),'ro','MarkerSize',10,'LineWidth',2); % 终点
title('环境建模结果');
axis equal tight;

二、遗传算法核心模块

1. 染色体编码(路径点序列)

function chromosome = encodePath(start,goal,numPoints)
    % 生成路径点序列(起点+中间点+终点)
    midPoints = randi([2,mapSize(1)-1],numPoints-2,2);
    chromosome = [start; midPoints; goal];
end

2. 适应度函数设计

function fitness = calcFitness(chromosome,envMap,safeRadius)
    path = chromosome;
    penalty = 0;
    
    % 碰撞检测
    for i = 1:size(path,1)-1
        segment = bresenham(path(i,:),path(i+1,:));
        if any(envMap(segment(:,1),segment(:,2)))
            penalty = penalty + 1e6; % 碰撞惩罚
        end
        % 距离惩罚
        dist = norm(path(i+1,:) - path(i,:));
        penalty = penalty + 1/dist;
    end
    
    fitness = 1/(pathLength(path) + penalty);
end

function len = pathLength(path)
    len = 0;
    for i = 1:size(path,1)-1
        len = len + norm(path(i+1,:) - path(i,:));
    end
end

3. 遗传操作

%% 选择操作(锦标赛选择)
function selected = tournamentSelection(population,fitness,k)
    [~,idx] = sort(fitness,'descend');
    candidates = idx(1:k);
    selected = population(candidates(1),:);
end

%% 交叉操作(顺序交叉OX)
function child = orderCrossover(parent1,parent2)
    n = size(parent1,1);
    points = sort(randperm(n,2));
    child = zeros(n,2);
    
    % 复制父代1片段
    child(points(1):points(2),:) = parent1(points(1):points(2),:);
    
    % 填充剩余基因
    ptr = points(2)+1;
    for i = 1:n
        if ptr > n
            ptr = 1;
        end
        if ~ismember(parent2(i,:),child)
            child(ptr,:) = parent2(i,:);
            ptr = ptr+1;
        end
    end
end

%% 变异操作(高斯扰动)
function mutated = mutate(chromosome,sigma)
    mutated = chromosome + sigma*randn(size(chromosome));
    mutated = max(min(mutated,mapSize),[1,1]);
end

三、路径规划主程序

%% 参数设置
popSize = 100;        % 种群大小
maxGen = 500;         % 最大迭代
pc = 0.8;             % 交叉概率
pm = 0.1;             % 变异概率
kTournament = 5;      % 锦标赛规模

% 初始化种群
population = zeros(popSize,20); % 假设路径包含18个中间点
for i = 1:popSize
    population(i,:) = encodePath([1,1],mapSize,18);
end

% 进化循环
bestFitness = zeros(maxGen,1);
for gen = 1:maxGen
    % 计算适应度
    fitness = zeros(popSize,1);
    for i = 1:popSize
        fitness(i) = calcFitness(population(i,:),envMap,safeRadius);
    end
    
    % 更新最优解
    [bestFit,idx] = max(fitness);
    bestFitness(gen) = bestFit;
    bestChromosome = population(idx,:);
    
    % 选择
    newPop = zeros(popSize,20);
    for i = 1:popSize
        parent1 = tournamentSelection(population,fitness,kTournament);
        parent2 = tournamentSelection(population,fitness,kTournament);
        
        % 交叉
        if rand < pc
            child = orderCrossover(parent1,parent2);
        else
            child = parent1;
        end
        
        % 变异
        if rand < pm
            child = mutate(child,2);
        end
        
        newPop(i,:) = child;
    end
    population = newPop;
end

% 路径可视化
figure;
plotPath(bestChromosome,envMap);
title('最优路径规划结果');

参考代码 通过matlab建立环境障碍物模型,通过遗传算法规划一条无碰撞路径,完成路径规划 www.youwenfan.com/contentcnd/65895.html

四、关键算法优化

1. 动态步长调整

% 根据环境复杂度调整种群规模
complexity = sum(envMap(:))/numel(envMap);
popSize = 50 + 50*(1-complexity);

2. 多目标优化

% 适应度函数扩展(路径长度+安全性)
fitness = w1*pathLength(path) + w2*collisionPenalty;

3. 并行计算加速

% 使用parfor加速适应度计算
parfor i = 1:popSize
    fitness(i) = calcFitness(population(i,:),envMap,safeRadius);
end

该方法通过改进遗传算法的编码方式和适应度函数,在复杂环境中实现了高效无碰撞路径规划。实际应用中可根据具体需求调整参数,如增加机器人动力学约束或融合传感器数据实现动态避障。

posted @ 2025-08-22 16:54  yes_go  阅读(51)  评论(0)    收藏  举报