using System; using System.Collections.Generic; using System.Linq; using Graph; namespace TSP { public class Tour where T : IComparable { public List> Cities { get; set; } = new List>(); 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 this[int index] { get => Cities.Find(vertex => vertex.Id == index); } public Tour(List> cities) => Cities = cities; public Tour() => Cities = new List>(); public Tour DeepCopy() { Tour other = (Tour)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}"; } }