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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
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 HeuristicLab.Problems.TravelingSalesman;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Analysis.AlgorithmBehavior {
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// An operator that analyzes the quality of schemas.
|
---|
39 | /// </summary>
|
---|
40 | [Item("SchemaQualityAnalyzer", "An operator that analyzes broken inheritance of building blocks.")]
|
---|
41 | [StorableClass]
|
---|
42 | public sealed class SchemaQualityAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
43 | private const string ResultsParameterName = "Results";
|
---|
44 | private const string PopulationGraphResultParameterName = "PopulationGraph";
|
---|
45 | private const string SchemataParameterName = "Schemata";
|
---|
46 | private const string SchemaQualityMatrixParameterName = "SchemaQualityMatrix";
|
---|
47 | private const string QualitiesParameterName = "Qualities";
|
---|
48 | private const string DistanceMatrixParameterName = "DistanceMatrix";
|
---|
49 | private const string GenerationsParameterName = "Generations";
|
---|
50 |
|
---|
51 | #region IAnalyzer Members
|
---|
52 | public bool EnabledByDefault {
|
---|
53 | get { return true; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Parameter properties
|
---|
58 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
59 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
62 | get { return (ILookupParameter<DistanceMatrix>)Parameters[DistanceMatrixParameterName]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
65 | get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
|
---|
66 | }
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | #region Properties
|
---|
70 | public ResultCollection Results {
|
---|
71 | get { return ResultsParameter.ActualValue; }
|
---|
72 | }
|
---|
73 | public DistanceMatrix DistanceMatrix {
|
---|
74 | get { return DistanceMatrixParameter.ActualValue; }
|
---|
75 | }
|
---|
76 | public HeuristicLab.Analysis.DataTable Qualities {
|
---|
77 | get { return ((HeuristicLab.Analysis.DataTable)ResultsParameter.ActualValue[QualitiesParameterName].Value); }
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | [StorableConstructor]
|
---|
82 | private SchemaQualityAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
83 | private SchemaQualityAnalyzer(SchemaQualityAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
84 | public SchemaQualityAnalyzer()
|
---|
85 | : base() {
|
---|
86 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should are stored."));
|
---|
87 | Parameters.Add(new LookupParameter<DistanceMatrix>(DistanceMatrixParameterName, "The distance matrix of the TSP."));
|
---|
88 | Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "Nr of generations."));
|
---|
89 | }
|
---|
90 |
|
---|
91 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
92 | return new SchemaQualityAnalyzer(this, cloner);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override IOperation Apply() {
|
---|
96 | var graph = (GenealogyGraph<Permutation>)Results[PopulationGraphResultParameterName].Value;
|
---|
97 | var schemata = (ItemArray<IntArray>)Results[SchemataParameterName].Value;
|
---|
98 |
|
---|
99 | var schemataMatrix = new Dictionary<IntArray, int[]>(new HeuristicLab.Analysis.AlgorithmBehavior.SchemaAnalyzer.SchemaEqualityComparer());
|
---|
100 | int qualityPoints = 0;
|
---|
101 | int worseQualityPoints = 0;
|
---|
102 | foreach (var schema in schemata) {
|
---|
103 | if (!schemataMatrix.ContainsKey(schema)) {
|
---|
104 | var categories = new int[5];
|
---|
105 | categories[0] = ExploreGlobalSchemaOccurrence(graph, schema, out qualityPoints, out worseQualityPoints);
|
---|
106 | categories[1] = qualityPoints;
|
---|
107 | categories[2] = worseQualityPoints;
|
---|
108 | categories[3] = (qualityPoints != 0 && worseQualityPoints != 0) ? (int)((qualityPoints / ((double)worseQualityPoints)) * 100.0) : 0;
|
---|
109 | categories[4] = ExploreSchemaGradientOverGenerations(graph, schema);
|
---|
110 | schemataMatrix.Add(schema, categories);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | // order schemas according to their global occurrence count
|
---|
115 | var sortedSchemataMatrix = schemataMatrix.OrderByDescending(x => x.Value[0]).ToDictionary(x => x.Key, x => x.Value);
|
---|
116 |
|
---|
117 | var stringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 6);
|
---|
118 | for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) {
|
---|
119 | var element = sortedSchemataMatrix.ElementAt(i);
|
---|
120 | stringMatrix[i, 0] = new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString();
|
---|
121 | for (int j = 0; j < element.Value.Count(); j++) {
|
---|
122 | stringMatrix[i, j + 1] = element.Value[j].ToString();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | stringMatrix.ColumnNames = new[] { "Schema", "GlobalSchemaOccurrence", "Better than Avg. Schemata", "Worse than Avg. Schemata", "Ratio", "Gradient" };
|
---|
126 |
|
---|
127 | Results.Add(new Result(SchemaQualityMatrixParameterName, stringMatrix));
|
---|
128 |
|
---|
129 | return base.Apply();
|
---|
130 | }
|
---|
131 |
|
---|
132 | private int ExploreSchemaGradientOverGenerations(GenealogyGraph<Permutation> graph, IntArray schema) {
|
---|
133 | int[] occurences = new int[GenerationsParameter.ActualValue.Value + 1];
|
---|
134 | double gradient = 0.0;
|
---|
135 |
|
---|
136 | foreach (var individual in graph.Values) {
|
---|
137 | if (SchemaAnalyzer.IsSubtour(schema, (Permutation)individual.Data)) {
|
---|
138 | occurences[(int)individual.Rank]++;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | for (int i = 0; i < occurences.Length - 1; i++) {
|
---|
143 | int occurence = occurences[i + 1] - occurences[i];
|
---|
144 |
|
---|
145 | if (occurences[i + 1] != 0 && occurences[i] != 0) {
|
---|
146 | if (occurence != 0) {
|
---|
147 | gradient += occurences[i + 1] - occurences[i];
|
---|
148 | } else {
|
---|
149 | gradient += 0.5; //TODO: scale this
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | return (int)Math.Round(gradient);
|
---|
155 | }
|
---|
156 |
|
---|
157 | private int ExploreGlobalSchemaOccurrence(GenealogyGraph<Permutation> graph, IntArray schema, out int qualityPoint, out int worstQualityPoint) {
|
---|
158 | int occurrences = 0;
|
---|
159 | qualityPoint = 0;
|
---|
160 | worstQualityPoint = 0;
|
---|
161 | foreach (var individual in graph.Values) {
|
---|
162 | if (SchemaAnalyzer.IsSubtour(schema, (Permutation)individual.Data)) {
|
---|
163 | occurrences++;
|
---|
164 |
|
---|
165 | Permutation p = (Permutation)individual.Data;
|
---|
166 | double pQuality = TSPDistanceMatrixEvaluator.Apply(DistanceMatrix, p);
|
---|
167 |
|
---|
168 | if (Qualities.Rows["CurrentAverageQuality"].Values[(int)individual.Rank] > pQuality) {
|
---|
169 | qualityPoint += 1;
|
---|
170 | } else {
|
---|
171 | worstQualityPoint += 1;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 | return occurrences;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|