Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7872


Ignore:
Timestamp:
05/22/12 17:38:36 (13 years ago)
Author:
svonolfe
Message:

Moved distance measure to helper class (#1782)

Location:
trunk/sources
Files:
3 added
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBCVRPInstanceProvider.cs

    r7618 r7872  
    4646      switch (parser.EdgeWeightType) {
    4747        case TSPLIBEdgeWeightTypes.ATT:
    48           instance.DistanceMeasure = CVRPDistanceMeasure.Att; break;
     48          instance.DistanceMeasure = DistanceMeasure.Att; break;
    4949        case TSPLIBEdgeWeightTypes.CEIL_2D:
    50           instance.DistanceMeasure = CVRPDistanceMeasure.UpperEuclidean; break;
     50          instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
    5151        case TSPLIBEdgeWeightTypes.EUC_2D:
    5252        case TSPLIBEdgeWeightTypes.EUC_3D:
    53           instance.DistanceMeasure = CVRPDistanceMeasure.RoundedEuclidean; break;
     53          instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
    5454        case TSPLIBEdgeWeightTypes.EXPLICIT:
    55           instance.DistanceMeasure = CVRPDistanceMeasure.Direct; break;
     55          instance.DistanceMeasure = DistanceMeasure.Direct; break;
    5656        case TSPLIBEdgeWeightTypes.GEO:
    57           instance.DistanceMeasure = CVRPDistanceMeasure.Geo; break;
     57          instance.DistanceMeasure = DistanceMeasure.Geo; break;
    5858        case TSPLIBEdgeWeightTypes.MAN_2D:
    5959        case TSPLIBEdgeWeightTypes.MAN_3D:
    60           instance.DistanceMeasure = CVRPDistanceMeasure.Manhattan; break;
     60          instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
    6161        case TSPLIBEdgeWeightTypes.MAX_2D:
    6262        case TSPLIBEdgeWeightTypes.MAX_3D:
    63           instance.DistanceMeasure = CVRPDistanceMeasure.Maximum; break;
     63          instance.DistanceMeasure = DistanceMeasure.Maximum; break;
    6464        default:
    6565          throw new InvalidDataException("The given edge weight is not supported by HeuristicLab.");
  • trunk/sources/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBTSPInstanceProvider.cs

    r7618 r7872  
    4646      switch (parser.EdgeWeightType) {
    4747        case TSPLIBEdgeWeightTypes.ATT:
    48           instance.DistanceMeasure = TSPDistanceMeasure.Att; break;
     48          instance.DistanceMeasure = DistanceMeasure.Att; break;
    4949        case TSPLIBEdgeWeightTypes.CEIL_2D:
    50           instance.DistanceMeasure = TSPDistanceMeasure.UpperEuclidean; break;
     50          instance.DistanceMeasure = DistanceMeasure.UpperEuclidean; break;
    5151        case TSPLIBEdgeWeightTypes.EUC_2D:
    52           instance.DistanceMeasure = TSPDistanceMeasure.RoundedEuclidean; break;
     52          instance.DistanceMeasure = DistanceMeasure.RoundedEuclidean; break;
    5353        case TSPLIBEdgeWeightTypes.EUC_3D:
    5454          throw new InvalidDataException("3D coordinates are not supported.");
    5555        case TSPLIBEdgeWeightTypes.EXPLICIT:
    56           instance.DistanceMeasure = TSPDistanceMeasure.Direct; break;
     56          instance.DistanceMeasure = DistanceMeasure.Direct; break;
    5757        case TSPLIBEdgeWeightTypes.GEO:
    58           instance.DistanceMeasure = TSPDistanceMeasure.Geo; break;
     58          instance.DistanceMeasure = DistanceMeasure.Geo; break;
    5959        case TSPLIBEdgeWeightTypes.MAN_2D:
    60           instance.DistanceMeasure = TSPDistanceMeasure.Manhattan; break;
     60          instance.DistanceMeasure = DistanceMeasure.Manhattan; break;
    6161        case TSPLIBEdgeWeightTypes.MAN_3D:
    6262          throw new InvalidDataException("3D coordinates are not supported.");
    6363        case TSPLIBEdgeWeightTypes.MAX_2D:
    64           instance.DistanceMeasure = TSPDistanceMeasure.Maximum; break;
     64          instance.DistanceMeasure = DistanceMeasure.Maximum; break;
    6565        case TSPLIBEdgeWeightTypes.MAX_3D:
    6666          throw new InvalidDataException("3D coordinates are not supported.");
  • trunk/sources/HeuristicLab.Problems.Instances/3.3/HeuristicLab.Problems.Instances-3.3.csproj

    r7849 r7872  
    117117    <Compile Include="Types\CTAPData.cs" />
    118118    <Compile Include="IDataDescriptor.cs" />
    119     <Compile Include="Types\CVRPData.cs" />
     119    <Compile Include="Types\DistanceHelper.cs" />
     120    <Compile Include="Types\VRP\CVRPData.cs" />
    120121    <Compile Include="Types\GQAPData.cs" />
     122    <Compile Include="Types\VRP\IVRPData.cs" />
    121123    <Compile Include="Types\QAPData.cs" />
    122124    <Compile Include="Types\TSPData.cs" />
  • trunk/sources/HeuristicLab.Problems.Instances/3.3/Types/TSPData.cs

    r7548 r7872  
    2323
    2424namespace HeuristicLab.Problems.Instances {
    25   public enum TSPDistanceMeasure { Direct, Euclidean, RoundedEuclidean, UpperEuclidean, Geo, Manhattan, Maximum, Att };
    26 
    2725  /// <summary>
    2826  /// Describes instances of the Traveling Salesman Problem (TSP).
     
    4543    /// Specifies the distance measure that is to be used.
    4644    /// </summary>
    47     public TSPDistanceMeasure DistanceMeasure { get; set; }
     45    public DistanceMeasure DistanceMeasure { get; set; }
    4846    /// <summary>
    4947    /// Optional! The distances are given in form of a distance matrix.
     
    8179    /// <returns>A full distance matrix between all cities.</returns>
    8280    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;
     81      return DistanceHelper.GetDistanceMatrix(DistanceMeasure, Coordinates, Distances, Dimension);
    9182    }
    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
    15783  }
    15884}
  • trunk/sources/HeuristicLab.Problems.Instances/3.3/Types/VRP/CVRPData.cs

    r7870 r7872  
    2222
    2323namespace HeuristicLab.Problems.Instances {
    24   public enum CVRPDistanceMeasure { Direct, Euclidean, RoundedEuclidean, UpperEuclidean, Geo, Manhattan, Maximum, Att };
    25 
    2624  /// <summary>
    2725  /// Describes instances of the Capacitated Vehicle Routing Problem (CVRP).
    2826  /// </summary>
    29   public class CVRPData {
     27  public class CVRPData: IVRPData {
    3028    /// <summary>
    3129    /// The name of the instance
     
    4543    ///the coordinates if no <see cref="Distances"/> is given.
    4644    /// </summary>
    47     public CVRPDistanceMeasure DistanceMeasure { get; set; }
     45    public DistanceMeasure DistanceMeasure { get; set; }
    4846    /// <summary>
    4947    /// Optional! The maximum number of vehicles that can be used.
     
    9088    /// </summary>
    9189    public double? BestKnownQuality { get; set; }
     90
     91    /// <summary>
     92    /// If only the coordinates are given, can calculate the distance matrix.
     93    /// </summary>
     94    /// <returns>A full distance matrix between all cities.</returns>
     95    public double[,] GetDistanceMatrix() {
     96      return DistanceHelper.GetDistanceMatrix(DistanceMeasure, Coordinates, Distances, Dimension);
     97    }
    9298  }
    9399}
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r7658 r7872  
    347347      if (data.Coordinates == null && data.Distances == null)
    348348        throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
    349       if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == TSPDistanceMeasure.Att
    350         || data.DistanceMeasure == TSPDistanceMeasure.Manhattan
    351         || data.DistanceMeasure == TSPDistanceMeasure.Maximum
    352         || data.DistanceMeasure == TSPDistanceMeasure.UpperEuclidean))
     349      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
     350        || data.DistanceMeasure == DistanceMeasure.Manhattan
     351        || data.DistanceMeasure == DistanceMeasure.Maximum
     352        || data.DistanceMeasure == DistanceMeasure.UpperEuclidean))
    353353        throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
    354354      if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
     
    363363
    364364      TSPEvaluator evaluator;
    365       if (data.DistanceMeasure == TSPDistanceMeasure.Att
    366         || data.DistanceMeasure == TSPDistanceMeasure.Manhattan
    367         || data.DistanceMeasure == TSPDistanceMeasure.Maximum
    368         || data.DistanceMeasure == TSPDistanceMeasure.UpperEuclidean) {
     365      if (data.DistanceMeasure == DistanceMeasure.Att
     366        || data.DistanceMeasure == DistanceMeasure.Manhattan
     367        || data.DistanceMeasure == DistanceMeasure.Maximum
     368        || data.DistanceMeasure == DistanceMeasure.UpperEuclidean) {
    369369        evaluator = new TSPDistanceMatrixEvaluator();
    370370        UseDistanceMatrix = new BoolValue(true);
    371371        DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
    372       } else if (data.DistanceMeasure == TSPDistanceMeasure.Direct && data.Distances != null) {
     372      } else if (data.DistanceMeasure == DistanceMeasure.Direct && data.Distances != null) {
    373373        evaluator = new TSPDistanceMatrixEvaluator();
    374374        UseDistanceMatrix = new BoolValue(true);
     
    378378        UseDistanceMatrix = new BoolValue(data.Dimension <= DistanceMatrixSizeLimit);
    379379        switch (data.DistanceMeasure) {
    380           case TSPDistanceMeasure.Euclidean:
     380          case DistanceMeasure.Euclidean:
    381381            evaluator = new TSPEuclideanPathEvaluator();
    382382            break;
    383           case TSPDistanceMeasure.RoundedEuclidean:
     383          case DistanceMeasure.RoundedEuclidean:
    384384            evaluator = new TSPRoundedEuclideanPathEvaluator();
    385385            break;
    386           case TSPDistanceMeasure.Geo:
     386          case DistanceMeasure.Geo:
    387387            evaluator = new TSPGeoPathEvaluator();
    388388            break;
Note: See TracChangeset for help on using the changeset viewer.