diff options
author | Toby Vincent <tobyv13@gmail.com> | 2021-05-02 14:58:01 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2021-05-02 15:02:02 -0500 |
commit | b03ade8a3c2d94c1931d09f8d09b7865826a9743 (patch) | |
tree | 1339db9c7dd814de9ec898bd167d809b9a09c1be /src/CS340.TSP | |
parent | 1bd4f32d974674186900cec0897567419afe0b88 (diff) |
removed CS340 namespace
Signed-off-by: Toby Vincent <tobyv13@gmail.com>
Diffstat (limited to 'src/CS340.TSP')
-rw-r--r-- | src/CS340.TSP/CS340.TSP.csproj | 13 | ||||
-rw-r--r-- | src/CS340.TSP/City.cs | 36 | ||||
-rw-r--r-- | src/CS340.TSP/Coordinates.cs | 38 | ||||
-rw-r--r-- | src/CS340.TSP/TSPSolver.cs | 127 | ||||
-rw-r--r-- | src/CS340.TSP/Tour.cs | 42 |
5 files changed, 0 insertions, 256 deletions
diff --git a/src/CS340.TSP/CS340.TSP.csproj b/src/CS340.TSP/CS340.TSP.csproj deleted file mode 100644 index dae9f9e..0000000 --- a/src/CS340.TSP/CS340.TSP.csproj +++ /dev/null @@ -1,13 +0,0 @@ -<Project Sdk="Microsoft.NET.Sdk"> - - <PropertyGroup> - <TargetFramework>net5.0</TargetFramework> - </PropertyGroup> - - <ItemGroup> - <ProjectReference Include="..\CS340.Graph\CS340.Graph.csproj" /> - <ProjectReference Include="..\CS340.Interfaces\CS340.Interfaces.csproj" /> - <ProjectReference Include="..\CS340.Extensions\CS340.Extensions.csproj" /> - </ItemGroup> - -</Project> diff --git a/src/CS340.TSP/City.cs b/src/CS340.TSP/City.cs deleted file mode 100644 index 4889bdb..0000000 --- a/src/CS340.TSP/City.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using Graph; -using Interfaces; - -namespace TSP -{ - using INode = INode<double>; - using IVertex = IVertex<Edge<double>, double>; - using Road = Edge<double>; - using Vertex = Vertex<Edge<double>, double>; - - public partial class City : Vertex, IVertex, IComparable<INode> - { - - public new double Key { get => this[Parent].Weight; } - - public Coordinate Location { get; set; } - - public City() { } - - public City(int id) => - Id = id; - - public City(int id, List<Road> edges) : this(id) => - Edges = edges; - - public City(IVertex city) : this(city.Id, city.Edges) => - Parent = city.Parent; - - public City(IVertex city, Coordinate location) : this(city) => - Location = location; - - public City(City city) : this(city, city.Location) { } - } -}
\ No newline at end of file diff --git a/src/CS340.TSP/Coordinates.cs b/src/CS340.TSP/Coordinates.cs deleted file mode 100644 index 1e74021..0000000 --- a/src/CS340.TSP/Coordinates.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Collections.Generic; -using System.IO; - -namespace TSP -{ - public struct Coordinate - { - public int X { get; set; } - public int Y { get; set; } - - public Coordinate(int x, int y) - { - X = x; - Y = y; - } - - public Coordinate(string coords) - { - string[] items = coords.Split(','); - X = int.Parse(items[0]); - Y = int.Parse(items[1]); - } - - - - public static List<Coordinate> Parse(string file) - { - List<Coordinate> coordinates = new List<Coordinate>(); - foreach (string coords in File.ReadAllLines(file)) - coordinates.Add(new Coordinate(coords)); - - return coordinates; - } - - - public override string ToString() => $"({X}, {Y})"; - } -} diff --git a/src/CS340.TSP/TSPSolver.cs b/src/CS340.TSP/TSPSolver.cs deleted file mode 100644 index 84d3945..0000000 --- a/src/CS340.TSP/TSPSolver.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using Extensions; -using Graph; -using Graph.IO; - -namespace TSP -{ - - using Map = Graph<Vertex<Edge<double>, double>, Edge<double>, double>; - using Road = Edge<double>; - - public static class TSPSolver - { - public static Tour MST(string file, string coords, int init) - { - Map map = GraphFile.Read(new FileReader(file)).MST(0); - return new Tour(map.Vertices, Coordinate.Parse(coords)); - } - - public static Tour Approximate(string file, string coords, int init) - { - Map map = GraphFile.Read(new FileReader(file)).MST(0); - Tour mstTour = new Tour(map.Vertices, Coordinate.Parse(coords)); - - Tour approximateTour = PreOrder(mstTour[init], mstTour, new Tour()); - - Debug.WriteLine("\nApproximate Best Tour"); - Debug.WriteLine(approximateTour); - - return approximateTour; - - Tour PreOrder(City city, Tour mst, Tour visited) - { - // set parent of current city to previous city - if (visited.Cities.Count > 0) - city.Parent = visited.Cities.Last().Id; - - // add current City to visited - visited.Cities.Add(city); - - // loop through each child, and recursivly traverse - foreach (Road road in city.Edges) - if (mst[road.V].Parent == city.Id) - PreOrder(mst[road.V], mst, visited); - - // on the last node, set init node's parent to the last node - if (visited.Cities.Count == mst.Cities.Count) - visited.Cities.First().Parent = visited.Cities.Last().Id; - - return visited; - } - } - - - public static Tour Solve(string file, string coords, int init) - { - Map map = GraphFile.Read(new FileReader(file)); - Tour bestTour = new Tour(map.Vertices, Coordinate.Parse(coords)); - - double BestWeight = double.MaxValue; - List<int> BestTour = new(); - - BruteForce(map, init, map.Vertices.Select(city => city.Id).ToList(), new List<int>(), 0); - - - int parent = BestTour.Last(); - - foreach (int city in BestTour.Skip(1)) - { - bestTour[city].Parent = parent; - parent = city; - } - - Debug.WriteLine("\nTrue Best Tour"); - Debug.WriteLine(bestTour); - - return bestTour; - - void BruteForce(Map map, int city, List<int> unvisitedInput, List<int> visitedInput, double weight) - { - // create local copy of Tour variables - List<int> unvisited = new(unvisitedInput); - List<int> visited = new(visitedInput); - - // remove current City from tour - int removed = unvisited.RemoveAll(c => c == city); - - //? verify that a single city was removed - Debug.Assert(removed == 1, - $"Failed to remove a city from the tour. \nTour: {unvisited}\nCity: {city}"); - - // set parent of current city to previous city - if (visited.Count > 0) - weight += map[city][visited.Last()].Weight; - - // add current City to visited - visited.Add(city); - - if (weight > BestWeight) - return; - - // loop through each city. Each level of recursion has a one less city in unvisited.Cities - // so each loop is (n - 1)! down to n == 0 as the base case - foreach (int neighbor in unvisited) - BruteForce(map, neighbor, unvisited, visited, weight); - - // if we traversed all cities in the graph - if (unvisited.Count == 0) - { - // add the return home - weight += map[visited.First()][visited.Last()].Weight; - - // set as best if lower weight than previous best - if (weight < BestWeight) - { - BestTour = visited; - BestWeight = weight; - Debug.WriteLine($"New best tour: {String.Join(" -> ", visited)} = {weight}"); - } - } - } - } - } -} diff --git a/src/CS340.TSP/Tour.cs b/src/CS340.TSP/Tour.cs deleted file mode 100644 index 5f530be..0000000 --- a/src/CS340.TSP/Tour.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Graph; - -namespace TSP -{ - using Vertex = Vertex<Edge<double>, double>; - public class Tour - { - public List<City> Cities { get; set; } = new List<City>(); - public double Weight - { - get - { - if (Cities.Count == 0) - return double.MaxValue; - - return Cities.Where(city => city.Parent != -1) - .Sum(city => city.Key); - } - } - - // indexer: get vertex where vertex.Id == index - public City this[int index] { get => Cities.Find(city => city.Id == index); } - - public Tour() => - Cities = new List<City>(); - - public Tour(Tour tour) : this(tour.Cities) { } - - public Tour(List<City> cities) => - Cities.AddRange(cities.Select(city => new City(city))); - - public Tour(List<Vertex> cities, List<Coordinate> coordinates) => - Cities.AddRange(cities.Select(city => new City(city, coordinates[city.Id]))); - - public override string ToString() => - $"{Weight},{String.Join(",", Cities.Select(city => city.Id))}"; - - } -} |