From 7db1fdcfadad94aa7aea2acf36ec5fb84b27331f Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Thu, 22 Apr 2021 01:43:55 -0500 Subject: cleaned up weight and key system --- src/CS340.TSP/TSP.cs | 74 ---------------------------------------------------- 1 file changed, 74 deletions(-) delete mode 100644 src/CS340.TSP/TSP.cs (limited to 'src/CS340.TSP/TSP.cs') diff --git a/src/CS340.TSP/TSP.cs b/src/CS340.TSP/TSP.cs deleted file mode 100644 index a26fe6c..0000000 --- a/src/CS340.TSP/TSP.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System.Diagnostics; -using System.Linq; -using System.Text.RegularExpressions; -using Graph; -using Graph.IO; - -namespace TSP -{ - - using Map = Graph, double>, Edge, double>; - - public static class TSP - { - static Tour BestTour = null; - static Map Map; - - public static Tour BruteForce(Map map, int init) - { - Map = map; - Tour intialTour = new Tour(map.Vertices); - GenerateTour(intialTour[init], new Tour(map.Vertices), new Tour()); - - return BestTour; - } - - static void GenerateTour(City city, Tour tour, Tour visited) - { - // create local copy of Tour variables - Tour currTour = new Tour(tour.Cities); - Tour currVisited = new Tour(visited.Cities); - City currCity = new City(city); - - // remove current City from tour - currTour.Cities.RemoveAt(currTour.Cities.FindIndex(city => city.Id == currCity.Id)); - - //? verify that city was removed - Debug.Assert(currVisited.Cities.Find(city => city.Id == currCity.Id) == null, - $"Failed to remove a city from the tour. \nTour: {tour}\nCity: {currCity.Id}"); - - // add current City to visited - if (currVisited.Cities.Count > 0) - { - currCity.Parent = currVisited.Cities.Last().Id; - currCity.Key = Map.Vertices[currCity.Id].Edges - .Where(edge => edge.V == currCity.Parent) - .Last().Weight; - } - - currVisited.Cities.Add(currCity); - - //? verify that city was added - Debug.Assert(currVisited.Cities.Find(city => city.Id == currCity.Id) != null, - $"Failed to add a city to the tour. \nTour: {tour}\nCity: {currCity.Id}"); - - // loop through each city. Each level of recursion has a one less city in currTour.Cities - // leaving the base case - foreach (City neighbor in currTour.Cities) - GenerateTour(neighbor, currTour, currVisited); - - - if (currTour.Cities.Count == 0) - { - currVisited.Cities.First().Parent = currVisited.Cities.Last().Id; - - if (BestTour == null || currVisited.Weight < BestTour.Weight) - BestTour = new Tour(currVisited.Cities); - // Debug.Print($"{currTour}"); - } - - - return; - } - } -} -- cgit v1.2.3-70-g09d2