Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Problems.VehicleRouting/3.3/VRPUtilities.cs @ 7481

Last change on this file since 7481 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25
26namespace HeuristicLab.Problems.VehicleRouting {
27  public struct DistanceMatrix {
28    public DoubleMatrix Matrix { get; set; }
29    public BoolValue UseDistanceMatrix { get; set; }
30  }
31
32  public sealed class VRPUtilities {
33    public static double CalculateDistance(int start, int end, DoubleMatrix coordinates) {
34      double distance = 0.0;
35
36      distance =
37          Math.Sqrt(
38            Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) +
39            Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2));
40
41      return distance;
42    }
43
44    private static DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) {
45      DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows);
46
47      for (int i = 0; i < distanceMatrix.Rows; i++) {
48        for (int j = i; j < distanceMatrix.Columns; j++) {
49          double distance = CalculateDistance(i, j, coordinates);
50
51          distanceMatrix[i, j] = distance;
52          distanceMatrix[j, i] = distance;
53        }
54      }
55
56      return distanceMatrix;
57    }
58
59    public static DistanceMatrix GetDistanceMatrix(DoubleMatrix coordinates, IParameter distanceMatrix, BoolValue useDistanceMatrix) {
60      DistanceMatrix result = new DistanceMatrix();
61     
62      if (useDistanceMatrix.Value) {
63        result.UseDistanceMatrix = new BoolValue(true);
64        if (distanceMatrix is IValueParameter<DoubleMatrix>) {
65          if ((distanceMatrix as IValueParameter<DoubleMatrix>).Value == null) {
66            (distanceMatrix as IValueParameter<DoubleMatrix>).Value = CreateDistanceMatrix(coordinates);
67          }
68
69          result.Matrix = (distanceMatrix as IValueParameter<DoubleMatrix>).Value;
70        } else {
71          if (distanceMatrix.ActualValue == null) {
72            distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates);
73          }
74
75          result.Matrix = (distanceMatrix.ActualValue as DoubleMatrix);
76        }
77      } else {
78        result.UseDistanceMatrix = new BoolValue(false);
79        result.Matrix = coordinates;
80      }
81
82      return result;
83    }
84
85    public static double GetDistance(int start, int end, DistanceMatrix distMatrix) {
86      double distance = 0.0;
87
88      if (distMatrix.UseDistanceMatrix.Value)
89        distance = distMatrix.Matrix[start, end];
90      else
91        distance = CalculateDistance(start, end, distMatrix.Matrix);
92
93      return distance;
94    }
95
96    public static double GetDistance(int start, int end,
97      DoubleMatrix coordinates, IParameter distanceMatrix, BoolValue useDistanceMatrix) {
98      double distance = 0.0;
99
100      if (useDistanceMatrix.Value) {
101        if (distanceMatrix is IValueParameter<DoubleMatrix>) {
102          if ((distanceMatrix as IValueParameter<DoubleMatrix>).Value == null) {
103            (distanceMatrix as IValueParameter<DoubleMatrix>).Value = CreateDistanceMatrix(coordinates);
104          }
105         
106          distance = (distanceMatrix as IValueParameter<DoubleMatrix>).Value[start, end];
107        } else {
108          if (distanceMatrix.ActualValue == null) {
109            distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates);
110          }
111
112          distance = (distanceMatrix.ActualValue as DoubleMatrix)[start, end];
113        }     
114      } else {
115        distance = CalculateDistance(start, end, coordinates);
116      }
117
118      return distance;
119    }
120
121    public static double GetDistance(int start, int end,
122      DoubleMatrix coordinates, DoubleMatrix distanceMatrix, BoolValue useDistanceMatrix) {
123      double distance = 0.0;
124
125      if (useDistanceMatrix.Value) {
126        distance = distanceMatrix[start, end];
127      } else {
128        distance = CalculateDistance(start, end, coordinates);
129      }
130
131      return distance;
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.