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).