【多目标遗传算法,Matlab源代码】 NSGA2
先说说这算法的核心——快速非支配排序。Matlab里实现这个的代码有点意思:
function [fronts, ranks] = non_dominated_sorting(pop) n = length(pop); dominates = false(n); % 支配关系矩阵 for i = 1:n for j = 1:n if all(pop(i).cost <= pop(j).cost) && any(pop(i).cost < pop(j).cost) dominates(i,j) = true; end end } % 分层核心逻辑 fronts = {}; current_front = find(all(~dominates,2)); % 找不被支配的个体 while ~isempty(current_front) fronts{end+1} = current_front; ranks(current_front) = length(fronts); dominated = any(dominates(current_front,:),1); dominates(dominated,:) = false; current_front = find(all(~dominates,2)); end end这段代码里有个骚操作:用逻辑矩阵处理支配关系,比传统的逐个比较快了不止一个量级。特别是dominated = any(dominates(current_front,:),1)这句,直接批量处理被支配个体,省去了双重循环。
接下来是拥挤度计算,这玩意儿直接影响解的分布均匀性:
function pop = calculate_crowding(pop, front) n = length(front); costs = [pop(front).cost]; [~, sorted_idx] = sortrows(costs'); % 边界处理 pop(front(sorted_idx(1))).crowding = inf; pop(front(sorted_idx(end))).crowding = inf; % 中间个体计算 norm = max(costs,[],2) - min(costs,[],2); for i = 2:n-1 delta = (costs(:,sorted_idx(i+1)) - costs(:,sorted_idx(i-1))) ./ norm; pop(front(sorted_idx(i))).crowding = sum(delta); end end这里用sortrows对目标函数值矩阵进行排序,比逐个维度排序高效。注意norm的处理防止了不同量纲的问题,这种归一化操作在实战中特别重要,否则拥挤度计算会翻车。
交叉变异操作也有讲究:
function child = crossover(parent1, parent2) alpha = 0.1; % 交叉系数 delta = abs(parent1.x - parent2.x); lower = min([parent1.x, parent2.x],[],2) - alpha*delta; upper = max([parent1.x, parent2.x],[],2) + alpha*delta; child.x = lower + (upper - lower).*rand(size(parent1.x)); end这个SBX模拟交叉的实现,用alpha控制搜索范围扩展,比传统均匀交叉更利于跳出局部最优。注意lower和upper的计算方式,既保留父母基因信息,又适当扩大搜索空间。
最后看看主循环的骨架:
while gen <= max_gen % 合并父代子代 combined_pop = [pop; offspring]; % 非支配排序 [fronts, ranks] = non_dominated_sorting(combined_pop); % 精英保留策略 new_pop = []; for k = 1:length(fronts) if length(new_pop) + length(fronts{k}) > pop_size last_front = fronts{k}; [~, idx] = sort([last_front.crowding], 'descend'); new_pop = [new_pop; last_front(idx(1:pop_size-length(new_pop)))]; break; end new_pop = [new_pop; fronts{k}]; end end这个精英保留策略是NSGA-II的精髓所在。当遇到需要截断的front时,不是简单按排名截取,而是根据拥挤度筛选,保证种群的多样性。这种设计让算法在收敛性和多样性之间找到了绝佳平衡点。
跑完算法后,拿pareto前沿可视化特别带劲:
function plot_pareto(pop) costs = [pop.cost]; scatter(costs(1,:), costs(2,:), 'filled'); xlabel('Objective 1'); ylabel('Objective 2'); title('Pareto Front'); end看着散点图上的解集逐渐逼近真实前沿,比玩俄罗斯方块消除一整行还解压。不过要注意目标函数间的量纲差异,必要时先做归一化处理。
折腾下来发现,NSGA-II在Matlab里实现确实方便,但想真正发挥威力还得注意三点:种群初始化要够分散、交叉变异参数要动态调整、停止准则别只用固定代数。这货就像川菜,火候把握好了才够劲。