基于自适应差分进化算法(Adaptive Differential Evolution, ADE)的MATLAB实现
一、算法原理
自适应差分进化算法通过动态调整缩放因子(F)和交叉概率(CR)提升全局搜索能力。核心改进包括:
- 参数自适应:根据种群适应度动态调整F和CR
- 变异策略:采用DE/rand/1策略生成变异向量
- 精英保留:始终保留当前最优解
二、代码
function [best_sol, best_fit, history] = AdaptiveDE(func, lb, ub, dim, NP, max_iter)% 参数初始化F_min = 0.1; F_max = 0.9; % 缩放因子范围CR_min = 0.1; CR_max = 0.9; % 交叉概率范围tau1 = 0.1; tau2 = 0.1; % 参数更新概率% 种群初始化pop = repmat(lb, NP, 1) + rand(NP, dim) .* repmat(ub-lb, NP, 1);fitness = arrayfun(@(i) func(pop(i,:)), 1:NP);% 记录最优解[best_fit, best_idx] = min(fitness);best_sol = pop(best_idx, :);history = zeros(max_iter, 1);% 主循环for gen = 1:max_iter% 自适应参数更新F = F_min + (F_max-F_min) * rand(NP,1);CR = CR_min + (CR_max-CR_min) * rand(NP,1);for i = 1:NP% 变异操作r1 = randi([1,NP]); while r1==i, r1=randi([1,NP]); endr2 = randi([1,NP]); while r2==i || r2==r1, r2=randi([1,NP]); endr3 = randi([1,NP]); while r3==i || r3==r1 || r3==r2, r3=randi([1,NP]); endV = pop(r1,:) + F(i) * (pop(r2,:) - pop(r3,:));% 交叉操作j_rand = randi(dim);U = pop(i,:);for j = 1:dimif rand() < CR(i) || j == j_randU(j) = V(j);endend% 边界处理U = max(U, lb);U = min(U, ub);% 选择操作new_fit = func(U);if new_fit < fitness(i)pop(i,:) = U;fitness(i) = new_fit;% 参数更新if rand() < tau1F(i) = F_min + rand()*(F_max-F_min);endif rand() < tau2CR(i) = CR_min + rand()*(CR_max-CR_min);endendend% 更新最优解[current_best, idx] = min(fitness);if current_best < best_fitbest_fit = current_best;best_sol = pop(idx, :);endhistory(gen) = best_fit;% 显示进度fprintf('Generation %d: Best Fitness = %.6f\n', gen, best_fit);end
end
三、关键代码解析
1. 参数自适应机制
% 自适应参数更新
F = F_min + (F_max-F_min) * rand(NP,1);
CR = CR_min + (CR_max-CR_min) * rand(NP,1);
- 每个个体独立调整F和CR
- 参数范围动态调整([0.1,0.9])
2. 变异与交叉操作
% DE/rand/1变异策略
V = pop(r1,:) + F(i) * (pop(r2,:) - pop(r3,:));% 二项交叉操作
for j = 1:dimif rand() < CR(i) || j == j_randU(j) = V(j);end
end
- 保证至少一个维度来自变异向量
- 随机维度确保多样性
四、测试函数示例
1. Rastrigin函数测试
% 定义目标函数
rastrigin = @(x) 10*numel(x) + sum(x.^2 - 10*cos(2*pi*x));% 参数设置
lb = -5.12*ones(1,10); % 10维搜索空间
ub = 5.12*ones(1,10);
NP = 50; max_iter = 1000;% 运行算法
[best_sol, best_fit, history] = AdaptiveDE(rastrigin, lb, ub, 10, NP, max_iter);% 可视化结果
figure;
plot(history);
xlabel('迭代次数'); ylabel('最优值');
title('Rastrigin函数优化过程');
2. Ackley函数测试
ackley = @(x) -20*exp(-0.2*sqrt(mean(x.^2))) - exp(mean(cos(2*pi*x))) + 20 + exp(1);lb = -32.768*ones(1,5);
ub = 32.768*ones(1,5);
[best_sol, best_fit] = AdaptiveDE(ackley, lb, ub, 5, 100, 500);
disp(['最优解: ', num2str(best_sol)]);
disp(['最优值: ', num2str(best_fit)]);
参考代码 自适应差分进化算法例程 www.youwenfan.com/contentcnh/59497.html
五、性能优化技巧
-
边界处理:采用反射边界策略
U = max(U, lb); U = min(U, ub);
-
精英保留:始终保留当前最优解
if new_fit < best_fitbest_sol = U;best_fit = new_fit; end
-
并行计算:使用
parfor
加速种群评估parfor i = 1:NPfitness(i) = func(pop(i,:)); end
六、应用场景示例
1. 工程优化问题
% 压力容器设计优化
pressure_vessel = @(x) 0.6224*x(1)*x(2)*x(3) + 1.7781*x(1)^2*x(3) + ...3.1661*x(2)^2*x(3) + 19.84*x(1)*x(2);lb = [0,0,0,0]; ub = [100,100,100,100];
[best_sol, best_fit] = AdaptiveDE(pressure_vessel, lb, ub, 4, 60, 1000);
2. 无人机路径规划
% 三维路径规划目标函数
path_cost = @(x) sum(x.^2) + 10*max(0, x(3)-50);lb = [-100,-100,-100]; ub = [100,100,100];
[best_sol, best_fit] = AdaptiveDE(path_cost, lb, ub, 3, 30, 500);