Changeset 12247 for branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 03/24/15 11:17:08 (10 years ago)
- Location:
- branches/HeuristicLab.DatasetRefactor/sources
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.DatasetRefactor/sources
- Property svn:mergeinfo changed
-
branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
-
branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPruningAnalyzer.cs
r12031 r12247 45 45 public SymbolicRegressionPruningAnalyzer() { 46 46 Parameters.Add(new ValueParameter<SymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, "The impact values calculator", new SymbolicRegressionSolutionImpactValuesCalculator())); 47 Parameters.Add(new ValueParameter<SymbolicDataAnalysisExpressionPruningOperator>(PruningOperatorParameterName, "The operator used to prune trees", new SymbolicRegressionPruningOperator( )));47 Parameters.Add(new ValueParameter<SymbolicDataAnalysisExpressionPruningOperator>(PruningOperatorParameterName, "The operator used to prune trees", new SymbolicRegressionPruningOperator(new SymbolicRegressionSolutionImpactValuesCalculator()))); 48 48 } 49 49 } -
branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPruningOperator.cs
r12031 r12247 22 22 #endregion 23 23 24 using System.Collections.Generic; 24 25 using System.Linq; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 using HeuristicLab. Parameters;28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 30 … … 32 33 [Item("SymbolicRegressionPruningOperator", "An operator which prunes symbolic regression trees.")] 33 34 public class SymbolicRegressionPruningOperator : SymbolicDataAnalysisExpressionPruningOperator { 34 private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator";35 36 35 protected SymbolicRegressionPruningOperator(SymbolicRegressionPruningOperator original, Cloner cloner) 37 36 : base(original, cloner) { … … 44 43 protected SymbolicRegressionPruningOperator(bool deserializing) : base(deserializing) { } 45 44 46 public SymbolicRegressionPruningOperator() { 47 var impactValuesCalculator = new SymbolicRegressionSolutionImpactValuesCalculator(); 48 Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, "The impact values calculator to be used for figuring out the node impacts.", impactValuesCalculator)); 45 public SymbolicRegressionPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator) 46 : base(impactValuesCalculator) { 49 47 } 50 48 51 protected override ISymbolicDataAnalysisModel CreateModel( ) {52 return new SymbolicRegressionModel( SymbolicExpressionTree, Interpreter, EstimationLimits.Lower, EstimationLimits.Upper);49 protected override ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) { 50 return new SymbolicRegressionModel(tree, interpreter, estimationLimits.Lower, estimationLimits.Upper); 53 51 } 54 52 … … 56 54 var regressionModel = (IRegressionModel)model; 57 55 var regressionProblemData = (IRegressionProblemData)ProblemData; 58 var trainingIndices = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size); 59 var estimatedValues = regressionModel.GetEstimatedValues(ProblemData.Dataset, trainingIndices); // also bounds the values 60 var targetValues = ProblemData.Dataset.GetDoubleValues(regressionProblemData.TargetVariable, trainingIndices); 56 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size); 57 return Evaluate(regressionModel, regressionProblemData, rows); 58 } 59 60 private static double Evaluate(IRegressionModel model, IRegressionProblemData problemData, 61 IEnumerable<int> rows) { 62 var estimatedValues = model.GetEstimatedValues(problemData.Dataset, rows); // also bounds the values 63 var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 61 64 OnlineCalculatorError errorState; 62 65 var quality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, estimatedValues, out errorState); … … 64 67 return quality; 65 68 } 69 70 public static ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, SymbolicRegressionSolutionImpactValuesCalculator impactValuesCalculator, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IRegressionProblemData problemData, DoubleLimit estimationLimits, IEnumerable<int> rows, double nodeImpactThreshold = 0.0, bool pruneOnlyZeroImpactNodes = false) { 71 var clonedTree = (ISymbolicExpressionTree)tree.Clone(); 72 var model = new SymbolicRegressionModel(clonedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper); 73 var nodes = clonedTree.IterateNodesPrefix().ToList(); 74 double quality = Evaluate(model, problemData, rows); 75 76 for (int i = 0; i < nodes.Count; ++i) { 77 var node = nodes[i]; 78 if (node is ConstantTreeNode) continue; 79 80 double impactValue, replacementValue; 81 impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality); 82 83 if (pruneOnlyZeroImpactNodes) { 84 if (!impactValue.IsAlmost(0.0)) continue; 85 } else if (nodeImpactThreshold < impactValue) { 86 continue; 87 } 88 89 var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode(); 90 constantNode.Value = replacementValue; 91 92 ReplaceWithConstant(node, constantNode); 93 i += node.GetLength() - 1; // skip subtrees under the node that was folded 94 95 quality -= impactValue; 96 } 97 return model.SymbolicExpressionTree; 98 } 66 99 } 67 100 }
Note: See TracChangeset
for help on using the changeset viewer.