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.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Analysis.AlgorithmBehavior {
|
---|
31 | using System.Collections.Generic;
|
---|
32 | using HeuristicLab.Data;
|
---|
33 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
34 | using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;
|
---|
35 |
|
---|
36 | /// <summary>
|
---|
37 | /// An operator that analyzes building blocks.
|
---|
38 | /// </summary>
|
---|
39 | [Item("BuildingBlockAnalyzer", "An operator that tracks the genealogy of every individual.")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class BuildingBlockAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
42 | private const string GlobalTraceMapParameterName = "GlobalTraceMap";
|
---|
43 | private const string ResultsParameterName = "Results";
|
---|
44 | private const string PopulationGraphResultParameterName = "PopulationGraph";
|
---|
45 | private const string BuildingBlockSchemaMatrixParameterName = "BuildingBlockSchemaMatrix";
|
---|
46 | private const string BuildingBlockPatternMatrixParameterName = "BuildingBlockPatternMatrix";
|
---|
47 |
|
---|
48 | #region IAnalyzer Members
|
---|
49 | public bool EnabledByDefault {
|
---|
50 | get { return true; }
|
---|
51 | }
|
---|
52 | #endregion
|
---|
53 |
|
---|
54 | #region Parameter properties
|
---|
55 | public ILookupParameter<TraceMapType> GlobalTraceMapParameter {
|
---|
56 | get { return (ILookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
59 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
60 | }
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | #region Properties
|
---|
64 | public TraceMapType GlobalTraceMap {
|
---|
65 | get { return GlobalTraceMapParameter.ActualValue; }
|
---|
66 | }
|
---|
67 | public ResultCollection Results {
|
---|
68 | get { return ResultsParameter.ActualValue; }
|
---|
69 | }
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 | [StorableConstructor]
|
---|
73 | private BuildingBlockAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
74 | private BuildingBlockAnalyzer(BuildingBlockAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
75 | public BuildingBlockAnalyzer()
|
---|
76 | : base() {
|
---|
77 | Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, "A global cache containing the whole genealogy."));
|
---|
78 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should are stored."));
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
82 | return new BuildingBlockAnalyzer(this, cloner);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override IOperation Apply() {
|
---|
86 | var graph = (GenealogyGraph<Permutation>)Results[PopulationGraphResultParameterName].Value;
|
---|
87 | var generationZero = graph.Values.Where(x => x.Rank == 0);
|
---|
88 |
|
---|
89 | // extract all subtours for each individual in generation zero
|
---|
90 | var subtours = new Dictionary<Permutation, IList<int[]>>();
|
---|
91 | foreach (var individual in generationZero) {
|
---|
92 | subtours[(Permutation)individual.Data] = ExtractSubtours((Permutation)individual.Data);
|
---|
93 | }
|
---|
94 |
|
---|
95 | // process all schemata
|
---|
96 | var schemataMatrix = new Dictionary<int[], int[]>(new SchemaEqualityComparer());
|
---|
97 | foreach (var entry in subtours) {
|
---|
98 | foreach (var schema in entry.Value) {
|
---|
99 | if (!schemataMatrix.ContainsKey(schema)) {
|
---|
100 | var categories = new int[3];
|
---|
101 | categories[0] = ExploreGlobalSchemaOccurrence(graph, schema);
|
---|
102 | categories[1] = ExploreBrokenInteritanceSchemaOccurrence(graph, schema);
|
---|
103 | categories[2] = ExploreUnbrokenInteritanceSchemaOccurrence(graph, schema);
|
---|
104 | schemataMatrix.Add(schema, categories);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | // order schemas according to their global occurrence count
|
---|
110 | var sortedSchemataMatrix = schemataMatrix.OrderByDescending(x => x.Value[0]).ToDictionary(x => x.Key, x => x.Value);
|
---|
111 |
|
---|
112 | var stringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 4);
|
---|
113 | for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) {
|
---|
114 | var element = sortedSchemataMatrix.ElementAt(i);
|
---|
115 | stringMatrix[i, 0] = new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString();
|
---|
116 | for (int j = 0; j < element.Value.Count(); j++) {
|
---|
117 | stringMatrix[i, j + 1] = element.Value[j].ToString();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | stringMatrix.ColumnNames = new[] { "Schema", "GlobalSchemaOccurrence", "BrokenInteritanceSchemaOccurrence", "UnbrokenInteritanceSchemaOccurrence" };
|
---|
121 |
|
---|
122 | Results.Add(new Result(BuildingBlockSchemaMatrixParameterName, stringMatrix));
|
---|
123 |
|
---|
124 | return base.Apply();
|
---|
125 | }
|
---|
126 |
|
---|
127 | private int ExploreGlobalSchemaOccurrence(GenealogyGraph<Permutation> graph, int[] schema) {
|
---|
128 | int occurrences = 0;
|
---|
129 | foreach (var individual in graph.Values)
|
---|
130 | if (IsSubtour(schema, (Permutation)individual.Data)) occurrences++;
|
---|
131 | return occurrences;
|
---|
132 | }
|
---|
133 |
|
---|
134 | private int ExploreBrokenInteritanceSchemaOccurrence(GenealogyGraph<Permutation> graph, int[] schema) {
|
---|
135 | // TODO
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 |
|
---|
139 | private int ExploreUnbrokenInteritanceSchemaOccurrence(GenealogyGraph<Permutation> graph, int[] schema) {
|
---|
140 | // TODO
|
---|
141 | return 0;
|
---|
142 | }
|
---|
143 |
|
---|
144 | private IList<int[]> ExtractSubtours(Permutation permutation) {
|
---|
145 | var subtours = new List<int[]>();
|
---|
146 | for (int i = 2; i <= permutation.Count() / 2; i++) { // increase schema length from 2 to n/2
|
---|
147 | for (int j = 0; j < permutation.Count(); j++) { // visit all positions in the permutation
|
---|
148 | var schema = new int[i];
|
---|
149 | for (int k = 0; k < i; k++) schema[k] = permutation[(j + k) % permutation.Length]; // copy edge to schema
|
---|
150 | subtours.Add(schema);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | return subtours;
|
---|
154 | }
|
---|
155 |
|
---|
156 | private bool IsSubtour(int[] tour, Permutation permutation) {
|
---|
157 | // determine starting position for subtour match
|
---|
158 | int idx = permutation.Select((x, index) => new { Value = x, Index = index }).Single(x => x.Value == tour[0]).Index;
|
---|
159 | bool isSubtour = true;
|
---|
160 | for (int i = 1; i < tour.Length; i++) {
|
---|
161 | if (tour[i] == permutation[(idx + 1) % permutation.Length]) { // check right side
|
---|
162 | idx = (idx + 1) % permutation.Length;
|
---|
163 | } else if (tour[i] == permutation[(idx - 1 + permutation.Length) % permutation.Length]) { // check left side
|
---|
164 | idx = (idx - 1 + permutation.Length) % permutation.Length;
|
---|
165 | } else {
|
---|
166 | isSubtour = false;
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | return isSubtour;
|
---|
171 | }
|
---|
172 |
|
---|
173 | private class SchemaEqualityComparer : EqualityComparer<int[]> {
|
---|
174 | public override bool Equals(int[] x, int[] y) {
|
---|
175 | if (object.ReferenceEquals(x, y)) return true;
|
---|
176 | if (x == null || y == null || x.Length != y.Length) return false;
|
---|
177 | EqualityComparer<int> comparer = EqualityComparer<int>.Default;
|
---|
178 | bool match = true;
|
---|
179 | for (int i = 0; i < x.Length && match; i++) // check normal tour
|
---|
180 | if (!comparer.Equals(x[i], y[i])) match = false;
|
---|
181 | if (!match) {
|
---|
182 | match = true;
|
---|
183 | for (int i = x.Length - 1; i >= 0 && match; i--) // check inverse tour
|
---|
184 | if (!comparer.Equals(x[i], y[i])) match = false;
|
---|
185 | }
|
---|
186 | return match;
|
---|
187 | }
|
---|
188 |
|
---|
189 | public override int GetHashCode(int[] obj) {
|
---|
190 | return 0; // return the same hash code for each object, otherwise Equals will not be called
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|