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

📄 ui_gen.m

📁 一些用matlab编写的经典遗传算法算例 挺有用的
💻 M
字号:
function [g,d] = ui_gen(varargin)
% UI_GEN - User interface
% 
tp=[];
cdvs=cdv;
ddvs=ddv;
c_index=1;lc_index=1;
d_index=1;ld_index=1;
cdv_list=cdv;
ddv_list=ddv;

if ~isempty(varargin);
  cdvs=get(varargin{1}(varargin{2}),'cdvs');
  ddvs=get(varargin{1}(varargin{2}),'ddvs');
  fig_name=sprintf('Design variables in chromosome: %s\n',...
		   get(varargin{1}(varargin{2}),'name'));    
  
  for k=1:length(varargin{1});
    cdvs_tmp=get(varargin{1}(k),'cdvs');
    ddvs_tmp=get(varargin{1}(k),'ddvs');
    
    if ~isempty(cdvs_tmp)
      for l=1:length(cdvs_tmp)
	cdv_list(lc_index)=cdvs_tmp(l);
	lc_index=lc_index+1;
      end
    end
    
    if ~isempty(ddvs_tmp)
      for l=1:length(cdvs_tmp)
	ddv_list(ld_index)=ddvs_tmp(l);
	ld_index=ld_index+1;
      end
    end
  
  end
  
  if ~and(isempty(cdvs),isempty(ddvs))
      figure;
      axis off;
      set(gcf,'numbertitle','off');
      set(gcf,'name',fig_name);
      [c_index d_index tp]=redraw(cdvs,ddvs,tp);
  end
  
end

while 1

  sel=selector;
  
  switch sel
    
   case 'c'
    cdv_tmp=cdv_in(cdvs,ddvs,cdv_list,ddv_list);      
    if ~isempty(cdv_tmp)
      cdvs(c_index)=cdv_tmp(1);
      tp=txt_cdv(cdvs(c_index),tp,1);
      if and(c_index==1,~isempty(varargin))
	set(gcf,'numbertitle','off');
	set(gcf,'name',fig_name);
      end
      c_index=c_index+1;
    end
    
   case 'd'
    ddv_tmp=ddv_in(cdvs,ddvs,cdv_list,ddv_list);      
    if ~isempty(ddv_tmp)
      ddvs(d_index)=ddv_tmp;
      tp=txt_ddv(ddvs(d_index),tp,1);
      if and(d_index==1,~isempty(varargin))
	set(gcf,'numbertitle','off');
	set(gcf,'name',fig_name);
      end
      d_index=d_index+1;
    end
   case 'k'
    
    name_del=input('Input the name of the variable to delete: ','s')
    
    ind_delete_c=find(cdvs,name_del);
    ind_delete_d=find(ddvs,name_del);

    if ind_delete_c
      cdvs=del_gen(cdvs,ind_delete_c);
      [c_index d_index tp]=redraw(cdvs,ddvs,tp);

    elseif ind_delete_d
      
      ddvs=del_gen(ddvs,ind_delete_d);
      [c_index d_index tp]=redraw(cdvs,ddvs,tp);

    else
     
      disp('Name does not exist')
      input('Press return to continue')
      
    end
    
   
   case 're'
    [c_index d_index tp]=redraw(cdvs,ddvs,tp);
   case 'ke'
    keyboard
    
   case 'e'
    
    name_mod=input('Name of variable to modify: ','s');
    cdv_edit=find(cdvs,name_mod);
    tp_ind=find_tp(tp,name_mod);
    ddv_edit=find(ddvs,name_mod);
    if cdv_edit
      fig=get(get(tp(tp_ind),'Parent'),'Parent');
      delete(tp(tp_ind));
      clc;
      cdvs(cdv_edit)=set(cdvs(cdv_edit),'name','');
      cdvs(cdv_edit)=cdv_in(cdvs,ddvs,cdv_list,ddv_list);
      
      txt=sprintf('name: %s\nlimits:[%d %d]\nBits: %d', ...
		  get(cdvs(cdv_edit),'name'), get(cdvs(cdv_edit), ...
					       'l_limit'), ...
		  get(cdvs(cdv_edit),'u_limit'), get(cdvs(cdv_edit),'bits'));
      figure(fig);
      tp(tp_ind)=place_txt(tp_ind,txt);
      set(tp(tp_ind),'Tag',get(cdvs(cdv_edit),'name'));
      
    elseif ddv_edit 
      fig=get(get(tp(tp_ind),'Parent'),'Parent');
      delete(tp(tp_ind));
      clc;
      ddvs(ddv_edit)=set(ddvs(ddv_edit),'name','');
      ddvs(ddv_edit)=ddv_in(cdvs,ddvs);
      txt=sprintf('name: %s\nPossible values:\n%s', ...
		  get(ddvs(ddv_edit),'name'), ...
		  num2str(get(ddvs(ddv_edit),'p_val')));
      figure(fig);
      tp(tp_ind)=place_txt(tp_ind,txt);
      set(tp(tp_ind),'Tag',get(ddvs(ddv_edit),'name'));
    else
      disp('Name does not exist')
      input('Press return to continue')
    end
    
   case 'q'
    fp=0;
    if ~isempty(tp)
      for k=1:length(tp)
	fp(k)=get(get(tp(k),'parent'),'parent');
      end
      close(fp)
    end
    break
  end
end
g=cdvs;
d=ddvs;
%==============END MAIN LOOP=================================


function tp = txt_cdv(cdv,tp,close)
if close 

  if ~mod(length(tp),30)
    fp=figure;
    set(fp,'Menubar','none')
    set(fp,'numbertitle','off')
    set(fp,'Name','Design variables')
    axis off;
  end
else 
  figure(gcf)
end
figure(gcf);

for k=1:length(cdv)
  index=mod(length(tp),30)+1;
  txt=sprintf('name: %s\nlimits:[%d %d]\nBits: %d',get(cdv(k),'name'), ...
	      get(cdv(k),'l_limit'),get(cdv(k),'u_limit'), ...
	      get(cdv(k),'bits'));
  tp=[tp place_txt(index,txt)];
  set(tp(end),'Tag',get(cdv(k),'name'));
end

%=============END FUNCTION===================================  
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

%=============END FUNCTION===================================    

function c = cdv_in(g,d,cdv_list,ddv_list)
% CDV_IN - 
%   

name=namecheck(g,d);
ind_c=find(cdv_list,name);
ind_d=find(ddv_list,name);
if ind_d 
  disp('name exist as discrete design');
  input('press return to continue')
  c=cdv;
  return;
end

if ind_c
  reuse=input('variable exist reuse: ? (y/n)','s');
  if strcmp(reuse,'y')
    c=cdv_list(ind_c);
    return;
  else
    c=cdv;
    return;
  end
end
    
h_lim=0; l_lim=0; tst=0; bit=0;
while h_lim<=l_lim
  if tst
    disp('Limits are not correct')
  end
  l_lim=input('Give low limit:');
  h_lim=input('Give high limit:');
  tst=1;
end
tst=0;
while bit<=0
  if tst
    disp('Wrong number of bits')
  end
  bit=input('give number of bits:');
  tst=1;
end

c=cdv(name,l_lim,h_lim,bit);
%=============END FUNCTION===================================    


function name = namecheck(g,d)
% NAMECHECK - 
% 
clc;
name=input('Give Unique name: ','s');
if or(find(g,name),find(d,name))
  disp('name already exist in current design');
  input('press return to continue');
  name=namecheck(g,d);
end
%=============END FUNCTION===================================    


function g_out = del_gen(g,ind)
% DEL_GEN - 
%   
l_gen=length(g);
if and(l_gen==1,ind==1)
  if isa(g,'cdv')
    g_out=cdv;
  else
    g_out=ddv;
  end
else
  if ind==1
    g_out=g(2:end);
  elseif ind==l_gen
    g_out=g(1:end-1);
  else
    g_out=g(1:ind-1);
    g_out=[g_out g(ind+1:end)];
  end
end
%=============END FUNCTION===================================    

function d = ddv_in(g,d,cdv_list,ddv_list)
% DDV_IN - 
%   
name=namecheck(g,d);

ind_c=find(cdv_list,name);
ind_d=find(ddv_list,name);
if ind_c 
  disp('name exist as a cdv');
  input('press return to continue')
  d=ddv;
  return;
end

if ind_d
  reuse=input('variable exist reuse y/n: ','s');
  if strcmp(reuse,'y')
    d=ddv_list(ind_d);
    return;
  else
    d=ddv;
    return;
  end
end


values=input('Input the possible values:');
if isempty(values)
  disp('variable must contain data');
  input('press return to continue');
  d=ddv_in(g,d);
else
  d=ddv(name,values);
end
%=============END FUNCTION===================================    

function tp = txt_ddv(ddv,tp,close)
% TXT_CDV - 
if close
  if isempty(tp)
    fp=figure;
    set(fp,'Menubar','none')
    set(fp,'numbertitle','off')
    set(fp,'Name','Design variables')
    axis off;
  end

  if ~mod(length(tp),30)
    fp=figure;
    set(fp,'Menubar','none')
    set(fp,'numbertitle','off')
    set(fp,'Name','Design variables')
    axis off;
  end
else 
  figure(gcf)
end

for k=1:length(ddv)
  index=mod(length(tp),30)+1;
  txt=sprintf('name: %s\nPossible values:\n%s',get(ddv(k),'name'), ...
		num2str(get(ddv(k),'p_val')));
  
  tp=[tp place_txt(index,txt)];
   set(tp(end),'Tag',get(ddv(k),'name'));
end
%=============END FUNCTION===================================    


function [c_ind,d_ind,tp] = redraw(g,d,tp)
% REDRAW - 
%   

delete(tp);
tp=[];
if ~find(g,'')
  tp=txt_cdv(g,tp,0);
  c_ind=length(g)+1;
else
  c_ind=1;
end
if ~find(d,'')
  tp=txt_ddv(d,tp,0);
  d_ind=length(d)+1;
else 
  d_ind=1;
end
%=============END FUNCTION===================================    

function sel = selector
% SELECTOR - 

clc;
disp('Add continuous variable press: c')
disp('Add discrete variable press: d')
disp('Kill a variable press: k')
disp('Edit a variable press: e')
disp('Quit: q')
sel=input('Select action by pressing corresponding key:','s');
%=============END FUNCTION===================================    

function ind = find_tp(tp,name)
% FIND_TP - 
%   

for k=1:length(tp)
  if strcmp(get(tp(k),'Tag'),name);
    ind=k;
    break
  else
    ind=0;
  end
end








⌨️ 快捷键说明

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