summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP/Tour.cs
blob: 456c9446caeee3a1f717f49614f5adfba106c785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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}";

    }
}