summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP/Tour.cs
blob: e6e5d40bbebd4aa3a042c9589aee3f9f6841933b (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
using System;
using System.Collections.Generic;
using System.Linq;
using Graph;

namespace TSP
{
    using Vertex = Vertex<Edge<double>, double>;
    public class Tour
    {
        public List<City> Cities { get; set; } = new List<City>();
        public double Weight
        {
            get => Cities
                .Where(city => city.Parent != -1)
                .Sum(city => city.Key);
        }

        // indexer: get vertex where vertex.Id == index 
        public City this[int index] { get => Cities.Find(vertex => vertex.Id == index); }

        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() =>
                $"tour: {String.Join(" -> ", Cities.Select(city => city.Id))}\n" +
                $"distance: {Weight}";

    }
}