summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP/Tour.cs
blob: d90ab7e0fef789c2f877e74ea63ce892d493b485 (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.Zip(coordinates, (city, coord) => new City(city, coord)));

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

    }
}