load_chrom.m

来自「一些用matlab编写的经典遗传算法算例。可以用于解决许多优化问题」· M 代码 · 共 63 行

M
63
字号
function chr = load_chrom(fname,varargin)
% LOAD_CHROM - Loads a chromosome vector.
%
%   c = load_chrom('fname',opt), loads a chromosome vector (generation)
%   from a file. The opt is a options is the generation to beloaded
%   or 'all', all the generations.  In the last case chr is a nxm
%   matrix with the n generations and m chromosomes.


fid=fopen(fname,'r');
gen=fgetl(fid);
gen=str2num(gen(20:end));

if ~isempty(varargin)
  if isa(varargin{1},'double')
    if varargin{1}<=gen
      gen=varargin{1};
    end
  elseif isa(varargin{1},'char')
    if strcmp(varargin{1},'all')
      ind=1;
      while ~feof(fid)
	chr(ind,:)=cload(fid);
	ind=ind+1;
      end
      fclose(fid);
    end
    return
  else 
    error('wrong argument')
  end
  
end


gen_load=sprintf('Generation %g',gen);
tst='';
while ~strcmp(tst,gen_load )
  b=ftell(fid);
  tst=fgetl(fid);
  ind=findstr(tst,'saved');
  if ind
    tst=tst(1:ind-2);
    if strcmp(tst,gen_load)
      fseek(fid,b,-1);
      chr=cload(fid);
      fclose(fid);
      break
    end
  end
  
  if strcmp(tst,'-1');
    fclose
    error('generation not found')
    break
  end
  
end
  



⌨️ 快捷键说明

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