Using GA algorithm to design the shortest journey between 22 locations---R language

Source: Internet
Author: User

Data mining Entry and actual combat public number: DATADW

Related Posts
The feature selection based on greedy algorithm for ︱ case of reprint

Design of shortest Journey-R language between 22 locations using GA algorithm


A total of 30 students from a graduating class, from 22 regions, we hope that on the holiday to say a walk on the trip, will all the students go home. To sum up, the tolls are a great expense, so I would like to design a travel plan to ensure that this trip is the shortest total distance.

Traveling trader Problem is a classical NP problem

NP is one of the seven mathematical problems in the world, which is non-deterministic polynomial, which is the non-deterministic problem of the complexity of polynomial.

If the enumeration method is used to solve the problem, there are 22 locations:
(22-1). /2 = 25545471085854720000 route Scenarios
GA algorithm

Genetic algorithm introduces the biological evolution principle of "survival of the fittest, the fittest" to the coded tandem group formed by optimization parameters, according to the selected fitness function and through the genetic replication, crossover and mutation of the individual screening, so that the high degree of adaptation of the individuals are retained to form a new group, the new group has inherited the information of the previous generation, And better than the previous generation. This cycle, the group of individual adaptation continues to improve until certain conditions are met. Genetic algorithm is simple and can be processed in parallel, and can reach the global optimal solution.

GA Algorithm Design 1. Generating the original chromosome population

The number of N cities is used as a possible path by real coding. For example, for 8 cities, the following chromosomes can be generated to represent a path, 8,6,4,2,7,5,3,1. Repeated operations generate a chromosome population that is equal to N.
2. Generate the Fitness function

Because it is the shortest path, the fitness function is generally the maximum value of the function, so take the total length of the path t of the reciprocal, that is fit

3. Select Chromosome

The generation of the parent chromosome by roulette.
4. Coding the chromosome population

Suppose you have a list of nine cities: w= (A,b,c,d,e,f,g,h,i).
There are two routes as follows:
W1= (A,D,B,H,F,I,G,E,C)
W2= (B,C,A,D,E,H,I,F,G)
Then these two routes can be encoded as:
W1= (142869753)
W2= (231458967)
5. Cross

The probability PC chooses to participate in the intersection of the individual (even number), with two point crossover operator to operate.
For example, for the following two chromosome individuals
(1 3 4 | 5 2 9 | 8 6 7)
(1 7 6 | 9 5 2 | 4 3 8)
The chromosome of the progeny can be obtained by the intersection of two points
(1 3 4 | 9 5 2 | 8 6 7)
(1 7 6 | 5 2 9 | 4 3 8)
6. Variation

The probability of the PM to choose to participate in the variation of the individual, with the change of the operation. Randomly select two sites in the individual to exchange genes.
If the swap point is 4 and 7, then after swapping for the b=123756489 of the a=123456789
7. Decoding

To decode the chromosomes and restore the real representation of the chromosomes.
8. Generational Evolution

On the basis of the new chromosome, the steps to select the chromosome are returned again, and the algorithm is stopped until the number of iterations is reached.

Algorithm Implementation

#加载packageslibrary (SP)
Library (maptools)
Library (geosphere)

source ("c:\\users\\shangfr\\desktop\\ Path optimization \\GA algorithm script. R "
data=read.csv (" c:\\users\\shangfr\\desktop\\ path optimization \\143 geo-coordinate. csv ") #读取城市经纬度数据
Border <-readshapepoly ("c:\\users\\shangfr\\desktop\\ path Optimization \\map\\bou2_4p.shp") #读取各省的边界数据等 # Initialization (list region distance matrix-clustering) da= Data[,1:2] rownames (DA) =data[,3] hc=hclust (Dist (DA)) Cutree (hc, H = ten) plot (hc) route= Creatdna (data,5) x = route[,1] y = route[,2] z = route[,3] cols=route[,4] Muer.lonlat = Cbind ( route[,1],route[,2]) # matrixmuer.dists = Distm (Muer.lonlat, fun=distvincentyellipsoid) # exact calculation, oval Ans=round (muer.dists /1000,2) roundots = List (x=x,y=y,ans=ans,z=z,cols=cols) species = Ga4tsp (dots=roundots,initdna=null,n=50 , cp=0.1,vp=0.01,maxiter=1000,maxstay=100,maxelite=2,drawing=true)

visualization of optimal paths

This figure is based on Baidu Echarts

R Language-ga algorithm script

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: 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.