Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstance.cs @ 7466

Last change on this file since 7466 was 7466, checked in by abeham, 12 years ago

#1614

  • included TSLIB's ATSP and CVRP problems as well
File size: 4.6 KB
Line 
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
22using System;
23namespace HeuristicLab.Problems.Instances.TSPLIB {
24  internal class TSPLIBTSPInstance : ITSPInstance {
25    public string Name { get; set; }
26    public string Description { get; set; }
27    public int Dimension { get; set; }
28    public TSPDistanceMeasure DistanceMeasure { get; set; }
29    public double[,] Distances { get; set; }
30    public double[,] Coordinates { get; set; }
31    public int[] BestKnownTour { get; set; }
32    public double? BestKnownQuality { get; set; }
33
34    public double[,] GetDistanceMatrix() {
35      if (Distances != null) return Distances;
36      Distances = new double[Dimension, Dimension];
37      for (int i = 0; i < Dimension - 1; i++)
38        for (int j = i + 1; j < Dimension; j++) {
39          Distances[i, j] = GetDistance(i, j);
40          Distances[j, i] = Distances[i, j];
41        }
42      return Distances;
43    }
44
45    private double GetDistance(int i, int j) {
46      switch (DistanceMeasure) {
47        case TSPDistanceMeasure.Att:
48          return AttDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
49        case TSPDistanceMeasure.Direct:
50          return Distances[i, j];
51        case TSPDistanceMeasure.Euclidean:
52          return EuclideanDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
53        case TSPDistanceMeasure.Geo:
54          return GeoDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
55        case TSPDistanceMeasure.Manhattan:
56          return ManhattanDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
57        case TSPDistanceMeasure.Maximum:
58          return MaximumDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]);
59        case TSPDistanceMeasure.RoundedEuclidean:
60          return Math.Round(EuclideanDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]));
61        case TSPDistanceMeasure.UpperEuclidean:
62          return Math.Ceiling(EuclideanDistance(Coordinates[i, 0], Coordinates[i, 1], Coordinates[j, 0], Coordinates[j, 1]));
63        default:
64          throw new InvalidOperationException("Distance measure is not known.");
65      }
66    }
67
68    private double AttDistance(double x1, double y1, double x2, double y2) {
69      return Math.Ceiling(Math.Sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 10.0));
70    }
71
72    private double EuclideanDistance(double x1, double y1, double x2, double y2) {
73      return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
74    }
75
76    private const double PI = 3.141592;
77    private const double RADIUS = 6378.388;
78    private double GeoDistance(double x1, double y1, double x2, double y2) {
79      double latitude1, longitude1, latitude2, longitude2;
80      double q1, q2, q3;
81      double length;
82
83      latitude1 = ConvertToRadian(x1);
84      longitude1 = ConvertToRadian(y1);
85      latitude2 = ConvertToRadian(x2);
86      longitude2 = ConvertToRadian(y2);
87
88      q1 = Math.Cos(longitude1 - longitude2);
89      q2 = Math.Cos(latitude1 - latitude2);
90      q3 = Math.Cos(latitude1 + latitude2);
91
92      length = (int)(RADIUS * Math.Acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0);
93      return (length);
94    }
95
96    private double ConvertToRadian(double x) {
97      return PI * (Math.Truncate(x) + 5.0 * (x - Math.Truncate(x)) / 3.0) / 180.0;
98    }
99
100    private double ManhattanDistance(double x1, double y1, double x2, double y2) {
101      return Math.Round(Math.Abs(x1 - x2) + Math.Abs(y1 - y2), MidpointRounding.AwayFromZero);
102    }
103
104    private double MaximumDistance(double x1, double y1, double x2, double y2) {
105      return Math.Max(Math.Round(Math.Abs(x1 - x2), MidpointRounding.AwayFromZero), Math.Round(Math.Abs(y1 - y2), MidpointRounding.AwayFromZero));
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.