Changeset 11269


Ignore:
Timestamp:
08/05/14 11:37:29 (7 years ago)
Author:
pfleck
Message:

#2208 Uses DistanceHelper for calculating initial distance matrix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/OrienteeringProblem.cs

    r11261 r11269  
    172172      }
    173173      ParameterizeSolutionCreator();
    174       CalculateDistanceMatrix();
     174      UpdateDistanceMatrix();
    175175    }
    176176    private void CoordinatesValue_ItemChanged(object sender, EventArgs<int, int> e) {
    177       CalculateDistanceMatrix();
     177      UpdateDistanceMatrix();
    178178    }
    179179    private void CoordinatesValue_Reset(object sender, EventArgs e) {
    180180      ParameterizeSolutionCreator();
    181       CalculateDistanceMatrix();
     181      UpdateDistanceMatrix();
    182182    }
    183183    private void StartingPointParameter_ValueChanged(object sender, EventArgs e) {
     
    295295    #endregion
    296296
    297     private void CalculateDistanceMatrix() {
    298       var distances = new double[Coordinates.Rows, Coordinates.Rows];
    299       for (int i = 0; i < Coordinates.Rows; i++) {
    300         for (int j = 0; j < Coordinates.Rows; j++) {
    301           double distanceX = Math.Abs(Coordinates[i, 0] - Coordinates[j, 0]);
    302           double distanceY = Math.Abs(Coordinates[i, 1] - Coordinates[j, 1]);
    303           distances[i, j] = Math.Sqrt(Math.Pow(distanceX, 2) + Math.Pow(distanceY, 2));
     297    private DistanceMatrix CalculateDistanceMatrix(double[,] coordinates) {
     298      var distances = DistanceHelper.GetDistanceMatrix(DistanceMeasure.Euclidean, coordinates, null, coordinates.GetLength(0));
     299
     300      return new DistanceMatrix(distances);
     301    }
     302    private void UpdateDistanceMatrix() {
     303      var coordinates = Coordinates;
     304      int dimension = coordinates.Rows;
     305      var distances = DistanceMatrix;
     306      for (int i = 0; i < dimension - 1; i++) {
     307        for (int j = i + 1; j < dimension; j++) {
     308          double x1 = coordinates[i, 0];
     309          double y1 = coordinates[i, 1];
     310          double x2 = coordinates[j, 0];
     311          double y2 = coordinates[j, 1];
     312          distances[i, j] = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
     313          distances[j, i] = distances[i, j];
    304314        }
    305315      }
    306       DistanceMatrix = new DistanceMatrix(distances);
    307316    }
    308317
    309318    private void InitializeInitialOrienteeringInstance() {
    310       Coordinates = new DoubleMatrix(new double[21, 2] {
     319      var coordinates = new double[21, 2] {
    311320        {  4.60,  7.10 }, {  5.70, 11.40 }, {  4.40, 12.30 }, {  2.80, 14.30 }, {  3.20, 10.30 },
    312321        {  3.50,  9.80 }, {  4.40,  8.40 }, {  7.80, 11.00 }, {  8.80,  9.80 }, {  7.70,  8.20 },
     
    314323        { 14.10, 14.20 }, { 11.20, 13.60 }, {  9.70, 16.40 }, {  9.50, 18.80 }, {  4.70, 16.80 },
    315324        {  5.00,  5.60 }
    316       });
    317       CalculateDistanceMatrix();
     325      };
     326      Coordinates = new DoubleMatrix(coordinates);
     327      DistanceMatrix = CalculateDistanceMatrix(coordinates);
    318328
    319329      StartingPoint.Value = 0;
     
    338348
    339349      Coordinates = new DoubleMatrix(data.Coordinates);
    340       if (data.Distances != null)
    341         DistanceMatrix = new DistanceMatrix(data.Distances);
    342       else
    343         CalculateDistanceMatrix();
    344 
    345       StartingPoint = new IntValue(0);// Depot is interpreted as start and endpoint (default)
    346       TerminusPoint = new IntValue(0);
     350      DistanceMatrix = data.Distances != null
     351        ? new DistanceMatrix(data.Distances)
     352        : CalculateDistanceMatrix(data.Coordinates);
     353
     354      StartingPoint = new IntValue(0); // Depot is interpreted as start point
     355      TerminusPoint = new IntValue(0); // Last city is interpreted als end point
    347356
    348357      MaximumDistance = new DoubleValue(data.Capacity * 2); // capacity is interpreted as max distance
    349358      Scores = new DoubleArray(data.Demands); // demands are interpreted as scores
    350 
    351 
    352359
    353360      OnReset();
Note: See TracChangeset for help on using the changeset viewer.