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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using System.Data;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
36 | /// <summary>
|
---|
37 | /// An operator for analyzing the diversity of a population of solutions for a Traveling Salesman Problems given in path representation using city coordinates.
|
---|
38 | /// </summary>
|
---|
39 | [Item("TSPPopulationDiversityAnalyzer", "An operator for analyzing the diversity of a population of solutions for a Traveling Salesman Problems given in path representation using city coordinates.")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class TSPPopulationDiversityAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
42 |
|
---|
43 | // TODO:
|
---|
44 | // - iterations sampling
|
---|
45 | // - view
|
---|
46 | // - extract population diversity basic behavior into separate project
|
---|
47 | // - analyze variation of values
|
---|
48 |
|
---|
49 | public const string PermutationKey = "Permutation";
|
---|
50 | public ScopeTreeLookupParameter<Permutation> PermutationParameter {
|
---|
51 | get { return (ScopeTreeLookupParameter<Permutation>)Parameters[PermutationKey]; }
|
---|
52 | }
|
---|
53 | public const string QualityKey = "Quality";
|
---|
54 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
55 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityKey]; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public const string StoreCompleteHistoryKey = "StoreCompleteHistory";
|
---|
59 | public ValueParameter<BoolValue> StoreCompleteHistoryParameter {
|
---|
60 | get { return (ValueParameter<BoolValue>)Parameters[StoreCompleteHistoryKey]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public const string CurrentSimilaritiesKey = "Current Solution Similarities";
|
---|
64 | public const string CurrentAverageSimilarityKey = "Current Average Population Similarity";
|
---|
65 | public const string AverageSimilarityProgressKey = "Average Population Similarity Progress";
|
---|
66 | public const string CurrentAverageMaximumSimilarityKey = "Current Average Maximum Population Similarity";
|
---|
67 | public const string AverageMaximumSimilarityProgressKey = "Average Maximum Population Similarity Progress";
|
---|
68 | public const string PopulationDiversityAnalysisResultsDetailsKey = "Population Diversity Analysis Results Details";
|
---|
69 |
|
---|
70 | public const string ResultsKey = "Results";
|
---|
71 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
72 | get { return (ValueLookupParameter<ResultCollection>)Parameters[ResultsKey]; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public TSPPopulationDiversityAnalyzer()
|
---|
76 | : base() {
|
---|
77 | Parameters.Add(new ScopeTreeLookupParameter<Permutation>(PermutationKey, "The TSP solutions given in path representation from which the best solution should be analyzed."));
|
---|
78 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityKey, "The qualities of the TSP solutions which should be analyzed."));
|
---|
79 | Parameters.Add(new ValueParameter<BoolValue>(StoreCompleteHistoryKey, "Flag that denotes whether the complete history of similarity values shall be stored.", new BoolValue(true)));
|
---|
80 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection in which the population diversity analysis results should be stored."));
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override IOperation Apply() {
|
---|
84 |
|
---|
85 | ItemArray<Permutation> permutations = PermutationParameter.ActualValue;
|
---|
86 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
87 | Permutation[] permutationsArray = permutations.ToArray();
|
---|
88 | DoubleValue[] qualitiesArray = qualities.ToArray();
|
---|
89 | int cities = permutationsArray.Length;
|
---|
90 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
91 |
|
---|
92 | #region sort permutations array
|
---|
93 | for (int i = 0; i < cities; i++) {
|
---|
94 | int minIndex = i;
|
---|
95 | for (int j = i + 1; j < cities; j++) {
|
---|
96 | if (qualitiesArray[j].Value < qualitiesArray[minIndex].Value)
|
---|
97 | minIndex = j;
|
---|
98 | }
|
---|
99 | if (minIndex != i) {
|
---|
100 | Permutation p = permutationsArray[i];
|
---|
101 | permutationsArray[i] = permutationsArray[minIndex];
|
---|
102 | permutationsArray[minIndex] = p;
|
---|
103 | DoubleValue d = qualitiesArray[i];
|
---|
104 | qualitiesArray[i] = qualitiesArray[minIndex];
|
---|
105 | qualitiesArray[minIndex] = d;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | int[][] edges = new int[cities][];
|
---|
111 | for (int i = 0; i < cities; i++)
|
---|
112 | edges[i] = CalculateEdgesVector(permutationsArray[i]);
|
---|
113 |
|
---|
114 | DoubleMatrix similarities = new DoubleMatrix(cities, cities);
|
---|
115 | DoubleArray maxSimilarities = new DoubleArray(cities);
|
---|
116 | double avgSimilarity = 0;
|
---|
117 | int n = 0;
|
---|
118 | for (int i = 0; i < cities; i++) {
|
---|
119 | similarities[i, i] = 1;
|
---|
120 | for (int j = (i + 1); j < cities; j++) {
|
---|
121 | double similarity = CalculateSimilarity(edges[i], edges[j]);
|
---|
122 | avgSimilarity += similarity;
|
---|
123 | n++;
|
---|
124 | similarities[i, j] = similarity;
|
---|
125 | similarities[j, i] = similarity;
|
---|
126 | if (maxSimilarities[i] < similarity)
|
---|
127 | maxSimilarities[i] = similarity;
|
---|
128 | if (maxSimilarities[j] < similarity)
|
---|
129 | maxSimilarities[j] = similarity;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | DoubleValue averageMaximumSimilarity = new DoubleValue(maxSimilarities.Average());
|
---|
133 | DoubleValue averageSimilarity = new DoubleValue(avgSimilarity / n);
|
---|
134 |
|
---|
135 | #region Store current solution similarities
|
---|
136 | if (results.ContainsKey(CurrentAverageSimilarityKey))
|
---|
137 | results[CurrentSimilaritiesKey].Value = similarities;
|
---|
138 | else
|
---|
139 | results.Add(new Result(CurrentSimilaritiesKey, similarities));
|
---|
140 | #endregion
|
---|
141 | #region Store average similarity values
|
---|
142 | if (results.ContainsKey(CurrentAverageSimilarityKey))
|
---|
143 | results[CurrentAverageSimilarityKey].Value = averageSimilarity;
|
---|
144 | else
|
---|
145 | results.Add(new Result(CurrentAverageSimilarityKey, averageSimilarity));
|
---|
146 |
|
---|
147 | if (!results.ContainsKey(AverageSimilarityProgressKey))
|
---|
148 | results.Add(new Result(AverageSimilarityProgressKey, new Analysis.DataTable(AverageSimilarityProgressKey)));
|
---|
149 | Analysis.DataTable averageSimilarityProgressDataTable = (Analysis.DataTable)(results[AverageSimilarityProgressKey].Value);
|
---|
150 | if (averageSimilarityProgressDataTable.Rows.Count == 0)
|
---|
151 | averageSimilarityProgressDataTable.Rows.Add(new Analysis.DataRow(AverageSimilarityProgressKey));
|
---|
152 | averageSimilarityProgressDataTable.Rows[AverageSimilarityProgressKey].Values.Add(averageSimilarity.Value);
|
---|
153 | #endregion
|
---|
154 | #region Store average maximum similarity values
|
---|
155 | if (results.ContainsKey(CurrentAverageMaximumSimilarityKey))
|
---|
156 | results[CurrentAverageMaximumSimilarityKey].Value = averageMaximumSimilarity;
|
---|
157 | else
|
---|
158 | results.Add(new Result(CurrentAverageMaximumSimilarityKey, averageMaximumSimilarity));
|
---|
159 |
|
---|
160 | if (!results.ContainsKey(AverageMaximumSimilarityProgressKey))
|
---|
161 | results.Add(new Result(AverageMaximumSimilarityProgressKey, new Analysis.DataTable(AverageMaximumSimilarityProgressKey)));
|
---|
162 | Analysis.DataTable averageMaximumSimilarityProgressDataTable = (Analysis.DataTable)(results[AverageMaximumSimilarityProgressKey].Value);
|
---|
163 | if (averageMaximumSimilarityProgressDataTable.Rows.Count == 0)
|
---|
164 | averageMaximumSimilarityProgressDataTable.Rows.Add(new Analysis.DataRow(AverageMaximumSimilarityProgressKey));
|
---|
165 | averageMaximumSimilarityProgressDataTable.Rows[AverageMaximumSimilarityProgressKey].Values.Add(averageMaximumSimilarity.Value);
|
---|
166 | #endregion
|
---|
167 | #region Store details
|
---|
168 | TSPPopulationDiversityAnalysisDetails details;
|
---|
169 | if (!results.ContainsKey(PopulationDiversityAnalysisResultsDetailsKey)) {
|
---|
170 | details = new TSPPopulationDiversityAnalysisDetails();
|
---|
171 | results.Add(new Result(PopulationDiversityAnalysisResultsDetailsKey, details));
|
---|
172 | } else {
|
---|
173 | details = (TSPPopulationDiversityAnalysisDetails)(results[PopulationDiversityAnalysisResultsDetailsKey].Value);
|
---|
174 | }
|
---|
175 | details.AverageSimilarities.Add(averageSimilarity);
|
---|
176 | details.AverageMaximumSimilarities.Add(averageMaximumSimilarity);
|
---|
177 | details.Similarities.Add(similarities);
|
---|
178 | details.MaximumSimilarities.Add(maxSimilarities);
|
---|
179 | if (!StoreCompleteHistoryParameter.Value.Value && details.Similarities.Count > 1) {
|
---|
180 | details.Similarities[details.Similarities.Count - 1] = null;
|
---|
181 | details.MaximumSimilarities[details.MaximumSimilarities.Count - 1] = null;
|
---|
182 | }
|
---|
183 | #endregion
|
---|
184 |
|
---|
185 | return base.Apply();
|
---|
186 | }
|
---|
187 |
|
---|
188 | private static int[] CalculateEdgesVector(Permutation permutation) {
|
---|
189 | int cities = permutation.Length;
|
---|
190 | int[] edgesVector = new int[cities];
|
---|
191 | for (int i = 0; i < (cities - 1); i++)
|
---|
192 | edgesVector[permutation[i]] = permutation[i + 1];
|
---|
193 | edgesVector[permutation[cities - 1]] = permutation[0];
|
---|
194 | return edgesVector;
|
---|
195 | }
|
---|
196 |
|
---|
197 | private double CalculateSimilarity(int[] edgesA, int[] edgesB) {
|
---|
198 | if (edgesA.Length != edgesB.Length)
|
---|
199 | throw new InvalidOperationException("ERROR in " + Name + ": Similarity can only be calculated between instances of an equal number of cities");
|
---|
200 | int cities = edgesA.Length;
|
---|
201 | int similarEdges = 0;
|
---|
202 | for (int i = 0; i < edgesA.Length; i++) {
|
---|
203 | if (edgesA[i] == edgesB[i] || edgesA[edgesB[i]] == i)
|
---|
204 | similarEdges++;
|
---|
205 | }
|
---|
206 | return (double)(similarEdges) / cities;
|
---|
207 | }
|
---|
208 |
|
---|
209 | }
|
---|
210 | }
|
---|