非支配快速排序算法详解
生活随笔
收集整理的這篇文章主要介紹了
非支配快速排序算法详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
非支配快速排序算法詳解
對NSGA-II的一個學習記錄
?? ?? ??
為易于理解,只用三個點舉例子。
Np=3; %三個點 current_vector = [1:1:Np]'; %當前向量 current_pf=1; %先找F1 RANK = zeros(Np,1); %RANK初始化 all_perm = [repmat([1:1:Np]',Np',1), reshape(repmat([1:1:Np],Np,1),Np^2,1)];%生成兩列 all_perm(all_perm(:,1)==all_perm(:,2),:) = []; %每個點和其余比較 d=[0 1 0 0 1 1]; %假設支配關系 dominated_particles = unique(all_perm(d==1,2));%依次找到各個點是否受人支配,不在乎受幾個人支配,只要受人支配了,那他就不是當前要找的等級。 non_dom_idx = ~ismember(current_vector,dominated_particles);%找到非支配的點的位置,這是一個邏輯值 RANK(current_vector(non_dom_idx)) = current_pf; %讓這些位置成為當前等級 all_perm(ismember(all_perm(:,1),current_vector(non_dom_idx)),:) = [];%找到以后,刪除,因為一個點只能屬于一個等級 all_perm(ismember(all_perm(:,2),current_vector(non_dom_idx)),:) = [];%找到以后,刪除,因為一個點只能屬于一個等級 current_vector(non_dom_idx) = [];%當前向量中刪除那些非支配點 current_pf = current_pf + 1; %等級+1,找下一個等級。
思路以及程序實現過程即為上述。其實在非支配快速排序中還要判斷是否已經把所有粒子給分好等級了。有兩個跳出循環的地方
1、判斷當前向量中是否只有一個點了,要是就一個點了,那他就是最后一個等級了,直接跳出就好了;
2、判斷當前向量中,是否沒有更多空間去放等級了。(沒看太明白,這塊直接跳出了,等級也沒加,可能就是一個判斷的過程。)不影響分等級。
有兩個函數,快速非支配排序和比較函數
function [RANK] = FastNonDominatedSorting_Vectorized(fitness)% InitializationNp = size(fitness,1);RANK = zeros(Np,1);current_vector = [1:1:Np]';current_pf = 1;all_perm = [repmat([1:1:Np]',Np',1), reshape(repmat([1:1:Np],Np,1),Np^2,1)];all_perm(all_perm(:,1)==all_perm(:,2),:) = []; %每個個體去和別人比。 一個個體有兩個目標函數值% Computing each Pareto Frontwhile ~isempty(current_vector) %非空,就是我這100個個體還沒分完等級呢。% Check if there is only a single particleif length(current_vector) == 1RANK(current_vector) = current_pf;break;end% Non-dominated particles% Note: nchoosek has an exponential grow in computation time, so% it's better to take all the combinations including repetitions using a% loops (quasi-linear grow) or repmats (linear grow)%all_perm = nchoosek(current_vector,2); %all_perm = [all_perm; [all_perm(:,2) all_perm(:,1)]]; d = dominates(fitness(all_perm(:,1),:),fitness(all_perm(:,2),:));%d是一個邏輯值,他判斷了所有個體和單個個體的支配關系。第一列是2到200,第二列全是1,先判斷2到200是不是支配了1。以此類推。dominated_particles = unique(all_perm(d==1,2)); % 找到被人支配的,unique就是不管是誰支配了1,不管有幾個,我只計數一個% Check if there is no room for more Pareto Frontsif sum(~ismember(current_vector,dominated_particles)) == 0break;end% Update ranks and current_vector 更新等級non_dom_idx = ~ismember(current_vector,dominated_particles); %找出那些沒有人支配的所在的位置,然后在current_vector中挑出來,就是第一等級RANK(current_vector(non_dom_idx)) = current_pf; %把這些定為第一等級,其余位置都是0,我不在乎他是第幾等級。用邏輯值來判斷all_perm(ismember(all_perm(:,1),current_vector(non_dom_idx)),:) = [];all_perm(ismember(all_perm(:,2),current_vector(non_dom_idx)),:) = [];%判斷完的就把剔除出去current_vector(non_dom_idx) = []; %判斷完的就剔除出去了current_pf = current_pf + 1; %找下一等級的去,知道找完了才停下來。end end % Function that returns true if x dominates y and false otherwise function d = dominates(x,y)d = (all(x<=y,2) & any(x<y,2)); end只是記錄學習,如有錯誤,請及時指出,謝謝大家。
參考:mathworks NSGA-II(2019年更新的那版)
總結
以上是生活随笔為你收集整理的非支配快速排序算法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django目录下的路由系统和视图函数
- 下一篇: word怎么显示计算机数字,如何使wor