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

📄 report.m

📁 一些用matlab编写的经典遗传算法算例。可以用于解决许多优化问题
💻 M
字号:
function report(fname,chr,a,time)
aprint(a);
[n m]=size(chr);
if (n==1)
  print('Generation 1',fname,chr,a);
else	
  fid=fopen(strcat(fname,'.html'),'w');
  fprintf(fid,'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
  fprintf(fid,'<html><head>');
  fprintf(fid,'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');
  fprintf(fid,'<title>');
  fprintf(fid,'MGA RESULT');
  fprintf(fid,'</title></head>');
  fprintf(fid,'<body>');
  fprintf(fid,'Test started: ');
  fprintf(fid,time);
  fprintf(fid,strcat('&nbsp;ended&nbsp;', datestr(now)));
  fprintf(fid,'<hr>');
  fprintf(fid,'<b>Figures:</b>&nbsp;');
  fprintf(fid,'<a href="fig1.eps.gz">Max and average fitness</a>&nbsp;&nbsp');
  fprintf(fid,'<a href="fig2.eps.gz">Maximum of the Species</a>&nbsp;&nbsp');
  fprintf(fid,'<a href="fig3.eps.gz">Distribution of members</a>');
  fprintf(fid,'<hr>');
  fprintf(fid,'<b>Species:</b>&nbsp;&nbsp;');
  
  for k=1:length(a)
    dname=get(a(k),'name');
    fprintf(fid,'<a href="');
    fprintf(fid,strcat(dname,'.html','">',dname,'</a>','&nbsp;&nbsp;&nbsp;'));
  end
  fprintf(fid,'<hr>');
  

  fprintf(fid,'<b>Optimized  species:</b>&nbsp;&nbsp;&nbsp;');
  for k=1:length(a)
    dname=get(a(k),'name');
    for l=n:-1:1
      if find(chr(l,:),dname)
	fprintf(fid,'<a href="');
	fprintf(fid,strcat('mga_node',num2str(l),'.html#',dname,'">',dname,'</a>','&nbsp;&nbsp;&nbsp;'));
	break;
      end
    end
  end
  fprintf(fid,'<hr>');


  [mx g_max]=max(chr(n,:));
  fprintf(fid,'<h4>Maximum fitness: %g ',mx);fprintf(fid,'<br>');
  fprintf(fid,'Average fitness: %g ',mean(chr(n,:)));fprintf(fid,'<br>');
  fprintf(fid,'Minimum fitness: %g ',min(chr(n,:)));fprintf(fid,'</h4>'); 
  fprintf(fid,'<hr>');
  fprintf(fid,'<b>Best Design</b><br>');
  write(fid,g_max);
  fprintf(fid,'<hr>');
    
  fprintf(fid,'<b>Generations</b><br>');		
  for k=n:-1:1
    fprintf(fid,'<a href="./');
    fprintf(fid,strcat('mga_node',num2str(k),'">'));
    fprintf(fid,strcat('Generation&nbsp; ',num2str(k)));
    fprintf(fid,'</a><br>');		
    if(k==n)
      tprint(strcat('Generation&nbsp;',num2str(n)),strcat('mga_node',num2str(n),'.html'),chr(n,:),a,1,0);    
    elseif(k==1)
      tprint(strcat('Generation&nbsp;',num2str(k)),strcat('mga_node',num2str(k),'.html'),chr(k,:),a,0,1);
    else
      tprint(strcat('Generation&nbsp;',num2str(k)),strcat('mga_node',num2str(k),'.html'),chr(k,:),a,1,1);
    end
    
  end	
  fclose(fid);		

end


function write(fid,c)
% SAVE_CHROM - saves a chromosome to file
%   

for j=1:length(c)
 fprintf(fid,'Name: %s',get(c(j),'name'));fprintf(fid,'<br>'); 
  txt=sprintf('Fitness: %g',get(c(j),'fitness'));
  fprintf(fid,'%s',txt); fprintf(fid,'<br>');		


  fprintf(fid,'Design variables:<br>');

  cdvs=get(c(j),'cdvs');
  if (~isempty(cdvs))
   for k=1:length(cdvs)
     fprintf(fid,'%s = %g',get(cdvs(k),'name'),get(cdvs(k),'value'));
     fprintf(fid,'<br>');	
   end
  end
	
  ddvs=get(c(j),'ddvs');
  if (~isempty(ddvs))
   for k=1:length(ddvs)
     fprintf(fid,'%s = %g',get(ddvs(k),'name'),get(ddvs(k),'value'));	
     fprintf(fid,'<br>');	
   end
  end
  fprintf(fid,'----------------------<br>');	
end


function tprint(tit,fname,chr,arch,prev,next)
% PRINT - Prints a summary of a generation  to a file specified by 'fname'
%   print('fname',chr,arch) saves the generation chr to file 'fname'
%   arch is the prototypes for the species.
% See also LOGBOOK
chr=sort(chr);
chr=fliplr(chr);
fid=fopen(fname,'w');
fprintf(fid,'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
fprintf(fid,'<html>');
fprintf(fid,'<head>');
fprintf(fid,'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');
fprintf(fid,'<title>');
fprintf(fid,tit);
fprintf(fid,'</title></head>');
fprintf(fid,'<body>');

node=sscanf(tit(17:end),'%i');
if(prev==1)
  tmp='<a href="';
  tmp=strcat(tmp,strcat('mga_node',num2str(node-1),'.html">'),'prev</a>&nbsp;&nbsp;');	
  fprintf(fid,tmp);
end

if(next==1)
  tmp='<a href="';
  tmp=strcat(tmp,strcat('mga_node',num2str(node+1),'.html">'),'next </a>');	
  fprintf(fid,tmp);
end



fprintf(fid,'<br>');
[mx g_max]=max(chr);
[mn g_min]=min(chr);

fprintf(fid,'<h4>Maximum fitness: %g ',mx);fprintf(fid,'<br>');
fprintf(fid,'Average fitness: %g ',mean(chr));fprintf(fid,'<br>');
fprintf(fid,'Minimum fitness: %g ',mn);fprintf(fid,'</h4>'); 
fprintf(fid,'<hr>');

fprintf(fid,'<b>Best Design</b><br>');
write(fid,g_max);
fprintf(fid,'<hr>');

fprintf(fid,'<b>Worst Design</b><br>');
write(fid,g_min);
fprintf(fid,'<hr>');

for k=1:length(arch)
  ind=find(chr,get(arch(k),'name'));
  if ind
    fprintf(fid,'<h4><a name="');
    fprintf(fid,get(arch(k),'name'));
    fprintf(fid,'">Members in design: %s</a></h4>',get(arch(k),'name'));
    fprintf(fid,'<hr>');
    write(fid,chr(ind));
  end
end
fprintf(fid,'<hr>');
fprintf(fid,'File created: %s',datestr(now));
fprintf(fid,'</body>');
fprintf(fid,'</html>');
fclose(fid);

function num = todec(dna)
% TODEC -
t=num2str(dna);
tmp='';
for k=1:length(t)      
if ~isspace(t(k))      
tmp=strcat(tmp,t(k));  
end                  
end
num=bin2dec(tmp);


function aprint(a)

for k=1:length(a)
  fid=fopen(strcat(get(a(k),'name'),'.html'),'w');
  fprintf(fid,'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
  fprintf(fid,'<html><head>');
  fprintf(fid,'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');  
  fprintf(fid,'<title></title></head><body>');
  fprintf(fid,strcat('<b>Chromosome name:</b>&nbsp;',get(a(k),'name'),'<br>'));
  cdvs=get(a(k),'cdvs');
  if ~isempty(cdvs)
    fprintf(fid,'<b>Continuous variables:</b><hr>');
    for l=1:length(cdvs)
      fprintf(fid,strcat('<b>Gene name:</b>&nbsp;',get(cdvs(l), 'name'),'&nbsp;'));
      l_lim=num2str(get(cdvs(l),'l_limit'));
      u_lim=num2str(get(cdvs(l),'u_limit'));
      fprintf(fid,strcat('<b>Limits</b>:&nbsp;','[&nbsp;',l_lim,'&nbsp;&nbsp;&nbsp;',u_lim,'&nbsp;]<br>'));
    end
  end
  fclose(fid);
end










⌨️ 快捷键说明

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