示例#1
0
文件: Astar.cs 项目: tayyl/TSP
        public Tuple <int, string> TSP(ICollection <City> VisitedCities, ICollection <Edge> CurrentEdges, ICollection <Edge> FinalEdges)
        {
            int  bestDistance = 0;
            City startingCity = cities.First();
            SortedList <double, List <City> > paths = new SortedList <double, List <City> >(new DuplicateKeyComparer <double>());
            List <City> currentBestDistancePath;

            paths.Add(0, new List <City>()
            {
                startingCity
            });
            do
            {
                currentBestDistancePath = paths.First().Value;
                paths.RemoveAt(0);

                foreach (City city in cities)
                {
                    if (!currentBestDistancePath.Contains(city))
                    {
                        if (neighbourMatrix[currentBestDistancePath.Last().Number, city.Number] != 0)
                        {
                            double F = pathDistance(currentBestDistancePath) + heuristic(startingCity, city);
                            paths.Add(F, new List <City>(currentBestDistancePath)
                            {
                                city
                            });
                            if (CurrentEdges.Count < cities.Count * 30)
                            {
                                App.Current.Dispatcher.BeginInvoke((Action) delegate
                                {
                                    CurrentEdges.Add(new Edge(currentBestDistancePath.Last(), city, neighbourMatrix[currentBestDistancePath.Last().Number, city.Number]));
                                });
                                System.Threading.Thread.Sleep(delay);
                            }
                        }
                    }
                }
                if (paths.First().Value.Count == cities.Count)
                {
                    double      F   = pathDistance(paths.First().Value) + heuristic(paths.First().Value.Last(), startingCity);
                    List <City> tmp = paths.First().Value;

                    paths.RemoveAt(0);
                    paths.Add(F, new List <City>(tmp)
                    {
                        startingCity
                    });
                }
            } while (paths.First().Value.Count != cities.Count + 1);
            currentBestDistancePath = paths.First().Value;
            App.Current.Dispatcher.BeginInvoke((Action) delegate {
                for (int i = 0; i < currentBestDistancePath.Count - 1; i++)
                {
                    FinalEdges.Add(new Edge(currentBestDistancePath[i], currentBestDistancePath[i + 1], neighbourMatrix[currentBestDistancePath[i].Number, currentBestDistancePath[i + 1].Number]));
                    VisitedCities.Add(currentBestDistancePath[i]);
                }
            });
            System.Threading.Thread.Sleep(delay);
            currentBestDistancePath.RemoveAt(cities.Count);
            bestDistance = pathDistance(currentBestDistancePath);
            return(new Tuple <int, string>(bestDistance, CreatePath(FinalEdges)));
        }
示例#2
0
文件: Astar.cs 项目: tayyl/TSP
 double heuristic(City city1, City city2)
 {
     return(Math.Sqrt(Math.Pow((city1.X - city2.X), 2)
                      + Math.Pow((city1.Y - city2.Y), 2)));
 }