1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 |
|
---|
24 | namespace HeuristicLab.Problems.Instances {
|
---|
25 | public enum TSPDistanceMeasure { Direct, Euclidean, RoundedEuclidean, UpperEuclidean, Geo, Manhattan, Maximum, Att };
|
---|
26 |
|
---|
27 | /// <summary>
|
---|
28 | /// Describes instances of the Traveling Salesman Problem (TSP).
|
---|
29 | /// </summary>
|
---|
30 | public class TSPInstance {
|
---|
31 | /// <summary>
|
---|
32 | /// The name of the instance
|
---|
33 | /// </summary>
|
---|
34 | public string Name { get; set; }
|
---|
35 | /// <summary>
|
---|
36 | /// Optional! The description of the instance
|
---|
37 | /// </summary>
|
---|
38 | public string Description { get; set; }
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// The number of cities.
|
---|
42 | /// </summary>
|
---|
43 | public int Dimension { get; set; }
|
---|
44 | /// <summary>
|
---|
45 | /// Specifies the distance measure that is to be used.
|
---|
46 | /// </summary>
|
---|
47 | public TSPDistanceMeasure DistanceMeasure { get; set; }
|
---|
48 | /// <summary>
|
---|
49 | /// Optional! The distances are given in form of a distance matrix.
|
---|
50 | /// </summary>
|
---|
51 | /// <remarks>
|
---|
52 | /// Either this property or <see cref="Coordinates"/> needs to be specified.
|
---|
53 | /// </remarks>
|
---|
54 | public double[,] Distances { get; set; }
|
---|
55 | /// <summary>
|
---|
56 | /// Optional! A a matrix of dimension [N, 2] matrix where each row is one of the cities
|
---|
57 | /// and the colmns represent x and y coordinates respectively.
|
---|
58 | /// </summary>
|
---|
59 | /// <remarks>
|
---|
60 | /// Either this property or <see cref="Distances"/> needs to be specified.
|
---|
61 | ///
|
---|
62 | /// If no distance matrix is given, the distances have to be calculated by the
|
---|
63 | /// specified distance measure. If a distance matrix is given in addtion to the
|
---|
64 | /// coordinates, the distance matrix takes precedence and the coordinates are
|
---|
65 | /// for display only.
|
---|
66 | /// </remarks>
|
---|
67 | public double[,] Coordinates { get; set; }
|
---|
68 |
|
---|
69 | /// <summary>
|
---|
70 | /// Optional! The best-known tour in path-encoding.
|
---|
71 | /// </summary>
|
---|
72 | public int[] BestKnownTour { get; set; }
|
---|
73 | /// <summary>
|
---|
74 | /// Optional! The quality of the best-known tour.
|
---|
75 | /// </summary>
|
---|
76 | public double? BestKnownQuality { get; set; }
|
---|
77 |
|
---|
78 | /// <summary>
|
---|
79 | /// If only the coordinates are given, can calculate the distance matrix.
|
---|
80 | /// </summary>
|
---|
81 | /// <returns>A full distance matrix between all cities.</returns>
|
---|
82 | public double[,] GetDistanceMatrix() {
|
---|
83 | if (Distances != null) return Distances;
|
---|
84 | Distances = new double[Dimension, Dimension];
|
---|
85 | for (int i = 0; i < Dimension - 1; i++)
|
---|
86 | for (int j = i + 1; j < Dimension; j++) {
|
---|
87 | Distances[i, j] = GetDistance(i, j);
|
---|
88 | Distances[j, i] = Distances[i, j];
|
---|
89 | }
|
---|
90 | return Distances;
|
---|
91 | }
|
---|
92 |
|
---|
93 | #region Private Helpers
|
---|
94 | private double GetDistance(int i, int j) {
|
---|
95 | switch (DistanceMeasure) {
|
---|
96 | case TSPDistanceMeasure.Att:
|
---|
97 | return AttDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
|
---|
98 | case TSPDistanceMeasure.Direct:
|
---|
99 | return Distances[i, j];
|
---|
100 | case TSPDistanceMeasure.Euclidean:
|
---|
101 | return EuclideanDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
|
---|
102 | case TSPDistanceMeasure.Geo:
|
---|
103 | return GeoDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
|
---|
104 | case TSPDistanceMeasure.Manhattan:
|
---|
105 | return ManhattanDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
|
---|
106 | case TSPDistanceMeasure.Maximum:
|
---|
107 | return MaximumDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
|
---|
108 | case TSPDistanceMeasure.RoundedEuclidean:
|
---|
109 | return Math.Round(EuclideanDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]));
|
---|
110 | case TSPDistanceMeasure.UpperEuclidean:
|
---|
111 | return Math.Ceiling(EuclideanDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]));
|
---|
112 | default:
|
---|
113 | throw new InvalidOperationException("Distance measure is not known.");
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | private double AttDistance(double x1, double y1, double x2, double y2) {
|
---|
118 | return Math.Ceiling(Math.Sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 10.0));
|
---|
119 | }
|
---|
120 |
|
---|
121 | private double EuclideanDistance(double x1, double y1, double x2, double y2) {
|
---|
122 | return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
|
---|
123 | }
|
---|
124 |
|
---|
125 | private const double PI = 3.141592;
|
---|
126 | private const double RADIUS = 6378.388;
|
---|
127 | private double GeoDistance(double x1, double y1, double x2, double y2) {
|
---|
128 | double latitude1, longitude1, latitude2, longitude2;
|
---|
129 | double q1, q2, q3;
|
---|
130 | double length;
|
---|
131 |
|
---|
132 | latitude1 = ConvertToRadian(x1);
|
---|
133 | longitude1 = ConvertToRadian(y1);
|
---|
134 | latitude2 = ConvertToRadian(x2);
|
---|
135 | longitude2 = ConvertToRadian(y2);
|
---|
136 |
|
---|
137 | q1 = Math.Cos(longitude1 - longitude2);
|
---|
138 | q2 = Math.Cos(latitude1 - latitude2);
|
---|
139 | q3 = Math.Cos(latitude1 + latitude2);
|
---|
140 |
|
---|
141 | length = (int)(RADIUS * Math.Acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0);
|
---|
142 | return (length);
|
---|
143 | }
|
---|
144 |
|
---|
145 | private double ConvertToRadian(double x) {
|
---|
146 | return PI * (Math.Truncate(x) + 5.0 * (x - Math.Truncate(x)) / 3.0) / 180.0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | private double ManhattanDistance(double x1, double y1, double x2, double y2) {
|
---|
150 | return Math.Round(Math.Abs(x1 - x2) + Math.Abs(y1 - y2), MidpointRounding.AwayFromZero);
|
---|
151 | }
|
---|
152 |
|
---|
153 | private double MaximumDistance(double x1, double y1, double x2, double y2) {
|
---|
154 | return Math.Max(Math.Round(Math.Abs(x1 - x2), MidpointRounding.AwayFromZero), Math.Round(Math.Abs(y1 - y2), MidpointRounding.AwayFromZero));
|
---|
155 | }
|
---|
156 | #endregion
|
---|
157 | }
|
---|
158 | }
|
---|