code.m

来自「遗传算法源码,在matlab环境下做的,可以仿真 简单的遗传运算」· M 代码 · 共 25 行

M
25
字号
function Ret=Code(LenChrom,Opts,Bound)
% In this function ,it converts a set varibles into a chromosome
% LenChrom   input : length of chromosome
% Opts       input : tag of coding method
% Bound      input : boundary of varibles
% Ret        output: chromosome
switch Opts
    case 'binary' % binary coding
        Pick=rand(1,sum(LenChrom));
        Bits=ceil(Pick-0.5);
        Temp=sum(LenChrom)-1:-1:0;
        Ret=sum(Bits.*(2.^Temp));
    case 'grey'   % grey coding
        Pick=rand(1,sum(LenChrom));
        Bits=ceil(Pick-0.5);
        GreyBits=Bits;
        for i=2:length(GreyBits)
            GreyBits(i)=bitxor(Bits(i-1),Bits(i));
        end
        Temp=sum(LenChrom)-1:-1:0;
        Ret=sum(GreyBits.*(2.^Temp));
    case 'float'   % float coding
        Pick=rand(1,length(LenChrom));
        Ret=Bound(:,1)'+(Bound(:,2)-Bound(:,1))'.*Pick;
end

⌨️ 快捷键说明

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