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

📄 equ_gen2.m

📁 王小平《遗传算法——理论、应用与软件实现》随书光盘
💻 M
字号:
function x2=equ_gen2(s_length,num_inp,equ_par,const_par,A,B,Awgt,Bwgt,ind_par)
% function x2=equ_gen2(s_length,num_inp,equ_par,const_par,A,B,Awgt,Bwgt,ind_par)
%
% Automatically generates a string
%
% input variables assigned {u1, u2, ......., uN}
%
% functions:
%               r = sqrt
%               s = square
%               e = exp
%               l = log
%
%

%
% step 1: Always start with an open bracket
%
x2='(';
o_bracket=1;
c_bracket=0;
%
% The following operators are used in the GA:
%


% p_node: vary amount of constants/inputs : operators
% P-const: vary number of constants c.f no of datafile inputs
% s_length: used to give some measure of string length

p_node=equ_par(1);  %[0.1];
p_const=equ_par(2); %[0.3];

% const_min: minimum value of randomly generated constant
% const_max: maximum value of randomly generated constant

const_min=const_par(1); %[0.01];
const_max=const_par(2); %[10];

Awgt5=ind_par(1); Awgt6=ind_par(2);
Bwgt6=ind_par(3); Bwgt7=ind_par(4); 


%
% Generating the expression from the list above
%
%       u1...uN = input data columns
%
%

%
% Initialise Inputs 
%
for i=1:num_inp,
     
     eval(['C' num2str(i) '=''(u' num2str(i) ')'';']);
end
 
for i=1:s_length
%
% Choose either an operator {A} or an input or constant
%
iflag=1; 
        if rand(1) < p_node

         op_choice1=ceil(rand(1)*sum(Awgt)+eps);
         x2=[x2 A(op_choice1)];
         if op_choice1 == Awgt5:1:Awgt6,
            o_bracket = o_bracket + 1;
         end
        else
                iflag=2;
                if rand(1) < p_const 

                 z1= rand(1)*(const_max-const_min);
                 x2=[x2 '([' num2str(z1) '])'];

                else

                 
                 op_choice1=ceil(rand(1)*num_inp+eps);
                 z1= ['C' num2str(op_choice1)];
                 x2=[x2 eval(z1)];
                 iflag3 = 1;
                end
        end
%
if iflag == 2
%
% logically must take from the following set {^, *, -, +, / )}
%

        op_choice2=ceil(rand(1)*sum(Bwgt)+eps);
        x2=[x2 B(op_choice2)];
%
% end loop with a bracket unless op_choice B6 !!!
%
        if op_choice2 == Bwgt6:1:Bwgt7;
                op_choice2=ceil(rand(1)*(sum(Bwgt)-Bwgt(6))+eps);
                x2=[x2 B(op_choice2)];
                x2 =[x2 '('];
                         o_bracket=o_bracket+1;
                         c_bracket=c_bracket+1;
                        
        else
                x2 =[x2 '('];
                         o_bracket=o_bracket+1;
        end

else
%
% if the operator was sqrt log etc have to generate a bracket
%
        x2 = [x2 '('];
        
                o_bracket=o_bracket+1;
%
% end the outer if statement
%
end
end
%
% At end of the expression generator loop
% the equation has to end logically and then
%  close via number of close brackets
%
% the expresssion up to now will always end
% with a '(' so just add a variable to finish)
%

        op_choice1=ceil(rand(1)*num_inp+eps);
        z1= ['C' num2str(op_choice1)];
        x2=[x2 eval(z1)];
        
        
        b_close=o_bracket-c_bracket;
 

        for i=1:b_close
                x2=[x2 ')'];
        end


% End

⌨️ 快捷键说明

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