NSGA2算法MATLAB实现(能够自定义优化函数)
以前写了一个简单的NSGA2的算法能够用在ZDT1函数上:http://www.omegaxyz.com/2017/05/04/nsga2matlabzdt1/

那个NSGA2的算法不具有普遍性,下面参考课国外的课题小组的代码重新修改了内部冗余内容,使之能够自定义优化函数。

更多内容访问omegaxyz.com

NSGA2的过程为:

1、随机产生一个初始父代Po,在此基础上采用二元锦标赛选择、交叉和变异操作产生子代Qo, Po 和Qo群体规模均为N

2、将Pt和Qt并入到Rt中(初始时t=0),对Rt进行快速非支配解排序,构造其所有不同等级的非支配解集F1、F2……..

3、按照需要计算Fi中所有个体的拥挤距离,并根据拥挤比较运算符构造Pt+1,直至Pt+1规模为N,图中的Fi为F3

具体解释请见:http://www.omegaxyz.com/2017/04/14/nsga-iiintro/

C++代码请见(测试函数ZDT1):http://www.omegaxyz.com/2017/04/20/nsga2zdt1/

下面是完整版的代码:

①nsga2-optimization.m

function nsga_2_optimization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%此处可以更改
%更多机器学习内容请访问omegaxyz.com
pop = 500; %种群数量
gen = 500; %迭代次数
M = 2; %目标数量
V = 30; %维度
min_range = zeros(1, V); %下界
max_range = ones(1,V); %上界
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
chromosome = initialize_variables(pop, M, V, min_range, max_range);
chromosome = non_domination_sort_mod(chromosome, M, V);for i = 1 : genpool = round(pop/2);tour = 2;parent_chromosome = tournament_selection(chromosome, pool, tour);mu = 20;mum = 20;offspring_chromosome = genetic_operator(parent_chromosome,M, V, mu, mum, min_range, max_range);[main_pop,~] = size(chromosome);[offspring_pop,~] = size(offspring_chromosome);clear tempintermediate_chromosome(1:main_pop,:) = chromosome;intermediate_chromosome(main_pop + 1 : main_pop + offspring_pop,1 : M+V) = offspring_chromosome;intermediate_chromosome = non_domination_sort_mod(intermediate_chromosome, M, V);chromosome = replace_chromosome(intermediate_chromosome, M, V, pop);if ~mod(i,100)clc;fprintf('%d generations completed\n',i);end
endif M == 2plot(chromosome(:,V + 1),chromosome(:,V + 2),'*');xlabel('f_1'); ylabel('f_2');title('Pareto Optimal Front');
elseif M == 3plot3(chromosome(:,V + 1),chromosome(:,V + 2),chromosome(:,V + 3),'*');xlabel('f_1'); ylabel('f_2'); zlabel('f_3');title('Pareto Optimal Surface');
end

②initialize_variables.m

function f = initialize_variables(N, M, V, min_range, max_range)
min = min_range;
max = max_range;
K = M + V;
for i = 1 : Nfor j = 1 : Vf(i,j) = min(j) + (max(j) - min(j))*rand(1);endf(i,V + 1: K) = evaluate_objective(f(i,:), M, V);
end

③non_domination_sort_mod.m

function f = non_domination_sort_mod(x, M, V)
[N, ~] = size(x);
clear m
front = 1;
F(front).f = [];
individual = [];for i = 1 : Nindividual(i).n = 0;individual(i).p = [];for j = 1 : Ndom_less = 0;dom_equal = 0;dom_more = 0;for k = 1 : Mif (x(i,V + k) < x(j,V + k))dom_less = dom_less + 1;elseif (x(i,V + k) == x(j,V + k))dom_equal = dom_equal + 1;elsedom_more = dom_more + 1;endendif dom_less == 0 && dom_equal ~= Mindividual(i).n = individual(i).n + 1;elseif dom_more == 0 && dom_equal ~= Mindividual(i).p = [individual(i).p j];endend   if individual(i).n == 0x(i,M + V + 1) = 1;F(front).f = [F(front).f i];end
endwhile ~isempty(F(front).f)Q = [];for i = 1 : length(F(front).f)if ~isempty(individual(F(front).f(i)).p)for j = 1 : length(individual(F(front).f(i)).p)individual(individual(F(front).f(i)).p(j)).n = ...individual(individual(F(front).f(i)).p(j)).n - 1;if individual(individual(F(front).f(i)).p(j)).n == 0x(individual(F(front).f(i)).p(j),M + V + 1) = ...front + 1;Q = [Q individual(F(front).f(i)).p(j)];endendendendfront =  front + 1;F(front).f = Q;
end[temp,index_of_fronts] = sort(x(:,M + V + 1));
for i = 1 : length(index_of_fronts)sorted_based_on_front(i,:) = x(index_of_fronts(i),:);
end
current_index = 0;%% Crowding distancefor front = 1 : (length(F) - 1)distance = 0;y = [];previous_index = current_index + 1;for i = 1 : length(F(front).f)y(i,:) = sorted_based_on_front(current_index + i,:);endcurrent_index = current_index + i;sorted_based_on_objective = [];for i = 1 : M[sorted_based_on_objective, index_of_objectives] = ...sort(y(:,V + i));sorted_based_on_objective = [];for j = 1 : length(index_of_objectives)sorted_based_on_objective(j,:) = y(index_of_objectives(j),:);endf_max = ...sorted_based_on_objective(length(index_of_objectives), V + i);f_min = sorted_based_on_objective(1, V + i);y(index_of_objectives(length(index_of_objectives)),M + V + 1 + i)...= Inf;y(index_of_objectives(1),M + V + 1 + i) = Inf;for j = 2 : length(index_of_objectives) - 1next_obj  = sorted_based_on_objective(j + 1,V + i);previous_obj  = sorted_based_on_objective(j - 1,V + i);if (f_max - f_min == 0)y(index_of_objectives(j),M + V + 1 + i) = Inf;elsey(index_of_objectives(j),M + V + 1 + i) = ...(next_obj - previous_obj)/(f_max - f_min);endendenddistance = [];distance(:,1) = zeros(length(F(front).f),1);for i = 1 : Mdistance(:,1) = distance(:,1) + y(:,M + V + 1 + i);endy(:,M + V + 2) = distance;y = y(:,1 : M + V + 2);z(previous_index:current_index,:) = y;
end
f = z();

④tournament_selection.m

function f = tournament_selection(chromosome, pool_size, tour_size)
[pop, variables] = size(chromosome);
rank = variables - 1;
distance = variables;
for i = 1 : pool_sizefor j = 1 : tour_sizecandidate(j) = round(pop*rand(1));if candidate(j) == 0candidate(j) = 1;endif j > 1while ~isempty(find(candidate(1 : j - 1) == candidate(j)))candidate(j) = round(pop*rand(1));if candidate(j) == 0candidate(j) = 1;endendendendfor j = 1 : tour_sizec_obj_rank(j) = chromosome(candidate(j),rank);c_obj_distance(j) = chromosome(candidate(j),distance);endmin_candidate = ...find(c_obj_rank == min(c_obj_rank));if length(min_candidate) ~= 1max_candidate = ...find(c_obj_distance(min_candidate) == max(c_obj_distance(min_candidate)));if length(max_candidate) ~= 1max_candidate = max_candidate(1);endf(i,:) = chromosome(candidate(min_candidate(max_candidate)),:);elsef(i,:) = chromosome(candidate(min_candidate(1)),:);end
end

⑤genetic_operator.m

function f  = genetic_operator(parent_chromosome, M, V, mu, mum, l_limit, u_limit)
[N,m] = size(parent_chromosome);clear m
p = 1;
was_crossover = 0;
was_mutation = 0;for i = 1 : N% With 90 % probability perform crossoverif rand(1) < 0.9% Initialize the children to be null vector.child_1 = [];child_2 = [];% Select the first parentparent_1 = round(N*rand(1));if parent_1 < 1parent_1 = 1;end% Select the second parentparent_2 = round(N*rand(1));if parent_2 < 1parent_2 = 1;end% Make sure both the parents are not the same. while isequal(parent_chromosome(parent_1,:),parent_chromosome(parent_2,:))parent_2 = round(N*rand(1));if parent_2 < 1parent_2 = 1;endend% Get the chromosome information for each randomnly selected% parentsparent_1 = parent_chromosome(parent_1,:);parent_2 = parent_chromosome(parent_2,:);% Perform corssover for each decision variable in the chromosome.for j = 1 : V% SBX (Simulated Binary Crossover).% For more information about SBX refer the enclosed pdf file.% Generate a random numberu(j) = rand(1);if u(j) <= 0.5bq(j) = (2*u(j))^(1/(mu+1));elsebq(j) = (1/(2*(1 - u(j))))^(1/(mu+1));end% Generate the jth element of first childchild_1(j) = ...0.5*(((1 + bq(j))*parent_1(j)) + (1 - bq(j))*parent_2(j));% Generate the jth element of second childchild_2(j) = ...0.5*(((1 - bq(j))*parent_1(j)) + (1 + bq(j))*parent_2(j));% Make sure that the generated element is within the specified% decision space else set it to the appropriate extrema.if child_1(j) > u_limit(j)child_1(j) = u_limit(j);elseif child_1(j) < l_limit(j)child_1(j) = l_limit(j);endif child_2(j) > u_limit(j)child_2(j) = u_limit(j);elseif child_2(j) < l_limit(j)child_2(j) = l_limit(j);endendchild_1(:,V + 1: M + V) = evaluate_objective(child_1, M, V);child_2(:,V + 1: M + V) = evaluate_objective(child_2, M, V);was_crossover = 1;was_mutation = 0;% With 10 % probability perform mutation. Mutation is based on% polynomial mutation. else% Select at random the parent.parent_3 = round(N*rand(1));if parent_3 < 1parent_3 = 1;end% Get the chromosome information for the randomnly selected parent.child_3 = parent_chromosome(parent_3,:);% Perform mutation on eact element of the selected parent.for j = 1 : Vr(j) = rand(1);if r(j) < 0.5delta(j) = (2*r(j))^(1/(mum+1)) - 1;elsedelta(j) = 1 - (2*(1 - r(j)))^(1/(mum+1));end% Generate the corresponding child element.child_3(j) = child_3(j) + delta(j);% Make sure that the generated element is within the decision% space.if child_3(j) > u_limit(j)child_3(j) = u_limit(j);elseif child_3(j) < l_limit(j)child_3(j) = l_limit(j);endendchild_3(:,V + 1: M + V) = evaluate_objective(child_3, M, V);% Set the mutation flagwas_mutation = 1;was_crossover = 0;endif was_crossoverchild(p,:) = child_1;child(p+1,:) = child_2;was_cossover = 0;p = p + 2;elseif was_mutationchild(p,:) = child_3(1,1 : M + V);was_mutation = 0;p = p + 1;end
end
f = child;

⑥replace_chromosome.m

function f  = replace_chromosome(intermediate_chromosome, M, V,pop)[N, m] = size(intermediate_chromosome);% Get the index for the population sort based on the rank
[temp,index] = sort(intermediate_chromosome(:,M + V + 1));clear temp m% Now sort the individuals based on the index
for i = 1 : Nsorted_chromosome(i,:) = intermediate_chromosome(index(i),:);
end% Find the maximum rank in the current population
max_rank = max(intermediate_chromosome(:,M + V + 1));% Start adding each front based on rank and crowing distance until the
% whole population is filled.
previous_index = 0;
for i = 1 : max_rank% Get the index for current rank i.e the last the last element in the% sorted_chromosome with rank i. current_index = max(find(sorted_chromosome(:,M + V + 1) == i));% Check to see if the population is filled if all the individuals with% rank i is added to the population. if current_index > pop% If so then find the number of individuals with in with current% rank i.remaining = pop - previous_index;% Get information about the individuals in the current rank i.temp_pop = ...sorted_chromosome(previous_index + 1 : current_index, :);% Sort the individuals with rank i in the descending order based on% the crowding distance.[temp_sort,temp_sort_index] = ...sort(temp_pop(:, M + V + 2),'descend');% Start filling individuals into the population in descending order% until the population is filled.for j = 1 : remainingf(previous_index + j,:) = temp_pop(temp_sort_index(j),:);end
        return;elseif current_index < pop% Add all the individuals with rank i into the population.f(previous_index + 1 : current_index, :) = ...sorted_chromosome(previous_index + 1 : current_index, :);else% Add all the individuals with rank i into the population.f(previous_index + 1 : current_index, :) = ...sorted_chromosome(previous_index + 1 : current_index, :);
        return;end% Get the index for the last added individual.previous_index = current_index;
end

⑦自定义评价函数(我选用的ZDT1函数)

function f = evaluate_objective(x, M, V)
f = [];
f(1) = x(1);
g = 1;
sum = 0;
for i = 1:Vsum = sum + x(i);
end
sum = sum + 9*(sum / (V-1));
g = g + sum;
f(2) = g * (1 - sqrt(x(1) / g));
end

500个种群运行500代的结果:


代码打包下载:http://download.csdn.net/download/xyisv/10217700
更多内容访问omegaxyz.com

网站文章采用知识共享许可协议BY-NC-SA4.0授权
© 2018 • OmegaXYZ-版权所有 转载请注明出处

NSGA2算法MATLAB相关推荐

  1. 多目标优化算法matlab代码大合集

    [NSGA2]基于NSGA2算法求解多目标优化问题Matlab源码2 [水母搜索优化器算法]基于水母搜索优化器算法求解多目标优化问题(JellyfishSearchOptimizer,JSO)[粒子群 ...

  2. dst matlab,DSTcode DST跟踪算法MATLAB代码,复杂环境中仿多目标 实现的单 Other systems 其他 272万源代码下载- www.pudn.com...

    文件名称: DSTcode下载  收藏√  [ 5  4  3  2  1 ] 开发工具: matlab 文件大小: 82 KB 上传时间: 2017-03-17 下载次数: 0 提 供 者: Mar ...

  3. 图片缩放 算法 matlab,图像放大算法总结及MATLAB源程序.doc

    图像放大算法总结及MATLAB源程序 1,插值算法(3种): (1)最邻近插值(近邻取样法): 最近插值的的思想很简单就是把这个非整数坐标作一个四舍五入,取最近的整数点坐标处的点的颜色.可见,最邻近插 ...

  4. TDOA定位的Chan算法MATLAB源代码

    TDOA定位的Chan算法MATLAB源代码 . function [POS_ref,POS1,POS2,POS3,POS4] = TDOA_chan(R,Pbs,Q) %************** ...

  5. dijkstra算法matlab代码_头脑风暴优化(BSO)算法(附MATLAB代码)

    BSO讲解https://www.zhihu.com/video/1252605855767736320 B站搜索:随心390,同步观看视频 各位小伙伴可在闲鱼搜索 优化算法交流地,即可搜索到官方闲鱼 ...

  6. Algorithm之PrA:PrA之nLP非线性规划算法+Matlab 优化工具箱的GUI求解非线性规划

    Algorithm之PrA:PrA之nLP非线性规划算法+Matlab 优化工具箱的GUI求解非线性规划 目录 PrA之nLP非线性规划算法 操作图文教程 PrA之nLP非线性规划算法 (1).编写M ...

  7. matlab虚拟力,31无线传感网络布局优化的虚拟力导向粒子群算法MATLAB源代码

    无线传感网络布局优化的虚拟力导向粒子群算法MATLAB源代码 本源代码主要参考了下面的文献:王雪, 王晟, 马俊杰. 无线传感器网络布局的虚拟力导向微粒群优化策略[J]. 电子学报, 2007, 11 ...

  8. 亮度均匀性 matlab,求:亮度保持的夜景图像直方图均衡算法 matlab程序

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 求:亮度保持的夜景图像直方图均衡算法 matlab程序 我是大四学生,最近在做一个论文,头疼死了,不知道这个论文的程序怎么写.这里是matlab论坛 我想 ...

  9. bartlett 算法 matlab,GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)

    以优化SVM算法的参数c和g为例,对GWO算法MATLAB源码进行了逐行中文注解. tic % 计时器 %% 清空环境变量 close all clear clc format compact %% ...

  10. NSGA2算法及其代码

    本人最近研究NSGA2算法,网上有很多示例代码,但是基本没有注释,代码看起来很头疼,因此我最近把整个代码研读了一遍,并做上中文注释,希望可以帮助到一些和我一样的初学者们.贴出代码之前,首先介绍一下NS ...

最新文章

  1. 如何在 Centos8 中安装 Lynis审计工具
  2. 【Python基础】零基础学习Python列表操作
  3. 入坑推荐系统,从Google这篇开始
  4. 魔兽世界·与你同行,一起追忆魔兽年华吧
  5. ftp客户端flashfxp破解教程
  6. 营业执照、组织机构代码、统一社会信用代码
  7. Socket(服务器端)通信连接失败解决方法
  8. 计算机专业考研可关注哪些公众号,考研应关注哪些公众号?
  9. 【精】JAVA各大厂问题汇总-HELLO XF
  10. 高级驾驶辅助系统ADAS简介
  11. 解密:股票短线起涨点的挂单玄机!
  12. win10系统设置webp文件默认用照片查看器打开的两种方法
  13. Windows 上的网络通信编程
  14. [开源精品] C#.NET im 聊天通讯架构设计 -- FreeIM 支持集群、职责分明、高性能
  15. 嵌入式新闻早班车-第16期
  16. 我花了十八年时间才能和你坐在一起喝咖啡
  17. 微信端视频播放防被浏览器劫持
  18. Windows 10 第七个大版本更新来了,10个全新功能安排得明明白白~
  19. 深度学习模型可解释性的研究进展_化盈盈
  20. 物联网IoT与万维物联网WoT

热门文章

  1. 查找算法之四 斐波那契查找(C++版本)
  2. JavaWeb之Servlet编程
  3. maya嵌入python_#113 如何给Maya添加一个Python Command Shell ? | 一半君的总结纸
  4. python mysql使用教程_python进阶之Mysql入门教程
  5. ppt上的倒计时小工具_英孚线上精品小班课平台操作指南这些课堂小工具你都知道吗?更多课前指引看这里!...
  6. 北方民族大学c语言期末考试试题,2018年北方民族大学软件工程832C语言程序设计与数据结构之C程序设计考研核心题库...
  7. qt中如何模拟按钮点击_怎么在qt中实现一个按钮列表?
  8. qq数据泄露_用这个开源项目来解决你团队里猪队友泄露公司敏感信息的问题
  9. 22. Django进阶:文件上传
  10. SQL:postgresql中合并多个geom数据ST_Union以及比较两个geom数据是否相同ST_Equals