Mathematical modelling (1)--Genetic algorithm (GA)

Source: Internet
Author: User
Tags character set cos numeric new set numeric value rand sin
Fundamentals

Genetic algorithm is a global optimization algorithm, and it is not easy to get into the local optimal point by group search technique.
The basic idea: to replace the problem parameter space with the coding space, from a population that represents the potential solution set of the problem, according to the principle of survival of the fittest in the process of biological evolution, the basis of adaptability as the evaluation of individual merits and demerits, repeated use of selection, crossover, mutation operator to the group, so that it constantly evolve, the correspondence relation between the genetic algorithm and the evolutionary approach to the optimal solution of biological genetic concepts

Biological Genetic Concept the concepts in genetic algorithms
Fittest When the algorithm stops, the feasible solution with the most target value may be retained most
Individual Feasible solution
Chromosome Coding of feasible solutions
Gene The characteristics of each component of the feasible solution
Adaptability Value of fitness function
Population A set of feasible solutions selected by the value of fitness function
Mating The process of generating a new set of feasible solutions by mating principle
Variation The process of changing a component of a code
Individual merits Each solution should be based on a measure of good or bad, represented by a moderate value function
Basic StepsUnder a certain coding scheme, an initial group is randomly generated by the corresponding coding method, the encoded individual is transformed into the decision variable of the problematic space, and the adaptive value of the individual is selected according to the selection method (i.e. the survival of the fittest), from which some individuals form a mating pool. By the crossover and mutation of the two genetic operators to the mating pool random 22 pairs of individuals to operate, and the formation of a new generation of the population from all the existing parents and descendants of the best selection of some of the most excellent individuals as a novel parent, repeated steps to make it evolve over and over, until the convergence of the basis of judgment to meet three basic operators of genetic manipulation Select

Choose the principle of survival of the fittest, that is, the current group of individuals according to a certain method, select a part of the individual, used to build the mating pool

The effect of the selection operator is to improve the average fitness of the group. Because the selection operator does not produce a new individual, the adaptive value of the best individual in the group does not change as a result of the selection operation. Cross

crossover , first of all the individuals in the mating pool are randomly paired 22, then select the intersection in a certain way, exchanging all the genes after the intersection.

Crossover operator is the main method to produce new individuals, and he determines the global search ability variation of genetic algorithm.

mutation is a change in one or some of the individual's genetic values according to a small probability set in advance.

Mutation operator is only the auxiliary method of generating new individuals, it determines the local search ability coding strategy of genetic algorithm

Genetic algorithms do not operate on the actual decision variables of the optimization problem, so the primary problem of applying genetic algorithm is to express the decision variables into string structure data by coding. Binary encoding

Representation of the parameter of the problem space as a chromosome string based on the character set {0,1}
If the maximum value of the two-tuple function is obtained
F (x1,x2) =x1^2+x2^2
x1={1,2,3,4,5,6,7}
x2={1,2,3,4,5,6,7}
The arithmetic object of the genetic algorithm is the symbol string of the individual, so the variable x1 must be encoded as a symbol string x2. In the subject, it is represented by an unsigned binary integer. Because of X1, X2 is an integer between 0 and 7, so 3-bit unsigned binary integers are used to represent them, and the 6-bit unsigned binary numbers that are concatenated together form the individual genotype, representing a viable solution.
For example, the phenotype of genotype x=101110 is: x=[5,6].
Individual phenotype x and genotype x can be converted to each other by encoding and decoding programs. Real Code

In the real code [5], each gene value of an individual is represented by a real number within a range, and the length of the individual's encoding is equal to that of the decision variable. If an optimization problem contains 5 variables, then
{7.10 5.35 3.20 4.12 2.73} is a genotype of an individual, corresponding to the phenotype of
X=[7.10,5.35,3.20,4.12,2.73] decimal encoding

Decimal encoding converts a numeric value (real number) of a pending parameter into a numeric character of a certain length and forms a string. If each parameter value range is within [0, 10), a decimal number is encoded in the preceding real-coded example, which can be expressed as:
{7 1 0 5 3 5 3 2 0 4 1 2 2 7 3} Application Examples

Examples of examples in mathematical modeling algorithms and Applications (second edition)

(The data is truncated here, then the data is saved to a plain text file, and the download link is mounted)
parameter setting: population size: m=50 Max algebra: g=1000 crossover rate: Pc=1 guaranteed full evolutionary variability of the population: pm=0.1 in general, the likelihood of mutation occurring less coding strategy

The w1w2...w102 sequence is used as a chromosome, where 0<=wi<=1 (i=2,3,..., 101), w1=0,w102=1, each sequence corresponds to an individual in the population.
For example (102 genes are too many, this is represented by 9 genes), the chromosome sequence of an individual is classified as
"0.23 0.82 0.45 0.74 0.87 0.11 0.56 0.69 0.78"
, W1W2...W9 represents the fitness value of the 1-9 target, which is arranged in ascending order to get the sequence in the subject.
6-1-3-7-8-4-9-2-5
This is the initial population of the process of transforming a gene into a viable solution according to the coding scheme.

First, a better initial population is obtained by using a typical approximate algorithm--modified loop algorithm.
objective Function

The objective function is to detect the path length of all targets, that is, to detect 100 enemy targets from the base and then fly back to the base of the path length, the appropriate value function is taken as the objective function
Min F (π1,π2,... π102) =σdπiπi+1 crossover operator

For the paired two parents F1 F2, randomly selected the T Gene Branch as the intersection point, the F1 of the first T gene and F2 of the post 102-t genes S1, the F2 of the first T gene and F1 after 102-t a s2, as shown in the figure

There are many ways of crossing, so we should choose a good cross way to ensure that the offspring can inherit the good characteristic mutation operator of the parent.

Mutation is a means of realizing group diversity, and it is also the guarantee of global optimization.
According to the given mutation rate, three integers are randomly selected for the chosen variant, which satisfies the 1 selection operator .

M individuals with the smallest target function values in the parent population and descendant populations evolve to the next generation of matlab code

CLEAR,CLC load Sj.txt; X=SJ (:, 1:2:8); x=x (:);% changes the 25*4 matrix to 100*1 Matrix Y=SJ (:, 2:2:8); Y=y (:); sj=[x,y]; d1=[70,40];% initial takeoff base sj0=[d1;sj;d1];%102 a base 
Calculating the distance matrix D sj=sj0*pi/180; 
D=zeros (102); For i=1:101 for j=i+1:102 Temp=cos (SJ (i,1)-SJ (j,1)) *cos (SJ (i,2)) *cos (SJ (j,2)) +sin (SJ (i,2)) *sin (s         
        J (j,2));     
    D (i,j) =6370*acos (temp);
End End D=d+d ';% symmetric matrix l=102;w=50;dai=100;
    % by the improved circle algorithm select good parents A for k=1:w c=randperm (100);% put 1 to 100 of these numbers randomly scrambling to get a number sequence c1=[1,c+1,102];% chromosome flag=1;
        While Flag>0 flag=0; For m=1:l-3 for N=m+2:l-1 if (d (C1 (m), C1 (n)) +d (C1 (m+1), C1 (n+1)) <d (C1 (m), C1 (m+1)) +d (C1 (n), C1 (
                    n+1)) flag=1;
                C1 (M+1:N) =c1 (n:-1:m+1);
End End End End J (K,C1) =1:102;
End j=j/102; J (:, 1) = 0;
J (:, 102) = 1;
Rand (' state ', sum (clock)); 
The% genetic algorithm realizes the process a=j;     
    For K=1:dai% produces 0~1 between random sequence numbers to encode% mating to produce offspring B b=a;  C=randperm (w);  
  % generated 1~50 random number  For i=1:2:w% from 1 to 50 in turn 22 pairs, namely I and (i+1) paired F=2+floor (100*rand (1));         
        % randomly produces intersection points temp=b (c (i), f:102);         
        B (c (i), f:102) =b (C (i+1), f:102);     B (c (i+1), f:102) =temp; % crossover replacement finished end% mutation produces descendant C By=find (rand (1,W) <0.1); 
    % returns the position of the random number <0.1 if Length (by) ==0% if the previous step is not found, then randomly produces a mutation point By=floor (W*rand (1)) +1; 
    End C=a (by,:); 
    L3=length (by);    For J=1:l3 Bw=2+floor (100*rand (1,3));    % randomly selects three integers bw=sort (BW); % Meet 1<u<v<w<102 C (J,:) =c (J,[1:BW (1) -1,BW (2) +1:BW (3), BW (1): BW (2), BW (3) +1:102]);% put u, The gene segment between V (including U and V) is inserted into w back end G=[a; B    C];  
    % get parent, cross descendant, mutation descendant collection G% in parent and descendant select good variety as new parent tl=size (g,1);    
    [Dd,ix]=sort (g,2);%dd is an ascending g,ix for index temp (1:TL) = 0;   For J=1:TL for i=1:101 temp (j) =temp (j) +d (ix (j,i), IX (J,I+1));      
    % re-obtains the distance matrix end end [Dz,iz]=sort (temp) According to the new sequence; A=g (IZ (1:w),:);% Select the lowest value of the target function w individuals evolve to the next generation end path=IX (IZ (1),:); Long=dz (1);%toc xx=sj0 (path,1); yy=sj0 (path,2);
 Plot (Xx,yy, '-O ');

sj.txt+ source. m Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.