1. Algorithm principle
To establish the mathematical model of sparrow search algorithm, the main rules are as follows:
- Finders usually have high energy reserves and are responsible for searching for areas with abundant food throughout the population, providing foraging areas and directions for all entrants. The level of energy reserve in model building depends on the Fitness Value of individual sparrows.
- Once the sparrow spots a predator, the individual starts to sing as an alarm signal. When the alarm value is greater than the safe value, the finder will take the participant to another safe area for foraging.
- The identity of discoverer and entrant is dynamic. Each sparrow can be a finder if it can find a better food source, but the proportion of finder and entrant to the population remains the same. In other words, if one sparrow becomes the discoverer, another sparrow must become the participant.
- The lower the energy of the participants, the worse their foraging position throughout the population. Some hungry enrollees are more likely to fly elsewhere to feed for more energy.
- In the foraging process, the participant can always search for the discoverer with the best food, and then get food from the best food or forage around the discoverer. At the same time, some participants may constantly monitor the discoverers and compete for food resources in order to increase their predation rate.
- When sensing danger, sparrows at the edge of the group quickly moved to a safe area to get a better position, while sparrows in the middle of the group moved randomly to get closer to other sparrows.
In the simulation experiment, we need to use virtual sparrows to search for food, and the population composed of N sparrows can be expressed as follows:
Where, D represents the dimension of the variable of the problem to be optimized, and n represents the number of sparrows. Then, the fitness values of all sparrows can be expressed as follows:
Where, f represents the fitness value.
In SSA, finders with better fitness values will get food first in the search process. In addition, because the finder is responsible for finding food for the entire sparrow population and providing a feeding direction for all participants. Therefore, the finder can obtain a larger foraging search area than the entrant. According to rules (1) and (2), during each iteration, the location of the finder is updated as follows:
Ii. Source code
% Clean environment CLC clear % Read data Load Data Input Output % Number of nodes inputnum=2; hiddennum=5; outputnum=1; Input_train =input(1:2000,:)'; input_test=input(1901:2000,:)'; output_train=output(1:1900)'; output_test=output(1901:2000)'; % select sample input and output data normalization [inputn,inputps]=mapminmax(input_train); [outputn,outputps]=mapminmax(output_train); % Net =newff(inputn,outputn,hiddennum); % initializes dim=21; maxgen=30; % number of iterations sizepop=20; % population size POPmax =5; popmin=-5; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % iterative optimization x = bestX % % result analysis plot (yy) the title ([' fitness curves If (maxgen) = 'max2str'); Xlabel (' Evolution algebra '); Ylabel (' fitness '); W1 =x(1:inputnum*hiddennum); w1=x(1:inputnum*hiddennum) B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum); w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum); B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum); Net. Iw = {1, 1} reshape (w1, hiddennum inputnum); Net. Lw = {2, 1} reshape (w2, outputnum hiddennum); net.b{1}=reshape(B1,hiddennum,1); net.b{2}=B2; %% training % net. Trainparam. epochs=100; Net. TrainParam. Lr = 0.1; Net. TrainParam. Goal = 0.00001; % net [net,tr]= net (net,inputn,outputn); Inputn_test =mapminmax('apply',input_test,inputps); an=sim(net,inputn_test); test_simu=mapminmax('reverse',an,outputps); error=test_simu-output_test; Figure (2) plot(error) title(' Simulation prediction error ','fontsize',12); Xlabel (' simulation times ','fontsize',12); Ylabel (' error 100 ','fontsize',12);Copy the code