Changeset 8216
- Timestamp:
- 07/04/12 18:00:00 (12 years ago)
- Location:
- branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/Analyzers/BuildingBlockAnalyzer.cs
r8191 r8216 29 29 30 30 namespace HeuristicLab.Analysis.AlgorithmBehavior { 31 using System; 31 32 using System.Collections.Generic; 32 33 using HeuristicLab.Data; … … 37 38 /// An operator that analyzes building blocks. 38 39 /// </summary> 39 [Item("BuildingBlockAnalyzer", "An operator that tracks the genealogy of every individual.")]40 [Item("BuildingBlockAnalyzer", "An operator that analyzes building blocks.")] 40 41 [StorableClass] 41 42 public sealed class BuildingBlockAnalyzer : SingleSuccessorOperator, IAnalyzer { … … 44 45 private const string PopulationGraphResultParameterName = "PopulationGraph"; 45 46 private const string BuildingBlockSchemaMatrixParameterName = "BuildingBlockSchemaMatrix"; 47 private const string BrokenInteritanceSchemaOccurrenceName = "BrokenInteritanceSchemaOccurrenceMatrix"; 48 private const string UnbrokenInteritanceSchemaOccurrenceName = "UnbrokenInteritanceSchemaOccurrence"; 46 49 private const string BuildingBlockPatternMatrixParameterName = "BuildingBlockPatternMatrix"; 47 50 … … 100 103 var categories = new int[3]; 101 104 categories[0] = ExploreGlobalSchemaOccurrence(graph, schema); 102 categories[1] = ExploreBrokenInteritanceSchemaOccurrence(graph, schema);103 categories[2] = ExploreUnbrokenInteritanceSchemaOccurrence(graph, schema);105 categories[1] = -1; 106 categories[2] = -1; 104 107 schemataMatrix.Add(schema, categories); 105 108 } … … 109 112 // order schemas according to their global occurrence count 110 113 var sortedSchemataMatrix = schemataMatrix.OrderByDescending(x => x.Value[0]).ToDictionary(x => x.Key, x => x.Value); 111 112 var s tringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 4);114 IList<string> rowNames = new List<string>(); 115 var schemataStringMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, 3); 113 116 for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) { 114 117 var element = sortedSchemataMatrix.ElementAt(i); 115 stringMatrix[i, 0] = new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString();118 rowNames.Add(new Permutation(PermutationTypes.RelativeUndirected, element.Key).ToString()); 116 119 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)); 120 schemataStringMatrix[i, j] = element.Value[j].ToString(); 121 } 122 } 123 schemataStringMatrix.RowNames = rowNames; 124 schemataStringMatrix.ColumnNames = new[] { "GlobalSchemaOccurrence", "<unused>", "<unused>" }; 125 Results.Add(new Result(BuildingBlockSchemaMatrixParameterName, schemataStringMatrix)); 126 127 var brokenInteritanceSchemaOccurrenceMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, generationZero.Count()); 128 IList<string> brokenRowNames = new List<string>(); 129 IList<string> brokenColumnNames = new List<string>(); 130 for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) { 131 var entry = sortedSchemataMatrix.ElementAt(i); 132 brokenRowNames.Add(new Permutation(PermutationTypes.RelativeUndirected, entry.Key).ToString()); 133 for (int j = 0; j < generationZero.Count(); j++) { 134 var individual = generationZero.ElementAt(j); 135 if (i == 0) brokenColumnNames.Add(((Permutation)individual.Data).ToString()); 136 brokenInteritanceSchemaOccurrenceMatrix[i, j] = CalculateBrokenInheritanceOccurrences(entry.Key, individual).ToString(); 137 } 138 } 139 brokenInteritanceSchemaOccurrenceMatrix.RowNames = brokenRowNames; 140 brokenInteritanceSchemaOccurrenceMatrix.ColumnNames = brokenColumnNames; 141 Results.Add(new Result(BrokenInteritanceSchemaOccurrenceName, brokenInteritanceSchemaOccurrenceMatrix)); 142 143 var unbrokenInteritanceSchemaOccurrenceMatrix = new StringMatrix(sortedSchemataMatrix.Keys.Count, generationZero.Count()); 144 IList<string> unbrokenRowNames = new List<string>(); 145 IList<string> unbrokenColumnNames = new List<string>(); 146 for (int i = 0; i < sortedSchemataMatrix.Keys.Count; i++) { 147 var entry = sortedSchemataMatrix.ElementAt(i); 148 unbrokenRowNames.Add(new Permutation(PermutationTypes.RelativeUndirected, entry.Key).ToString()); 149 for (int j = 0; j < generationZero.Count(); j++) { 150 var individual = generationZero.ElementAt(j); 151 if (i == 0) unbrokenColumnNames.Add(((Permutation)individual.Data).ToString()); 152 unbrokenInteritanceSchemaOccurrenceMatrix[i, j] = CalculateUnbrokenInheritanceOccurrences(entry.Key, individual).ToString(); 153 } 154 } 155 unbrokenInteritanceSchemaOccurrenceMatrix.RowNames = unbrokenRowNames; 156 unbrokenInteritanceSchemaOccurrenceMatrix.ColumnNames = brokenColumnNames; 157 Results.Add(new Result(UnbrokenInteritanceSchemaOccurrenceName, unbrokenInteritanceSchemaOccurrenceMatrix)); 123 158 124 159 return base.Apply(); 160 } 161 162 private int CalculateBrokenInheritanceOccurrences(int[] schema, GenealogyGraphNode individual) { 163 // TODO 164 return -1; 165 } 166 167 private int CalculateUnbrokenInheritanceOccurrences(int[] schema, GenealogyGraphNode individual) { 168 var filteredDescendants = individual.Descendants().Where(x => IsMatch(schema, ((Permutation)x.Data).ToArray())); 169 return filteredDescendants.Any() ? (int)filteredDescendants.Max(x => x.Rank) : 0; 125 170 } 126 171 … … 130 175 if (IsSubtour(schema, (Permutation)individual.Data)) occurrences++; 131 176 return occurrences; 132 }133 134 private int ExploreBrokenInteritanceSchemaOccurrence(GenealogyGraph<Permutation> graph, int[] schema) {135 // TODO136 return 0;137 }138 139 private int ExploreUnbrokenInteritanceSchemaOccurrence(GenealogyGraph<Permutation> graph, int[] schema) {140 // TODO141 return 0;142 177 } 143 178 … … 171 206 } 172 207 208 // attention: brute force matching 209 // should be replaced with KMP, BM or RK 210 private bool IsMatch(int[] pattern, int[] tour) { 211 bool match = false; 212 for (int i = 0; i <= tour.Length - pattern.Length && !match; i++) { 213 for (int j = 0; j < pattern.Length && (match = pattern[j] == tour[i + j] || pattern[j] == -1); j++) ; 214 } 215 return match; 216 } 217 218 private IList<int[]> GenerateWildcardSchemata(int[] schema) { 219 string formatString = "{0:d" + schema.Length + "}"; 220 var combs = new List<bool[]>(); 221 string comb = string.Empty; 222 223 // create all desired wildcard combinations 224 int i = 0; 225 int wildcardCount = 0; // first wildcard combination contains no wildcards 226 while (wildcardCount < schema.Length) { 227 if (wildcardCount > schema.Length / 8 && wildcardCount < schema.Length / 2) 228 combs.Add(comb.Select(x => x == '1').ToArray()); // '1' becomes true which indicates a wildcard 229 wildcardCount = (comb = string.Format(formatString, Convert.ToString(++i, 2))).Count(x => x == '1'); 230 } 231 232 // create all desired wildcard instances 233 var wildcardSchemata = new List<int[]>(); 234 foreach (var c in combs) { 235 wildcardSchemata.Add(schema.Select((x, index) => c[index] ? -1 : x).ToArray()); // -1 is a wildcard in the schema 236 } 237 238 return wildcardSchemata; 239 } 240 241 173 242 private class SchemaEqualityComparer : EqualityComparer<int[]> { 174 243 public override bool Equals(int[] x, int[] y) { -
branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/Analyzers/GenealogyAnalyzer.cs
r8161 r8216 202 202 foreach (var parent in parents) { 203 203 if (GlobalTraceMap.ContainsKey(parent)) { 204 // handle cycle204 // handle mutation 205 205 double quality = Evaluate(parent); 206 206 var node = new GenealogyGraphNode(parent) { Quality = quality, Label = label, Rank = generation - 0.5 }; -
branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior/3.3/HeuristicLab.Analysis.AlgorithmBehavior-3.3.csproj
r8191 r8216 75 75 <ItemGroup> 76 76 <Compile Include="Analyzers\BuildingBlockAnalyzer.cs" /> 77 <Compile Include="Analyzers\BrokenInteritanceSchemaAnalyzer.cs" /> 78 <Compile Include="Analyzers\SchemaAnalyzer.cs" /> 77 79 <Compile Include="Analyzers\GenealogyAnalyzer.cs" /> 78 80 <Compile Include="GenealogyGraph.cs" />
Note: See TracChangeset
for help on using the changeset viewer.