这里用到的源码是从https://cn.mathworks.com/matlabcentral/fileexchange/10429-nsga-ii--a-multi-objective-optimization-algorithm 下载的,网上搜到说这个比较好,看了看挺简洁的,注释也很全面,自己一边看一边把注释都翻译成中文的,分享出来给初学者们一起学习下哈哈。
1.主程序
function nsga_2(pop,gen)
%% function nsga_2(pop,gen)
% is a multi-objective optimization function where the input arguments are
% pop - Population size
% gen - Total number of generations
% 这是一个多目标优化函数,pop是种群大小,gen是进化代数。
% This functions is based on evolutionary algorithm for finding the optimal
% solution for multiple objective i.e. pareto front for the objectives.
% Initially enter only the population size and the stoping criteria or
% the total number of generations after which the algorithm will
% automatically stopped.
%该函数基于多目标进化算法求解最优方案,即多目标的帕累托前沿。
%最初只输入种群规模和终止条件。
% You will be asked to enter the number of objective functions, the number
% of decision variables and the range space for the decision variables.
%你需要输入目标个数和决策变量个数,以及决策变量的空间范围。
% Also you will have to define your own objective funciton by editing the
% evaluate_objective() function. A sample objective function is described
% in evaluate_objective.m. Kindly make sure that the objective function
% which you define match the number of objectives that you have entered as
% well as the number of decision variables that you have entered. The
% decision variable space is continuous for this function, but the
% objective space may or may not be continuous.
%你还需要通过编辑evaluate_objective()函数来定义你自己的目标函数;evaluate_objective.m里定义了一个目标函数的例子。
%请确保你定义的目标函数与你输入的目标个数、决策变量个数相匹配。该函数的决策变量空间是连续的,但是目标空间可以是连续或者离散的。
%另外网上资料说objective_description_function.m 要自己重写
% Original algorithm NSGA-II was developed by researchers in Kanpur Genetic
% Algorithm Labarotary and kindly visit their website for more information
% http://www.iitk.ac.in/kangal/
% Copyright (c) 2009, Aravind Seshadri
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%% Simple error checking 错误处理
% Number of Arguments
% Check for the number of arguments. The two input arguments are necessary
% to run this function.
%确认必要的两个输入已经定义
if nargin < 2
%error('NSGA-II: Please enter the population size and number of generations as input arguments.');
error('NSGA-II:请输入种群大小和迭代次数!');
end
% Both the input arguments need to of integer data type
%两个输入都需要是整数
if isnumeric(pop) == 0 || isnumeric(gen) == 0
%error('Both input arguments pop and gen should be integer datatype');
error('NSGA-II:两个输入参数必须是整数!');
end
% Minimum population size has to be 20 individuals
%种群大小至少为