Genetic algorithm to solve Java__c language of traveling business problem

Source: Internet
Author: User

Reproduced from: Wangqiuyun

Original link: http://blog.csdn.net/wangqiuyun/article/details/12838903


One, TSP problem

TSP (travelling salesman Problem) is one of the famous problems in the field of mathematics, which is the traveling salesman problem and the traveling salesman problem. Suppose a traveling businessman wants to visit N cities, he must choose the path he wants to take, the limit of which is that each city can only visit once, and finally return to the original city. The path's selection target is the minimum value in all paths required for the path to be travelled.

TSP problem is a combinatorial optimization problem. This problem can be proved to have NPC computational complexity. TSP can be divided into two kinds, one is symmetric tsp (symmetric tsp) and the other is asymmetric problem (asymmetric tsp). All TSP problems can be described in one graph (graph):

V={C1, c2, ..., CI, ..., cn},i = 1,2, ..., N, is a collection of all cities. CI represents the first city, N is the number of cities;

E={(R, s): R,s∈v} is a collection of connections between all cities;

C = {Crs:r,s∈v} is the cost metric for connections between all cities (generally the distance between cities);

If CRS = CSR, then the TSP problem is symmetric or asymmetric.


A TSP problem can be expressed as:

The solution traverses graph G = (V, E, C), all nodes at once and back to the starting node, making the path cost of connecting these nodes the lowest.

Second, genetic algorithm

Genetic algorithm (genetic algorithms) is a widely used and efficient method of stochastic search and optimization based on the theory of biological evolution. The main feature is the group search strategy and the information exchange between the individuals in the group, the search does not depend on the gradient information. It was developed in the early 70 by Professor Holland (Holland) of the University of Michigan (Michigan). 1975 Professor Holland published the first monograph on genetic algorithms, "adaptability in natural systems and artificial systems" ("Adaptationin Natural and Artificial Systems"). Genetic algorithm is not designed for the purpose of solving the optimization problem, it is the main framework of evolutionary algorithm, which is the development of artificial intelligence. So far, genetic algorithms are the most widely known algorithms in evolutionary algorithms.

The implementation steps of genetic fire algorithm are as follows (taking the minimum of objective function as an example).
The first step is to initialize the T←0 evolutionary algebra counter, T is the maximal evolutionary algebra, and the randomly generated m individual as the initial group P (t);
The second step: individual evaluation calculates the fitness of each individual in P (t);
The third step: select operation will select operator to effect the group;
The fourth step: cross operation will effect the crossover operator on the group;
The fifth step: mutation operation will be the mutation operator in the group, and through the above operation to get the next generation group P (t + 1);
Sixth step: Terminate condition Judgment t≦t:t←t+1 go to step 2;t>t: Terminate output solution.

Genetic algorithm Application steps:
1 Determine the decision variables and various constraints, that is, the individual's expression x and the solution space of the problem;
2 to establish a quantitative method of mathematical description of the optimization model (maximum or minimum of the objective function);
3) Chromosome coding method;
4) decoding method;
5 Quantitative evaluation method of individual fitness F (x)
6) design of genetic operators;
7 determine the operating parameters.

Three, genetic algorithm solves TSP problem

In fact, I did not want to write this genetic algorithm for TSP, because I have a genetic algorithm for 01 backpack, but some readers suggest that, coupled with the fact that the TSP and 01 knapsack difference is still very large, then write another. For NP problems such as TSP, using heuristic search algorithm is a wise choice, because the accurate calculation is already powerless.

The first thing to do with genetic algorithms is to determine how the dye is coded, it uses different coding methods according to different problem models, there are binary code and integer code and floating point code, in the face of TSP, I definitely use integer encoding, because it is very simple, for each city with an integer to number, for example, there are 48 cities, Use 0 to 47来 to identify each city, and then a path is a chromosome encoding, chromosome length of 48, such as: 0,1,2,3,4...47 is a chromosome, it means that the traveller from the city of No. 0, in turn to visit the 1,2,... The city of No. 47th returns to City No. 0; the second key point of genetic algorithm is the evaluation function, the evaluation function of TSP is very simple, that is, the total length of the path of chromosome encoding expression; Finally, it's very simple, in this model, it's all about 0 to 47 of the 48 numbers, Find the shortest path from it (think of the 48-digit full arrangement, then compare ...). )。 With a clear understanding of these, we can follow the above genetic algorithm framework for programming.

We use the TSP problem still comes from the tsplib data att48, this is a symmetric tsp problem, the city scale is 48, its optimal value is 10628. The distance calculation method is shown in the following figure:

The specific code is as follows: [java]  view plain  copy package noah;      import  java.io.bufferedreader;   import java.io.fileinputstream;   import  java.io.ioexception;   import java.io.inputstreamreader;   Import java.util.Random ;      public class ga {           private int scale;//  Population scale        private int cityNum;  //  number of cities, chromosome length        private int MAX_GEN; //  run algebra        private int[][] distance; //  distance matrix         private int bestT;//  best occurrence Algebra        private  int bestlength; //  Best length        private int[] bestTour;  //  Best Path     &NBsp     //  initial population, parent population, row number indicates population size, one line represents an individual, i.e. chromosome, column represents chromosome gene fragment         private int[][] oldPopulation;       private int[][]  newpopulation;//  new population, descendant population        

Related Article

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.