diff options
Diffstat (limited to 'src/CS340.TSP/Tour.cs')
-rw-r--r-- | src/CS340.TSP/Tour.cs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/CS340.TSP/Tour.cs b/src/CS340.TSP/Tour.cs new file mode 100644 index 0000000..456c944 --- /dev/null +++ b/src/CS340.TSP/Tour.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Graph; + +namespace TSP +{ + + public class Tour<T> where T : IComparable<T> + { + public List<Vertex<T>> Cities { get; set; } = new List<Vertex<T>>(); + public T 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; + } + } + + // indexer: get vertex where vertex.Id == index + public Vertex<T> this[int index] { get => Cities.Find(vertex => vertex.Id == index); } + + public Tour(List<Vertex<T>> cities) => Cities = cities; + public Tour() => Cities = new List<Vertex<T>>(); + + public Tour<T> DeepCopy() + { + Tour<T> other = (Tour<T>)this.MemberwiseClone(); + other.Cities = Cities.ConvertAll(city => city.DeepCopy()); + return other; + } + public override string ToString() => + $"tour: {String.Join(" -> ", Cities.Select(city => city.Id))}\n" + + $"distance: {Weight}"; + + } +} |