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

📄 ui_chrom.m

📁 一些用matlab编写的经典遗传算法算例 挺有用的
💻 M
字号:
function chr = ui_chrom()
% UI_CHROM - User interface for constructing chromosomes
%   
tp=[];
ind=0;
ind_edit=0
while 1
  sel=selector;
  switch sel
    
   case 'n'
    if ind_edit
      set(tp(ind),'fontweight','normal');
      ind=ind_edit;
      ind_edit=0;
    end
    
    ind=ind+1;
    chr(ind)=chrom;
    c_name=namecheck(chr);
    chr(ind)=set(chr(ind),'name',c_name);
    txt=sprintf('Name: %s',c_name);
    tp=[tp place_txt(ind,txt)];
    
    if ind==1
      set(tp(ind),'fontweight','bold');
    else 
      set(tp(ind),'fontweight','bold');
      set(tp(ind-1),'fontweight','normal');
    end
    
    
    axis off
    
    set(gcf,'menubar','none')
   
   case 'r'
    chr(ind)=set(chr(ind),'name','');    
    c_name=namecheck(chr);
    chr(ind)=set(chr(ind),'name',c_name);    
    
    tmp_string=get(tp(ind),'string');
    tmp=size(tmp_string);
   
    if tmp(1)==1 
      txt=sprintf('Name: %s',c_name);
      set(tp(ind),'string',txt);
    else
      txt=sprintf('Name: %s',c_name);
      t=get(tp(ind),'string');
      for k=2:tmp(1)
	txt=strvcat(txt,t(k,:));
      end
      set(tp(ind),'string',txt);
    end
    
            
   case 'e'
    
      name_e=input('input the name of the chromosome to edit: ','s');
      ind_e=find(chr,name_e);
      if ind_e
	
	ind_edit=ind;
	ind=ind_e;
	set(tp(ind_edit),'fontweight','normal');
	set(tp(ind),'fontweight','bold');
      else 
	disp('chromosome does not exist');
      end
    
   case 'a'
    if ind
      [cdvs ddvs]=ui_gen(chr,ind);
      chr(ind)=set(chr(ind),'cdvs',cdvs,'ddvs',ddvs);
      
      if isempty(ddvs)
	l_ddv=0;
      else
	l_ddv=length(ddvs);
      end
      
      if isempty(cdvs)
	l_cdv=0;
      else
      l_cdv=length(cdvs);
      end
      tmp_string=get(tp(ind),'string');
      txt=strvcat(tmp_string(1,:),...
		  sprintf('# cdvs %g\n# ddvs %g\n',l_cdv,l_ddv))
      
      set(tp(ind),'string',txt);
    else
      disp('You can not add anything to non-existent chromosomes')
    end
    
    
   case 'k'
    
    name_del=input('Input the name of the chromosome to delete: ','s')
    ind_delete=find(chr,name_del);
    
    if ind_delete
      
      chr=del_chrom(chr,ind_delete);
      ind=ind-1;
      
    else
      
      disp('Name does not exist')
      input('Press return to continue')
      
    end
    
   case 'ke'
   
    keyboard
    
   case 'u'
    
    set(tp(ind),'fontweight','normal');
    
    name_u=input('Input the name of the chromosome: ','s');
    t=find(chr,name_u);
    if t
      tmp_string=get(tp(t),'string');
      ind=ind+1;
      chr(ind)=chr(t);
      chr(ind)=set(chr(ind),'name','');
      u_name=namecheck(chr);
      chr(ind)=set(chr(ind),'name',u_name);
      
      txt=sprintf('Name: %s',u_name);
         
     
      tmp=size(tmp_string);
   
      if tmp(1)==1 
	txt=sprintf('Name: %s',u_name);
	tp=[tp place_txt(ind,txt)];
      else
	txt=sprintf('Name: %s',u_name);
	
	for k=2:tmp(1)
	  txt=strvcat(txt,tmp_string(k,:));
	end
	tp=[tp place_txt(ind,txt)];
      end
      set(tp(end),'fontweight','bold');
    
    
    else
      disp('name does not exist')
      input('press return to continue')
    end
   
        
   case 'q'
    break
   otherwise
    disp('Not a valid choice press return to continue');
  end

end
if ~isempty(tp)
  close(get(get(tp(1),'parent'),'parent'));
end

%=============END MAIN===============================================

function sel = selector
% SELECTOR - 
clc;
disp('New chromosome: n')
disp('Rename: r')
disp('Edit chromosome: e')
disp('Add/edit design variables press: a')
disp('Use existing design: u')
disp('Kill chromosome: k')
disp('Quit: q')
sel=input('Select action by pressing corresponding key:','s');
%----------END SELECTOR

function name = namecheck(chr)
% NAMECHECK - 
% 
clc;
name=input('Give Unique name: ','s');
if find(chr,name)
  disp('Name already exist.')
  input('');
  name=namecheck(chr);
end
%----------END NAMECHECK


function g_out = del_chrom(g,ind)
% 
l_chrom=length(g);
if and(l_chrom==1,ind==1)
  g_out=chrom;
else
  if ind==1
    g_out=g(2:end);
  elseif ind==l_chrom
    g_out=g(1:end-1);
  else
    g_out=g(1:ind-1);
    g_out=[g_out g(ind+1:end)];
  end
end
%----------END DEL_CHROM


function tp = place_txt(index,txt)
% PLACE_TXT - 
%   
figure(gcf);
pos_x=[-0.1 0.15 0.4 0.65 0.90 1.15];
pos_y=[0.9 0.75 0.6 0.45 0.3 0.15];


if index<=5
  tp=text(pos_x(index),pos_y(1),txt);
elseif and(index>5,index<=10)
  tp=text(pos_x(index-5),pos_y(2),txt);
elseif and(index>10,index<=15)
  tp=text(pos_x(index-10),pos_y(3),txt);
elseif and(index>15,index<=20)
  tp=text(pos_x(index-15),pos_y(4),txt);
elseif and(index>20,index<=25)
  tp=text(pos_x(index-20),pos_y(5),txt);
else and(index>25,index<=30)
  tp=text(pos_x(index-25),pos_y(6),txt);
end







⌨️ 快捷键说明

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