Solving the TSP Problem Based on the climbing algorithm (Java)

Source: Internet
Author: User

I. TSP problems

Traveling salesman problem (TSP) is also known as traveling salesman and traveling salesman. It is one of the famous problems in mathematics. Assume that a travel merchant wants to visit n cities, and he must select the path to go. The path is restricted that each city can only visit once and finally return to the original city. The path selection target is that the required path is the minimum value among all paths.

The TSP problem is a combination optimization problem. This problem can prove to be complicated in NPC computing. TSP can be divided into two types: symmetric TSP and asypolicric TSP ). A graph can be used to describe all TSP problems:


V = {C1, C2 ,..., CI ,..., CN}, I = 1, 2 ,..., NIs a collection of all cities.CiIndicatesICities,NIndicates the number of cities;

E = {(R, S): R, S, V}Is a collection of connections between all cities;

C = {CRS: R, S, V}It is the cost measurement of connection between all cities (generally the distance between cities );

IfCRS = CSRThe TSP is symmetric. Otherwise, the TSP is asymmetric.

A tsp problem can be expressed:

Solving a traversal GraphG = (V, E, C ),All nodes return to the Start Node once, which minimizes the path cost for connecting to these nodes.


Ii. HikingAlgorithm

The climbing algorithm is a local preferred method. It uses heuristic methods to improve deep priority search. It uses feedback information to help generate resolution decisions. This algorithm selects an optimal solution from the near solution space of the current solution each time as the current solution until it reaches a local optimal solution. It is a type of artificial intelligence algorithm.

The implementation of the climbing algorithm is very simple. Its main disadvantage is that it will fall into the local optimal solution, rather than searching for the global optimal solution. As shown in: If point C is the current solution, the climbing algorithm will stop searching when it finds the local optimal solution of point A, because it cannot obtain a better solution when it moves slightly in that direction.


Steps for implementing the climbing algorithm:

Iii. Algorithm for Solving TSP

In this Java implementation, we chose to use the data att48 on tsplib, which is a symmetric TSP problem. The city size is 48 and its optimal value is 10628. The distance calculation method is shown below:

DetailsCodeAs follows:


Package Noah; import Java. io. bufferedreader; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. util. random; public class hillclimbing {private int max_gen; // number of iterations private int citynum; // number of cities, code length private int [] [] distance; // distance matrix private int bestt; // The best occurrence algebra private int [] bestgh; // the best path encoding private int bestevaluation; private random; Public hillclimb Ing () {}/*** constructor of GA ** @ Param N * Number of cities * @ Param g * Running algebra ***/Public hillclimbing (int n, int g) {citynum = N; max_gen = g;} // give the compiler an instruction to tell it to silence some internal warnings of the annotated code element @ suppresswarnings ("resource ") /*** initialize the hillclimbing algorithm class * @ Param filename data file name, which stores the coordinate data of all city nodes * @ throws ioexception */private void Init (string filename) throws ioexception {// read data int [] X; int [] Y; string strbuff; bufferedreader data = New bufferedreader (New inputstreamreader (New fileinputstream (filename); distance = new int [citynum] [citynum]; X = new int [citynum]; y = new int [citynum]; for (INT I = 0; I <citynum; I ++) {// read a row of data in the format of 1 6734 1453 strbuff = data. readline (); // string [] strcol = strbuff. split (""); X [I] = integer. valueof (strcol [1]); // X coordinate Y [I] = integer. valueof (strcol [2]); // y coordinate} // calculate the distance matrix // The distance calculation method varies depending on the problem. // Using att48 as the case, it has 48 cities. The distance calculation method is pseudo-Euclidean distance and the optimal value is 10628for (INT I = 0; I <citynum-1; I ++) {distance [I] [I] = 0; // the diagonal line is 0for (Int J = I + 1; j <citynum; j ++) {double rij = math. SQRT (X [I]-X [J]) * (X [I]-X [J]) + (Y [I]-y [J]) * (Y [I]-y [J])/10.0); // round the integer to integer int tij = (INT) math. round (rij); If (tij <rij) {distance [I] [J] = tij + 1; distance [J] [I] = distance [I] [J];} else {distance [I] [J] = tij; distance [J] [I] = distance [I] [J] ;}} distance [citynum-1] [citynum-1] = 0; bestgh = new int [citynum]; bestevaluation = integer. max_value; bestt = 0; random = new random (system. currenttimemillis ();} // initialization code ghhvoid initgroup () {int I, j; bestgh [0] = random. nextint (65535) % citynum; for (I = 1; I <citynum;) // encode the length {bestgh [I] = random. nextint (65535) % citynum; For (j = 0; j <I; j ++) {If (bestgh [I] = bestgh [J]) {break ;}} If (j = I) {I ++ ;}} public int evaluate (INT [] CHR) {int Len = 0; // chromosome, starting city, city 1, City 2... city Nfor (INT I = 1; I <citynum; I ++) {Len + = distance [CHR [I-1] [CHR [I];} // city n, starting city Len + = distance [CHR [citynum-1] [CHR [0]; return Len ;} // climbing algorithm public void pashan (INT [] GH, int t) {int I, temp, TT = 0; int ran1, ran2; int e; // evaluate the new value int [] tempgh = new int [citynum]; bestevaluation = evaluate (GH); // mountain generation tfor (TT = 0; TT <t; TT ++) {for (I = 0; I <citynum; I ++) {tempgh [I] = GH [I];} ran1 = random. nextint (65535) % citynum; ran2 = random. nextint (65535) % citynum; while (ran1 = ran2) {ran2 = random. nextint (65535) % citynum;} // implement the neighbor operation temp = tempgh [ran1]; tempgh [ran1] = tempgh [ran2]; tempgh [ran2] = temp; E = evaluate (tempgh); // evaluate the new value if (E <bestevaluation) {bestt = tt; bestevaluation = E; for (I = 0; I <citynum; I + +) {GH [I] = tempgh [I] ;}}} public void solve () {initgroup (); // initialize the code pashan (bestgh, max_gen); system. out. println ("optimum length occurrence algebra:"); system. out. println (bestt); system. out. println ("optimal length"); system. out. println (bestevaluation); system. out. println ("Optimal Path:"); For (INT I = 0; I <citynum; I ++) {system. out. print (bestgh [I] + ","); if (I % 10 = 0 & I! = 0) {system. out. println () ;}}/ *** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {system. out. println ("start .... "); hillclimbing = new hillclimbing (48,500 0); hillclimbing. init ("C: // data.txt"); hillclimbing. solve ();}}


Running result:

Iv. Summary

Because of its simple structure, the climbing algorithm is hard to solve large-scale problems with multiple constraints and is difficult to obtain better solutions. However, in small-scale NP problems, the solution quality is good. In addition, the climbing algorithm has a simple structure, and in some cases, the overall efficiency is better than that of the astar algorithm.

Note: Some content in this article comes from the network,ProgramAnd the analysis results are my own results. For more information, please note!


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