Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/TSPPopulationDiversityAnalyzer.cs @ 11638

Last change on this file since 11638 was 11208, checked in by bburlacu, 10 years ago

#1772: Merged trunk changes.

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