示例#1
0
        public void AnnealingTest1()
        {
            file = root + "\\bays29.xml";

            XDocument          tspFile    = XDocument.Load(file);
            AdjacencyMatrix    testMatrix = new AdjacencyMatrix(tspFile);
            SimulatedAnnealing test       = new SimulatedAnnealing(testMatrix);

            test.SetTemperature(10, (float)0.997, 1, 5000);
            for (int i = 0; i < 1000; i++)
            {
                float result = test.Calculate();
                if (result > 3500)  // best result world known is 2020
                {
                    Assert.Fail();
                }
            }
        }
示例#2
0
        private static void Main(string[] args)
        {
            Options options = null;

            Parser.Default.ParseArguments <Options>(args)
            .WithParsed(res => options = res)
            .WithNotParsed(err => Environment.Exit(1));

            var stopWatch = Stopwatch.StartNew();

            if (!File.Exists(options.FileName))
            {
                Console.WriteLine($"File {options.FileName} doesn't exist.");
                return;
            }

            Console.Write($"Start parsing file: {options.FileName} ");

            City city;

            try
            {
                using (var reader = new StreamReader(options.FileName))
                {
                    city = (City) new XmlSerializer(typeof(City)).Deserialize(reader);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(
                    $"Unable to parse osm file: {e.Message}. See about downloading here: https://github.com/bruce-willis/City-Roads/blob/develop/docs/download.md");
                return;
            }

            Console.WriteLine($"Elapsed time: {stopWatch.Elapsed}");

            TimeHelper.MeasureTime(() => SvgHelper.GenerateSvg(city, options), "generationg svg file");

            if (options.GenerateNodesList)
            {
                TimeHelper.MeasureTime(() => CsvHelper.WriteNodesInfo(options.OutputDirectory), "creating csv with nodes' information");
            }

            if (options.GenerateAdjacencyList)
            {
                TimeHelper.MeasureTime(() => CsvHelper.WriteAdjacencyList(options.OutputDirectory), "creating csv with adjacency list");
            }

            if (options.GenerateAdjacencyMatrix)
            {
                TimeHelper.MeasureTime(() => CsvHelper.WriteAdjacencyMatrix(options.OutputDirectory), "creating csv with adjacency matrix");
            }

            DistanceHelper.AddNodes(city);

            if (options.FindShortestPathes)
            {
                DistanceHelper.CompareAlgorithms(options.OutputDirectory);
            }

            if (options.SolveSalesmanProblem)
            {
                Directory.CreateDirectory(Path.Combine(options.OutputDirectory, "Salesman"));
                TimeHelper.MeasureTime(() => CommonSalesman.Distances.GetType(),
                                       "calculating distances and pathes between destinations");
                TimeHelper.MeasureTime(() => NearestNeighbour.Calculate(options.OutputDirectory),
                                       "solving travelling salesman problem using nearest neighbour");
                TimeHelper.MeasureTime(() => NearestNeighbour.Calculate(options.OutputDirectory, withRandom: true),
                                       "solving travelling salesman problem using nearest neighbour and random");
                //int i = 0;
                //double d1, d2;
                //do
                //{
                //    d1 = NearestNeighbour.Calculate(city, options.OutputDirectory);
                //    d2 = NearestNeighbour.CalculateWithRandom(city, options.OutputDirectory);
                //    Console.WriteLine(i++);
                //} while (d1 <= d2);
                //Console.WriteLine(d1);
                //Console.WriteLine(d2);
                TimeHelper.MeasureTime(() => SimulatedAnnealing.Calculate(10, 0.00001, options.OutputDirectory),
                                       "solving tsp using simulated annealing");
            }


            Console.WriteLine($"\nJob done! Now it's time for tea. Total time elapsed: {stopWatch.Elapsed}");
            Console.WriteLine(new Random().Next(0, 2) == 1
                ? "Лучший в СПбГУ - Факультет ПМ-ПУ"
                : "Ответ на главный вопрос жизни, вселенной и всего такого - 42");
        }
示例#3
0
        private void btnBest_Click(object sender, RoutedEventArgs e)
        {
            ClearLabels();
            ClearClusters();
            CalculateDistancesAndCreateAreas();
            ClearLines();

            List <int> bestOrder = new List <int>();
            Dictionary <Cluster, double>      lastDistances = new Dictionary <Cluster, double>();
            Dictionary <Cluster, List <int> > bestOrders    = new Dictionary <Cluster, List <int> >();

            double lastDistance = double.MaxValue;

            for (int i = 0; i < NrOfIterations; i++)
            {
                if (NrOfSalesmans <= 1)
                {
                    List <int> order = annealingForOne.Calculate();
                    if (annealingForOne.CurrentDistance < lastDistance)
                    {
                        bestOrder = order;
                    }
                }
                else
                {
                    foreach (Cluster clust in Clusters)
                    {
                        if (!lastDistances.ContainsKey(clust))
                        {
                            lastDistances.Add(clust, double.MaxValue);
                        }

                        if (!bestOrders.ContainsKey(clust))
                        {
                            bestOrders.Add(clust, new List <int>());
                        }

                        List <int> order = clust.Annealing.Calculate();
                        if (clust.Annealing.CurrentDistance < lastDistances[clust])
                        {
                            bestOrders[clust] = order;
                        }
                    }
                }
            }

            if (NrOfSalesmans <= 1)
            {
                DrawLines(bestOrder);
                txtBlockDistance.Text = String.Format("{0}", annealingForOne.CurrentDistance);
                txtBlockMaxTime.Text  = String.Format("{0}", annealingForOne.CurrentDistance);
            }
            else
            {
                double summedDistance = 0;
                foreach (KeyValuePair <Cluster, List <int> > key in bestOrders)
                {
                    DrawLines(key.Value, key.Key);
                    summedDistance += key.Key.Annealing.CurrentDistance;
                }
                txtBlockDistance.Text = String.Format("{0}", summedDistance);
                txtBlockMaxTime.Text  = String.Format("{0}", Clusters.Max(x => x.Annealing.CurrentDistance));
            }
        }