找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1585|回复: 0
收起左侧

NSGA-II的Matlab示例代码下载

[复制链接]
ID:422559 发表于 2018-11-7 19:49 | 显示全部楼层 |阅读模式
NSGA-II示例代码,英文注释
0.png

Matlab源程序如下:
  1. function nsga_2(pop,gen)

  2. %% function nsga_2(pop,gen)
  3. % is a multi-objective optimization function where the input arguments are
  4. % pop - Population size
  5. % gen - Total number of generations
  6. %
  7. % This functions is based on evolutionary algorithm for finding the optimal
  8. % solution for multiple objective i.e. pareto front for the objectives.
  9. % Initially enter only the population size and the stoping criteria or
  10. % the total number of generations after which the algorithm will
  11. % automatically stopped.
  12. %
  13. % You will be asked to enter the number of objective functions, the number
  14. % of decision variables and the range space for the decision variables.
  15. % Also you will have to define your own objective funciton by editing the
  16. % evaluate_objective() function. A sample objective function is described
  17. % in evaluate_objective.m. Kindly make sure that the objective function
  18. % which you define match the number of objectives that you have entered as
  19. % well as the number of decision variables that you have entered. The
  20. % decision variable space is continuous for this function, but the
  21. % objective space may or may not be continuous.
  22. %
  23. % Original algorithm NSGA-II was developed by researchers in Kanpur Genetic
  24. % Algorithm Labarotary and kindly visit their website for more information


  25. %  Copyright (c) 2009, Aravind Seshadri
  26. %  All rights reserved.
  27. %
  28. %  Redistribution and use in source and binary forms, with or without
  29. %  modification, are permitted provided that the following conditions are
  30. %  met:
  31. %
  32. %     * Redistributions of source code must retain the above copyright
  33. %       notice, this list of conditions and the following disclaimer.
  34. %     * Redistributions in binary form must reproduce the above copyright
  35. %       notice, this list of conditions and the following disclaimer in
  36. %       the documentation and/or other materials provided with the distribution
  37. %      
  38. %  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. %  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. %  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. %  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  42. %  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  43. %  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  44. %  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  45. %  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  46. %  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. %  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  48. %  POSSIBILITY OF SUCH DAMAGE.

  49. %% Simple error checking
  50. % Number of Arguments
  51. % Check for the number of arguments. The two input arguments are necessary
  52. % to run this function.
  53. if nargin < 2
  54.     error('NSGA-II: Please enter the population size and number of generations as input arguments.');
  55. end
  56. % Both the input arguments need to of integer data type
  57. if isnumeric(pop) == 0 || isnumeric(gen) == 0
  58.     error('Both input arguments pop and gen should be integer datatype');
  59. end
  60. % Minimum population size has to be 20 individuals
  61. if pop < 20
  62.     error('Minimum population for running this function is 20');
  63. end
  64. if gen < 5
  65.     error('Minimum number of generations is 5');
  66. end
  67. % Make sure pop and gen are integers
  68. pop = round(pop);
  69. gen = round(gen);
  70. %% Objective Function
  71. % The objective function description contains information about the
  72. % objective function. M is the dimension of the objective space, V is the
  73. % dimension of decision variable space, min_range and max_range are the
  74. % range for the variables in the decision variable space. User has to
  75. % define the objective functions using the decision variables. Make sure to
  76. % edit the function 'evaluate_objective' to suit your needs.
  77. [M, V, min_range, max_range] = objective_description_function();

  78. %% Initialize the population
  79. % Population is initialized with random values which are within the
  80. % specified range. Each chromosome consists of the decision variables. Also
  81. % the value of the objective functions, rank and crowding distance
  82. % information is also added to the chromosome vector but only the elements
  83. % of the vector which has the decision variables are operated upon to
  84. % perform the genetic operations like corssover and mutation.
  85. chromosome = initialize_variables(pop, M, V, min_range, max_range);


  86. %% Sort the initialized population
  87. % Sort the population using non-domination-sort. This returns two columns
  88. % for each individual which are the rank and the crowding distance
  89. % corresponding to their position in the front they belong. At this stage
  90. % the rank and the crowding distance for each chromosome is added to the
  91. % chromosome vector for easy of computation.
  92. chromosome = non_domination_sort_mod(chromosome, M, V);

  93. %% Start the evolution process
  94. % The following are performed in each generation
  95. % * Select the parents which are fit for reproduction
  96. % * Perfrom crossover and Mutation operator on the selected parents
  97. % * Perform Selection from the parents and the offsprings
  98. % * Replace the unfit individuals with the fit individuals to maintain a
  99. %   constant population size.

  100. for i = 1 : gen
  101.     % Select the parents
  102.     % Parents are selected for reproduction to generate offspring. The
  103.     % original NSGA-II uses a binary tournament selection based on the
  104.     % crowded-comparision operator. The arguments are
  105.     % pool - size of the mating pool. It is common to have this to be half the
  106.     %        population size.
  107.     % tour - Tournament size. Original NSGA-II uses a binary tournament
  108.     %        selection, but to see the effect of tournament size this is kept
  109.     %        arbitary, to be choosen by the user.
  110.     pool = round(pop/2);
  111.     tour = 2;
  112.     % Selection process
  113.     % A binary tournament selection is employed in NSGA-II. In a binary
  114.     % tournament selection process two individuals are selected at random
  115.     % and their fitness is compared. The individual with better fitness is
  116.     % selcted as a parent. Tournament selection is carried out until the
  117.     % pool size is filled. Basically a pool size is the number of parents
  118.     % to be selected. The input arguments to the function
  119.     % tournament_selection are chromosome, pool, tour. The function uses
  120.     % only the information from last two elements in the chromosome vector.
  121.     % The last element has the crowding distance information while the
  122.     % penultimate element has the rank information. Selection is based on
  123.     % rank and if individuals with same rank are encountered, crowding
  124.     % distance is compared. A lower rank and higher crowding distance is
  125.     % the selection criteria.
  126.     parent_chromosome = tournament_selection(chromosome, pool, tour);

  127.     % Perfrom crossover and Mutation operator
  128.     % The original NSGA-II algorithm uses Simulated Binary Crossover (SBX) and
  129.     % Polynomial  mutation. Crossover probability pc = 0.9 and mutation
  130.     % probability is pm = 1/n, where n is the number of decision variables.
  131.     % Both real-coded GA and binary-coded GA are implemented in the original
  132.     % algorithm, while in this program only the real-coded GA is considered.
  133.     % The distribution indeices for crossover and mutation operators as mu = 20
  134.     % and mum = 20 respectively.
  135.     mu = 20;
  136.     mum = 20;
  137.     offspring_chromosome = ...
  138.         genetic_operator(parent_chromosome, ...
  139.         M, V, mu, mum, min_range, max_range);

  140.     % Intermediate population
  141.     % Intermediate population is the combined population of parents and
  142.     % offsprings of the current generation. The population size is two
  143.     % times the initial population.
  144.    
  145.     [main_pop,temp] = size(chromosome);
  146.     [offspring_pop,temp] = size(offspring_chromosome);
  147.     % temp is a dummy variable.
  148.     clear temp
  149.     % intermediate_chromosome is a concatenation of current population and
  150.     % the offspring population.
  151.     intermediate_chromosome(1:main_pop,:) = chromosome;
  152.     intermediate_chromosome(main_pop + 1 : main_pop + offspring_pop,1 : M+V) = ...
  153.         offspring_chromosome;

  154.     % Non-domination-sort of intermediate population
  155.     % The intermediate population is sorted again based on non-domination sort
  156.     % before the replacement operator is performed on the intermediate
  157.     % population.
  158.     intermediate_chromosome = ...
  159.         non_domination_sort_mod(intermediate_chromosome, M, V);
  160.     % Perform Selection
  161.     % Once the intermediate population is sorted only the best solution is
  162.     % selected based on it rank and crowding distance. Each front is filled in
  163.     % ascending order until the addition of population size is reached. The
  164.     % last front is included in the population based on the individuals with
  165.     % least crowding distance
  166.     chromosome = replace_chromosome(intermediate_chromosome, M, V, pop);
  167.     if ~mod(i,100)
  168.         clc
  169.         fprintf('%d generations completed\n',i);
  170.     end
  171. end

  172. %% Result
  173. % Save the result in ASCII text format.
  174. save solution.txt chromosome -ASCII

  175. %% Visualize
  176. % The following is used to visualize the result if objective space
  177. % dimension is visualizable.
  178. if M == 2
  179.     plot(chromosome(:,V + 1),chromosome(:,V + 2),'*');
  180. elseif M ==3
  181.     plot3(chromosome(:,V + 1),chromosome(:,V + 2),chromosome(:,V + 3),'*');
  182. end
  183.    
复制代码

所有资料51hei提供下载:
NSGA-II.zip (153.77 KB, 下载次数: 5)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表