Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/25/18 13:37:03 (6 years ago)
Author:
bburlacu
Message:

#2950: Simplify code in SymbolicDataAnalysisBuildingBlockAnalyzer and fix build error.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisBuildingBlockAnalyzer.cs

    r16255 r16258  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
     24using System.Text;
    2525using HeuristicLab.Analysis;
    2626using HeuristicLab.Common;
     
    4141    private const string SimplifyTreesParameterName = "SimplifyTrees";
    4242
    43     private readonly InfixExpressionFormatter formatter = new InfixExpressionFormatter();
    4443    private Dictionary<int, DataRow> hashToRow = new Dictionary<int, DataRow>();
    4544
     45    #region parameters
    4646    public IValueLookupParameter<IntValue> MinimumSubtreeLengthParameter {
    4747      get { return (IValueLookupParameter<IntValue>)Parameters[MinimumSubtreeLengthParameterName]; }
     
    5151      get { return (IValueLookupParameter<BoolValue>)Parameters[SimplifyTreesParameterName]; }
    5252    }
     53    #endregion
    5354
     55    #region parameter properties
    5456    public IntValue MinimumSubtreeLength {
    5557      get { return MinimumSubtreeLengthParameter.ActualValue; }
     
    5961      get { return SimplifyTreesParameter.ActualValue; }
    6062    }
     63    #endregion
    6164
    6265    public override void InitializeState() {
     
    6568      hashToRow = new Dictionary<int, DataRow>();
    6669    }
    67 
    6870
    6971    [StorableHook(HookType.AfterDeserialization)]
     
    102104      var expressionCounts = new Dictionary<int, int>();
    103105
    104       int totalCount = 0; // total number of subtrees examined
     106      int totalCount = 0; // total number of examined subtrees
     107
     108      // count hashes
    105109      foreach (var tree in SymbolicExpressionTree) {
    106110        var hashNodes = tree.Root.GetSubtree(0).GetSubtree(0).MakeNodes();
     
    114118          ++totalCount;
    115119          var hash = s.CalculatedHashValue;
    116           if (expressions.TryGetValue(hash, out string str)) {
     120          if (expressions.ContainsKey(hash)) {
    117121            expressionCounts[hash]++;
    118           } else {
    119             // set constant and weight values so the tree is formatted nicely by the formatter
    120             var nodes = new HashNode<ISymbolicExpressionTreeNode>[1 + s.Size];
    121             Array.Copy(simplified, i - s.Size, nodes, 0, nodes.Length);
    122             var subtree = nodes.ToSubtree();
     122            continue;
     123          }
    123124
    124             foreach (var node in subtree.IterateNodesPostfix()) {
    125               if (node is ConstantTreeNode constantTreeNode) {
    126                 constantTreeNode.Value = 0;
    127               } else if (node is VariableTreeNode variableTreeNode) {
    128                 variableTreeNode.Weight = 1;
    129               }
    130             }
    131 
    132             expressions[hash] = formatter.Format(subtree);
    133             expressionCounts[hash] = 1;
     125          var sb = new StringBuilder();
     126          for (int j = i - s.Size; j < i; ++j) {
     127            sb.Append(GetLabel(simplified[j].Data)).Append(" ");
    134128          }
     129          sb.Append(GetLabel(simplified[i].Data));
     130          expressions[hash] = sb.ToString();
     131          expressionCounts[hash] = 1;
    135132        }
    136133      }
    137134
    138       var mostCommon = expressionCounts.OrderByDescending(x => x.Value).Take(10).ToList();
    139       var mostCommonLabels = mostCommon.Select(x => expressions[x.Key]).ToList();
    140 
     135      // fill in values for existing rows
    141136      foreach (var t in hashToRow) {
    142137        var hash = t.Key;
    143138        var row = t.Value;
    144139
    145         if (expressionCounts.TryGetValue(hash, out int count)) {
    146           row.Values.Add((double)count / totalCount);
    147         } else {
    148           row.Values.Add(0);
    149         }
     140        expressionCounts.TryGetValue(hash, out int count);
     141        row.Values.Add(count);
    150142      }
    151143
    152144      var nValues = dt.Rows.Any() ? dt.Rows.Max(x => x.Values.Count) : 0;
    153145
    154       for (int i = 0; i < mostCommon.Count; ++i) {
    155         var hash = mostCommon[i].Key;
    156         var count = mostCommon[i].Value;
     146      // check if we have new rows
     147      foreach (var t in expressionCounts.OrderByDescending(x => x.Value).Take(10)) {
     148        var hash = t.Key;
     149        var count = t.Value;
     150        var label = expressions[hash];
    157151
    158152        if (hashToRow.ContainsKey(hash)) {
    159153          continue;
    160154        }
    161         var label = mostCommonLabels[i];
    162155        var row = new DataRow(label) { VisualProperties = { StartIndexZero = true } };
    163         // pad with zeroes
    164         for (int j = 0; j < nValues - 1; ++j) {
    165           row.Values.Add(0);
     156        if (nValues > 0) {
     157          row.Values.AddRange(Enumerable.Repeat<double>(0, nValues - 1)); // pad with zeroes
    166158        }
    167         row.Values.Add((double)count / totalCount);
     159        row.Values.Add(count);
    168160        dt.Rows.Add(row);
    169161        hashToRow[hash] = row;
    170162      }
     163
    171164      return base.Apply();
     165    }
     166
     167    private static string GetLabel(ISymbolicExpressionTreeNode node) {
     168      if (node is ConstantTreeNode constant) {
     169        return "C";
     170      }
     171      if (node is VariableTreeNode variable) {
     172        return variable.VariableName;
     173      }
     174      if (node.Symbol is Addition) {
     175        return "+";
     176      }
     177      if (node.Symbol is Subtraction) {
     178        return "-";
     179      }
     180      if (node.Symbol is Multiplication) {
     181        return "*";
     182      }
     183      if (node.Symbol is Division) {
     184        return "/";
     185      }
     186      return node.Symbol.ToString();
    172187    }
    173188  }
Note: See TracChangeset for help on using the changeset viewer.