- Timestamp:
- 08/03/16 18:54:14 (8 years ago)
- Location:
- branches/symbreg-factors-2650
- Files:
-
- 13 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisVariableFrequencyAnalyzer.cs
r14185 r14232 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) { … … 151 173 var varCondNode = node as VariableConditionTreeNode; 152 174 IncReferenceCount(references, varCondNode.VariableName); 175 } else if (node.Symbol is FactorVariable) { 176 var factorNode = node as FactorVariableTreeNode; 177 if (aggregateFactorVariables) { 178 IncReferenceCount(references, factorNode.VariableName); 179 } else { 180 IncReferenceCount(references, factorNode.ToString()); 181 } 153 182 } 154 183 }); 155 184 } else { 156 GetVariableReferences(references, tree.Root, 0 );185 GetVariableReferences(references, tree.Root, 0, aggregateFactorVariables); 157 186 } 158 187 return references; 159 188 } 160 189 161 private static void GetVariableReferences(Dictionary<string, int> references, ISymbolicExpressionTreeNode node, int currentLag ) {190 private static void GetVariableReferences(Dictionary<string, int> references, ISymbolicExpressionTreeNode node, int currentLag, bool aggregateFactorVariables) { 162 191 if (node.Symbol is LaggedVariable) { 163 192 var laggedVarNode = node as LaggedVariableTreeNode; … … 166 195 var varNode = node as VariableTreeNode; 167 196 IncReferenceCount(references, varNode.VariableName, currentLag); 197 } else if (node.Symbol is FactorVariable) { 198 var factorNode = node as FactorVariableTreeNode; 199 if (aggregateFactorVariables) { 200 IncReferenceCount(references, factorNode.VariableName, currentLag); 201 } else { 202 IncReferenceCount(references, factorNode.ToString(), currentLag); 203 } 168 204 } else if (node.Symbol is VariableCondition) { 169 205 var varCondNode = node as VariableConditionTreeNode; 170 206 IncReferenceCount(references, varCondNode.VariableName, currentLag); 171 GetVariableReferences(references, node.GetSubtree(0), currentLag );172 GetVariableReferences(references, node.GetSubtree(1), currentLag );207 GetVariableReferences(references, node.GetSubtree(0), currentLag, aggregateFactorVariables); 208 GetVariableReferences(references, node.GetSubtree(1), currentLag, aggregateFactorVariables); 173 209 } else if (node.Symbol is Integral) { 174 210 var laggedNode = node as LaggedTreeNode; 175 211 for (int l = laggedNode.Lag; l <= 0; l++) { 176 GetVariableReferences(references, node.GetSubtree(0), currentLag + l );212 GetVariableReferences(references, node.GetSubtree(0), currentLag + l, aggregateFactorVariables); 177 213 } 178 214 } else if (node.Symbol is Derivative) { 179 215 for (int l = -4; l <= 0; l++) { 180 GetVariableReferences(references, node.GetSubtree(0), currentLag + l );216 GetVariableReferences(references, node.GetSubtree(0), currentLag + l, aggregateFactorVariables); 181 217 } 182 218 } else if (node.Symbol is TimeLag) { 183 219 var laggedNode = node as LaggedTreeNode; 184 GetVariableReferences(references, node.GetSubtree(0), currentLag + laggedNode.Lag );220 GetVariableReferences(references, node.GetSubtree(0), currentLag + laggedNode.Lag, aggregateFactorVariables); 185 221 } else { 186 222 foreach (var subtree in node.Subtrees) { 187 GetVariableReferences(references, subtree, currentLag );223 GetVariableReferences(references, subtree, currentLag, aggregateFactorVariables); 188 224 } 189 225 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/ArithmeticExpressionGrammar.cs
r14185 r14232 53 53 constant.MaxValue = 20; 54 54 var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable(); 55 var factorVariableSymbol = new FactorVariable(); 55 56 56 var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };57 var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol, factorVariableSymbol }; 57 58 var functionSymbols = new List<Symbol>() { add, sub, mul, div }; 58 59 … … 65 66 SetSubtreeCount(constant, 0, 0); 66 67 SetSubtreeCount(variableSymbol, 0, 0); 68 SetSubtreeCount(factorVariableSymbol, 0, 0); 67 69 68 70 // allow each symbol as child of the start symbol -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/FullFunctionalExpressionGrammar.cs
r14185 r14232 115 115 constant.MaxValue = 20; 116 116 var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable(); 117 var factorVariable = new FactorVariable(); 117 118 var laggedVariable = new LaggedVariable(); 118 119 laggedVariable.InitialFrequency = 0.0; … … 123 124 var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, square, pow, sqrt, root, exp, 124 125 airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, fresnelCosineIntegral, fresnelSineIntegral, gamma, hypCosineIntegral, hypSineIntegral, norm, psi, sineIntegral, 125 @if, gt, lt, and, or, not,xor, timeLag, integral, derivative, constant, variableSymbol, laggedVariable,autoregressiveVariable, variableCondition };126 @if, gt, lt, and, or, not,xor, timeLag, integral, derivative, constant, variableSymbol, factorVariable, laggedVariable,autoregressiveVariable, variableCondition }; 126 127 var unaryFunctionSymbols = new List<Symbol>() { square, sqrt, sin, cos, tan, log, exp, not, timeLag, integral, derivative, 127 128 airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, fresnelCosineIntegral, fresnelSineIntegral, gamma, hypCosineIntegral, hypSineIntegral, norm, psi, sineIntegral … … 130 131 var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, variableCondition }; 131 132 var ternarySymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or, xor }; 132 var terminalSymbols = new List<Symbol>() { variableSymbol, constant, laggedVariable, autoregressiveVariable };133 var terminalSymbols = new List<Symbol>() { variableSymbol, factorVariable, constant, laggedVariable, autoregressiveVariable }; 133 134 134 135 foreach (var symb in allSymbols) -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs
r14185 r14232 104 104 constant.MaxValue = 20; 105 105 var variableSymbol = new Variable(); 106 var factorVariable = new FactorVariable(); 106 107 var laggedVariable = new LaggedVariable(); 107 108 var autoregressiveVariable = new AutoregressiveTargetVariable(); … … 114 115 var specialFunctions = new GroupSymbol(SpecialFunctionsName, new List<ISymbol> { airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, 115 116 fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral}); 116 var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol });117 var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol, factorVariable }); 117 118 var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols }); 118 119 … … 122 123 var comparisonSymbols = new GroupSymbol(ComparisonsName, new List<ISymbol> { gt, lt }); 123 124 var booleanOperationSymbols = new GroupSymbol(BooleanOperatorsName, new List<ISymbol> { and, or, not, xor }); 124 var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols }); 125 var conditionalSymbols = new GroupSymbol(ConditionalSymbolsName, new List<ISymbol> { conditionSymbols, comparisonSymbols, booleanOperationSymbols }); // TODO: factorVariableBool? 125 126 126 127 var timeSeriesSymbols = new GroupSymbol(TimeSeriesSymbolsName, new List<ISymbol> { timeLag, integral, derivative, laggedVariable, autoregressiveVariable }); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r14024 r14232 198 198 <Compile Include="Symbols\AiryB.cs" /> 199 199 <Compile Include="Symbols\Bessel.cs" /> 200 <Compile Include="Symbols\FactorVariable.cs" /> 201 <Compile Include="Symbols\FactorVariableTreeNode.cs" /> 200 202 <Compile Include="Symbols\Xor.cs" /> 201 203 <Compile Include="Symbols\Erf.cs" /> -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisImpactValuesCalculator.cs
r12720 r14232 5 5 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 6 6 public interface ISymbolicDataAnalysisSolutionImpactValuesCalculator : IItem { 7 double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows);8 double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double qualityForImpactsCalculation = double.NaN);9 7 void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, 10 8 IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation, double qualityForImpactsCalculation = double.NaN); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs
r14185 r14232 83 83 public const byte Erf = 43; 84 84 public const byte Bessel = 44; 85 public const byte FactorVariable = 46; 85 86 86 87 private static Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() { … … 130 131 { typeof(Norm), OpCodes.Norm}, 131 132 { typeof(Erf), OpCodes.Erf}, 132 { typeof(Bessel), OpCodes.Bessel} 133 { typeof(Bessel), OpCodes.Bessel}, 134 { typeof(FactorVariable), OpCodes.FactorVariable } 133 135 }; 134 136 -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r14185 r14232 66 66 private static MethodInfo erf = thisType.GetMethod("Erf", new Type[] { typeof(double) }); 67 67 private static MethodInfo bessel = thisType.GetMethod("Bessel", new Type[] { typeof(double) }); 68 private static MethodInfo string_eq = typeof(string).GetMethod("Equals", new Type[] {typeof(string)}); 68 69 #endregion 69 70 … … 627 628 return; 628 629 } 630 case OpCodes.FactorVariable: { 631 FactorVariableTreeNode varNode = currentInstr.dynamicNode as FactorVariableTreeNode; 632 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array 633 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.data); 634 // load correct column of the current variable 635 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 636 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 637 if (!state.InLaggedContext) { 638 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 639 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.VariableValue); 640 il.Emit(System.Reflection.Emit.OpCodes.Call, string_eq); 641 // TODO: convert bool to 1 / 0? 642 } else { 643 var nanResult = il.DefineLabel(); 644 var normalResult = il.DefineLabel(); 645 il.Emit(System.Reflection.Emit.OpCodes.Dup); 646 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); 647 il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult); 648 il.Emit(System.Reflection.Emit.OpCodes.Dup); 649 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows); 650 il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult); 651 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 652 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.VariableValue); 653 il.Emit(System.Reflection.Emit.OpCodes.Call, string_eq); 654 // TODO: convert bool to 1 / 0? 655 il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult); 656 il.MarkLabel(nanResult); 657 il.Emit(System.Reflection.Emit.OpCodes.Pop); // rowIndex 658 il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference 659 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN); 660 il.MarkLabel(normalResult); 661 } 662 return; 663 } 629 664 case OpCodes.LagVariable: { 630 665 var nanResult = il.DefineLabel(); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r14185 r14232 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 143 144 var variableTreeNode = (VariableTreeNode)instr.dynamicNode; 144 145 instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName); 146 } else if (instr.opCode == OpCodes.FactorVariable) { 147 var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode; 148 instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName); 145 149 } else if (instr.opCode == OpCodes.LagVariable) { 146 150 var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode; … … 455 459 return ((IList<double>)currentInstr.data)[row] * variableTreeNode.Weight; 456 460 } 461 case OpCodes.FactorVariable: { 462 if (row < 0 || row >= dataset.Rows) return double.NaN; 463 var factorVarTreeNode = currentInstr.dynamicNode as FactorVariableTreeNode; 464 return ((IList<string>)currentInstr.data)[row] == factorVarTreeNode.VariableValue ? 1 : 0; 465 } 457 466 case OpCodes.LagVariable: { 458 467 var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r14185 r14232 147 147 var variableTreeNode = (VariableTreeNode)instr.dynamicNode; 148 148 instr.value = ((IList<double>)instr.data)[row] * variableTreeNode.Weight; 149 } 150 } else if (instr.opCode == OpCodes.FactorVariable) { 151 if (row < 0 || row >= dataset.Rows) instr.value = double.NaN; 152 else { 153 var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode; 154 instr.value = ((IList<string>)instr.data)[row] == factorTreeNode.VariableValue ? 1 : 0; 149 155 } 150 156 } else if (instr.opCode == OpCodes.LagVariable) { … … 392 398 } 393 399 break; 400 case OpCodes.FactorVariable: { 401 var factorVariableTreeNode = instr.dynamicNode as FactorVariableTreeNode; 402 instr.data = dataset.GetReadOnlyStringValues(factorVariableTreeNode.VariableName); 403 } 404 break; 394 405 case OpCodes.LagVariable: { 395 406 var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode; -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModelComplexityCalculator.cs
r14185 r14232 39 39 return 1; 40 40 } 41 case OpCodes.Variable: { 41 case OpCodes.Variable: 42 case OpCodes.FactorVariable: { 42 43 return 2; 43 44 } 44 case OpCodes.Add: 45 case OpCodes.Add: 45 46 case OpCodes.Sub: { 46 47 double complexity = 0; … … 50 51 return complexity; 51 52 } 52 case OpCodes.Mul: 53 case OpCodes.Mul: 53 54 case OpCodes.Div: { 54 55 double complexity = 1; … … 60 61 } 61 62 case OpCodes.Sin: 62 case OpCodes.Cos: 63 case OpCodes.Cos: 63 64 case OpCodes.Tan: 64 case OpCodes.Exp: 65 case OpCodes.Exp: 65 66 case OpCodes.Log: { 66 67 double complexity = CalculateComplexity(node.GetSubtree(0)); … … 75 76 return complexity * complexity * complexity; 76 77 } 77 case OpCodes.Power: 78 case OpCodes.Power: 78 79 case OpCodes.Root: { 79 80 double complexity = CalculateComplexity(node.GetSubtree(0)); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs
r14185 r14232 208 208 209 209 protected virtual void UpdateGrammar() { 210 SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value; 211 SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value; 212 foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) { 210 var problemData = ProblemData; 211 var ds = problemData.Dataset; 212 var grammar = SymbolicExpressionTreeGrammar; 213 grammar.MaximumFunctionArguments = MaximumFunctionArguments.Value; 214 grammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value; 215 foreach (var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) { 213 216 if (!varSymbol.Fixed) { 214 varSymbol.AllVariableNames = ProblemData.InputVariables.Select(x => x.Value);215 varSymbol.VariableNames = ProblemData.AllowedInputVariables;217 varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<double>(x)); 218 varSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<double>(x)); 216 219 } 217 220 } 218 foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) { 221 foreach (var factorSymbol in grammar.Symbols.OfType<FactorVariable>()) { 222 if (!factorSymbol.Fixed) { 223 factorSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<string>(x)); 224 factorSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<string>(x)); 225 factorSymbol.VariableValues = factorSymbol.VariableNames 226 .ToDictionary(varName => varName, varName => ds.GetStringValues(varName).Distinct().ToList()); 227 } 228 } 229 foreach (var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) { 219 230 if (!varSymbol.Fixed) { 220 varSymbol.AllVariableNames = ProblemData.InputVariables.Select(x => x.Value);221 varSymbol.VariableNames = ProblemData.AllowedInputVariables;231 varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<double>(x)); 232 varSymbol.VariableNames = problemData.AllowedInputVariables; 222 233 } 223 234 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs
r14185 r14232 21 21 22 22 using System.Collections.Generic; 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 36 37 [StorableConstructor] 37 38 protected SymbolicDataAnalysisSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { } 38 public abstract double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows);39 public abstract double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double qualityForImpactsCalculation = double.NaN);40 39 public abstract void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation, double qualityForImpactsCalculation = double.NaN); 41 40 42 protected static double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,41 protected IEnumerable<double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 43 42 IDataset dataset, IEnumerable<int> rows) { 44 43 //optimization: constant nodes return always the same value 45 44 ConstantTreeNode constantNode = node as ConstantTreeNode; 46 if (constantNode != null) return constantNode.Value; 45 FactorVariableTreeNode factorNode = node as FactorVariableTreeNode; 46 if (constantNode != null) { 47 yield return constantNode.Value; 48 } else if (factorNode != null) { 49 // valid replacements are either all off or all on 50 yield return 0; 51 yield return 1; 52 } else { 53 var rootSymbol = new ProgramRootSymbol().CreateTreeNode(); 54 var startSymbol = new StartSymbol().CreateTreeNode(); 55 rootSymbol.AddSubtree(startSymbol); 56 startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone()); 47 57 48 var rootSymbol = new ProgramRootSymbol().CreateTreeNode(); 49 var startSymbol = new StartSymbol().CreateTreeNode(); 50 rootSymbol.AddSubtree(startSymbol); 51 startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone()); 52 53 var tempTree = new SymbolicExpressionTree(rootSymbol); 54 // clone ADFs of source tree 55 for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) { 56 tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone()); 58 var tempTree = new SymbolicExpressionTree(rootSymbol); 59 // clone ADFs of source tree 60 for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) { 61 tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone()); 62 } 63 yield return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median(); 64 yield return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Average(); // TODO perf 57 65 } 58 return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median();59 66 } 60 67 }
Note: See TracChangeset
for help on using the changeset viewer.