- Timestamp:
- 02/23/11 17:12:26 (14 years ago)
- 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 41 41 42 42 #region parameter properties 43 public I LookupParameter<ItemArray<SymbolicExpressionTree>> SymbolicExpressionTreeParameter {44 get { return (I LookupParameter<ItemArray<SymbolicExpressionTree>>)Parameters[SymbolicExpressionTreeParameterName]; }43 public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 44 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 45 45 } 46 46 public ILookupParameter<ResultCollection> ResultCollectionParameter { … … 49 49 #endregion 50 50 #region properties 51 public ItemArray< SymbolicExpressionTree> SymbolicExpressionTrees {51 public ItemArray<ISymbolicExpressionTree> SymbolicExpressionTrees { 52 52 get { return SymbolicExpressionTreeParameter.ActualValue; } 53 53 } … … 63 63 public SymbolicDataAnalysisAnalyzer() 64 64 : 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.")); 66 66 Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, "The result collection to store the analysis results.")); 67 67 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/VariableFrequencyAnalyser.cs
r5553 r5555 38 38 [Item("VariableFrequencyAnalyser", "Calculates the accumulated frequencies of variable-symbols over all trees in the population.")] 39 39 [StorableClass] 40 public class VariableFrequencyAnalyser : SymbolicDataAnalysisAnalyzer {40 public sealed class VariableFrequencyAnalyser : SymbolicDataAnalysisAnalyzer { 41 41 private const string VariableFrequenciesParameterName = "VariableFrequencies"; 42 42 private const string AggregateLaggedVariablesParameterName = "AggregateLaggedVariables"; … … 60 60 #endregion 61 61 [StorableConstructor] 62 pr otectedVariableFrequencyAnalyser(bool deserializing) : base(deserializing) { }63 pr otectedVariableFrequencyAnalyser(VariableFrequencyAnalyser original, Cloner cloner)62 private VariableFrequencyAnalyser(bool deserializing) : base(deserializing) { } 63 private VariableFrequencyAnalyser(VariableFrequencyAnalyser original, Cloner cloner) 64 64 : base(original, cloner) { 65 65 } … … 69 69 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))); 70 70 } 71 public override IDeepCloneable Clone(Cloner cloner) { 72 return new VariableFrequencyAnalyser(this, cloner); 73 } 74 71 75 72 76 public override IOperation Apply() { 73 ItemArray< SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;77 ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue; 74 78 ResultCollection results = ResultCollection; 75 79 … … 98 102 row.Values.Add(0.0); 99 103 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 106 104 return base.Apply(); 107 105 } … … 114 112 var variableReferences = GetVariableReferences(tree, aggregateLaggedVariables); 115 113 foreach (var pair in variableReferences) { 116 if (variableFrequencies.ContainsKey(pair.Key)) {117 }118 114 totalNumberOfSymbols += pair.Value; 119 115 } … … 124 120 } 125 121 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 145 122 private static IEnumerable<KeyValuePair<string, int>> GetVariableReferences(ISymbolicExpressionTree tree, bool aggregateLaggedVariables = true) { 146 123 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); 150 156 } 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 } 153 176 } 154 177 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisAnalyzer.cs
r5553 r5555 23 23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 24 24 using HeuristicLab.Optimization; 25 using HeuristicLab.Parameters; 25 26 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 26 27 public interface ISymbolicDataAnalysisAnalyzer : IAnalyzer { 27 I LookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { get; }28 IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { get; } 28 29 ILookupParameter<ResultCollection> ResultCollectionParameter { get; } 29 30 }
Note: See TracChangeset
for help on using the changeset viewer.