Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Routing.TSP/3.3/TSPDistanceMatrixInjectorBase.cs @ 3644

Last change on this file since 3644 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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 System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28
29namespace HeuristicLab.Routing.TSP {
30  /// <summary>
31  /// Base class to inject the calculated distance matrix of the TSP path into the current scope.
32  /// </summary>
33  public abstract class TSPDistanceMatrixInjectorBase : OperatorBase {
34    /// <summary>
35    /// Initializes a new instance of <see cref="TSPDistanceMatrixInjectorBase"/> with three variable infos
36    /// (<c>Cities</c>, <c>Coordinates</c> and <c>DistanceMatrix</c>).
37    /// </summary>
38    public TSPDistanceMatrixInjectorBase()
39      : base() {
40      AddVariableInfo(new VariableInfo("Cities", "Number of cities", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Coordinates", "City coordinates", typeof(DoubleMatrixData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("DistanceMatrix", "Distance matrix containing all distances between cities", typeof(DoubleMatrixData), VariableKind.New));
43    }
44
45    /// <summary>
46    /// Generates a distance matrix with the distances between all cities in the current path.
47    /// </summary>
48    /// <param name="scope">The current scope with the path.</param>
49    /// <returns><c>null</c>.</returns>
50    public override IOperation Apply(IScope scope) {
51      int cities = GetVariableValue<IntData>("Cities", scope, true).Data;
52      double[,] coordinates = GetVariableValue<DoubleMatrixData>("Coordinates", scope, true).Data;
53      double[,] distanceMatrix = new double[cities, cities];
54
55      for (int i = 0; i < cities; i++) {
56        for (int j = 0; j < cities; j++) {
57          distanceMatrix[i, j] = CalculateDistance(coordinates[i, 0],
58                                                   coordinates[i, 1],
59                                                   coordinates[j, 0],
60                                                   coordinates[j, 1]);
61        }
62      }
63      scope.AddVariable(new Variable(scope.TranslateName("DistanceMatrix"), new DoubleMatrixData(distanceMatrix)));
64
65      return null;
66    }
67
68    /// <summary>
69    /// Calculates the distance between two points in the path.
70    /// </summary>
71    /// <param name="x1">The x coordinate of point 1.</param>
72    /// <param name="y1">The y coordinate of point 1.</param>
73    /// <param name="x2">The x coordinate of point 2.</param>
74    /// <param name="y2">The y coordinate of point 2.</param>
75    /// <returns>The calculated distance.</returns>
76    protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);
77  }
78}
Note: See TracBrowser for help on using the repository browser.