找回密码
 注册
关于网站域名变更的通知
查看: 293|回复: 1
打印 上一主题 下一主题

NSGA2算法MATLAB实现(能够自定义优化函数)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-8-18 14:05 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

6 q! F# B) a1 x( `4 G4 CNSGA2的算法不具有普遍性,下面参考课国外的课题小组的代码重新修改了内部冗余内容,使之能够自定义优化函数。+ y( I$ t. u9 l1 X& T+ P6 t

4 i: v5 F% Y; f - ~6 Q8 x0 S5 H; K) R3 v  ?
0 t+ a# Q1 ~  S+ W, N  I
NSGA2的过程为:
- m; _8 i0 b2 T* h4 P; r$ e7 _* z$ ^; Q2 F7 D
1、随机产生一个初始父代Po,在此基础上采用二元锦标赛选择、交叉和变异操作产生子代Qo, Po 和Qo群体规模均为N
: @9 ^/ U0 B7 [2 p
- r+ I9 w( n. j# ?6 r( O0 T* X2、将Pt和Qt并入到Rt中(初始时t=0),对Rt进行快速非支配解排序,构造其所有不同等级的非支配解集F1、F2……..3 E3 @7 `; t! C, r2 r! K
0 I$ U% j3 K9 S( _5 \: c8 W
3、按照需要计算Fi中所有个体的拥挤距离,并根据拥挤比较运算符构造Pt+1,直至Pt+1规模为N,图中的Fi为F3/ D: S$ i; F( C" S' c- I( I: S
2 U5 v( Z$ y6 b$ j, L" r
8 {  q* m5 f. y. l
下面是完整版的代码:8 F2 l6 Z3 h0 z6 F- [" ?) m1 b6 B

; Y& j0 t$ @' C; U7 d! h①nsga2-optimization.m
& c' W/ E! }3 Z& A) \. }0 L9 _' I: Y# v0 U  ^& ~4 V
  • 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 : gen
  •     pool = 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 temp
  •     intermediate_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
  • end
  • if M == 2
  •     plot(chromosome(:,V + 1),chromosome(:,V + 2),'*');
  •     xlabel('f_1'); ylabel('f_2');
  •     title('Pareto Optimal Front');
  • elseif M == 3
  •     plot3(chromosome(:,V + 1),chromosome(:,V + 2),chromosome(:,V + 3),'*');
  •     xlabel('f_1'); ylabel('f_2'); zlabel('f_3');
  •     title('Pareto Optimal SuRFace');
  • end7 N$ y& \  e# x. Z% O; f9 X

( F, i0 n( v0 x$ ?# a
$ y. T! N5 G, n% H0 Y- y  C②initialize_variables.m$ s8 V9 C% E- X" O7 z. B7 f% r" I

9 a4 N& n7 L7 R. v0 u6 y% ~9 J
  • function f = initialize_variables(N, M, V, min_range, max_range)
  • min = min_range;
  • max = max_range;
  • K = M + V;
  • for i = 1 : N
  •     for j = 1 : V
  •         f(i,j) = min(j) + (max(j) - min(j))*rand(1);
  •     end
  •     f(i,V + 1: K) = evaluate_objective(f(i,:), M, V);
  • end3 z3 i& X  Y7 M

4 C: }- O+ ^. G  ]
+ C7 k2 x# r, U0 ^③non_domination_sort_mod.m
* }1 v6 D  u( l3 Z( N
) [8 }8 A# i3 |9 ~% N0 \) o% }
  • function f = non_domination_sort_mod(x, M, V)
  • [N, ~] = size(x);
  • clear m
  • front = 1;
  • F(front).f = [];
  • individual = [];
  • for i = 1 : N
  •     individual(i).n = 0;
  •     individual(i).p = [];
  •     for j = 1 : N
  •         dom_less = 0;
  •         dom_equal = 0;
  •         dom_more = 0;
  •         for k = 1 : M
  •             if (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;
  •             else
  •                 dom_more = dom_more + 1;
  •             end
  •         end
  •         if dom_less == 0 && dom_equal ~= M
  •             individual(i).n = individual(i).n + 1;
  •         elseif dom_more == 0 && dom_equal ~= M
  •             individual(i).p = [individual(i).p j];
  •         end
  •     end
  •     if individual(i).n == 0
  •         x(i,M + V + 1) = 1;
  •         F(front).f = [F(front).f i];
  •     end
  • end
  • while ~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 == 0
  •                                x(individual(F(front).f(i)).p(j),M + V + 1) = ...
  •                         front + 1;
  •                     Q = [Q individual(F(front).f(i)).p(j)];
  •                 end
  •             end
  •        end
  •    end
  •    front =  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 distance
  • for 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,:);
  •     end
  •     current_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),:);
  •         end
  •         f_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) - 1
  •             next_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;
  •             else
  •                 y(index_of_objectives(j),M + V + 1 + i) = ...
  •                      (next_obj - previous_obj)/(f_max - f_min);
  •             end
  •          end
  •     end
  •     distance = [];
  •     distance(:,1) = zeros(length(F(front).f),1);
  •     for i = 1 : M
  •         distance(:,1) = distance(:,1) + y(:,M + V + 1 + i);
  •     end
  •     y(:,M + V + 2) = distance;
  •     y = y(:,1 : M + V + 2);
  •     z(previous_index:current_index,:) = y;
  • end
  • f = z();
    , a$ k4 F7 G8 A5 D, p' Q5 S* h. U) O6 p4 ]4 D
4 W$ ^0 U0 N5 ~

& g8 y1 k/ O5 f# o6 ^8 I. O' d      ④tournament_selection.m
6 R. C& |+ H2 O
1 S& o) k$ p- X: {6 A$ c/ A
  • function f = tournament_selection(chromosome, pool_size, tour_size)
  • [pop, variables] = size(chromosome);
  • rank = variables - 1;
  • distance = variables;
  • for i = 1 : pool_size
  •     for j = 1 : tour_size
  •         candidate(j) = round(pop*rand(1));
  •         if candidate(j) == 0
  •             candidate(j) = 1;
  •         end
  •         if j > 1
  •             while ~isempty(find(candidate(1 : j - 1) == candidate(j)))
  •                 candidate(j) = round(pop*rand(1));
  •                 if candidate(j) == 0
  •                     candidate(j) = 1;
  •                 end
  •             end
  •         end
  •     end
  •     for j = 1 : tour_size
  •         c_obj_rank(j) = chromosome(candidate(j),rank);
  •         c_obj_distance(j) = chromosome(candidate(j),distance);
  •     end
  •     min_candidate = ...
  •         find(c_obj_rank == min(c_obj_rank));
  •     if length(min_candidate) ~= 1
  •         max_candidate = ...
  •         find(c_obj_distance(min_candidate) == max(c_obj_distance(min_candidate)));
  •         if length(max_candidate) ~= 1
  •             max_candidate = max_candidate(1);
  •         end
  •         f(i,:) = chromosome(candidate(min_candidate(max_candidate)),:);
  •     else
  •         f(i,:) = chromosome(candidate(min_candidate(1)),:);
  •     end
  • end
    % c, V+ ~$ j$ R
9 }& D9 C5 T$ ^4 h: o7 R

7 ?; K' l, v9 V1 K2 {⑤genetic_operator.m! T0 X5 h4 Z) T  W: }6 A. t4 }

  e4 C# Y1 b7 `; D
  • 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 crossover
  •     if rand(1) < 0.9
  •         % Initialize the children to be null vector.
  •         child_1 = [];
  •         child_2 = [];
  •         % Select the first parent
  •         parent_1 = round(N*rand(1));
  •         if parent_1 < 1
  •             parent_1 = 1;
  •         end
  •         % Select the second parent
  •         parent_2 = round(N*rand(1));
  •         if parent_2 < 1
  •             parent_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 < 1
  •                 parent_2 = 1;
  •             end
  •         end
  •         % Get the chromosome information for each randomnly selected
  •         % parents
  •         parent_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 number
  •             u(j) = rand(1);
  •             if u(j) <= 0.5
  •                 bq(j) = (2*u(j))^(1/(mu+1));
  •             else
  •                 bq(j) = (1/(2*(1 - u(j))))^(1/(mu+1));
  •             end
  •             % Generate the jth element of first child
  •             child_1(j) = ...
  •                 0.5*(((1 + bq(j))*parent_1(j)) + (1 - bq(j))*parent_2(j));
  •             % Generate the jth element of second child
  •             child_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);
  •             end
  •             if 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);
  •             end
  •         end
  •         child_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 < 1
  •             parent_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 : V
  •            r(j) = rand(1);
  •            if r(j) < 0.5
  •                delta(j) = (2*r(j))^(1/(mum+1)) - 1;
  •            else
  •                delta(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);
  •            end
  •         end
  •         child_3(:,V + 1: M + V) = evaluate_objective(child_3, M, V);
  •         % Set the mutation flag
  •         was_mutation = 1;
  •         was_crossover = 0;
  •     end
  •     if was_crossover
  •         child(p,:) = child_1;
  •         child(p+1,:) = child_2;
  •         was_cossover = 0;
  •         p = p + 2;
  •     elseif was_mutation
  •         child(p,:) = child_3(1,1 : M + V);
  •         was_mutation = 0;
  •         p = p + 1;
  •     end
  • end
  • f = child;4 u. {% D2 D9 e4 c: F' _- D; M
   7 C6 g! z9 c2 B3 u0 a# Y! j% H* f! D
⑥replace_chromosome.m/ u- w) h3 C' j2 h( s) A

$ ]5 p  @" o# ^: N, X
  • 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 : N
  •     sorted_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 : remaining
  •             f(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+ q8 z2 ~) t8 N( i
            / n1 y% P) Z  Y* R% S

* ~0 q$ h: W( R, j& F: w) n⑦自定义评价函数(我选用的ZDT1函数)
! L4 r$ S: I; p; r/ n- w- v; c9 H: P5 n- Z
  • function f = evaluate_objective(x, M, V)
  • f = [];
  • f(1) = x(1);
  • g = 1;
  • sum = 0;
  • for i = 2:V
  •     sum = sum + x(i);
  • end
  • g = g + 9*sum / (V-1));
  • f(2) = g * (1 - sqrt(x(1) / g));
  • end% a+ A( I* w% I7 {* |/ `

6 l; _9 W* d- U& }: F
+ {: p. W" v0 a+ ^) k$ X" o500个种群运行500代的结果:1 b" a& \! U* R+ }
' L# k! h; x0 o3 Q3 f# a; j
( H* }# `) s! _: J8 O

' h) I9 B) l* c# i; Y/ u% _( E# N
( d3 J9 \' |0 ^2 \. Y4 Y
2 }+ A: J4 j1 e1 I
4 _: ]: h- q; B, M+ B7 R  {/ A0 w0 R

, `$ K1 a) e6 G/ e% V1 t* D9 e! s* E5 r
) D' n! M/ L8 R* U& V. A! f' t( [

该用户从未签到

2#
发表于 2020-8-18 15:00 | 只看该作者
NSGA2算法MATLAB实现(能够自定义优化函数)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-24 18:22 , Processed in 0.250000 second(s), 27 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

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