⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coding.m

📁 一个非常好的遗传算法参考程序,可以优化带约束条件的函数.
💻 M
字号:
function [binary_space,bits_sum,bits]=coding(min_confines,max_confines,population,decimal_step)

%Coding 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)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Coding is the first step in SGA(Simple Genetic Algorithm )
%this coding function is to code variable by binary coding method  
%~
%Usage:
%[binary_space,bits_sum,bits_No,bits]=coding(min_confines,max_confines,population,decimal_step)
%
%in this:
%
%min_confines is the minimum of input value in decimal-space
%max_confines is the maximum of input value in decimal-space
%i.e. X belong to [min_confines,max_confines]
% min_confines < max_confines
%~~
%decimal_step is the search step in decimal-space
%it must be smaller than (max_confines-min_confines)/2
%~~~
%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_sum,bits_No,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])
%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%  See Also DECODING ,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;
elseif size(decimal_step)~=size((max_confines-min_confines)) %check the size of decimal_step 
       disp('the decimal_step and the confines_deta must be in same size!!')
       break;
elseif (decimal_step<(max_confines-min_confines)/2)~=1 %check the size of decimal_step 
       disp('Warning !! one of elements in the decimal_step matrix is too large')
       disp('please input decimal_step again ,it must be smaller than (max_confines-min_confines)/2 ')
       break;
elseif (population>0)~=1 % population check
       disp('Warning!! every population members must be even Integer and larger than 0')
       break;
elseif mod(population,2)~=0 % population check
       disp('Warning!! the population must be even integer and larger than 0')
       break;                     
elseif (decimal_step>=0)~=1
       disp('Warning!! the decimal_step must be >= 0 ')
       break;
else
   %add all the binary parameters into a long-chromosome  
   % count the sum bits
confines_deta=(max_confines-min_confines);
bits=ceil(log2(confines_deta./decimal_step));
   if min(bits>=1)~=1
      disp('the bits must be > 0 ,please chose the min_confines and max_confines again.')
   break;
   end
bits_sum=sum(bits,2);
[line,parameter_numbers]=size(bits);
%next->Use sparse matrix to store binary_space
binary_space=(randint(population,bits_sum)); 

%~~~~~~~~~~~~~plot~~~~~~~~~~~~~~~
%figure(1)
%plot binary_space in sparse model
%binary_space_sparse=sparse(binary_space); 
%spy(binary_space_sparse);
%hold on;
%ini bits_No

%bits_No is important
bits_No=[0,bits];
% to find out which is the binary parameter
%i=0:0.1:population;
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

%title('the Initialization of the coded binary-space in sparsity pattern');
%xlabel('Non-Zero bits');
%ylabel('Population');
  
end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%  end of coding 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -