蚁群优化(ACO)是一组概率元启发法和智能优化算法,受到蚂蚁社会行为的启发。ACO 算法也被归类为群体智能方法,因为这种范式的实现是通过模拟这些算法结构中的蚂蚁行为来实现的。
第一个 ACO 算法是 Marco Dorigo 在 1991 年的博士论文中提出的,称为 Ant System 算法。一些研究人员提出了各种版本的蚂蚁算法,例如最大-最小蚂蚁系统、蚁群系统和 ACOR(连续域的 ACO)。
在这篇文章中,我们将与您分享 ACO 在 MATLAB 中的完整开源实现。ACO 的实施是为了解决三个不同项目中的以下问题:
旅行商问题(TSP)
clc;
clear;
close all;
%% Problem Definition
model = CreateModel();
CostFunction = @(tour) TourLength(tour, model);
nVar = model.n;
%% ACO Parameters
MaxIt = 300; % Maximum Number of Iterations
nAnt = 40; % Number of Ants (Population Size)
Q = 1;
tau0 = 10Q/(nVarmean(model.D(😃)); % Initial Phromone
alpha = 1; % Phromone Exponential Weight
beta = 1; % Heuristic Exponential Weight
rho = 0.05; % Evaporation Rate
%% Initialization
eta = 1./model.D; % Heuristic Information Matrix
tau = tau0*ones(nVar, nVar); % Phromone Matrix
BestCost = zeros(MaxIt, 1); % Array to Hold Best Cost Values
% Empty Ant
empty_ant.Tour = [];
empty_ant.Cost = [];
% Ant Colony Matrix
ant = repmat(empty_ant, nAnt, 1);
% Best Ant
BestSol.Cost = inf;
%% ACO Main Loop
for it = 1:MaxIt
% Move Ants
for k = 1:nAnt
ant(k).Tour = randi([1 nVar]);
for l = 2:nVar
i = ant(k).Tour(end);
P = tau(i, 😃.^alpha.*eta(i, 😃.^beta;
P(ant(k).Tour) = 0;
P = P/sum§;
j = RouletteWheelSelection§;
ant(k).Tour = [ant(k).Tour j];
end
ant(k).Cost = CostFunction(ant(k).Tour);
if ant(k).Cost<BestSol.Cost
BestSol = ant(k);
end
end
% Update Phromones
for k = 1:nAnt
tour = ant(k).Tour;
tour = [tour tour(1)]; %#ok
for l = 1:nVar
i = tour(l);
j = tour(l+1);
tau(i, j) = tau(i, j)+Q/ant(k).Cost;
end
end
% Evaporation
tau = (1-rho)*tau;
% Store Best Cost
BestCost(it) = BestSol.Cost;
% Show Iteration Information
disp(['Iteration ’ num2str(it) ': Best Cost = ’ num2str(BestCost(it))]);
% Plot Solution
figure(1);
PlotSolution(BestSol.Tour, model);
pause(0.01);
end
%% Results
figure;
plot(BestCost, ‘LineWidth’, 2);
xlabel(‘Iteration’);
ylabel(‘Best Cost’);
grid on;
二次分配问题 (QAP)
clc;
clear;
close all;
%% Problem Definition
model = CreateModel();
CostFunction = @§ MyCost(p, model);
nVar = model.n;
%% ACO Parameters
MaxIt = 500; % Maximum Number of Iterations
nAnt = 50; % Number of Ants (Population Size)
Q = 1;
tau0 = 10; % Initial Phromone
alpha = 0.3; % Phromone Exponential Weight
rho = 0.1; % Evaporation Rate
%% Initialization
tau = tau0*ones(model.m, nVar);
BestCost = zeros(MaxIt, 1); % Array to Hold Best Cost Values
% Empty Ant
empty_ant.Tour = [];
empty_ant.Cost = [];
% Ant Colony Matrix
ant = repmat(empty_ant, nAnt, 1);
% Best Ant
BestSol.Cost = inf;
%% ACO Main Loop
for it = 1:MaxIt
% Move Ants
for k = 1:nAnt
ant(k).Tour = [];
for l = 1:nVar
P = tau(:, l).^alpha;
P(ant(k).Tour) = 0;
P = P/sum§;
j = RouletteWheelSelection§;
ant(k).Tour = [ant(k).Tour j];
end
ant(k).Cost = CostFunction(ant(k).Tour);
if ant(k).Cost<BestSol.Cost
BestSol = ant(k);
end
end
% Update Phromones
for k = 1:nAnt
tour = ant(k).Tour;
for l = 1:nVar
tau(tour(l), l) = tau(tour(l), l)+Q/ant(k).Cost;
end
end
% Evaporation
tau = (1-rho)*tau;
% Store Best Cost
BestCost(it) = BestSol.Cost;
% Show Iteration Information
disp(['Iteration ’ num2str(it) ': Best Cost = ’ num2str(BestCost(it))]);
% Plot Solution
figure(1);
PlotSolution(BestSol.Tour, model);
pause(0.01);
end
%% Results
figure;
plot(BestCost, ‘LineWidth’, 2);
xlabel(‘Iteration’);
ylabel(‘Best Cost’);
grid on;
背包问题(BK)
clc;
clear;
close all;
%% Problem Definition
model = CreateModel();
CostFunction = @(x) MyCost(x, model);
nVar = model.n;
%% ACO Parameters
MaxIt = 300; % Maximum Number of Iterations
nAnt = 40; % Number of Ants (Population Size)
Q = 1;
tau0 = 0.1; % Initial Phromone
alpha = 1; % Phromone Exponential Weight
beta = 0.02; % Heuristic Exponential Weight
rho = 0.1; % Evaporation Rate
%% Initialization
N = [0 1];
eta = [model.w./model.v
model.v./model.w]; % Heuristic Information
tau = tau0*ones(2, nVar); % Phromone Matrix
BestCost = zeros(MaxIt, 1); % Array to Hold Best Cost Values
% Empty Ant
empty_ant.Tour = [];
empty_ant.x = [];
empty_ant.Cost = [];
empty_ant.Sol = [];
% Ant Colony Matrix
ant = repmat(empty_ant, nAnt, 1);
% Best Ant
BestSol.Cost = inf;
%% ACO Main Loop
for it = 1:MaxIt
% Move Ants
for k = 1:nAnt
ant(k).Tour = [];
for l = 1:nVar
P = tau(:, l).^alpha.*eta(:, l).^beta;
P = P/sum(P);
j = RouletteWheelSelection(P);
ant(k).Tour = [ant(k).Tour j];
end
ant(k).x = N(ant(k).Tour);
[ant(k).Cost, ant(k).Sol] = CostFunction(ant(k).x);
if ant(k).Cost<BestSol.Cost
BestSol = ant(k);
end
end
% Update Phromones
for k = 1:nAnt
tour = ant(k).Tour;
for l = 1:nVar
tau(tour(l), l) = tau(tour(l), l)+Q/ant(k).Cost;
end
end
% Evaporation
tau = (1-rho)*tau;
% Store Best Cost
BestCost(it) = BestSol.Cost;
% Show Iteration Information
if BestSol.Sol.IsFeasible
FeasiblityFlag = '*';
else
FeasiblityFlag = '';
end
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it)) ' ' FeasiblityFlag]);
end
%% Results
figure;
plot(BestCost, 'LineWidth', 2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;
…
