summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP
diff options
context:
space:
mode:
Diffstat (limited to 'src/CS340.TSP')
-rw-r--r--src/CS340.TSP/City.cs57
-rw-r--r--src/CS340.TSP/Program.cs6
-rw-r--r--src/CS340.TSP/Road.cs26
-rw-r--r--src/CS340.TSP/TSP.cs10
-rw-r--r--src/CS340.TSP/Tour.cs30
5 files changed, 104 insertions, 25 deletions
diff --git a/src/CS340.TSP/City.cs b/src/CS340.TSP/City.cs
new file mode 100644
index 0000000..1990094
--- /dev/null
+++ b/src/CS340.TSP/City.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Graph;
+using Interfaces;
+
+namespace TSP
+{
+
+ public class City : IVertex<Road, double>, IComparable<INode<double>>
+ {
+ public int Id { get; set; }
+ public double Key { get; set; }
+
+ public int Parent { get; set; } = -1;
+ public List<Road> Edges { get; set; } = new List<Road>();
+
+ // indexer for accessing edge with v of index
+ public Road this[int index] { get => Edges.Find(edge => edge.V == index); }
+
+ public City() { }
+
+ public City(int id) =>
+ Id = id;
+
+ public City(int id, List<Road> edges) : this(id) =>
+ Edges = edges;
+
+ public City(City vertex)
+ {
+ Id = vertex.Id;
+ Parent = vertex.Parent;
+ Key = vertex.Key;
+ foreach (Road edge in vertex.Edges)
+ {
+ Road newEdge = new Road(edge.U, edge.V, edge.Weight);
+ Edges.Add(newEdge);
+ }
+ }
+ public City(Vertex<Edge<double>, double> vertex)
+ {
+ Id = vertex.Id;
+ Parent = vertex.Parent;
+ Key = vertex.Key;
+ foreach (Edge<double> edge in vertex.Edges)
+ {
+ Road newEdge = new Road(edge.U, edge.V, edge.Weight);
+ Edges.Add(newEdge);
+ }
+ }
+
+ public int CompareTo(INode<double> node) =>
+ Key.CompareTo(node.Key);
+
+ public override string ToString() => $"{Id} {String.Join(" ", Edges.Select(e => (e.V, e.Weight)))}";
+ }
+} \ No newline at end of file
diff --git a/src/CS340.TSP/Program.cs b/src/CS340.TSP/Program.cs
index 16c041c..618c58a 100644
--- a/src/CS340.TSP/Program.cs
+++ b/src/CS340.TSP/Program.cs
@@ -7,8 +7,8 @@ using Interfaces;
namespace TSP
{
- using Graph = Graph<double>;
- using Tour = Tour<double>;
+
+ using Map = Graph<Vertex<Edge<double>, double>, Edge<double>, double>;
class Program
{
@@ -17,7 +17,7 @@ namespace TSP
{
foreach (string file in Directory.GetFiles("graphs/"))
{
- Graph graph = GraphFile.Read(new FileReader(file));
+ Map graph = GraphFile.Read(new FileReader(file));
// Graph mst = graph.MST(0);
// GraphFile.Print(mst, new ConsoleWriter());
// Console.WriteLine(
diff --git a/src/CS340.TSP/Road.cs b/src/CS340.TSP/Road.cs
new file mode 100644
index 0000000..9711c3c
--- /dev/null
+++ b/src/CS340.TSP/Road.cs
@@ -0,0 +1,26 @@
+using System;
+using Interfaces;
+
+namespace TSP
+{
+ public class Road : IEdge<double>
+ {
+ public int U { get; set; }
+ public int V { get; set; }
+ public double Weight { get; set; }
+
+ public Road() { }
+
+ public Road(int u, int v, double weight)
+ {
+ U = u;
+ V = v;
+ Weight = weight;
+ }
+
+ public int CompareTo(IEdge<double> edge) =>
+ Weight.CompareTo(edge.Weight);
+
+ public override string ToString() => $"{U} {V} {Weight}";
+ }
+}
diff --git a/src/CS340.TSP/TSP.cs b/src/CS340.TSP/TSP.cs
index de7eda8..a26fe6c 100644
--- a/src/CS340.TSP/TSP.cs
+++ b/src/CS340.TSP/TSP.cs
@@ -1,18 +1,20 @@
using System.Diagnostics;
using System.Linq;
+using System.Text.RegularExpressions;
using Graph;
+using Graph.IO;
namespace TSP
{
- using City = Vertex<double>;
- using Tour = Tour<double>;
+
+ using Map = Graph<Vertex<Edge<double>, double>, Edge<double>, double>;
public static class TSP
{
static Tour BestTour = null;
- static Graph<double> Map;
+ static Map Map;
- public static Tour BruteForce(Graph<double> map, int init)
+ public static Tour BruteForce(Map map, int init)
{
Map = map;
Tour intialTour = new Tour(map.Vertices);
diff --git a/src/CS340.TSP/Tour.cs b/src/CS340.TSP/Tour.cs
index 80db19d..e6e5d40 100644
--- a/src/CS340.TSP/Tour.cs
+++ b/src/CS340.TSP/Tour.cs
@@ -5,30 +5,24 @@ using Graph;
namespace TSP
{
-
- public class Tour<T> where T : IComparable<T>
+ using Vertex = Vertex<Edge<double>, double>;
+ public class Tour
{
- public List<Vertex<T>> Cities { get; set; } = new List<Vertex<T>>();
- public T Weight
+ public List<City> Cities { get; set; } = new List<City>();
+ public double Weight
{
- get
- {
- dynamic _weight = default(T);
- Cities.Where(city => city.Parent != -1).ToList()
- .ForEach(city =>
- _weight += city.Edges
- .Where(edge => edge.V == city.Parent)
- .First().Weight);
- return _weight;
- }
+ get => Cities
+ .Where(city => city.Parent != -1)
+ .Sum(city => city.Key);
}
// indexer: get vertex where vertex.Id == index
- public Vertex<T> this[int index] { get => Cities.Find(vertex => vertex.Id == index); }
+ public City this[int index] { get => Cities.Find(vertex => vertex.Id == index); }
- public Tour(List<Vertex<T>> cities) => Cities.AddRange(cities.Select(city => new Vertex<T>(city)));
- public Tour() => Cities = new List<Vertex<T>>();
- public Tour(Tour<T> tour) : this(tour.Cities) { }
+ public Tour(List<Vertex> cities) => Cities.AddRange(cities.Select(city => new City(city)));
+ public Tour(List<City> cities) => Cities.AddRange(cities.Select(city => new City(city)));
+ public Tour() => Cities = new List<City>();
+ public Tour(Tour tour) : this(tour.Cities) { }
public override string ToString() =>