Genetic algorithm _ Genetic algorithm

Source: Internet
Author: User
Tags clear screen rand

I. BACKGROUND

Genetic algorithm (GENETICALGORITHM) is proposed by Professor Johnh.holland and his students in the United States, and the basic theory and method of genetic algorithm are systematically expounded in the 1975 "self-adaptability of natural and artificial systems". It is a kind of stochastic search algorithm which draws lessons from natural selection and genetic mechanism.

Two. Knowledge of evolution

individual (individual): a single organism that makes up a population.

population (Population) : The evolution of organisms takes the form of groups, and such a group is called a population.

Gene (Gene) : a genetic factor.

chromosome (chromosome) : contains a group of genes.

heredity, crossover and mutation (Genetic,crossoverand Mutation): A new individual will inherit genes from both parents, with a certain probability of genetic variation.

Genetic algorithm is the simulation of Darwin's genetic selection and natural phase-out of the biological evolutionary process of the algorithm (survival of the fittest, the fittest)



Three. Algorithm idea

  Generate an initial population   construct fitness function According to the objective function of the problem   according to the good or bad of the adaptive value, the best individual is the optimal solution after several generations.   Initial population   Coding Method-Binary Code   fitness function   Genetic operations   selection strategy   STOP criteria
Five, for example



      









MATLAB Implementation code is as follows:

%%% A traveling businessman who wants to visit 31 provincial capitals, needs to choose the shortest path.%%%%% population number np=200% gene dimension n=31% Iteration number g=1000%%% genetic algorithm to solve TSP problem%%%%%%% clear all; % clears all variables close to all;      % clear Figure CLC; % Clear screen c=[1304 2312; 3639 1315; 4177 2244; 3712 1399; 3488 1535; 3326 1556; 3238 1229; 4196 1004; 4312 790; 4386 570; 3007
1970;
2562 1756;
2788 1491;
2381 1676;
1332 695;
3715 1678;
3918 2179;
4061 2370;
3780 2212;
3676 2578;
4029 2838;
4263 2931;
3429 1908;
3507 2367;
3394 2643;
3439 3201;
2935 3240;
3140 3550;
2545 2357;
2778 2826; 2370 2975];%31 Capital coordinates n=size (c,1);  The scale of the%tsp problem, that is, the number of cities D=zeros (N); 
     % of any two city distance interval matrices, initialization is 0 any two city distance interval matrix% for i=1:n for j=1:n D (i,j) = ((c (i,1) (j,1)) ^2+ (C (i,2)-C (j,2)) ^2) ^0.5;  End End np=200; % population scale g=1000; % max Genetic algebra F=zeros (np,n);      % for storage population f=[];  % Population Update intermediate storage for I=1:NP f (i,:) =randperm (N);% randomly generated initial population end r=f (1,:);  % storage of optimal population len=zeros (np,1); % Storage path length Fitness=zeros (np,1);               % Storage normalized adaptive value of gen=0;
        % record number of iterations%%%% genetic algorithm loop%%%%%%%%% while gen<g% of path length% for I=1:NP Len (i,1) =d (f (I,n), F (i,1));
         For j=1: (N-1) len (i,1) = Len (i,1) +d (f (i,j), F (i,j+1));  End End Maxlen=max (LEN);  % store Longest path minlen=min (len);
     % Store Shortest path%%%%%% Update shortest path%%%%% rr=find (Len==minlen);
     
     R=f (RR (1,1),:);
     %%% calculates the normalized adaptive value%%%%% for I=1:length (len) fitness (i,1) = (n (len (i,1)-minlen)/(maxlen-minlen+0.001));
     End%%%% Select operation%%%%%% nn=0;
             For I=1:NP if fitness (i,1) >=rand nn=nn+1;
         F (NN,:) =f (i,:);
     End End [Aa,bb]=size (F);
         While AA&LT;NP nnper=randperm (NN);
         A=f (Nnper (1),:);
         B=f (Nnper (2),:);   %%%%% crossover Operation%%%% W=ceil (N/10); % crossover point number p=unidrnd (n-w+1);
             % randomly selected crossover range from P--p+w for I=1:w X=find (a==b (1,p+i-1));
             Y=find (B==a (1,p+i-1));
             % value Exchange Temp=a (1,P+I-1);
             A (1,p+i-1) =b (1,p+i-1);
        B (1,p+i-1) =temp;     
             Temp=a (1,X);
             A (1,x) =b (1,y);
             B (1,y) =temp;
           %%% value Exchange End%%%%% mutation Operation%%%% P1=floor (1+n*rand ());
           
           P2=floor (1+n*rand ());
                 While P1==p2 P1=floor (1+n*rand ());
           P2=floor (1+n*rand ());
           End% value Exchange Tmp=a (p1);
           A (p1) =a (p2);
           
           A (p2) =tmp;
           Tmp=b (p1);
           B (p1) =b (p2);
           
           B (p2) =tmp; F=[f; A
           B];
     [Aa,bb]=size (F);    End If AA&GT;NP f=f (1:NP,:);% to keep the population scale as NP end f=f;  % Update population f (1,:) =r;
     % keep each generation of the best individual clear F;    gen=gen+1;
 % Iteration number plus 1 rlength (gen) =minlen;
     End figure for i=1:n-1 plot ([C (R (i), 1), C (R (i+1), 1)],[c (R (i), 2), C (R (i+1), 2)], ' bo-');
 Hold on;
 End Plot ([C (R (N), 1), C (r (1), 1)],[c (R (N), 2), C (r (1), 2)], ' ro-'); 

 Title ([' Optimize shortest distance: ', Num2str (Minlen)]);
 Figure plot (rlength); Xlabel (' IterativeNumber ') ylabel (' objective function value ') title (' Adaptive Evolution curve ')
     
 
 
 
  



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.