- Timestamp:
- 04/04/17 17:52:44 (8 years ago)
- Location:
- trunk/sources
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs
r14185 r14826 89 89 var varTreeNode = tree as VariableTreeNode; 90 90 var constTreeNode = tree as ConstantTreeNode; 91 var factorVarTreeNode = tree as FactorVariableTreeNode; 92 var binFactorVarTreeNode = tree as BinaryFactorVariableTreeNode; 91 93 if (varTreeNode != null) { 92 94 builder.Append("(var " + varTreeNode.VariableName); 95 } else if (factorVarTreeNode != null) { 96 builder.Append("(factor " + factorVarTreeNode.VariableName); 97 } else if (binFactorVarTreeNode != null) { 98 builder.Append("(factor " + binFactorVarTreeNode.VariableName + "=" + binFactorVarTreeNode.VariableValue); 93 99 } else if (constTreeNode != null) { 94 100 builder.Append("(const"); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisVariableFrequencyAnalyzer.cs
r14185 r14826 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Globalization; 24 25 using System.Linq; 25 26 using HeuristicLab.Analysis; … … 41 42 private const string VariableFrequenciesParameterName = "VariableFrequencies"; 42 43 private const string AggregateLaggedVariablesParameterName = "AggregateLaggedVariables"; 44 private const string AggregateFactorVariablesParameterName = "AggregateFactorVariables"; 43 45 private const string VariableImpactsParameterName = "VariableImpacts"; 44 46 … … 52 54 public IValueLookupParameter<BoolValue> AggregateLaggedVariablesParameter { 53 55 get { return (IValueLookupParameter<BoolValue>)Parameters[AggregateLaggedVariablesParameterName]; } 56 } 57 public IValueLookupParameter<BoolValue> AggregateFactorVariablesParameter { 58 get { return (IValueLookupParameter<BoolValue>)Parameters[AggregateFactorVariablesParameterName]; } 54 59 } 55 60 #endregion … … 59 64 set { AggregateLaggedVariablesParameter.Value = value; } 60 65 } 66 public BoolValue AggregateFactorVariables { 67 get { return AggregateFactorVariablesParameter.ActualValue; } 68 set { AggregateFactorVariablesParameter.Value = value; } 69 } 61 70 #endregion 62 71 [StorableConstructor] … … 70 79 Parameters.Add(new LookupParameter<DoubleMatrix>(VariableImpactsParameterName, "The relative variable relevance calculated as the average relative variable frequency over the whole run.")); 71 80 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))); 81 Parameters.Add(new ValueLookupParameter<BoolValue>(AggregateFactorVariablesParameterName, "Switch that determines whether all references to factor variables should be aggregated regardless of the value. Turn off to analyze all factor variable references with different values separately.", new BoolValue(true))); 82 } 83 84 [StorableHook(HookType.AfterDeserialization)] 85 private void AfterDeserialization() { 86 // BackwardsCompatibility3.3 87 #region Backwards compatible code, remove with 3.4 88 if (!Parameters.ContainsKey(AggregateFactorVariablesParameterName)) { 89 Parameters.Add(new ValueLookupParameter<BoolValue>(AggregateFactorVariablesParameterName, "Switch that determines whether all references to factor variables should be aggregated regardless of the value. Turn off to analyze all factor variable references with different values separately.", new BoolValue(true))); 90 } 91 #endregion 72 92 } 73 93 … … 93 113 int numberOfValues = datatable.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First(); 94 114 95 foreach (var pair in SymbolicDataAnalysisVariableFrequencyAnalyzer.CalculateVariableFrequencies(expressions, AggregateLaggedVariables.Value)) {115 foreach (var pair in CalculateVariableFrequencies(expressions, AggregateLaggedVariables.Value, AggregateFactorVariables.Value)) { 96 116 if (!datatable.Rows.ContainsKey(pair.Key)) { 97 117 // initialize a new row for the variable and pad with zeros … … 128 148 } 129 149 130 public static IEnumerable<KeyValuePair<string, double>> CalculateVariableFrequencies(IEnumerable<ISymbolicExpressionTree> trees, bool aggregateLaggedVariables = true) { 150 public static IEnumerable<KeyValuePair<string, double>> CalculateVariableFrequencies(IEnumerable<ISymbolicExpressionTree> trees, 151 bool aggregateLaggedVariables = true, bool aggregateFactorVariables = true) { 131 152 132 153 var variableFrequencies = trees 133 .SelectMany(t => GetVariableReferences(t, aggregateLaggedVariables ))154 .SelectMany(t => GetVariableReferences(t, aggregateLaggedVariables, aggregateFactorVariables)) 134 155 .GroupBy(pair => pair.Key, pair => pair.Value) 135 156 .ToDictionary(g => g.Key, g => (double)g.Sum()); … … 141 162 } 142 163 143 private static IEnumerable<KeyValuePair<string, int>> GetVariableReferences(ISymbolicExpressionTree tree, bool aggregateLaggedVariables = true) { 164 private static IEnumerable<KeyValuePair<string, int>> GetVariableReferences(ISymbolicExpressionTree tree, 165 bool aggregateLaggedVariables = true, bool aggregateFactorVariables = true) { 144 166 Dictionary<string, int> references = new Dictionary<string, int>(); 145 167 if (aggregateLaggedVariables) { 146 168 tree.Root.ForEachNodePrefix(node => { 147 if (node.Symbol is Variable) { 148 var varNode = node as VariableTreeNode; 149 IncReferenceCount(references, varNode.VariableName); 150 } else if (node.Symbol is VariableCondition) { 151 var varCondNode = node as VariableConditionTreeNode; 152 IncReferenceCount(references, varCondNode.VariableName); 169 if (node is IVariableTreeNode) { 170 var factorNode = node as BinaryFactorVariableTreeNode; 171 if (factorNode != null && !aggregateFactorVariables) { 172 IncReferenceCount(references, factorNode.VariableName + "=" + factorNode.VariableValue); 173 } else { 174 var varNode = node as IVariableTreeNode; 175 IncReferenceCount(references, varNode.VariableName); 176 } 153 177 } 154 178 }); 155 179 } else { 156 GetVariableReferences(references, tree.Root, 0 );180 GetVariableReferences(references, tree.Root, 0, aggregateFactorVariables); 157 181 } 158 182 return references; 159 183 } 160 184 161 private static void GetVariableReferences(Dictionary<string, int> references, ISymbolicExpressionTreeNode node, int currentLag) { 162 if (node.Symbol is LaggedVariable) { 163 var laggedVarNode = node as LaggedVariableTreeNode; 164 IncReferenceCount(references, laggedVarNode.VariableName, currentLag + laggedVarNode.Lag); 165 } else if (node.Symbol is Variable) { 166 var varNode = node as VariableTreeNode; 167 IncReferenceCount(references, varNode.VariableName, currentLag); 168 } else if (node.Symbol is VariableCondition) { 169 var varCondNode = node as VariableConditionTreeNode; 170 IncReferenceCount(references, varCondNode.VariableName, currentLag); 171 GetVariableReferences(references, node.GetSubtree(0), currentLag); 172 GetVariableReferences(references, node.GetSubtree(1), currentLag); 185 private static void GetVariableReferences(Dictionary<string, int> references, ISymbolicExpressionTreeNode node, int currentLag, bool aggregateFactorVariables) { 186 if (node is IVariableTreeNode) { 187 var laggedVarTreeNode = node as LaggedVariableTreeNode; 188 var binFactorVariableTreeNode = node as BinaryFactorVariableTreeNode; 189 var varConditionTreeNode = node as VariableConditionTreeNode; 190 if (laggedVarTreeNode != null) { 191 IncReferenceCount(references, laggedVarTreeNode.VariableName, currentLag + laggedVarTreeNode.Lag); 192 } else if (binFactorVariableTreeNode != null) { 193 if (aggregateFactorVariables) { 194 IncReferenceCount(references, binFactorVariableTreeNode.VariableName, currentLag); 195 } else { 196 IncReferenceCount(references, binFactorVariableTreeNode.VariableName + "=" + binFactorVariableTreeNode.VariableValue, currentLag); 197 } 198 } else if (varConditionTreeNode != null) { 199 IncReferenceCount(references, varConditionTreeNode.VariableName, currentLag); 200 GetVariableReferences(references, node.GetSubtree(0), currentLag, aggregateFactorVariables); 201 GetVariableReferences(references, node.GetSubtree(1), currentLag, aggregateFactorVariables); 202 } else { 203 var varNode = node as IVariableTreeNode; 204 IncReferenceCount(references, varNode.VariableName, currentLag); 205 } 173 206 } else if (node.Symbol is Integral) { 174 207 var laggedNode = node as LaggedTreeNode; 175 208 for (int l = laggedNode.Lag; l <= 0; l++) { 176 GetVariableReferences(references, node.GetSubtree(0), currentLag + l );209 GetVariableReferences(references, node.GetSubtree(0), currentLag + l, aggregateFactorVariables); 177 210 } 178 211 } else if (node.Symbol is Derivative) { 179 212 for (int l = -4; l <= 0; l++) { 180 GetVariableReferences(references, node.GetSubtree(0), currentLag + l );213 GetVariableReferences(references, node.GetSubtree(0), currentLag + l, aggregateFactorVariables); 181 214 } 182 215 } else if (node.Symbol is TimeLag) { 183 216 var laggedNode = node as LaggedTreeNode; 184 GetVariableReferences(references, node.GetSubtree(0), currentLag + laggedNode.Lag );217 GetVariableReferences(references, node.GetSubtree(0), currentLag + laggedNode.Lag, aggregateFactorVariables); 185 218 } else { 186 219 foreach (var subtree in node.Subtrees) { 187 GetVariableReferences(references, subtree, currentLag );220 GetVariableReferences(references, subtree, currentLag, aggregateFactorVariables); 188 221 } 189 222 }
Note: See TracChangeset
for help on using the changeset viewer.