How to solve the problem ?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
function [F,front,Q, individual,index_of_objectives, f,temp,index_of_fronts,sorted_based_on_front,sorted_based_on_objective,y,next_obj, f_max, f_min,previous_obj, distance,z] = non_domination_sort_mod(comb10a, M, V)
[N, m2] = size(comb10a);
clear m2
% Initialize the front number to 1.
front = 1;
F(front).f = [];
individual = [];
for i = 1 : N
% Number of individuals that dominate this individual
individual(i).n = 0;
% Individuals which this individual dominate
individual(i).p = [];
for j = 1 : N
dom_less = 0;
dom_equal = 0;
dom_more = 0;
for k = 1 : M
if (comb10a(i,V + k) < comb10a(j,V + k))
dom_less = dom_less + 1;
elseif (comb10a(i,V + k) == comb10a(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
comb10a(i,M + V + 1) = 1;
F(front).f = [F(front).f i];
end
end
% Find the subsequent fronts
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
comb10a(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(comb10a(:,M + V + 1));
for i = 1 : length(index_of_fronts)
sorted_based_on_front(i,:) = comb10a(index_of_fronts(i),:);
end
current_index = 0;
for front = 1 : (length(F) - 1)
% objective = [];
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;
% Sort each individual based on the objective
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();
end
i am following this coding to find out the non dominated sorting and the crowding distance of my matrix comb10a ,where M is th objective function and V is the decision variables .But every time in the line of
[temp,index_of_fronts] = sort(comb10a(:,M + V + 1));
this i am getting one error
Index in position 2 exceeds array bounds (must not exceed 12).
Error in non_domination_sort_mod1wed (line 58)
[temp,index_of_fronts] = sort(comb10a(:,M + V + 1));
Error in mainnsga2 (line 34)
[F,front,Q, individual,temp,index_of_fronts,sorted_based_on_front,f] = non_domination_sort_mod1wed(comb10a, M, V)
3 comentarios
Jan
el 13 de Mayo de 2022
If the code is simplified, it is much easier to debug. Obviously the code produces an error, because the loop let it use an index outside the existing range. There is no magic workaround but you have to check the indices to find out, why M + V + 1 > columns .
Respuestas (1)
Image Analyst
el 12 de Mayo de 2022
Put these lines in
[rows, columns] = size(comb10a)
if M + V + 1 > columns
errorMessage = sprintf('Hey! The matrix comb10a has only %d columns but you are trying to sort column #%d!, which does NOT exist!', columns, M + V + 1)
uiwait(errordlg(errorMessage));
return;
else
[temp,index_of_fronts] = sort(comb10a(:, M + V + 1);
end
Now what do you see?
2 comentarios
Image Analyst
el 13 de Mayo de 2022
I'd go over the debugging training in this link
so you can learn how to step through your code and figure out how/why you're trying to sort on column 13 when there are only 12 columns in the matrix.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!