1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Analysis;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HEAL.Attic;
|
---|
28 | using HeuristicLab.PluginInfrastructure;
|
---|
29 |
|
---|
30 | namespace 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 | [StorableType("B68CC721-AC64-44A7-BFEA-B4F0ABE1402D")]
|
---|
40 | public sealed class TSPPopulationDiversityAnalyzer : PopulationDiversityAnalyzer<Permutation> {
|
---|
41 | [StorableConstructor]
|
---|
42 | private TSPPopulationDiversityAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
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 | }
|
---|