- Timestamp:
- 05/04/15 13:08:49 (10 years ago)
- Location:
- branches/GBT
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GBT
- Property svn:mergeinfo changed
/trunk/sources (added) merged: 12333,12358-12361,12365
- Property svn:mergeinfo changed
-
branches/GBT/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic (added) merged: 12358-12359,12361
- Property svn:mergeinfo changed
-
branches/GBT/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs
r12012 r12378 45 45 private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees"; 46 46 private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees"; 47 private const string TotalNumberOfPrunedNodesParameterName = "Number of pruned nodes"; 47 48 private const string RandomParameterName = "Random"; 48 private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes";49 private const string NodeImpactThresholdParameterName = "ImpactThreshold";50 private const string PruningOperatorParameterName = "PruningOperator";51 49 private const string ResultsParameterName = "Results"; 52 50 private const string PopulationSizeParameterName = "PopulationSize"; … … 54 52 55 53 #region private members 54 private DataReducer prunedNodesReducer; 56 55 private DataReducer prunedSubtreesReducer; 57 56 private DataReducer prunedTreesReducer; … … 61 60 62 61 #region parameter properties 63 public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter {64 get { return (IValueParameter<SymbolicDataAnalysisExpressionPruningOperator>)Parameters[PruningOperatorParameterName]; }65 }66 public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter {67 get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; }68 }69 public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter {70 get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; }71 }72 62 public ILookupParameter<IRandom> RandomParameter { 73 63 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } … … 91 81 92 82 #region properties 93 protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; }}83 protected abstract SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get; } 94 84 protected int UpdateInterval { get { return UpdateIntervalParameter.Value.Value; } } 95 85 … … 112 102 get { return PruningProbabilityParameter.Value.Value; } 113 103 set { PruningProbabilityParameter.Value.Value = value; } 114 }115 116 protected bool PruneOnlyZeroImpactNodes {117 get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }118 set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }119 }120 protected double NodeImpactThreshold {121 get { return NodeImpactThresholdParameter.Value.Value; }122 set { NodeImpactThresholdParameter.Value.Value = value; }123 104 } 124 105 #endregion … … 140 121 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner) 141 122 : base(original, cloner) { 123 if (original.prunedNodesReducer != null) 124 this.prunedNodesReducer = (DataReducer)original.prunedNodesReducer.Clone(); 142 125 if (original.prunedSubtreesReducer != null) 143 126 this.prunedSubtreesReducer = (DataReducer)original.prunedSubtreesReducer.Clone(); … … 197 180 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random number generator.")); 198 181 Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data.")); 199 Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, "The impact threshold below which an individual should be pruned.", new DoubleValue(0.0)));200 Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, "Switch to determine of only zero impact individuals should be pruned.", new BoolValue(false)));201 182 Parameters.Add(new LookupParameter<IntValue>(PopulationSizeParameterName, "The population of individuals.")); 202 183 #endregion … … 250 231 UpdateCounter = 0; 251 232 252 if (pruned SubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); }233 if (prunedNodesReducer == null || prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); } 253 234 254 235 var prune = CreatePruningOperation(); 236 var reducePrunedNodes = ExecutionContext.CreateChildOperation(prunedNodesReducer); 255 237 var reducePrunedSubtrees = ExecutionContext.CreateChildOperation(prunedSubtreesReducer); 256 238 var reducePrunedTrees = ExecutionContext.CreateChildOperation(prunedTreesReducer); … … 258 240 var collectResults = ExecutionContext.CreateChildOperation(resultsCollector); 259 241 260 return new OperationCollection { prune, reducePruned Subtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() };242 return new OperationCollection { prune, reducePrunedNodes, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() }; 261 243 } 262 244 263 245 private void InitializeOperators() { 246 prunedNodesReducer = new DataReducer(); 247 prunedNodesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedNodesParameter.ActualName; 248 prunedNodesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values 249 prunedNodesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter 250 prunedNodesReducer.TargetParameter.ActualName = TotalNumberOfPrunedNodesParameterName; 251 264 252 prunedSubtreesReducer = new DataReducer(); 265 253 prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName; … … 275 263 276 264 valuesCollector = new DataTableValuesCollector(); 265 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedNodesParameterName)); 277 266 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName)); 278 267 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName)); -
branches/GBT/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs
r12189 r12378 34 34 [StorableClass] 35 35 [Item("SymbolicExpressionTreePruningOperator", "An operator that replaces introns with constant values in a symbolic expression tree.")] 36 public abstract class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator {36 public abstract class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator, ISymbolicExpressionTreeOperator { 37 37 #region parameter names 38 38 private const string ProblemDataParameterName = "ProblemData"; … … 41 41 private const string PrunedSubtreesParameterName = "PrunedSubtrees"; 42 42 private const string PrunedTreesParameterName = "PrunedTrees"; 43 private const string PrunedNodesParameterName = "PrunedNodes"; 43 44 private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition"; 44 45 private const string NodeImpactThresholdParameterName = "ImpactThreshold"; … … 72 73 get { return (ILookupParameter<IntValue>)Parameters[PrunedTreesParameterName]; } 73 74 } 75 public ILookupParameter<IntValue> PrunedNodesParameter { 76 get { return (ILookupParameter<IntValue>)Parameters[PrunedNodesParameterName]; } 77 } 74 78 public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter { 75 79 get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; } … … 87 91 88 92 #region properties 89 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } } 90 protected ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { get { return ImpactValuesCalculatorParameter.Value; } } 91 protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } } 92 protected bool PruneOnlyZeroImpactNodes { 93 public ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { 94 get { return ImpactValuesCalculatorParameter.Value; } 95 set { ImpactValuesCalculatorParameter.Value = value; } 96 } 97 public bool PruneOnlyZeroImpactNodes { 93 98 get { return PruneOnlyZeroImpactNodesParameter.Value.Value; } 94 99 set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; } 95 100 } 96 p rotecteddouble NodeImpactThreshold {101 public double NodeImpactThreshold { 97 102 get { return NodeImpactThresholdParameter.Value.Value; } 98 103 set { NodeImpactThresholdParameter.Value.Value = value; } 99 104 } 100 protected ISymbolicExpressionTree SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } }101 protected DoubleValue Quality { get { return QualityParameter.ActualValue; } }102 protected DoubleLimit EstimationLimits { get { return EstimationLimitsParameter.ActualValue; } }103 protected ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } }104 105 #endregion 105 106 … … 114 115 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisModel>(SymbolicDataAnalysisModelParameterName)); 115 116 Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName)); 117 Parameters.Add(new LookupParameter<IntValue>(PrunedNodesParameterName, "A counter of how many nodes were pruned.")); 116 118 Parameters.Add(new LookupParameter<IntValue>(PrunedSubtreesParameterName, "A counter of how many subtrees were replaced.")); 117 119 Parameters.Add(new LookupParameter<IntValue>(PrunedTreesParameterName, "A counter of how many trees were pruned.")); … … 131 133 132 134 public override IOperation Apply() { 133 var model = CreateModel(SymbolicExpressionTree, Interpreter, ProblemData, EstimationLimits); 134 var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); 135 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size); 135 var tree = SymbolicExpressionTreeParameter.ActualValue; 136 var problemData = ProblemDataParameter.ActualValue; 137 var fitnessCalculationPartition = FitnessCalculationPartitionParameter.ActualValue; 138 var estimationLimits = EstimationLimitsParameter.ActualValue; 139 var interpreter = InterpreterParameter.ActualValue; 140 141 var model = CreateModel(tree, interpreter, problemData, estimationLimits); 142 var nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); 143 var rows = Enumerable.Range(fitnessCalculationPartition.Start, fitnessCalculationPartition.Size); 136 144 var prunedSubtrees = 0; 137 145 var prunedTrees = 0; 146 var prunedNodes = 0; 138 147 139 148 double quality = Evaluate(model); … … 144 153 145 154 double impactValue, replacementValue; 146 ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality);155 ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality); 147 156 148 if (PruneOnlyZeroImpactNodes) { 149 if (!impactValue.IsAlmost(0.0)) continue; 150 } else if (NodeImpactThreshold < impactValue) { 151 continue; 152 } 157 if (PruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue; 158 if (!PruneOnlyZeroImpactNodes && impactValue > NodeImpactThreshold) continue; 153 159 154 160 var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode(); 155 161 constantNode.Value = replacementValue; 156 162 163 var length = node.GetLength(); 157 164 ReplaceWithConstant(node, constantNode); 158 i += node.GetLength()- 1; // skip subtrees under the node that was folded165 i += length - 1; // skip subtrees under the node that was folded 159 166 160 167 quality -= impactValue; 161 162 168 prunedSubtrees++; 169 prunedNodes += length; 163 170 } 164 171 … … 166 173 PrunedSubtreesParameter.ActualValue = new IntValue(prunedSubtrees); 167 174 PrunedTreesParameter.ActualValue = new IntValue(prunedTrees); 175 PrunedNodesParameter.ActualValue = new IntValue(prunedNodes); 176 QualityParameter.ActualValue.Value = quality; 168 177 169 178 return base.Apply(); 170 }171 172 public ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {173 var model = CreateModel((ISymbolicExpressionTree)tree.Clone(), Interpreter, ProblemData, EstimationLimits);174 var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();175 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);176 177 double quality = Evaluate(model);178 179 for (int i = 0; i < nodes.Count; ++i) {180 var node = nodes[i];181 if (node is ConstantTreeNode) continue;182 183 double impactValue, replacementValue;184 ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality);185 186 if (PruneOnlyZeroImpactNodes) {187 if (!impactValue.IsAlmost(0.0)) continue;188 } else if (NodeImpactThreshold < impactValue) {189 continue;190 }191 192 var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();193 constantNode.Value = replacementValue;194 195 ReplaceWithConstant(node, constantNode);196 i += node.GetLength() - 1; // skip subtrees under the node that was folded197 198 quality -= impactValue;199 }200 return model.SymbolicExpressionTree;201 179 } 202 180
Note: See TracChangeset
for help on using the changeset viewer.