📄 decoding.m
字号:
function [decimal_space]=decoding(min_confines,max_confines,binary_space,bits)
%Decoding Function Of Simple Genetic Algorithm Program (Version 1.0.0.1 )
%Support multi-dimesion parameters
%By chen yi ,CQU .QQ:2376635 Email:cdey@10mail.net (April,17th,2002 AAPP dat)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Decoding is the second step in SGA(Simple Genetic Algorithm )
%this decoding function is deconding the binary value which coded by coding
%function ,that is to say you should use coding function first
%it can transfer binary value to decimal value
%~
%Usage:
%decimal_space=decoding(min_confines,max_confines,binary_space,bits,bits_No)
% in the method of 'X=Decimal*decimal_step+min_confines'
%~~
%decimal_space is the decimal number which from binary_space
%min_confines is the minimum of input value in decimal-space
%max_confines is the maximum of input value in decimal-space
%~~~
%population is the random decimal value in [min_confines,max_confines]
% it is given by yourself and it must be integer and larger than 0
%~~~~~~
%binary_space is the coded space in 0 or 1
%
%bits_No is the genetic position pointer of binary_space
%
%bits_sum is the total length of the binary_space of all the parameters
%
%bits is the length of every parameter
%e.g.
% f(x)=2x x belong to [1,7]
%[binary_space,bits]=coding(1,7,10,0.01)
%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)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% See Also CODING ,SELECTION ,CROSSOVER,MUTATION,FITNESS,
% FITNESS_FUNCTION, SGA
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if size(min_confines)~=size(max_confines) % min_confines & max_confines check
disp(':( Warning !! The size of min_confines&max_confines do not match !')
break;%Reinforce the robust of this function
elseif (min_confines<max_confines)~=1 % min_confines & max_confines check
disp(':( Warning!! One of The Min=Min! Check your Data!')
break;
else
[population,bits_sum]=size(binary_space);
confines_deta=(max_confines-min_confines);
coded_step=confines_deta./(2.^bits-1);
[line,parameter_numbers]=size(min_confines);
%~~~~~~~~bits_No is important to all the sga
%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
% 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(j,k);
end
twos_sum(j,i)=sum(twos(j,:),2);
decimal_space(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(2)
% i=population:-1:1;
% plot(decimal_space(i,:),i,'*');
% xlabel('initialized decimal-space');
% ylabel('population');
% title('the corresponding deciaml-space of binary-space');
% grid on;
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% end of decoding
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -