Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/Evaluators/TSPDistanceMatrixEvaluator.cs @ 8331

Last change on this file since 8331 was 8331, checked in by jkarder, 12 years ago

#1331: merged r8086:8330 from trunk

File size: 3.4 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.TravelingSalesman {
31  /// <summary>
32  /// A base class for operators which evaluate TSP solutions given in path representation using a distance matrix.
33  /// </summary>
34  [Item("TSPDistanceMatrixEvaluator", "Evaluate TSP solutions given in path representation using the distance matrix.")]
35  [StorableClass]
36  public sealed class TSPDistanceMatrixEvaluator : TSPEvaluator, ITSPDistanceMatrixEvaluator {
37
38    public ILookupParameter<Permutation> PermutationParameter {
39      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
40    }
41    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
42      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
43    }
44
45    [StorableConstructor]
46    private TSPDistanceMatrixEvaluator(bool deserializing) : base(deserializing) { }
47    private TSPDistanceMatrixEvaluator(TSPDistanceMatrixEvaluator original, Cloner cloner) : base(original, cloner) { }
48    public TSPDistanceMatrixEvaluator()
49      : base() {
50      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));
51      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
52    }
53
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new TSPDistanceMatrixEvaluator(this, cloner);
56    }
57
58    public static double Apply(DistanceMatrix distances, Permutation tour) {
59      if (distances == null || distances.Rows == 0 || distances.Columns == 0
60        || distances.Rows != distances.Columns)
61        throw new InvalidOperationException("TSPDistanceMatrixEvaluator: The distance matrix is empty or not square");
62      if (tour == null) throw new ArgumentNullException("tour", "TSPDistanceMatrixEvaluator: No tour is given.");
63      Permutation p = tour;
64      double length = 0;
65      for (int i = 0; i < p.Length - 1; i++)
66        length += distances[p[i], p[i + 1]];
67      length += distances[p[p.Length - 1], p[0]];
68      return length;
69    }
70
71    public override IOperation Apply() {
72      Permutation p = PermutationParameter.ActualValue;
73      DistanceMatrix dm = DistanceMatrixParameter.ActualValue;
74
75      QualityParameter.ActualValue = new DoubleValue(Apply(dm, p));
76      return base.Apply();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.