Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/TSPPopulationDiversityAnalyzer.cs @ 10814

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

Updated year of copyrights to 2012 (#1716)

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