summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP/Tour.cs
blob: 80db19d9463ce1c2691b5b631d0f287222bf0234 (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
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.AddRange(cities.Select(city => new Vertex<T>(city)));
        public Tour() => Cities = new List<Vertex<T>>();
        public Tour(Tour<T> tour) : this(tour.Cities) { }


        public override string ToString() =>
                $"tour: {String.Join(" -> ", Cities.Select(city => city.Id))}\n" +
                $"distance: {Weight}";

    }
}