I. WSN model
Genetic algorithm
• Genetic Algorithm (GA) is an evolutionary Algorithm whose basic principle is to imitate the evolutionary law of “natural selection and survival of the fittest” in biology. It was first proposed by Professor J. Holland of University of Michigan in 1967. • Genetic algorithms start with a population that represents the set of possible potential solutions to a problem. A population consists of a number of individuals encoded by a gene. Therefore, the first step is to achieve the mapping from phenotype to genotype, that is, coding. After the generation of the initial population, according to the principle of survival of the fittest and survival of the fittest, generation by generation evolution produces better and better approximate solutions. In each generation, individuals are selected according to their fitness in the problem domain. By virtue of genetic operators of natural genetics, a population representing the new solution set is generated. This process leads to the evolution of the population like natural evolution, in which the population in the later generation is more adaptable to the environment than the previous generation, and the optimal individual in the last generation is through decoding, which can be regarded as the approximate optimal solution of the problem.
• Genetic algorithms have three basic operations: Selection, Crossover and Mutation. • (1) Choice. The purpose of selection is to select good individuals from the current population so that they have a chance to father the next generation. According to the fitness value of each individual, according to certain rules or methods, some excellent individuals from the previous generation of population were selected and passed on to the next generation of population. Selection is based on the probability that highly adaptable individuals will contribute one or more offspring to the next generation. • (2) cross. A new generation of individuals can be obtained through crossover operation, and the new individuals combine the characteristics of the parent individuals. Individuals in the population are randomly paired, and for each individual, some chromosomes between them are exchanged with crossover probability. • (3) variation. To change the value of one or more loci to the other alleles with the probability of variation for each individual in the population. As in biology, variation is rare and provides opportunities for the creation of new individuals.
The basic steps of genetic algorithm:
1) Coding: GA first represents the solution data of the solution space as the genotype string structure data of the genetic space before searching, and the different combinations of these string structure data constitute the different points. 2) Generation of initial group: N initial string structure data are randomly generated, and each string structure data is called an individual, and N individuals constitute a group. GA starts to evolve with these N string structure data as the initial point. 3) Fitness evaluation: fitness indicates the advantages and disadvantages of an individual or solution. The definition of adaptive function is also different.
4) Selection: The purpose of selection is to select good individuals from the current population so that they have a chance to reproduce as fathers for the next generation. Genetic algorithm embodies this idea through the selection process, the principle of selection is that adaptable individuals have a high probability of contributing one or more offspring to the next generation. Selection embodies Darwinian principle of survival of the fittest. 5) Crossover: crossover operation is the most important genetic operation in genetic algorithm. A new generation of individuals can be obtained through crossover operation, and the new individuals combine the characteristics of their parents. Crossover embodies the idea of information exchange. 6) Variation: firstly, an individual is randomly selected in the population, and the value of a certain string in the string structure data is randomly changed with a certain probability for the selected individual. As in biology, the probability of variation in GA is very low, and the value is usually very small.
Genetic Algorithm Toolbox:
• MATLAB embedded genetic algorithm toolbox: Gadst • Sheffield Genetic algorithm toolbox: Gatbx • UnC Genetic algorithm toolbox: GAOT
Initializega function:
The ga functions:
Optimization of initial weights and thresholds of BP neural network by genetic algorithm:
Three, code,
%% Genetic algorithm main function CLC; clear all; close all; Warning off %% Genetic algorithm parameter maxgen=200; Sizepop =20; % population size, number of individuals, i.e. chromosome number pcross=[0.6]; % cross probability, [0,1] why is it written as an array? Pmutation = [0.01]; % variation probability, lenchrom=[1 1 1 1] between [0,1]; Bound =[0,0.9* PI; 0,0.9* PI; 0,0.9* PI; 0,0.9* PI; 0,0.9* PI]; 0,0.9* PI]; Struct ('fitness',zeros(1,sizepop),'chrom',[]); Population structure avGFITNESS =0; % population average fitness bestfitness=0; % population optimum fitness bestChrom =[]; % chromosome bf=[]; % best fitness of recorded population AF =[]; For I =1:sizepop individuals. Chrom (I,:)=Code(lenchrom,bound); X =individuals. Chrom (I,:); individuals.fitness(i)=fun(x); The initial population was completed above end %, and the fitness of each individual in the initial population was calculated. The best chromosome [bestfitness bestindex]=min(individuals. % gets the minimum fitness function value and position of the initial population. Bestchrom =individuals. Chrom (bestindex,:); bestchrom=individuals. Avgfitness =sum(individuals. Fitness)/sizepop; % chromosome mean fitness trace=[]; %% for I =1:maxgen % Select individuals=Select(individuals,sizepop); Avgfitness =sum(individuals. Fitness)/ sizePOP; % after selection of the population average fitness % Cross individuals. The chrom = Cross (pcross lenchrom, individuals, chrom, sizepop, bound). % % return after crossing of chromosome variation individuals. The chrom = Mutation (pmutation lenchrom, individuals, chrom, sizepop, [I, maxgen], bound). %% nonlinear optimization, can be commented out, after the comment is the general genetic algorithm % every 10 generations of evolution, with the resulting value as the initial value of nonlinear optimization, this is the genetic algorithm and nonlinear programming function optimization algorithm combined, nonlinear if(mod(i,10)==0) individuals.chrom=nonlinear(individuals.chrom,sizepop); For j=1:sizepop x=individuals. Chrom (j,:); individuals.fitness(j)=fun(x); Find the optimal chromosome end % and their location in the population [newbestfitness newbestindex] = min (individuals. Fitness); % to replace the best chromosome in the last evolution if bestFitness > newBestFitness % If the function value of the new generation of chromosomes is small, that is, high fitness bestfitness= newBestFitness; % then assign the new fitness value to bestfitness bestchrom=individuals. Chrom (newbestindex,:); % Give bestChrom the new best chromosome end avgfitness=sum(individuals. Fitness)/sizepop; bf=[bf bestfitness]; af=[af avgfitness]; %trace=[trace,avgfitness bestfitness]; % Record the best fitness and average fitness in each generation of evolution end % Evolution end % Draw image analysis [r, C]=size(bf); plot([1:c],bf,'r-',[1:c],af,'b--','LineWidth',2); Title ([' num2str(maxgen)]); Xlabel (' Evolution algebra '); Ylabel (' function value '); Ylim ([8] 1.5); Legend (' best value of generations ',' average value of generations '); Disp (' function valued variable '); Grid on % window displays DISP ([BestFitness X]); % % [r c]=size(trace); % % calculation of trace figure (1) % plot ([1: r] ', trace (:, 1), 'r -', [] 1: r, trace (:, 2), 'b -'); % title([' num2str(maxgen)]); % xlabel(' evolution algebra '); % ylabel(' function value '); % legend(' Max ',' Max ',' Max '); % disp(' function value variable '); % Grid on % % window displays % DISP ([bestFitness X]);Copy the code
Iv. References
The complete code to download www.cnblogs.com/ttmatlab/p/…