createpop.m

来自「基本遗传算法的matlab源程序」· M 代码 · 共 37 行

M
37
字号
function [gene,genek_bitnum]=createPop(bounds,sizePop,accuracy)
%  to create a population at the starting point
%  syntax:[gene,genek_bitnum]=createPop(bounds,sizePop,accuracy)
%
%  Output Arguments:
%      gene         ---- the gene string of all individual
%      genek_bitnum ---- length of gene string
%  Input Arguments:
%      bounds       ---- a matrix of upper and lower bounds on the variables
%      sizePop      ---- the population size
%      accuracy     ---- the calculation accuracy
%
%  Author:Yan Anxin
%  ID number:081810
%  Date:finished on 2008.11.29
%  Yax235 DreamWorks, SEE, SEU, 2# Sipailou Nanjing, 210096, P.R.China 

[n,]=size(bounds);gene='';
genek_bitnum=ones();
for k=1:n              %to get the length of every gene string 
    a=bounds(k,1);b=bounds(k,2);
    genek_bitnum(k)=1;
    while  (2^genek_bitnum(k)-1)<((b-a)/accuracy)
        genek_bitnum(k)=genek_bitnum(k)+1;
    end
end

for ki=1:sizePop
    gene_str='';
    for kj=1:n
        clear str;      %to randomly generat initial population
        str=num2str(dec2bin((unidrnd(2^genek_bitnum(kj))-1),genek_bitnum(kj)));
        gene_str=[gene_str str];
    end
    gene=[gene;gene_str];  
end

⌨️ 快捷键说明

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