📄 selection.m
字号:
function [max_fitness_temp_position,decimal_space_selected,binary_space_selected,maxfitness_selection]=selection(decimal_space,binary_space,fitness_value,bits)
%Selection Function Of Simple Genetic Algorithm Program (Version 1.0.0.1)
%support multi-parameters
%By chen yi ,CQU .QQ:2376635 Email:cdey@10mail.net (May,10th,2002)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Select-reproduction by Desired value method is the third step in
% SGA(Simple Genetic Algorithm )
%~Usage:
%[decimal_space_selected,maxfitness_selection]=selection(decimal_space_old,fitness)
%~~
%bits_No in coding.m is different from bits_No in decoding.m
%bits_No=[0,bits_No] in decoding.m
%decimal_space is decimal_space produced by DECODING function
%~~~
%fitness is test function of fitness in SGA from FITNESS by FITNESS function
%
%maxfitness_selection is the maximum value of fitness_function at present
%~~~~
%decimal_space_selected is the rest decimal_space which selected by the
%SELECTION function
%~~~~~
%max_fitness_temp_position is the position of selected decimal_space
% e.g.
%f(x)=1./a+sin(b)+exp(c)+log2(d)+10 , a,b,c,d belong to [1,7],[2,8],[3,9],[4,10]
%[binary_space,bits_sum,bits]=coding([1,2,3,4],[7,8,9,10],10,[0.01,0.01,0.01,0.01])
%[decimal_space]=decoding([1,2,3,4],[7,8,9,10],binary_space,bits)
%[fitness_value]=fitness(decimal_space)
%[max_fitness_temp_position,decimal_space_selected,binary_space_selected,maxfitness_selection]=selection(decimal_space,binary_space,fitness_value,bits)
%
% See Also DECODING ,CODING ,CROSSOVER,MUTATION,FITNESS,
% FITNESS_FUNCTION, SGA
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
[population,parameter_numbers]=size(decimal_space);
%
fitness_desired_value=sum(fitness_value,2)/population;
% MAX(X) is the largest element in X
maxfitness_selection=max(fitness_value);
%bits_No is important
bits_No=[0,bits];
% to find out which is the binary parameter
%i=0:0.1:population;
for j=1:1:parameter_numbers
%to avoid that Index exceeds matrix dimensions
bits_No(j+1)=bits_No(j+1)+bits_No(j);
end
for i=1:1:population
%work out the next Desired value of fitness
M(i)=fitness_value(i)/fitness_desired_value;
%select the number of decimal_space_old
index_number(i)=floor(M(i));
end
%sum the non-zeros members
if sum(index_number,2)< population
% rest_population=population-sum(index_number,2);
% SORT(X) sorts the elements of X in ascending order.
index_number=sort(index_number,2);
for j=population:-1:(sum(index_number,2)+1)% attention the -1 must be there
index_number(j)=index_number(j)+1 ;
if sum(index_number)==population
break;
end
end
end
%set temperary value
fitness_value_temp=fitness_value;
decimal_space_temp=decimal_space*0;%set 0 to this temp matrix
%set the order of decimal_space by fitness_value order
for i=population:-1:1
[max_fitness_temp(i),max_fitness_temp_position(i)]=max(fitness_value_temp);
fitness_value_temp(max_fitness_temp_position(i))=0;
decimal_space_temp(i,:)=decimal_space(max_fitness_temp_position(i),:);
% decimal_space_temp(i,:)=decimal_space_temp_sort(i,:);
end
%set 0 to record the fitness position
if min(find(index_number))>1
for i=1:1:(min(find(index_number))-1)
max_fitness_temp_position(i)=max_fitness_temp_position(population);
end
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%set 0 in the small probability position for the first time
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for k=1:1:population
decimal_space(k,:)=index_number(k).*decimal_space_temp(k,:);
decimal_space_temp(k,:)=decimal_space(k,:);
%index_number(k).*decimal_space_temp(k,:);
if index_number(k)~=0
decimal_space(k,:)=decimal_space_temp(k,:)/index_number(k);
end
end
% to see if the index of every parameter is the same length
for i=1:1:parameter_numbers
index(i)=min(find(decimal_space_temp(:,i)));
end
index=min(index);
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i=index:1:population
if (decimal_space(i,:))~=(decimal_space_temp(i,:))
%index_temp=min(find(decimal_space));
if index==1
break;
end
for j=(index-1):-1:1
if decimal_space(j,:)~=decimal_space(i,:)
decimal_space(j)=decimal_space_temp(i)-decimal_space(i);
max_fitness_temp_position(j)=max_fitness_temp_position(i);
if decimal_space(j,:)~=decimal_space(i,:)
if j==1
decimal_space(j,:)=decimal_space(population,:);
else
decimal_space((j-1),:)=decimal_space(j,:)-decimal_space(i,:);
decimal_space(j,:)=decimal_space(i,:);
max_fitness_temp_position(j-1)=max_fitness_temp_position(i);
end
else %if decimal_space(j)==decimal_space(i);
break;
end
end
end
end
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
decimal_space_selected=decimal_space;
%initialize binary_space_selected
binary_space_selected=binary_space;
%
%for k=1:1:parameter_numbers
for i=population:-1:1
% for j=(bits_No(k)+1):1:bits_No(k+1)
binary_space_selected(i,:)=binary_space(max_fitness_temp_position(i),:);
% end
end
%end
%max_fitness_temp_position
%binary_space_selected_sparse=sparse(binary_space_selected);
%~~~~~~~~~~~~~~plot~~~~~~~~~~~~~~~~~~
% SPY Visualize sparsity pattern.
% SPY(S) plots the sparsity pattern of the matrix S.
%figure(3)
%subplot(1,2,1)
%spy(binary_space_selected_sparse,'k');
%title('the Selected binary-space in sparsity pattern');
%xlabel('Non-Zero bits');
%ylabel('Population');
%hold on;
%i=0:0.1:population
%for j=1:1:parameter_numbers
% plot(bits_No(j+1),i,'r');
%end
%figure(4)
%subplot(1,2,2)
%i=population:-1:1
%plot(decimal_space_selected(i,:),i,'*');
%xlabel('selected decimal-space');
%ylabel('population');
%title('the crosponding deciaml-space of binary-space');
%grid on;
%
%%% end of selection.m
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -