public Hive(int totalNumberBees, int numberInactive, int numberActive, int numberScout, int maxNumberVisits, int maxNumberCycles, CitiesData citiesData) { random = new Random(0); this.totalNumberBees = totalNumberBees; this.numberInactive = numberInactive; this.numberActive = numberActive; this.numberScout = numberScout; this.maxNumberVisits = maxNumberVisits; this.maxNumberCycles = maxNumberCycles; //this.maxCyclesWithNoImprovement = maxCyclesWithNoImprovement; //this.citiesData = new CitiesData(citiesData.cities.Length); // hive's copy of problem-specific data this.citiesData = citiesData; // reference to CityData // this.probPersuasion & this.probMistake are hard-coded in class definition this.bees = new Bee[totalNumberBees]; this.bestMemoryMatrix = citiesData.GenerateRandomMemoryMatrix(); // alternative initializations are possible this.bestMeasureOfQuality = citiesData.PathLength(this.bestMemoryMatrix); this.indexesOfInactiveBees = new int[numberInactive]; // indexes of bees which are currently inactive for (int i = 0; i < totalNumberBees; ++i) // initialize each bee, and best solution { int currStatus; // depends on i. need status before we can initialize Bee if (i < numberInactive) { currStatus = 0; // inactive indexesOfInactiveBees[i] = i; // curr bee is inactive } else if (i < numberInactive + numberScout) { currStatus = 2; // scout } else { currStatus = 1; // active } int[] randomMemoryMatrix = citiesData.GenerateRandomMemoryMatrix(); double mq = citiesData.PathLength(randomMemoryMatrix); int numberOfVisits = 0; bees[i] = new Bee(currStatus, randomMemoryMatrix, mq, numberOfVisits); // instantiate current bee // does this bee have best solution? if (bees[i].measureOfQuality < bestMeasureOfQuality) // curr bee is better (< because smaller is better) { Array.Copy(bees[i].memoryMatrix, this.bestMemoryMatrix, bees[i].memoryMatrix.Length); this.bestMeasureOfQuality = bees[i].measureOfQuality; } } // each bee } // TravelingSalesmanHive ctor
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; }
public ClasterFogel(int R, CitiesData citiedata) { this.citiesData = citiedata; this.R = R; }