Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/28/16 17:55:52 (8 years ago)
Author:
bburlacu
Message:

#1772: Extract common methods (used by the schema creator and the schema frequency analyzer) in static SchemaUtil class. Make AnyNode constructor public.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSchemaFrequencyAnalyzer.cs

    r13624 r14427  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Text;
    2625using System.Threading.Tasks;
    2726using HeuristicLab.Common;
     
    3433using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3534
    36 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
     35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3736  [Item("SymbolicDataAnalysisSchemaFrequencyAnalyzer", "An analyzer which counts schema frequencies in the population.")]
    3837  [StorableClass]
     
    157156      var generation = Generation.Value;
    158157
    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();
    160159      var vertices = population.Where(x => x.InDegree == 2).OrderByDescending(x => x.Quality).ToList();
    161160      ResultCollection resultCollection;
     
    182181      }
    183182      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();
    185184      int[][] matchingIndices;
    186185      if (ExecuteInParallel) {
     
    188187        Parallel.For(0, schemas.Count, new ParallelOptions { MaxDegreeOfParallelism = MaximumDegreeOfParallelism }, i => {
    189188          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();
    191190        });
    192191      } 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();
    194193      }
    195194
     
    230229      if (!schemaStatistics.Any()) return base.Apply(); // shouldn't ever happen
    231230      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      };
    234234      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()));
    236236      mostFrequent.RowNames = schemaStatistics.Select(x => x.Item1);
    237237      mostFrequent.ColumnNames = columnNames;
     
    301301        for (int j = i + 1; j < indices.Length; ++j) {
    302302          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]);
    304305          agg += similarityMatrix[a, b];
    305306        }
     
    307308      return agg / count;
    308309    }
    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 line
    328         // and closing ')' also on new line
    329         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; // wildcard
    338         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     }
    351310  }
    352311}
Note: See TracChangeset for help on using the changeset viewer.