/// <summary> /// Starts the TSP algorithm. /// To stop before all generations are calculated, set <see cref="Halt"/> to true. /// </summary> /// <param name="populationSize">Number of random tours to create before starting the algorithm.</param> /// <param name="maxGenerations">Number of times to perform the crossover operation before stopping.</param> /// <param name="groupSize">Number of tours to examine in each generation. Top 2 are chosen as the parent tours whose children replace the worst 2 tours in the group.</param> /// <param name="mutation">Odds that a child tour will be mutated..</param> /// <param name="iterationForConverge">Iteration for converge...</param> /// <param name="seed">Seed for the random number generator.</param> /// <param name="chanceToUseCloseCity">The odds (out of 100) that a city that is known to be close will be used in any given link.</param> /// <param name="cityList">List of cities in the tour.</param> public void Begin(int populationSize, int maxGenerations, int groupSize, int mutation, int iterationForConverge, int seed, int chanceToUseCloseCity, Cities cityList) { rand = new Random(seed); this.cityList = cityList; population = new Population(); population.CreateRandomPopulation(populationSize, cityList, rand, chanceToUseCloseCity); displayTour(population.BestTour, 0, false); bool foundNewBestTour = false; TspForm tspform = new TspForm(); int ItterForStop = 0; int Waiter = 0; for (generation = 0; generation < maxGenerations; generation++) { Waiter += 1; if (Waiter >= 100) { if (TspForm.iteration == 0 || generation >= 50 && generation <= maxGenerations / 100) { TspForm.iteration = generation; } if (TspForm.iteration != generation) { double stop_work = TspForm.iteration - generation / 1.4; if (stop_work <= 0) { ItterForStop += 1; } } if (ItterForStop == iterationForConverge) { Halt = true;//Break the algorithm if we found the best tour } } if (Halt) { break; // GUI has requested we exit. } foundNewBestTour = makeChildren(groupSize, mutation); if (foundNewBestTour) { displayTour(population.BestTour, generation, false); } } displayTour(population.BestTour, generation, true); }
/// <summary> /// Open the XML file that contains the list of cities. /// </summary> /// <param name="fileName">Name of the XML file.</param> /// <returns>The city list.</returns> /// <exception cref="FileNotFoundException">fileName parameter is invalid.</exception> /// <exception cref="InvalidCastException">XML File is not properly formatted.</exception> public void OpenCityList(string fileName) { DataSet cityDS = new DataSet(); try { TspForm tspform = new TspForm(); if (TspForm.use_xml_or_kml == true) { this.Clear(); cityDS.ReadXml(fileName); DataRowCollection cities = cityDS.Tables[0].Rows; foreach (DataRow city in cities) { this.Add(new City(Convert.ToInt32(city["X"], CultureInfo.CurrentCulture), Convert.ToInt32(city["Y"], CultureInfo.CurrentCulture))); } } else { this.Clear(); XmlDocument xml_doc = new XmlDocument(); xml_doc.Load(fileName); //Get Main nodes from kml XmlNodeList Main_nodes = xml_doc.GetElementsByTagName("Document"); //We go in a loop the path to the tag we need(Document/Placemark/LookAt(longitude and latitude )) foreach (XmlNode First_child in Main_nodes) { foreach (XmlNode Second_child in First_child.ChildNodes) { if (Second_child.Name == "Folder") { foreach (XmlNode Thrid_child in Second_child.ChildNodes) { if (Thrid_child.Name == "Placemark") { foreach (XmlNode Fourth_child in Thrid_child.ChildNodes) { if (Fourth_child.Name == "Point") { foreach (XmlNode Fifth_child in Fourth_child.ChildNodes) { //Get the cordinates like 31.28600940737578,51.50013904238215,0 if (Fifth_child.Name == "coordinates")//X and Y { //Split and get 31.28600940737578 split_cordinates_all = Fifth_child.InnerText.Split(','); //Now we get 31 and split_cordinates_x = split_cordinates_all[0].Split('.'); // 28600940737578 split_cordinates_y = split_cordinates_all[1].Split('.'); //Console.WriteLine(split_cordinates_x[0] + "." + split_cordinates_x[1] + //"," + split_cordinates_y[0] + "." + split_cordinates_y[1]); } } // Add to the city list integer cordinates this.Add(new City(Convert.ToInt32(split_cordinates_x[0], CultureInfo.CurrentCulture), Convert.ToInt32(split_cordinates_y[0], CultureInfo.CurrentCulture))); // Add to the city list fraction cordinates TspForm.fraction_cordinates.Add(new TspForm.Fraction_Cordinates() { fraction_x = split_cordinates_x[1], fraction_y = split_cordinates_y[1] }); } } } } } } } } } finally { cityDS.Dispose(); } }