Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/VRPUtilities.cs @ 5534

Last change on this file since 5534 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25
26namespace HeuristicLab.Problems.VehicleRouting {
27  public sealed class VRPUtilities {
28    public static double CalculateDistance(int start, int end, DoubleMatrix coordinates) {
29      double distance = 0.0;
30
31      distance =
32          Math.Sqrt(
33            Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) +
34            Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2));
35
36      return distance;
37    }
38
39    private static DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) {
40      DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows);
41
42      for (int i = 0; i < distanceMatrix.Rows; i++) {
43        for (int j = i; j < distanceMatrix.Columns; j++) {
44          double distance = CalculateDistance(i, j, coordinates);
45
46          distanceMatrix[i, j] = distance;
47          distanceMatrix[j, i] = distance;
48        }
49      }
50
51      return distanceMatrix;
52    }
53
54    public static double GetDistance(int start, int end,
55      DoubleMatrix coordinates, IParameter distanceMatrix, BoolValue useDistanceMatrix) {
56      double distance = 0.0;
57
58      if (useDistanceMatrix.Value) {
59        if (distanceMatrix is IValueParameter<DoubleMatrix>) {
60          if ((distanceMatrix as IValueParameter<DoubleMatrix>).Value == null) {
61            (distanceMatrix as IValueParameter<DoubleMatrix>).Value = CreateDistanceMatrix(coordinates);
62          }
63         
64          distance = (distanceMatrix as IValueParameter<DoubleMatrix>).Value[start, end];
65        } else {
66          if (distanceMatrix.ActualValue == null) {
67            distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates);
68          }
69
70          distance = (distanceMatrix.ActualValue as DoubleMatrix)[start, end];
71        }     
72      } else {
73        distance = CalculateDistance(start, end, coordinates);
74      }
75
76      return distance;
77    }
78
79    public static double GetDistance(int start, int end,
80      DoubleMatrix coordinates, DoubleMatrix distanceMatrix, BoolValue useDistanceMatrix) {
81      double distance = 0.0;
82
83      if (useDistanceMatrix.Value) {
84        distance = distanceMatrix[start, end];
85      } else {
86        distance = CalculateDistance(start, end, coordinates);
87      }
88
89      return distance;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.