Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances/3.3/Types/DistanceHelper.cs @ 12012

Last change on this file since 12012 was 12012, checked in by ascheibe, 9 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26
27namespace HeuristicLab.Problems.Instances {
28  public enum DistanceMeasure { Direct, Euclidean, RoundedEuclidean, UpperEuclidean, Geo, Manhattan, Maximum, Att };
29 
30  public static class DistanceHelper {
31    /// <summary>
32    /// If only the coordinates are given, can calculate the distance matrix.
33    /// </summary>
34    /// <returns>A full distance matrix between all cities.</returns>
35    public static double[,] GetDistanceMatrix(DistanceMeasure distanceMeasure, double[,] coordinates, double[,] distances, int dimension) {
36      if (distances != null) return distances;
37     
38      distances = new double[dimension, dimension];
39      for (int i = 0; i < dimension - 1; i++)
40        for (int j = i + 1; j < dimension; j++) {
41          distances[i, j] = GetDistance(i, j, distanceMeasure, coordinates, distances);
42          distances[j, i] = distances[i, j];
43        }
44
45      return distances;
46    }
47
48    #region Private Helpers
49    private static double GetDistance(int i, int j, DistanceMeasure distanceMeasure, double[,] coordinates, double[,] distances) {
50      switch (distanceMeasure) {
51        case DistanceMeasure.Att:
52          return AttDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]);
53        case DistanceMeasure.Direct:
54          return distances[i, j];
55        case DistanceMeasure.Euclidean:
56          return EuclideanDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]);
57        case DistanceMeasure.Geo:
58          return GeoDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]);
59        case DistanceMeasure.Manhattan:
60          return ManhattanDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]);
61        case DistanceMeasure.Maximum:
62          return MaximumDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]);
63        case DistanceMeasure.RoundedEuclidean:
64          return Math.Round(EuclideanDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]));
65        case DistanceMeasure.UpperEuclidean:
66          return Math.Ceiling(EuclideanDistance(coordinates[i, 0], coordinates[i, 1], coordinates[j, 0], coordinates[j, 1]));
67        default:
68          throw new InvalidOperationException("Distance measure is not known.");
69      }
70    }
71
72    private static double AttDistance(double x1, double y1, double x2, double y2) {
73      return Math.Ceiling(Math.Sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 10.0));
74    }
75
76    private static double EuclideanDistance(double x1, double y1, double x2, double y2) {
77      return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
78    }
79
80    private const double PI = 3.141592;
81    private const double RADIUS = 6378.388;
82    private static double GeoDistance(double x1, double y1, double x2, double y2) {
83      double latitude1, longitude1, latitude2, longitude2;
84      double q1, q2, q3;
85      double length;
86
87      latitude1 = ConvertToRadian(x1);
88      longitude1 = ConvertToRadian(y1);
89      latitude2 = ConvertToRadian(x2);
90      longitude2 = ConvertToRadian(y2);
91
92      q1 = Math.Cos(longitude1 - longitude2);
93      q2 = Math.Cos(latitude1 - latitude2);
94      q3 = Math.Cos(latitude1 + latitude2);
95
96      length = (int)(RADIUS * Math.Acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0);
97      return (length);
98    }
99
100    private static double ConvertToRadian(double x) {
101      return PI * (Math.Truncate(x) + 5.0 * (x - Math.Truncate(x)) / 3.0) / 180.0;
102    }
103
104    private static double ManhattanDistance(double x1, double y1, double x2, double y2) {
105      return Math.Round(Math.Abs(x1 - x2) + Math.Abs(y1 - y2), MidpointRounding.AwayFromZero);
106    }
107
108    private static double MaximumDistance(double x1, double y1, double x2, double y2) {
109      return Math.Max(Math.Round(Math.Abs(x1 - x2), MidpointRounding.AwayFromZero), Math.Round(Math.Abs(y1 - y2), MidpointRounding.AwayFromZero));
110    }
111    #endregion
112  }
113}
Note: See TracBrowser for help on using the repository browser.