summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP/Tour.cs
blob: 99c0f5d85ec616cdddcc9f0c095f0f2dfa81f471 (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
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() =>
            Cities = new List<City>();

        public Tour(Tour tour) : this(tour.Cities) { }

        public Tour(List<City> cities) =>
            Cities.AddRange(cities.Select(city => new City(city)));

        public Tour(List<Vertex> cities, List<Coordinate> coordinates) =>
            Cities.AddRange(cities.Select(city => new City(city, coordinates[city.Id])));

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

    }
}