Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/TSPPopulationDiversityAnalyzer.cs @ 4703

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

Reviewed diversity analysis branch and merged it into the trunk (#1188)

File size: 3.0 KB
Line 
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
22using HeuristicLab.Analysis;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Problems.TravelingSalesman {
28  /// <summary>
29  /// An operator for analyzing the diversity of solutions of Traveling Salesman Problems given in path representation.
30  /// </summary>
31  [Item("TSPPopulationDiversityAnalyzer", "An operator for analyzing the diversity of solutions of Traveling Salesman Problems given in path representation.")]
32  [StorableClass]
33  public sealed class TSPPopulationDiversityAnalyzer : PopulationDiversityAnalyzer<Permutation> {
34    [StorableConstructor]
35    private TSPPopulationDiversityAnalyzer(bool deserializing) : base(deserializing) { }
36    public TSPPopulationDiversityAnalyzer() : base() { }
37
38    protected override double[,] CalculateSimilarities(Permutation[] solutions) {
39      int count = solutions.Length;
40      double[,] similarities = new double[count, count];
41      int[][] edges = new int[count][];
42
43      for (int i = 0; i < count; i++)
44        edges[i] = CalculateEdgesVector(solutions[i]);
45
46      for (int i = 0; i < count; i++) {
47        similarities[i, i] = 1;
48        for (int j = i + 1; j < count; j++) {
49          similarities[i, j] = CalculateSimilarity(edges[i], edges[j]);
50          similarities[j, i] = similarities[i, j];
51        }
52      }
53      return similarities;
54    }
55
56    private int[] CalculateEdgesVector(Permutation permutation) {
57      // transform path representation into adjacency representation
58      int[] edgesVector = new int[permutation.Length];
59      for (int i = 0; i < permutation.Length - 1; i++)
60        edgesVector[permutation[i]] = permutation[i + 1];
61      edgesVector[permutation[permutation.Length - 1]] = permutation[0];
62      return edgesVector;
63    }
64
65    private double CalculateSimilarity(int[] edgesA, int[] edgesB) {
66      // calculate relative number of identical edges
67      int identicalEdges = 0;
68      for (int i = 0; i < edgesA.Length; i++) {
69        if ((edgesA[i] == edgesB[i]) || (edgesA[edgesB[i]] == i))
70          identicalEdges++;
71      }
72      return ((double)identicalEdges) / edgesA.Length;
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.