summaryrefslogtreecommitdiffstats
path: root/src/CS340.TSP/Tour.cs
blob: a4ef10f3ba9b48aa777b685b7d3150a9454b9c45 (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
{
    using Vertex = Vertex<Edge<double>, double>;
    public class Tour
    {
        public List<City> Cities { get; set; } = new List<City>();
        public double Weight
        {
            get
            {
                if (Cities.Count == 0)
                    return double.MaxValue;

                return 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(city => city.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}";

    }
}