优化:蚁群算法求解TSP,二次分配问题QAP,背包问题(BK), Matlab

蚁群优化(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;

在这里插入图片描述

扫码关注公众号获取完整代码

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值