一、简介
1 蚁群算法的提出
蚁群算法(ant colony optimization, ACO),又称蚂蚁算法,是一种用来寻找优化路径的机率型算法。它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。遗传算法在模式识别、神经网络、机器学习、工业优化控制、自适应控制、生物科学、社会科学等方面都得到应用。
2 算法的基本原理
二、源代码
clear all;
warning('off'); % 消除警告
false = 0;
true = 1;
%% (1)Setup the movie stuff 设置视频信息
make_movie = true;
% make_pot = false; % 这个地方暂时可以理解为可以省略,与下程序相互对应;
% make video showing potential field | Takes a long time to make, so usually set to false
% 设置视频的字段需要很长时间,所以通常设置为false
writerObj = [];
% Define the plane of points to evaluate potential function, if you are making video of potential function
% 如果你制作势能函数的视频,定义点的平面来评估势能函数
x = linspace(0, 50, 100); % 0-50中平均分100个数,组成1*100double矩阵
y = linspace(0, 50, 100); % 0-50中平均分100个数,组成1*100double矩阵
z = 5; % 这个地方的5代表的是什么?
[X,Y] = meshgrid(x,y); % X为100*100double,Y为100*100double
% meshgrid用于从数组x和y产生网格.生成的网格矩阵X和Y大小是相同的.它也可以是更高维的.
[rx, cx] = size(X); % rx = 100;cx = 100
Z = zeros(rx, cx); % Z为100*100double
% Setup the movie file and frame rate 设置视频文件和帧速率
if( make_movie )
writerObj = VideoWriter('3D_test3.avi'); % 定义一个视频文件用来存动画
writerObj.FrameRate = 60; % 将帧写入视频FrameRate:帧速率(每秒钟60张图)
open(writerObj); % 打开该视频文件
end
% initial perspective in video 视频的初始化角度
az = -60;
el = 20;
% final perspective in video 视频的最后角度
azf = -60;
elf = 20;
%% (2)Parameter specifications for drones 设置无人机的参数
Nd =2; % number of drones in swarm (无人机种群数量)
ind_c = -1; % index for center/lead drone (中心/领头无人机的指数) 这条语句的作用是什么?
radius =1.5; % radius for drones and possibly obstacles (无人机和可能障碍物的半径)
dims = [0, 50; 0, 50; 0, 50]; % first row is lower and upper x bounds (设置三维坐标的界限)
% second row is lower and upper y bounds
% third row is lower and upper z bounds
xc = [40,40,40]'; % initial location for central/lead drone (中央/领头无人机的初始位置)
end_loc = [5, 5, 5]'; % Desired end location (预期结束位置)
%% (3)Setup the waypoint object 设置路径对象
dist_thresh = 2; % 这个函数的设置似乎没有什么影响
% The distance threshold(距离阈值) the swarm must be within of the waypoint(距离阈值群必须在航路点内)
% to make the waypoint change to the new one
% 群体的距离阈值必须在航点内,以使航点变为新的航点
% 如何连接航路点?
wypt = Waypoints( dist_thresh );
wypt.addPoint( [ 15; 50; 20 ] ); % first waypoint (第一个航点,就是蓝色的点)
wypt.addPoint( [ 0; 50; 0 ] ); % second waypoint (第二个航点)
wypt.addPoint( end_loc ); % last waypoint (最后一个航点)
% 蓝色的点
wypt2 = Waypoints( dist_thresh ); % wypt和wypt2的区别在哪里?为什么优先考虑wypt2的改变值
wypt2.addPoint( [ 15; 50; 20 ] ); % first waypoint
wypt2.addPoint( [ 0; 50; 0 ] ); % second waypoint
wypt2.addPoint( end_loc ); % last waypoint
classdef Queue < handle
properties
items;
count;
end
methods
function obj = Queue()
obj.items = [];
obj.count = 0;
end
function push( obj, v )
if( ~isempty(v) )
obj.count = obj.count + 1;
obj.items(obj.count).d =v;
end
end
function v = pop( obj )
if( obj.count > 0 )
v = obj.items(1).d;
obj.items = obj.items(2:end);
obj.count = obj.count - 1;
else
v = [ ];
end
end
end
end
三、运行结果
四、备注
版本:2014a