Well, today, the mountain climbing algorithm (hill algorithm) written in Java is used to solve the TSP problem.
Hill climbing algorithm is similar to other intelligent algorithms, is a method to solve the maximum value of multi-peak function algorithm, the basic idea of mountain climbing algorithm is that the new solution is not inferior to the current solution is transferred, otherwise does not transfer. The popular explanation is the rabbit mountain climbing example, other blogs on the very meticulous, here no longer repeat.
The algorithm of the mountain climbing algorithm is described as:
-
- STEP1: Initialize. By means of a method, the initial solution is S0, the current solution s = S0, the value of the global optimal solution is BS = INF, the Global optimal solution is BS = S0, the current number of iterations is Count = 0, the maximum number of iterations is maxcount;
- STEP2: Evaluate the current solution. Evaluates the current solution s by Eval () of the evaluation function;
- STEP3: Update the optimal solution. If Eval (s) is superior to (or not inferior to) BS, then the global optimal solution bs = S, the value of the global optimal solution bs = Eval (BS);
- STEP3: Updates the current solution. So that the current solution s = BS, the number of iterations count++;
- STEP4: Termination condition determination. If Count < MaxCount, go to STEP2, no person terminates the program and outputs the result.
The following dry goods:
1 PackageHill_algorithm;2 3 ImportJava.util.Random;4 5 /**6 * @file_name Satsp.java7 * @authorXzl8 * @date August 11, 20189 * @detail simulated annealing to solve travel salesman problemTen */ One A Public classHATSP { - - Public Static Double[] HA () { the - //parameter list - intMaxCount = 10000; - + //Initialize - Double[] xy =data.xy (); + intN =xy.length; A at DoubleBS; - DoubleNowbs; - int[] BS =New int[N]; - int[] S =New int[N]; - - //generating Random initial solutions in for(inti = 0; i < N; i++) { -S[i] = i + 1; to } + for(intk = 0; K < n;k++) { -Random Rand =NewRandom (); the intR =Rand.nextint (N); * inttmp; $TMP =S[r];Panax NotoginsengS[R] =S[k]; -S[K] =tmp; the } +BS =Evaluate.eval (S); A the //into the iterative process + intEffI = 0; - for(intCount = 0; Count < MaxCount; count++) { $ $ //produce a new solution - int[] NewS =New int[N]; - DoubleR =math.random (); the - if(R < 0.33) {WuyiNewS =Sharking.swap (S); the}Else if(R > 0.67) { -NewS =Sharking.insert (S); Wu}Else { -NewS =Sharking.flip (S); About } $Nowbs =Evaluate.eval (NewS); - - //update of the solution - if(Nowbs <BS) { ABS =Nowbs; +effi++; theSystem.out.println ("the" + EffI + "sub-valid iterations appear in the" + Count + "iterations, corresponding to the solution" +BS); -System.arraycopy (NewS, 0, BS, 0, N); $ } theSystem.arraycopy (BS, 0, S, 0, N); the } the the //result Output - Double[] Solution =New Double[N + 2]; inSolution[0] =BS; theSOLUTION[1] =MaxCount; the About for(inti = 2; i < n+2; i++) { theSolution[i] = bs[i-2]; the } the returnsolution; + } -}
To solve the TSP in 31 cities, the solution result is as follows, the total number of iterations is 10,000 times, the effective iteration number is 89 times, the last effective iteration appears in the No. 3637 time. With the current known optimal solution error 0.16%.
Java Edition HA_TSP