Changeset 14427 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers
- Timestamp:
- 11/28/16 17:55:52 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSchemaFrequencyAnalyzer.cs
r13624 r14427 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using System.Threading.Tasks; 27 26 using HeuristicLab.Common; … … 34 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 35 34 36 namespace HeuristicLab.Problems.DataAnalysis.Symbolic .Tracking.Analyzers{35 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 37 36 [Item("SymbolicDataAnalysisSchemaFrequencyAnalyzer", "An analyzer which counts schema frequencies in the population.")] 38 37 [StorableClass] … … 157 156 var generation = Generation.Value; 158 157 159 var population = PopulationGraph. GetByRank(generation).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList();158 var population = PopulationGraph.Vertices.Where(x => x.InDegree == 2 && x.Rank > generation - 1).ToList(); 160 159 var vertices = population.Where(x => x.InDegree == 2).OrderByDescending(x => x.Quality).ToList(); 161 160 ResultCollection resultCollection; … … 182 181 } 183 182 var schemas = SchemaCreator.GenerateSchemas(vertices, MinimumSchemaLength, StrictSchemaMatching).ToList(); 184 var schemaStrings = schemas.Select(x => SubtreeToString(x.Root.GetSubtree(0).GetSubtree(0),StrictSchemaMatching)).ToList();183 var schemaStrings = schemas.Select(x => x.Root.GetSubtree(0).GetSubtree(0).FormatToString(StrictSchemaMatching)).ToList(); 185 184 int[][] matchingIndices; 186 185 if (ExecuteInParallel) { … … 188 187 Parallel.For(0, schemas.Count, new ParallelOptions { MaxDegreeOfParallelism = MaximumDegreeOfParallelism }, i => { 189 188 var schema = schemas[i]; 190 matchingIndices[i] = Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v] .Root, schema.Root)).ToArray();189 matchingIndices[i] = Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v], schema)).ToArray(); 191 190 }); 192 191 } else { 193 matchingIndices = schemas.Select(x => Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v] .Root, x.Root)).ToArray()).ToArray();192 matchingIndices = schemas.Select(x => Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v], x)).ToArray()).ToArray(); 194 193 } 195 194 … … 230 229 if (!schemaStatistics.Any()) return base.Apply(); // shouldn't ever happen 231 230 var columnNames = new[] { "Count", "Avg Quality", "Avg Length", "Avg Genotype Similarity", "Avg Phenotype Similarity", "Avg Population Quality" }; 232 var mostFrequent = new DoubleMatrix(schemaStatistics.Count, schemaStatistics[0].Item2.Length); 233 mostFrequent.SortableView = true; 231 var mostFrequent = new DoubleMatrix(schemaStatistics.Count, schemaStatistics[0].Item2.Length) { 232 SortableView = true 233 }; 234 234 schemaStatistics.Sort((a, b) => { if (a.Item2[0].Equals(b.Item2[0])) return b.Item2[1].CompareTo(a.Item2[1]); return b.Item2[0].CompareTo(a.Item2[0]); }); 235 mostFrequentPerGeneration.Add( new Tuple<string, double[]>(schemaStatistics[0].Item1, new[] { (double)generation }.Concat(schemaStatistics[0].Item2).ToArray()));235 mostFrequentPerGeneration.Add(Tuple.Create(schemaStatistics[0].Item1, new[] { (double)generation }.Concat(schemaStatistics[0].Item2).ToArray())); 236 236 mostFrequent.RowNames = schemaStatistics.Select(x => x.Item1); 237 237 mostFrequent.ColumnNames = columnNames; … … 301 301 for (int j = i + 1; j < indices.Length; ++j) { 302 302 var b = indices[j]; 303 if (double.IsNaN(similarityMatrix[a, b])) similarityMatrix[a, b] = similarityFunction(trees[a], trees[b]); 303 if (double.IsNaN(similarityMatrix[a, b])) 304 similarityMatrix[a, b] = similarityFunction(trees[a], trees[b]); 304 305 agg += similarityMatrix[a, b]; 305 306 } … … 307 308 return agg / count; 308 309 } 309 310 private static string SubtreeToString(ISymbolicExpressionTreeNode node, bool strict = false) {311 StringBuilder strBuilder = new StringBuilder();312 // internal nodes or leaf nodes?313 if (node is AnySubtree)314 return "# ";315 316 if (node.SubtreeCount > 0) {317 strBuilder.Append("(");318 // symbol on same line as '('319 string label = string.Empty;320 if (node is AnyNode)321 label = "=";322 else {323 var name = node.Symbol.Name;324 label = ShortNames.ContainsKey(name) ? ShortNames[name] : name;325 }326 strBuilder.Append(label + " ");327 // each subtree expression on a new line328 // and closing ')' also on new line329 foreach (var subtree in node.Subtrees) {330 strBuilder.Append(SubtreeToString(subtree, strict));331 }332 strBuilder.Append(") ");333 } else {334 // symbol in the same line with as '(' and ')'335 var v = node as VariableTreeNode;336 var c = node as ConstantTreeNode;337 var w = node as AnyNode; // wildcard338 string label = string.Empty;339 if (w != null)340 label = "=";341 else if (v != null)342 label = strict ? string.Format("{0:0.00}_{1}", v.Weight, v.VariableName) : string.Format("{0}", v.VariableName);343 else if (c != null)344 label = strict ? string.Format("{0:0.00}", c.Value) : "C";345 strBuilder.Append(label);346 if (node.Parent != null && node != node.Parent.Subtrees.Last())347 strBuilder.Append(" ");348 }349 return strBuilder.ToString();350 }351 310 } 352 311 }
Note: See TracChangeset
for help on using the changeset viewer.