Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5555


Ignore:
Timestamp:
02/23/11 17:12:26 (14 years ago)
Author:
gkronber
Message:

#1418 implemented variable frequency analyzer for symbolic data analysis

Location:
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisAnalyser.cs

    r5553 r5555  
    4141
    4242    #region parameter properties
    43     public ILookupParameter<ItemArray<SymbolicExpressionTree>> SymbolicExpressionTreeParameter {
    44       get { return (ILookupParameter<ItemArray<SymbolicExpressionTree>>)Parameters[SymbolicExpressionTreeParameterName]; }
     43    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     44      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
    4545    }
    4646    public ILookupParameter<ResultCollection> ResultCollectionParameter {
     
    4949    #endregion
    5050    #region properties
    51     public ItemArray<SymbolicExpressionTree> SymbolicExpressionTrees {
     51    public ItemArray<ISymbolicExpressionTree> SymbolicExpressionTrees {
    5252      get { return SymbolicExpressionTreeParameter.ActualValue; }
    5353    }
     
    6363    public SymbolicDataAnalysisAnalyzer()
    6464      : base() {
    65       Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees that should be analyzed."));
     65      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees that should be analyzed."));
    6666      Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, "The result collection to store the analysis results."));
    6767    }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/VariableFrequencyAnalyser.cs

    r5553 r5555  
    3838  [Item("VariableFrequencyAnalyser", "Calculates the accumulated frequencies of variable-symbols over all trees in the population.")]
    3939  [StorableClass]
    40   public class VariableFrequencyAnalyser : SymbolicDataAnalysisAnalyzer {
     40  public sealed class VariableFrequencyAnalyser : SymbolicDataAnalysisAnalyzer {
    4141    private const string VariableFrequenciesParameterName = "VariableFrequencies";
    4242    private const string AggregateLaggedVariablesParameterName = "AggregateLaggedVariables";
     
    6060    #endregion
    6161    [StorableConstructor]
    62     protected VariableFrequencyAnalyser(bool deserializing) : base(deserializing) { }
    63     protected VariableFrequencyAnalyser(VariableFrequencyAnalyser original, Cloner cloner)
     62    private VariableFrequencyAnalyser(bool deserializing) : base(deserializing) { }
     63    private VariableFrequencyAnalyser(VariableFrequencyAnalyser original, Cloner cloner)
    6464      : base(original, cloner) {
    6565    }
     
    6969      Parameters.Add(new ValueLookupParameter<BoolValue>(AggregateLaggedVariablesParameterName, "Switch that determines whether all references to a variable should be aggregated regardless of time-offsets. Turn off to analyze all variable references with different time offsets separately.", new BoolValue(true)));
    7070    }
     71    public override IDeepCloneable Clone(Cloner cloner) {
     72      return new VariableFrequencyAnalyser(this, cloner);
     73    }
     74
    7175
    7276    public override IOperation Apply() {
    73       ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
     77      ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
    7478      ResultCollection results = ResultCollection;
    7579
     
    98102        row.Values.Add(0.0);
    99103
    100 
    101       //foreach (var pair in VariableFrequencyAnalyser.CalculateVariableFrequencies(expressions, inputVariables)) {
    102       //  VariableFrequencies.Rows[pair.Key].Values.Add(pair.Value);
    103       //  results["Variable frequencies"].Value = VariableFrequencies;
    104       //}
    105 
    106104      return base.Apply();
    107105    }
     
    114112        var variableReferences = GetVariableReferences(tree, aggregateLaggedVariables);
    115113        foreach (var pair in variableReferences) {
    116           if (variableFrequencies.ContainsKey(pair.Key)) {
    117           }
    118114          totalNumberOfSymbols += pair.Value;
    119115        }
     
    124120    }
    125121
    126     //public static IEnumerable<KeyValuePair<string, double>> CalculateVariableFrequencies(IEnumerable<SymbolicExpressionTree> trees) {
    127     //  Dictionary<string, double> variableReferencesSum = new Dictionary<string, double>();
    128     //  Dictionary<string, double> variableFrequencies = new Dictionary<string, double>();
    129     //  foreach (var inputVariable in inputVariables)
    130     //    variableReferencesSum[inputVariable] = 0.0;
    131     //  foreach (var tree in trees) {
    132     //    var variableReferences = GetVariableReferenceCount(tree, inputVariables);
    133     //    foreach (var pair in variableReferences) {
    134     //      variableReferencesSum[pair.Key] += pair.Value;
    135     //    }
    136     //  }
    137     //  double totalVariableReferences = variableReferencesSum.Values.Sum();
    138     //  foreach (string inputVariable in inputVariables) {
    139     //    double relFreq = variableReferencesSum[inputVariable] / totalVariableReferences;
    140     //    variableFrequencies.Add(inputVariable, relFreq);
    141     //  }
    142     //  return variableFrequencies;
    143     //}
    144 
    145122    private static IEnumerable<KeyValuePair<string, int>> GetVariableReferences(ISymbolicExpressionTree tree, bool aggregateLaggedVariables = true) {
    146123      Dictionary<string, int> references = new Dictionary<string, int>();
    147       int currentTimeLag = 0;
    148       tree.Root.ForEachNodePrefix(n => {
    149         if (n.Symbol is LaggedVariable) {
     124      if (aggregateLaggedVariables) {
     125        tree.Root.ForEachNodePrefix(node => {
     126          if (node.Symbol is Variable) {
     127            var varNode = node as VariableTreeNode;
     128            IncReferenceCount(references, varNode.VariableName);
     129          } else if (node.Symbol is VariableCondition) {
     130            var varCondNode = node as VariableConditionTreeNode;
     131            IncReferenceCount(references, varCondNode.VariableName);
     132          }
     133        });
     134      } else {
     135        GetVariableReferences(references, tree.Root, 0);
     136      }
     137      return references;
     138    }
     139
     140    private static void GetVariableReferences(Dictionary<string, int> references, ISymbolicExpressionTreeNode node, int currentLag) {
     141      if (node.Symbol is LaggedVariable) {
     142        var laggedVarNode = node as LaggedVariableTreeNode;
     143        IncReferenceCount(references, laggedVarNode.VariableName, currentLag + laggedVarNode.Lag);
     144      } else if (node.Symbol is Variable) {
     145        var varNode = node as VariableTreeNode;
     146        IncReferenceCount(references, varNode.VariableName, currentLag);
     147      } else if (node.Symbol is VariableCondition) {
     148        var varCondNode = node as VariableConditionTreeNode;
     149        IncReferenceCount(references, varCondNode.VariableName, currentLag);
     150        GetVariableReferences(references, node.GetSubTree(0), currentLag);
     151        GetVariableReferences(references, node.GetSubTree(1), currentLag);
     152      } else if (node.Symbol is Integral) {
     153        var laggedNode = node as LaggedTreeNode;
     154        for (int l = laggedNode.Lag; l <= 0; l++) {
     155          GetVariableReferences(references, node.GetSubTree(0), currentLag + l);
    150156        }
    151       });
    152       return references;
     157      } else if (node.Symbol is Derivative) {
     158        var laggedNode = node as LaggedTreeNode;
     159        for (int l = laggedNode.Lag; l <= 0; l++) {
     160          GetVariableReferences(references, node.GetSubTree(0), currentLag + l);
     161        }
     162      } else if (node.Symbol is TimeLag) {
     163        var laggedNode = node as LaggedTreeNode;
     164        GetVariableReferences(references, node.GetSubTree(0), currentLag + laggedNode.Lag);
     165      }
     166    }
     167
     168    private static void IncReferenceCount(Dictionary<string, int> references, string variableName, int timeLag = 0) {
     169      string referenceId = variableName +
     170        (timeLag == 0 ? "" : timeLag < 0 ? "(t" + timeLag + ")" : "(t+" + timeLag + ")");
     171      if (references.ContainsKey(referenceId)) {
     172        references[referenceId]++;
     173      } else {
     174        references[referenceId] = 1;
     175      }
    153176    }
    154177  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisAnalyzer.cs

    r5553 r5555  
    2323using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2424using HeuristicLab.Optimization;
     25using HeuristicLab.Parameters;
    2526namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    2627  public interface ISymbolicDataAnalysisAnalyzer : IAnalyzer {
    27     ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { get; }
     28    IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { get; }
    2829    ILookupParameter<ResultCollection> ResultCollectionParameter { get; }
    2930  }
Note: See TracChangeset for help on using the changeset viewer.