📄 mutato3.m
字号:
for j=1:HGH2,
if any(fun_name(j)==arg2 | '{'==arg2 | '['==arg2)==0;
arg_type(2)=2;
arg_flag=1;
break
end
end
end
if arg_flag==0,
arg_type(2)=3;
end
end
% Set up argument use vector
arg_use=ones(old_num_fun_arg,1);
% Move through the UDF a socket at a time
sub_tree=temp_func(1:type_ind(1));
for j=1:new_num_fun_arg,
% For given socket type, check to see if an old
% Argument of the same type is available
if soc_type(j)==1,
user_arg='[1]';
else
for k=1:old_num_fun_arg,
if arg_use(k)==1 & arg_type(k)<=soc_type(j),
eval(['user_arg=arg' num2str(k)])
arg_use(k)=0;
break
end
if k==old_num_fun_arg, % If no suitable arguments are left
if soc_type(j)==2,
% Randomly generate argument
inp_choice=ceil(rand(1)*(num_inp)+eps);
eval(['user_arg=''u' num2str(inp_choice) ''';'])
else % Socket type 3 - sub tree
% Randomly generate sub tree
user_arg=equ_gen3(1,num_inp,equ_par,mut_fun_set,...
mut_num_fun_arg_set,user_def_fun);
end
end
end
end
% Have selected an argument for socket "j"
% Construct the sub tree
if j~=new_num_fun_arg,
sub_tree_new=temp_func(csc_ind(j):type_ind(j+1));
sub_tree=[sub_tree '(' user_arg ')' sub_tree_new];
else
sub_tree_end=temp_func(csc_ind(j):length(temp_func));
sub_tree=[sub_tree '(' user_arg ')' sub_tree_end];
end
end % Arguments inserted into all sockets
end % No arguments in UDF
end % New function type (ie base set or UDF)
%
% Reconstruct the tree
%
xmut=[xstart sub_tree xend];
end
elseif x(i)=='{',
%
% Extract sub tree
%
% Search right
bo_count=1;
bc_count=0;
s_point=i-1;
for j=i+1:L,
if x(j) == '}',
bc_count=bc_count+1;
if bc_count == bo_count;
e_point = j+1;
break;
end
elseif x(j) == '{',
bo_count=bo_count+1;
end
end
% take out the relevant string
xstart=x(1:s_point);
sub_tree=x(s_point:e_point);
if e_point == L,
xend=[')'];
else
xend=x(e_point:L);
end
% Mask sockets of any nested UDF's
if bo_count > 1,
onest_ind=findstr(sub_tree,'{');
cnest_ind=findstr(sub_tree,'}');
% Search for nested sockets
nest_sub_tree=sub_tree(onest_ind(2):cnest_ind(length(cnest_ind)-1));
osnest_ind=findstr(nest_sub_tree,'<') + onest_ind(1);
csnest_ind=findstr(nest_sub_tree,'>') + onest_ind(1);
% Mask nested sockets
temp_sub_tree=strrep(sub_tree,'<','$');
temp_sub_tree=strrep(temp_sub_tree,'>','$');
else
temp_sub_tree=sub_tree;
end
% Find all sockets in old UDF
osc_ind=findstr(temp_sub_tree,'<');
csc_ind=findstr(temp_sub_tree,'>');
type_ind=osc_ind+1;
% Count number of arguments in old UDF
old_num_fun_arg=length(osc_ind);
%
% Identify and extract arguments
%
for j=1:old_num_fun_arg,
eval(['arg' num2str(j) '=sub_tree(type_ind(' num2str(j) ')+2:csc_ind(' num2str(j) ')-2);'])
eval(['arg' num2str(j)] )
end
sub_tree
% Create vector of old argument socket types
arg_type=str2num(sub_tree(type_ind)');
% Set up argument use vector
arg_use=ones(old_num_fun_arg,1);
%
% If new funtion is from the base set
%
if any(xnew==fun_name(1:HGH2)),
if new_num_fun_arg > old_num_fun_arg,
% Generate extra arguments
for j=old_num_fun_arg:new_num_fun_arg;
eval(['arg' num2str(j) '=equ_gen3(1,num_inp,equ_par,mut_fun_set,mut_num_fun_arg_set,user_def_fun);'])
end
end
% Construct sub tree
if new_num_fun_arg==1,
sub_tree=[xnew '(' arg1 ')'];
else % num_fun_arg==2
sub_tree=['(' arg1 xnew arg2 ')'];
end
else % If new funtion is a UDF
% Look-up UDF
temp_func=deblank(user_def_fun(str2num(xnew),:));
% Find all sockets
osc_ind=findstr(temp_func,'<');
csc_ind=findstr(temp_func,'>');
type_ind=osc_ind+1;
if new_num_fun_arg==0,
sub_tree=temp_func;
else
% Create vector of socket types
soc_type=str2num(temp_func(type_ind)');
% Move through the UDF a socket at a time
sub_tree=temp_func(1:type_ind(1));
for j=1:new_num_fun_arg,
% For given socket type, check to see if an old
% Argument of the same type is available
if soc_type(j)==1,
user_arg='[1]';
else
for k=1:old_num_fun_arg,
if arg_use(k)==1 & arg_type(k)<=soc_type(j),
eval(['user_arg=arg' num2str(k)])
arg_use(k)=0;
break
end
if k==old_num_fun_arg, % If no suitable arguments are left
if soc_type(j)==2,
% Randomly generate argument
inp_choice=ceil(rand(1)*(num_inp)+eps);
eval(['user_arg=''u' num2str(inp_choice) ''';'])
else % Socket type 3 - sub tree
% Randomly generate sub tree
user_arg=equ_gen3(1,num_inp,equ_par,mut_fun_set,...
mut_num_fun_arg_set,user_def_fun);
end
end
end
end
% Have selected an argument for socket "j"
% Construct the sub tree
if j~=new_num_fun_arg,
sub_tree_new=temp_func(csc_ind(j):type_ind(j+1));
sub_tree=[sub_tree '(' user_arg ')' sub_tree_new];
else
sub_tree_end=temp_func(csc_ind(j):length(temp_func));
sub_tree=[sub_tree '(' user_arg ')' sub_tree_end];
end
end % Arguments inserted into all sockets
end % No arguments in UDF
end % New function type (ie base set or UDF)
%
% Reconstruct the tree
%
xmut=[xstart sub_tree xend];
elseif x(i)=='[',
% Take out the relevant string and mutate constant to input
if rand(1) < p_c2u,
sel_bra=length(obr_ind) - (num_fun - fun_choice);
xstart=x(1:i-1);
xend=x(cbr_ind(sel_bra)+1:L);
eval(['xnew=' '''(u' num2str(ceil(rand(1)*num_inp + eps)) ')''' ';']);
% Reconstruct String
xmut=[xstart xnew xend];
else
xmut=x;
end
else % input
% Take out relevant string, then Select an input or constant and swap
if rand(1) > p_c2u,
xstart=x(1:i); % at a 'u'
end_ind=i+2;
while ~(x(end_ind) == ')'),
end_ind=end_ind+1;
end %while
xend=x(end_ind:L); %at a ')'
xnew=num2str(ceil(rand(1)*num_inp + eps));
% Reconstruct String
xmut=[xstart xnew xend];
else
xstart=x(1:i-1); % at a '('
end_ind=i+2;
while ~(x(end_ind) == ')'),
end_ind=end_ind+1;
end %while
xend=x(end_ind:L); %at a ')'
xnew='[1]';
% Reconstruct String
xmut=[xstart xnew xend];
end
end
%
% Unmask string
%
xmut=strrep(xmut,'[&','[-');
xmut=strrep(xmut,'e@','e+');
xmut=strrep(xmut,'e#','e-');
%
% End
%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -