nsga-II算法的matlab代码

时间: 2023-09-14 17:10:05 浏览: 42
以下是NSGA-II算法的MATLAB代码: ```matlab %% NSGA-II Algorithm function [pop, front] = NSGAII(pop_size,gen_size,vari_num,x_min,x_max) % Parameters Setting pc = 0.9; % crossover probability pm = 1/vari_num; % mutation probability eta_c = 20; % crossover distribution index eta_m = 20; % mutation distribution index pop = rand(pop_size,vari_num) .* repmat((x_max-x_min),pop_size,1) + repmat(x_min,pop_size,1); % initial population for i = 1:pop_size obj(i,1) = f1(pop(i,:)); obj(i,2) = f2(pop(i,:)); end % NSGA-II Algorithm for i = 1:gen_size % Non-dominated Sorting [front,~] = non_domination_sort(obj); % Crowding Distance Calculation for j = 1:length(front) crowd_dis(j,:) = crowding_distance(obj(front{j},:)); end % Mating Selection & Variation pop_new = zeros(pop_size,vari_num); obj_new = zeros(pop_size,2); count = 0; for j = 1:length(front) [temp,index] = sort(crowd_dis(j,:),'descend'); front_member = front{j}; pop_temp = pop(front_member,:); for k = 1:length(front_member) if rand < pc && k ~= length(front_member) p1 = front_member(k); p2 = front_member(k+1); pop_new(count+1,:) = crossover(pop_temp(p1,:),pop_temp(p2,:),eta_c); obj_new(count+1,:) = [f1(pop_new(count+1,:)),f2(pop_new(count+1,:))]; count = count + 1; elseif rand < pm p = front_member(k); pop_new(count+1,:) = mutation(pop_temp(p,:),eta_m,x_min,x_max); obj_new(count+1,:) = [f1(pop_new(count+1,:)),f2(pop_new(count+1,:))]; count = count + 1; end end if count >= pop_size break; end end % Combine Parent & Offspring Populations pop = [pop;pop_new(1:pop_size-count,:)]; obj = [obj;obj_new(1:pop_size-count,:)]; end % Results Output [front,~] = non_domination_sort(obj); for i = 1:length(front) plot(obj(front{i},1),obj(front{i},2),'o'); hold on; end xlabel('f_1'); ylabel('f_2'); end %% Non-dominated Sorting function [front,rank] = non_domination_sort(obj) n = size(obj,1); rank = inf(1,n); dominate = false(n); for i = 1:n for j = i+1:n if all(obj(i,:) <= obj(j,:)) && any(obj(i,:) < obj(j,:)) dominate(i,j) = true; elseif all(obj(i,:) >= obj(j,:)) && any(obj(i,:) > obj(j,:)) dominate(j,i) = true; end end end for i = 1:n if sum(dominate(:,i)) == 0 rank(i) = 1; end end front{1} = find(rank == 1); i = 1; while ~isempty(front{i}) Q = []; for j = front{i} for k = 1:n if dominate(j,k) dominate(j,k) = false; if sum(dominate(k,:)) == 0 rank(k) = i + 1; Q = [Q,k]; end end end end i = i + 1; front{i} = Q; end end %% Crowding Distance Calculation function [crowd_dis] = crowding_distance(obj) n = size(obj,1); crowd_dis = zeros(1,n); f_max = max(obj,[],1); f_min = min(obj,[],1); for i = 1:size(obj,2) [~,index] = sort(obj(:,i)); crowd_dis(index(1)) = inf; crowd_dis(index(end)) = inf; for j = 2:n-1 crowd_dis(index(j)) = crowd_dis(index(j)) + (obj(index(j+1),i) - obj(index(j-1),i))/(f_max(i) - f_min(i)); end end end %% SBX Crossover Operator function [offspring] = crossover(p1,p2,eta_c) n = length(p1); u = rand(1,n); offspring = zeros(1,n); for i = 1:n if u(i) <= 0.5 if abs(p1(i)-p2(i)) > 1e-10 if p1(i) < p2(i) x1 = p1(i); x2 = p2(i); else x1 = p2(i); x2 = p1(i); end y1 = (x1 - floor(x1)) + floor(x2); y2 = (x2 - floor(x2)) + floor(x1); if rand < 0.5 offspring(i) = y1; else offspring(i) = y2; end else offspring(i) = p1(i); end else if abs(p1(i)-p2(i)) > 1e-10 if p1(i) < p2(i) x1 = p1(i); x2 = p2(i); else x1 = p2(i); x2 = p1(i); end y1 = 2*x1 - x2; y2 = 2*x2 - x1; if y1 < 0 y1 = 0; elseif y1 > 1 y1 = 1; end if y2 < 0 y2 = 0; elseif y2 > 1 y2 = 1; end offspring(i) = y1; else offspring(i) = p1(i); end end end end %% Polynomial Mutation Operator function [offspring] = mutation(p,eta_m,x_min,x_max) n = length(p); offspring = p; for i = 1:n if rand < eta_m/n delta1 = (p(i) - x_min)/(x_max - x_min); delta2 = (x_max - p(i))/(x_max - x_min); u = rand; if u <= 0.5 deltaq = (2*u + (1 - 2*u)*(1-delta1)^(eta_m+1))^(1/(eta_m+1))-1; else deltaq = 1 - (2*(1-u) + 2*(u-0.5)*(1-delta2)^(eta_m+1))^(1/(eta_m+1)); end offspring(i) = p(i) + deltaq*(x_max - x_min); if offspring(i) < x_min offspring(i) = x_min; elseif offspring(i) > x_max offspring(i) = x_max; end end end end %% Objective Function 1 function [y1] = f1(x) y1 = x(1); end %% Objective Function 2 function [y2] = f2(x) y2 = (1+x(2))/x(1); end ``` 其中,`pop_size`为种群大小,`gen_size`为迭代次数,`vari_num`为变量个数,`x_min`和`x_max`分别为变量的上下界。`f1`和`f2`分别为两个目标函数,根据具体问题进行修改。

相关推荐

最新推荐

recommend-type

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx
recommend-type

华为OD机试D卷 - 机场航班调度程序 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

基于FPGA读取设计的心电图代码源码+全部资料齐全.zip

【资源说明】 基于FPGA读取设计的心电图代码源码+全部资料齐全.zip基于FPGA读取设计的心电图代码源码+全部资料齐全.zip 【备注】 1、该项目是高分课程设计项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过mac/window10/11/linux测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip

【资源说明】 基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依