RBF neural network optimization analysis after genetic algorithm (GA) optimization (Matlab code implementation)

Table of contents

1 Genetic algorithm

2 RBF neural network

3 Matlab code implementation

4 results


1 Genetic algorithm

Genetic algorithm is an optimization algorithm that simulates the evolution process in nature. It searches for the optimal solution or near-optimal solution by simulating the genetic, crossover and mutation processes of biological evolution.

The basic steps of genetic algorithm are as follows:

Initial population: Randomly generate a set of initial solutions as the population.

Fitness evaluation: According to the specific evaluation function of the problem, the fitness of each individual is calculated.

Selection operation: According to the size of fitness, select a part of individuals as parents to generate the next generation.

Crossover operation: Perform crossover operation on the selected parent individuals to generate new individuals.

Mutation operation: perform mutation operation on newly generated individuals to introduce new genes.

Replacement operation: Replace the original individuals with newly generated individuals to form a new population.

Judgment of termination condition: judge whether the termination condition is satisfied, if so, stop the algorithm, otherwise return to step 3.

The advantage of genetic algorithm is that it can find a better solution in a large-scale search space, and it is suitable for a variety of optimization problems. However, due to its randomness-based characteristics, it may fall into a local optimal solution, and the convergence speed of the algorithm is slow. Therefore, for complex problems, it is necessary to set parameters reasonably and run multiple times to obtain better results.

2 RBF neural network

The RBF network is a three-layer neural network , which includes an input layer, a hidden layer, and an output layer. The transformation from the input space to the hidden layer space is nonlinear, while the transformation from the hidden layer space to the output layer space is linear. The flow diagram is as follows:

The basic idea of ​​the RBF network is: use RBF as the "base" of the hidden unit to form the hidden layer space, so that the input vector can be directly mapped to the hidden space without the need for weight connections. When the central point of the RBF is determined, the mapping relationship is also determined. The mapping from the hidden layer space to the output space is linear, that is, the output of the network is the linear weighted sum of the output of the hidden unit, and the weight here is the adjustable parameter of the network. Among them, the role of the hidden layer is to map the vector from the low-dimensional p to the high-dimensional h, so that the low-dimensional linear inseparable situation can become linearly separable to the high-dimensional, mainly the idea of ​​​​the kernel function .

In this way, the mapping of the network from input to output is nonlinear, while the output of the network is linear in terms of adjustable parameters. The weight of the network can be directly solved by the linear equations, thus greatly speeding up the learning speed and avoiding the local minimum problem.


3 Matlab code implementation

GA.m

clear all
close all

G = 15;
Size = 30;
CodeL = 10;

for i = 1:3
    MinX(i) = 0.1*ones(1);
    MaxX(i) = 3*ones(1);
end
for i = 4:1:9
    MinX(i) = -3*ones(1);
    MaxX(i) = 3*ones(1);
end
for i = 10:1:12
    MinX(i) = -ones(1);
    MaxX(i) = ones(1);
end

E = round(rand(Size,12*CodeL));  %Initial Code!

BsJ = 0;

for kg = 1:1:G
    time(kg) = kg
    
    for s = 1:1:Size
        m = E(s,:);
        
        for j = 1:1:12
            y(j) = 0;
            
            mj = m((j-1)*CodeL + 1:1:j*CodeL);
            for i = 1:1:CodeL
                y(j) = y(j) + mj(i)*2^(i-1);
            end
            f(s,j) = (MaxX(j) - MinX(j))*y(j)/1023 + MinX(j);
        end
        
        % ************Step 1:Evaluate BestJ *******************
        p = f(s,:);
        
        [p,BsJ] = RBF(p,BsJ);
        
        BsJi(s) = BsJ;
    end
    
    [OderJi,IndexJi] = sort(BsJi);
    BestJ(kg) = OderJi(1);
    BJ = BestJ(kg);
    Ji = BsJi+1e-10;
    
    fi = 1./Ji;
    [Oderfi,Indexfi] = sort(fi);
    Bestfi = Oderfi(Size);
    BestS = E(Indexfi(Size),:);
    
    % ***************Step 2:Select and Reproduct Operation*********
    fi_sum = sum(fi);
    fi_Size = (Oderfi/fi_sum)*Size;
    
    fi_S = floor(fi_Size);
    
    kk = 1;
    for i = 1:1:Size
        for j = 1:1:fi_S(i)
            TempE(kk,:) = E(Indexfi(i),:);
            kk = kk + 1;
        end
    end
    
    % ****************Step 3:Crossover Operation*******************
    pc = 0.60;
    n = ceil(20*rand);
    for i = 1:2:(Size - 1)
        temp = rand;
        if pc>temp
            for j = n:1:20
                TempE(i,j) = E(i+1,j);
                TempE(i+1,j) = E(i,j);
            end
        end
    end
        TempE(Size,:) = BestS;
        E = TempE;
        
     %*****************Step 4:Mutation Operation*********************
     pm = 0.001 - [1:1:Size]*(0.001)/Size;
     for i = 1:1:Size
         for j = 1:1:12*CodeL
             temp = rand;
             if pm>temp
                 if TempE(i,j) == 0
                     TempE(i,j) = 1;
                 else
                     TempE(i,j) = 0;
                 end
             end
         end
     end
     
     %Guarantee TempE(Size,:) belong to the best individual
     TempE(Size,:) = BestS;
     E = TempE;
     %********************************************************************
 end

 
 Bestfi
 BestS
 fi
 Best_J = BestJ(G)
 figure(1);
 plot(time,BestJ);
 xlabel('Times');ylabel('BestJ');
 save pfile p;

RBF.m

function [p,BsJ] = RBF(p,BsJ)

ts = 0.001;

alfa = 0.05;
xite = 0.85;
x = [0,0]';
b = [p(1);p(2);p(3)];
c = [p(4) p(5) p(6);
    p(7) p(8) p(9)];
w = [p(10);p(11);p(12)];

w_1 = w;w_2 = w_1;
c_1 = c;c_2 = c_1;
b_1 = b;b_2 = b_1;
y_1 = 0;

for k = 1:500
    timef(k) = k*ts;
    
    u(k) = sin(5*2*pi*k*ts);
    
    y(k) = u(k)^3 + y_1/(1 + y_1^2);
    
    x(1) = u(k);
    x(2) = y(k);
    for j = 1:1:3
        h(j) = exp(-norm(x - c(:,j))^2/(2*b(j)*b(j)));
    end
    ym(k) = w_1'*h';
    
    e(k) = y(k) - ym(k);
    
    d_w = 0*w;d_b = 0*b;d_c = 0*c;
    for j = 1:1:3
        d_w(j) = xite*e(k)*h(j);
        d_b(j) = xite*e(k)*w(j)*h(j)*(b(j)^-3)*norm(x-c(:,j))^2;
        for i = 1:1:2
            d_c(i,j) = xite*e(k)*w(j)*h(j)*(x(i)-c(i,j))*(b(j)^-2);
        end
    end
    
    w = w_1 + d_w + alfa*(w_1 - w_2);
    b = b_1 + d_b + alfa*(b_1 - b_2);
    c = c_1 + d_c + alfa*(c_1 - c_2);
    
    y_1 = y(k);
    w_2 = w_1;
    w_1 = w;
    
    c_2 = c_1;
    c_1 = c;
    
    b_2 = b_1;
    b_1 = b;
end

B = 0;
for i = 1:500
    Ji(i) = abs(e(i));
    B = B + 100*Ji(i);
end

BsJ = B;
        

Test.m

clear all;
close all;

load pfile;
alfa = 0.05;
xite = 0.85;
x = [0,0]';

%M为1时
M = 2;
if M == 1
    b = [p(1);p(2);p(3)];
    c = [p(4) p(5) p(6);
         p(7) p(8) p(9)];
    w = [p(10);p(11);p(12)];
elseif M == 2
    b = 3*rand(3,1);
    c = 3*rands(2,3);
    w = rands(3,1);
end

w_1 = w;w_2 = w_1;
c_1 = c;c_2 = c_1;
b_1 = b;b_2 = b_1;

y_1 = 0;

ts = 0.001;
for k = 1:1500
    time(k) = k*ts;
    
    u(k) = sin(5*2*pi*k*ts);
    y(k) = u(k)^3 + y_1/(1 + y_1^2);
    
    x(1) = u(k);
    x(2) = y(k);
    
    for j = 1:3
        h(j) = exp(-norm(x-c(:,j))^2/(2*b(j)*b(j)));
    end
    
    ym(k) = w_1'*h';
    e(k) = y(k) - ym(k);
    
    d_w = 0*w;d_b = 0*b;d_c=0*c;
    for j = 1:1:3
        d_w(j) = xite*e(k)*h(j);
        d_b(j) = xite*e(k)*w(j)*h(j)*(b(j)^-3)*norm(x-c(:,j))^2;
        for i = 1:1:2
            d_c(i,j) = xite*e(k)*w(j)*h(j)*(x(i) - c(i,j))*(b(j)^-2);
        end
    end
    
    w = w_1 + d_w + alfa*(w_1 - w_2);
    b = b_1 + d_b + alfa*(b_1 - b_2);
    c = c_1 + d_c + alfa*(c_1 - c_2);
    
    y_1 = y(k);
    w_2 = w_1;
    w_1 = w;
    c_2 = c_1;
    c_1 = c;
    b_2 = b;
    
end
figure(1);
plot(time,ym,'r',time,y,'b');
xlabel('times(s)');ylabel('y and ym');

pfile.mat

p: [2.9915 2.9008 2.4982 1.0059 1.1056 0.8006 0.4780 1.6100 -1.3460 -0.7204 0.4076 0.2786]

4 results

Guess you like

Origin blog.csdn.net/Yan_she_He/article/details/128926572