TSP Genetic algorithm

Source: Internet
Author: User
Tags cos sin

ga--Genetic Algorithm

As with the simulated annealing algorithm, it is one of the modern optimization algorithms. Simulated annealing still accepts a relatively poor solution at a certain degree of acceptance.

Genetic algorithm, is really true and nature's genetic evolution has a very close connection, of course, genetic evolution is only in biology has been said, 8 words, natural selection, survival of the fittest.

    • Brief introduction

"Origin of Species", interested to see Darwin's writings.

Natural selection, survival of the fittest, these two words, can also be said to the genetic algorithm process pseudo-code description, natural selection, is our objective function, only the more satisfied with our objective function of the individual will stay, survival of the fittest, is we in the process of the algorithm to eliminate some individuals.

    • Gene encoding method

Biology tells us that heredity, mutation are all based on the population, how to express a solution? With his genes, hehe, the steps that make up this solution indicate, what is the first step, what is the second step, and how to encode in the program?

Commonly used two encoding methods: binary encoding, floating-point encoding.

Binary encoding: A certain precision binary can only represent a certain precision floating point number. Chestnuts, required to be accurate to 6 decimal places, and the interval is [ -1,2], at least to divide the interval into 3*10^6. Encoding also requires 22 bits. This involves converting a binary string into a real number with an interval of [ -1,2], which is converted to each other. For example, the resulting decimal number is x, then the corresponding [ -1,2] interval of the floating point is

Floating-point encoding: To improve the complexity of genetic algorithm, a floating-point encoding is proposed.

    • Adaptive scoring and selection functions

The adaptive scoring function is used to measure which individual should be eliminated. But can not say that the value of the nearly individual must be eliminated, there will be a probability exists, how to establish a probabilistic relationship? The usual method is the roulette method. Assuming that the population number is n, the fitness of an individual I is fi, then the probability of individual I being selected is:

So, the process of natural selection in genetic algorithms:

Adapting the scoring function--finding out the adaptive scoring values of each individual

Roulette Selection-The probability of individual being chosen

    • Gene recombination and Gene mutation

In biology, there are two kinds of genetic recombination.

Gene recombination is the exchange of two individual genes.

Gene mutation is a small probability event.

Makes a gene into his allele, causing a certain phenotype change.

    • Solving tsp problem

The TSP problem, which had already been done with the cost stream, and simulated annealing, is now discussed with genetics.

The process of procedure is the same as heredity and evolution in nature.

    1. Generate 50 individuals, form a population, and encode the genes.

    2. Gene recombination, mutation, and fatherhood, together with natural selection.

    3. Repeat 100 times evolution. Get a better solution.

clc,clear;sj0= Load ('Sj.txt'); x= Sj0 (:,1:2:8); x =x (:); y= Sj0 (:,2:2:8); y =y (:); SJ= [x, y];d 1 = [ -, +];SJ= [D1;SJ;D1]; SJ = sj*pi/ the;? D= Zeros (102);? fori =1:101     forj = i+1:102d (i,j)=6370*acos (cos (SJ (i,1)-SJ (J,1)) *cos (SJ (i,2)) *cos (SJ (J,2)) +sin (SJ (i,2)) *sin (SJ (J,2))); Endend?d= d + D';%W is the population number and G is the evolutionary algebra W= -; G= -; rand (' State', sum (clock));%The modified circle algorithm selects the initial population fork=1: w%selection of initial population C by improved circle algorithm=randperm ( -); %a fully arranged C1 that produces 1,...,100=[1, c+1,102]; %generating the initial solution fort=1:102%This layer loop is the change ring flag=0; %Modify Circle Exit Flag form=1: -             forn=m+2:101                ifD (C1 (m), C1 (n)) +d (C1 (m+1), C1 (n+1)) <d (C1 (m), C1 (m+1)) +d (C1 (n), C1 (n+1)) C1 (M+1: N) =c1 (n:-1: m+1); flag=1; %Modify Loop End End Endifflag==0J (K,C1)=1:102; Break%record the better solution and exit the current layer loop end EndEnd?%chromosome Encoding J (:,1) =0; J= j/102;?%100 times Evolution forK =1: G A=J; C=Randperm (W);  fori =1:2: w%Gene recombination F=2+ Floor ( -*rand (1)); %generation of cross-operation of chromosomes to TMP= A (c (i), [F:102]); A (c (i), [F:102]) = A (c (i+1), [F:102]); A (c (I+1), F:102) =tmp;    End? %Mutation by= [];  while~isempty (by)%the number of mutated individuals is also random by=find (Rand (1, W) <0.1); End B= A (by,:); %mutated chromosomes forj =1: Length (by) BW= Sort (2+floor ( -*rand (1,3))); %3 address B for mutation operation (J,:)= B (j,[1: BW (1), BW (2)+1: BW (3), BW (1): BW (2), BW (3)+1:102]); End G=[j; A       B]; %Parent and descendant%The gene translates into a solution space, translating chromosomes into sequences of 1,..., 102 Ind1 [SG,IND1]= Sort (G,2); Num= Size (G,1); %Total individual number of parent-child populationLong= Zeros (1, num); %the merits and demerits of each individual forj =1: Num fori =1:101            Long(j) =Long(j) +d (Ind1 (j,i), Ind1 (j,i+1)); End end [Slong,ind2]= Sort (Long); J= G (Ind2 (1: w),:); End?path= Ind1 (Ind2 (1),:); Flong= Slong (1); XX= SJ (PATH,1); YY= SJ (PATH,2);p lot (Xx,yy,'- o');

Reference:

Acdreamer

Sishuqui

TSP Genetic algorithm

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.