​【路径规划】基于萤火虫算法求解障碍地形matlab源码

​1 简介

一种基于萤火虫算法的移动机器人路径规划方法.

2 部分代码

clc;
clear;
close all;

%% Problem Definition

model=CreateModel();

model.n=3;  % number of Handle Points

CostFunction=@(x) MyCost(x,model);    % Cost Function

nVar=model.n;       % Number of Decision Variables

VarSize=[1 nVar];   % Size of Decision Variables Matrix

VarMin.x=model.xmin;           % Lower Bound of Variables
VarMax.x=model.xmax;           % Upper Bound of Variables
VarMin.y=model.ymin;           % Lower Bound of Variables
VarMax.y=model.ymax;           % Upper Bound of Variables


%% GSO Parameters

MaxIt=50;          % Maximum Number of Iterations

nPop=100;           % Population Size (Swarm Size)

w=1;                % Inertia Weight
wdamp=0.98;         % Inertia Weight Damping Ratio
c1=1.5;             % Personal Learning Coefficient
c2=1.5;             % Global Learning Coefficient

%RANGE
range_init = 5.0;
range_boundary = 50.2;

%LUCIFERIN
luciferin_init = 25;
luciferin_decay = 0.4;
luciferin_enhancement = 0.6;

%Neighbors
k_neigh = 20;
beta = 0.5;

step_size = 5;

%% Initialization

% Create Empty Glowworm Structure
empty_glowworm.Position=[];
empty_glowworm.range=[];
empty_glowworm.luciferin=[];
empty_glowworm.Cost=[];
empty_glowworm.Sol=[];
empty_glowworm.neighbors=[];
empty_glowworm.Best.Position=[];
empty_glowworm.Best.Cost=[];
empty_glowworm.Best.Sol=[];

% Initialize Global Best
GlobalBest.Cost=inf;

% Create glowworms Matrix
glowworm=repmat(empty_glowworm,nPop,1);

% Initialization Loop
for i=1:nPop
% Initialize Position
   if i > 1
       glowworm(i).Position=CreateRandomSolution(model);
   else
       % Straight line from source to destination
       xx = linspace(model.xs, model.xt, model.n+2);
       yy = linspace(model.ys, model.yt, model.n+2);
       glowworm(i).Position.x = xx(2:end-1);
       glowworm(i).Position.y = yy(2:end-1);
   end

   % Initialize luciferin
   glowworm(i).luciferin.x=repmat( luciferin_init , 1 , nVar);
   glowworm(i).luciferin.y=repmat( luciferin_init , 1 , nVar);
   
   %Initialize range 
   glowworm(i).range.x = repmat( range_init , 1 , nVar);
   glowworm(i).range.y = repmat( range_init , 1 , nVar);

   neighbors = [];
   % Evaluation
  [glowworm(i).Cost, glowworm(i).Sol]=CostFunction(glowworm(i).Position);
   
   % Update Personal Best
   glowworm(i).Best.Position=glowworm(i).Position;
   glowworm(i).Best.Cost=glowworm(i).Cost;
   glowworm(i).Best.Sol=glowworm(i).Sol;
   
   % Update Global Best
   if glowworm(i).Best.Cost<GlobalBest.Cost
       
       GlobalBest=glowworm(i).Best;
       
   end
end

% Array to Hold Best Cost Values at Each Iteration
BestCost=zeros(MaxIt,1);


for it=1:MaxIt
   
   for i=1:nPop
       
       
       
   end
   % Update Best Cost Ever Found
   BestCost(it)=GlobalBest.Cost;
   
    % Show Iteration Information
   if GlobalBest.Sol.IsFeasible
       Flag=' *';
   else
       Flag=[', Violation = ' num2str(GlobalBest.Sol.Violation)];
   end
   disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it)) Flag]);
   
   % Plot Solution
   figure(1);
   PlotSolution(GlobalBest.Sol,model);
   pause(0.01);
end
img =gcf;  %获取当前画图的句柄
print(img, '-dpng', '-r600', './img2.png')         %即可得到对应格式和期望dpi的图像
%% Results

figure;
plot(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;
img =gcf;  %获取当前画图的句柄
print(img, '-dpng', '-r600', './img.png')         %即可得到对应格式和期望dpi的图像
       

3 仿真结果

4 参考文献

[1]李凤玲, 陈珊, 范兴江, & 刘源. (2019). 基于萤火虫算法动态未知环境的路径规划. 自动化与仪表(6).

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1.版本:matlab2014/2019a,内含运行结果,不会运行可私信 2.领域:智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,更多内容可点击博主头像 3.内容:标题所示,对于介绍可点击主页搜索博客 4.适合人群:本科,硕士等教研学习使用 5.博客介绍:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可si信 ### 团队长期从事下列领域算法的研究和改进: ### 1 智能优化算法及应用 **1.1 改进智能优化算法方面(单目标和多目标)** **1.2 生产调度方面** 1.2.1 装配线调度研究 1.2.2 车间调度研究 1.2.3 生产线平衡研究 1.2.4 水库梯度调度研究 **1.3 路径规划方面** 1.3.1 旅行商问题研究(TSP、TSPTW) 1.3.2 各类车辆路径规划问题研究(vrp、VRPTW、CVRP) 1.3.3 机器人路径规划问题研究 1.3.4 无人机三维路径规划问题研究 1.3.5 多式联运问题研究 1.3.6 无人机结合车辆路径配送 **1.4 三维装箱求解** **1.5 物流选址研究** 1.5.1 背包问题 1.5.2 物流选址 1.5.4 货位优化 ##### 1.6 电力系统优化研究 1.6.1 微电网优化 1.6.2 配电网系统优化 1.6.3 配电网重构 1.6.4 有序充电 1.6.5 储能双层优化调度 1.6.6 储能优化配置 ### 2 神经网络回归预测、时序预测、分类清单 **2.1 bp预测和分类** **2.2 lssvm预测和分类** **2.3 svm预测和分类** **2.4 cnn预测和分类** ##### 2.5 ELM预测和分类 ##### 2.6 KELM预测和分类 **2.7 ELMAN预测和分类** ##### 2.8 LSTM预测和分类 **2.9 RBF预测和分类** ##### 2.10 DBN预测和分类 ##### 2.11 FNN预测 ##### 2.12 DELM预测和分类 ##### 2.13 BIlstm预测和分类 ##### 2.14 宽度学习预测和分类 ##### 2.15 模糊小波神经网络预测和分类 ##### 2.16 GRU预测和分类 ### 3 图像处理算法 **3.1 图像识别** 3.1.1 车牌、交通标志识别(新能源、国内外、复杂环境下车牌) 3.1.2 发票、身份证、银行卡识别 3.1.3 人脸类别和表情识别 3.1.4 打靶识别 3.1.5 字符识别(字母、数字、手写体、汉字、验证码) 3.1.6 病灶识别 3.1.7 花朵、药材、水果蔬菜识别 3.1.8 指纹、手势、虹膜识别 3.1.9 路面状态和裂缝识别 3.1.10 行为识别 3.1.11 万用表和表盘识别 3.1.12 人民币识别 3.1.13 答题卡识别 **3.2 图像分割** **3.3 图像检测** 3.3.1 显著性检测 3.3.2 缺陷检测 3.3.3 疲劳检测 3.3.4 病害检测 3.3.5 火灾检测 3.3.6 行人检测 3.3.7 水果分级 **3.4 图像隐藏** **3.5 图像去噪** **3.6 图像融合** **3.7 图像配准** **3.8 图像增强** **3.9 图像压缩** ##### 3.10 图像重建 ### 4 信号处理算法 **4.1 信号识别** **4.2 信号检测** **4.3 信号嵌入和提取** **4.4 信号去噪** ##### 4.5 故障诊断 ##### 4.6 脑电信号 ##### 4.7 心电信号 ##### 4.8 肌电信号 ### 5 元胞自动机仿真 **5.1 模拟交通流** **5.2 模拟人群疏散** **5.3 模拟病毒扩散** **5.4 模拟晶体生长** ### 6 无线传感器网络 ##### 6.1 无线传感器定位 ##### 6.2 无线传感器覆盖优化 ##### 6.3 室内定位 ##### 6.4 无线传感器通信及优化 ##### 6.5 无人机通信中继优化
萤火虫算法是一种基于自然界萤火虫行为的启发式优化算法,用于解决优化问题。它模拟了萤火虫的交互行为,通过光强度和吸引度来调整萤火虫的移动方向,从而找到最优解。 路径规划是指在给定起点和终点的情况下,确定一条最优路径以满足特定的约束条件。萤火虫算法可以应用于路径规划问题,通过优化萤火虫的移动策略来寻找最优路径。 以下是基于萤火虫算法路径规划的示例代码: ```python # 导入必要的库 import numpy as np # 定义萤火虫类 class Firefly: def __init__(self, position): self.position = position self.intensity = 0 def __str__(self): return f"Position: {self.position}, Intensity: {self.intensity}" # 定义路径规划函数 def path_planning(start, end, num_fireflies, max_iterations): # 初始化萤火虫群体 fireflies = [Firefly(start) for _ in range(num_fireflies)] # 迭代更新萤火虫位置和亮度 for _ in range(max_iterations): for i in range(num_fireflies): for j in range(num_fireflies): if fireflies[j].intensity > fireflies[i].intensity: # 计算吸引度 attractiveness = calculate_attractiveness(fireflies[i], fireflies[j]) # 更新位置 fireflies[i].position += attractiveness * (fireflies[j].position - fireflies[i].position) # 更新亮度 fireflies[i].intensity = calculate_intensity(fireflies[i]) # 选择最优路径 best_firefly = max(fireflies, key=lambda x: x.intensity) best_path = best_firefly.position return best_path # 计算吸引度 def calculate_attractiveness(firefly1, firefly2): distance = np.linalg.norm(firefly1.position - firefly2.position) attractiveness = 1 / (1 + distance) return attractiveness # 计算亮度 def calculate_intensity(firefly): intensity = np.linalg.norm(firefly.position) return intensity # 示例使用 start = np.array([0, 0]) end = np.array([10, 10]) num_fireflies = 10 max_iterations = 100 best_path = path_planning(start, end, num_fireflies, max_iterations) print("Best path:", best_path) ``` 这段代码演示了如何使用萤火虫算法进行路径规划。在示例中,我们定义了一个萤火虫类,包含位置和亮度属性。然后,我们定义了路径规划函数,其中使用萤火虫算法迭代更新萤火虫的位置和亮度。最后,我们选择亮度最高的萤火虫作为最优路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matlab科研辅导帮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值