[4154] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 |
|
---|
| 26 | namespace 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,
|
---|
[4619] | 55 | DoubleMatrix coordinates, IParameter distanceMatrix, BoolValue useDistanceMatrix) {
|
---|
[4154] | 56 | double distance = 0.0;
|
---|
| 57 |
|
---|
| 58 | if (useDistanceMatrix.Value) {
|
---|
[4847] | 59 | if (distanceMatrix is IValueParameter<DoubleMatrix>) {
|
---|
| 60 | if ((distanceMatrix as IValueParameter<DoubleMatrix>).Value == null) {
|
---|
| 61 | (distanceMatrix as IValueParameter<DoubleMatrix>).Value = CreateDistanceMatrix(coordinates);
|
---|
[4619] | 62 | }
|
---|
| 63 |
|
---|
[4847] | 64 | distance = (distanceMatrix as IValueParameter<DoubleMatrix>).Value[start, end];
|
---|
[4352] | 65 | } else {
|
---|
| 66 | if (distanceMatrix.ActualValue == null) {
|
---|
| 67 | distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates);
|
---|
| 68 | }
|
---|
[4154] | 69 |
|
---|
[4619] | 70 | distance = (distanceMatrix.ActualValue as DoubleMatrix)[start, end];
|
---|
| 71 | }
|
---|
[4154] | 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 | }
|
---|