📄 crossover.m
字号:
function [decimal_space_crossover,binary_space_crossover]=crossover(min_confines,max_confines,decimal_space_selected,binary_space_selected,bits,probability_crossover)
%Crossover Function Of Simple Genetic Algorithm Program (Version 1.0.0.1 )
%support multi-parameters
%By chen yi ,CQU .QQ:2376635 Email:cdey@10mail.net (April,19th,2002)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Crossover is the step after select in SGA(Simple Genetic Algorithm)
%by single point cross method
%
%min_confines is the minimum of input value in decimal-space
%max_confines is the maximum of input value in decimal-space
%~
%Usage:
%new_generation=crossover(decimal_space_rest,probability_crossover)
%~~
%decimal_space_rest is the rest decimal_space from select function
%~~~
%probability_crossover is the crossover probability in crossover step,
%your can give it's value to reach your need (About:0~1)
%~~~~
%e.g.
%[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(decimal_space,binary_space,fitness_value,bits)
%[decimal_space_crossover,binary_space_crossover]=crossover([1,2,3,4],[7,8,9,10],decimal_space_selected,binary_space_selected,bits,0.6)
%
%
% See Also DECODING,CODING ,SELECTION,MUTATION,FITNESS,
% FITNESS_FUNCTION, SGA
%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[population,parameter_numbers]=size(decimal_space_selected);
% [range_front,range_back]
[population,bits_sum]=size(binary_space_selected);
probability_pairs_numbers=ceil(probability_crossover*population/2);
%initialize binary_space_crossover
binary_space_crossover=binary_space_selected;
%
confines_deta=(max_confines-min_confines);
coded_step=confines_deta./(2.^bits-1);
%~~~~~~~~~~~~~~~~~~~~~
%bits_No is important
bits_No=[0,bits];
% to find out which is the binary parameter
for i=1:1:parameter_numbers
%to avoid that Index exceeds matrix dimensions
bits_No(i+1)=bits_No(i+1)+bits_No(i);
end
%~~~~~~~~~~~~~~~~~~~~~~
%
if probability_pairs_numbers<1
disp('the prabability of crossover is too small, please give another one!!')
break;
else
% set crossover points of every parameter
%to avoid the same sites in colume 1 be used in colume 2
%to avoid near relation's mating
%to initialize pairs
pairs=randint(population/2,2,[1,population]);
%test_nonzeros_pairs=nnz(pairs)
for i=1:1:population/2
while pairs(i,1)==pairs(i,2)|pairs(i,1)==0|pairs(i,2)==0
pairs(i,2)=randint(1,1,population);
pairs(i,1)=randint(1,1,population);
end
end
% avoid the pairs has 0 element
%while nnz(pairs)~=((population/2).*2)
% pairs=randint(population/2,2,[1,population]);
%end
% to choose crossover position from the pairs matrix
%crossover_position is the pointer of pairs matrix
%initialize crossover_position
crossover_position=randint(probability_pairs_numbers,1,[1,population/2]);
% to have different position in the next position
for i=1:1:probability_pairs_numbers
for j=1:1:probability_pairs_numbers
while ((i~=j)&(crossover_position(i,1)==crossover_position(j,1)))|crossover_position(j,1)==0
crossover_position(j,1)=randint(1,1,[1,population/2]);
end
end
end
% to avoid the crossover_position has 0 element
% while nnz(crossover_position)~=((probability_pairs_numbers).*1)
% crossover_position=randint(probability_pairs_numbers,1,[1,population/2]);
% end
% crossover begin
for i=1:1:probability_pairs_numbers
for k=1:1:parameter_numbers
mating_point(k)=randint(1,1,[bits_No(k)+1,bits_No(k+1)]);
for j=mating_point(k):1:bits_No(k+1)
crossover_parts=binary_space_crossover(pairs(crossover_position(i,1),1),j);
binary_space_crossover(pairs(crossover_position(i,1),1),j)=binary_space_crossover(pairs(crossover_position(i,1),2),j);
binary_space_crossover(pairs(crossover_position(i,1),2),j)=crossover_parts;
end
end
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`~~~~~~~~~~~~~~~~~
end
%binary_space_crossover_sparse=sparse(binary_space_crossover);
%
%~~~~~~~~~~~~~~~~~~~~~~plot~~~~~~~~~~~~~~~~~~~~~`
% SPY Visualize sparsity pattern.
% SPY(S) plots the sparsity pattern of the matrix S.
%figure(5)
%subplot(1,2,1)
%spy(binary_space_crossover_sparse,'b');
%title('the crossover 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
%decimal_space_crossover=decimal_space;
% important ,matrix of matlab begins from 1 ,not 0
for i=parameter_numbers:-1:1
for j=population:-1:1
for k=bits_No(i+1):-1:(bits_No(i)+1)
twos(j,k)=pow2(bits_No(i+1)-k)*binary_space_crossover(j,k);
end
twos_sum(j,i)=sum(twos(j,:),2);
decimal_space_crossover(j,i)=twos_sum(j,i)*coded_step(i)+min_confines(i);
end
twos=zeros(population,bits_sum);%clear twos at present i
% X=Decimal*coded_step+min_confines
end
%~~~~~~~~~~~~plot~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%figure(6)
%i=population:-1:1;
%plot(decimal_space_crossover(i,:),i,'*');
%xlabel('crossover decimal-space');
%ylabel('population');
%title('the crosponding deciaml-space of binary-space');
%grid on;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -