using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Tspform { public class randomAlg : Algorithm { static int N = 100; //City Count Tsp tspsolver; private double[] x;// x coordinator of city private double[] y;// y coordinator of city private double[,] dij;//distance of cities private int[] TK;//store the city which has visited private int[] route;//the route of visited private double bestL;// the shortest route public randomAlg(int i, double[] x, double[] y, int iStartCityNo, ref int[] bestL, Tsp tspsolver)//initialize the parameter { N = i; this.x = x; this.y = y; this.tspsolver = tspsolver; this.dij = new double[N, N]; this.TK = new int[N]; this.route = new int[N]; this.route = new int[N]; } public override void ResultRoute()// the salesman move { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dij[i, j] = (new Wj()).Getdistance(x[i], y[i], x[j], y[j]); } } int s = 1; Random rand = new Random(); while (s < N) { for (int num = 0; num < N - s; num++) { int j; j = s; if (!TK.Contains(j)) { route[s] = j; break; } } tspsolver.ShowLength(s, 2); s++; } bestL = AllRoute(0); tspsolver.ShowLength(s, bestL); tspsolver.DrawRoute(route);//invoke the draw route } public override double AllRoute(int j)//calculate the final route of the ant { double dis = 0; for (int i = 0; i < N - 1; i++) { dis = dis + dij[route[i], route[i + 1]]; } dis = dis + this.dij[route[N - 1], this.route[0]]; return dis; } } }