private void ShortPathInsideCluster(Cluster c) { int totalNumberBees = 6; int numberInactive = 1; int numberActive = 3; int numberScout = 2; int maxNumberVisits = 3; int maxNumberCycles = 10; CitiesData ncdc = new CitiesData(citiesData.citiesDistance, citiesData.coordinates, true, c.inputPathTo, c.outputPathFrom, c.cities); Hive hive = new Hive(totalNumberBees, numberInactive, numberActive, numberScout, maxNumberVisits, maxNumberCycles, ncdc); hive.Solve(false); c.path = hive.bestMemoryMatrix; }
public static void Main(string[] args) { //try //{ // Console.WriteLine(""); string filename; Console.WriteLine("Enter filename: "); filename = Console.ReadLine(); double[,] citycoordinates = ReadFileCoordinatesCity(filename); double[,] citymatrix = CoordinatesToDistanceMatrix(citycoordinates); //int [,] citymatrix= ReadFileCityToMatrix("1.txt"); CitiesData citiesData = new CitiesData(citymatrix, citycoordinates); Console.WriteLine("Number of cities = " + citiesData.citiesDistance.GetLength(0)); ClasterFogel cl = new ClasterFogel(5, citiesData); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); cl.Clustering(); Console.WriteLine("\n===================\nCLASTER ALGORITHM:"); Console.WriteLine(cl.ToString()); stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); int totalNumberBees = 6; int numberInactive = 1; int numberActive = 3; int numberScout = 2; int maxNumberVisits = 3; int maxNumberCycles = 10; Hive hive = new Hive(totalNumberBees, numberInactive, numberActive, numberScout, maxNumberVisits, maxNumberCycles, citiesData); bool doProgressBar = false; Stopwatch stopWatch1 = new Stopwatch(); stopWatch1.Start(); hive.Solve(doProgressBar); Console.WriteLine("\n===================\nBee ALGORITHM:"); Console.WriteLine(hive); stopWatch1.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts1 = stopWatch1.Elapsed; // Format and display the TimeSpan value. string elapsedTime1 = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts1.Hours, ts1.Minutes, ts1.Seconds, ts1.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime1); Console.ReadKey(); } // Main()
public void Clustering() { Random rand = new Random(); //bool[] Pool = new bool[citiedata.citiesDistance.GetLength(0)]; List <int> poolCities = new List <int>(); for (int i = 0; i < citiesData.citiesDistance.GetLength(0); i++) { poolCities.Add(i); } List <Cluster> Clusters = new List <Cluster>(); do { double[] centerCluster = new double[2]; int city = poolCities[rand.Next(poolCities.Count)]; centerCluster[0] = citiesData.coordinates[city, 0]; centerCluster[1] = citiesData.coordinates[city, 1]; double[] newcenterCluster = new double[2]; List <int> citiesFromCluster = new List <int>(); do { citiesFromCluster.Clear(); newcenterCluster = centerCluster; foreach (int i in poolCities) { if (Program.Distance(centerCluster[0], centerCluster[1], citiesData.coordinates[i, 0], citiesData.coordinates[i, 1]) <= R) { citiesFromCluster.Add(i); } } centerCluster = CenterMass(citiesFromCluster); } while (!(newcenterCluster[0] == centerCluster[0] && newcenterCluster[1] == centerCluster[1])); foreach (int i in citiesFromCluster) { poolCities.Remove(i); } Clusters.Add(new Cluster() { cities = citiesFromCluster.ToArray(), center = newcenterCluster }); } while (poolCities.Count != 0); //Clustering //Start SearchWay int totalNumberBees = 6; int numberInactive = 1; int numberActive = 3; int numberScout = 2; int maxNumberVisits = 3; int maxNumberCycles = 10; double[,] ncitiesCenters = new double[Clusters.Count, 2]; for (int i = 0; i < Clusters.Count; i++) { ncitiesCenters[i, 0] = Clusters[i].center[0]; ncitiesCenters[i, 1] = Clusters[i].center[1]; } CitiesData ncd = new CitiesData(Program.CoordinatesToDistanceMatrix(ncitiesCenters), ncitiesCenters); Hive hive = new Hive(totalNumberBees, numberInactive, numberActive, numberScout, maxNumberVisits, maxNumberCycles, ncd); bool doProgressBar = false; hive.Solve(doProgressBar); for (int i = 0; i < Clusters.Count - 1; i++) { ShortPathClusters(Clusters[hive.bestMemoryMatrix[i]], Clusters[hive.bestMemoryMatrix[i + 1]]); } ShortPathClusters(Clusters[Clusters.Count - 1], Clusters[0]); foreach (Cluster c in Clusters) { ShortPathInsideCluster(c); } int[] result = ResultPath(Clusters, hive); resultPath = result; this.clasters = Clusters; }