Changeset 10469
- Timestamp:
- 02/19/14 14:04:03 (11 years ago)
- Location:
- trunk/sources
- Files:
-
- 2 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification-3.4.csproj
r10368 r10469 150 150 <SubType>Code</SubType> 151 151 </Compile> 152 <Compile Include="SymbolicClassificationPruningOperator.cs" /> 152 153 <None Include="HeuristicLab.snk" /> 153 154 <None Include="Plugin.cs.frame" /> -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationPruningAnalyzer.cs
r10418 r10469 20 20 #endregion 21 21 22 using System;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;26 24 using HeuristicLab.Parameters; 27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 31 29 [StorableClass] 32 30 public sealed class SymbolicClassificationPruningAnalyzer : SymbolicDataAnalysisSingleObjectivePruningAnalyzer { 33 private const string ModelCreatorParameterName = "ModelCreator"; 34 #region parameter properties 35 public ILookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter { 36 get { return (ILookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 37 } 38 #endregion 39 #region properties 40 private ISymbolicClassificationModelCreator ModelCreator { 41 get { return ModelCreatorParameter.ActualValue; } 42 set { ModelCreatorParameter.ActualValue = value; } 43 } 44 #endregion 45 31 private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator"; 32 private const string PruningOperatorParameterName = "PruningOperator"; 46 33 private SymbolicClassificationPruningAnalyzer(SymbolicClassificationPruningAnalyzer original, Cloner cloner) 47 34 : base(original, cloner) { … … 55 42 56 43 public SymbolicClassificationPruningAnalyzer() { 57 // pruning parameters 58 Parameters.Add(new LookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName)); 59 impactValuesCalculator = new SymbolicClassificationSolutionImpactValuesCalculator(); 60 } 61 62 protected override ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, 63 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double lowerEstimationLimit = Double.MinValue, 64 double upperEstimationLimit = Double.MaxValue) { 65 var model = ModelCreator.CreateSymbolicClassificationModel(tree, Interpreter, lowerEstimationLimit, upperEstimationLimit); 66 model.RecalculateModelParameters((IClassificationProblemData)ProblemData, ProblemData.TrainingIndices); 67 return model; 44 Parameters.Add(new ValueParameter<SymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, "The impact values calculator", new SymbolicClassificationSolutionImpactValuesCalculator())); 45 Parameters.Add(new ValueParameter<SymbolicDataAnalysisExpressionPruningOperator>(PruningOperatorParameterName, "The operator used to prune trees", new SymbolicClassificationPruningOperator())); 68 46 } 69 47 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationSolutionImpactValuesCalculator.cs
r10273 r10469 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 25 using HeuristicLab.Core; 24 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 28 26 29 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification { 30 [StorableClass] 31 [Item("SymbolicClassificationSolutionImpactValuesCalculator", "Calculate symbolic expression tree node impact values for classification problems.")] 27 32 public class SymbolicClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator { 33 public SymbolicClassificationSolutionImpactValuesCalculator() { } 34 protected SymbolicClassificationSolutionImpactValuesCalculator(SymbolicClassificationSolutionImpactValuesCalculator original, Cloner cloner) 35 : base(original, cloner) { } 36 public override IDeepCloneable Clone(Cloner cloner) { 37 return new SymbolicClassificationSolutionImpactValuesCalculator(this, cloner); 38 } 39 [StorableConstructor] 40 protected SymbolicClassificationSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { } 41 28 42 public override double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows) { 29 43 var classificationModel = (ISymbolicClassificationModel)model; … … 34 48 35 49 public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN) { 50 double impactValue, replacementValue; 51 CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, originalQuality); 52 return impactValue; 53 } 54 55 public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, 56 IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, 57 double originalQuality = Double.NaN) { 36 58 var classificationModel = (ISymbolicClassificationModel)model; 37 59 var classificationProblemData = (IClassificationProblemData)problemData; … … 47 69 } 48 70 49 varreplacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);71 replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows); 50 72 var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue }; 51 73 … … 63 85 if (errorState != OnlineCalculatorError.None) newQuality = 0.0; 64 86 65 returnoriginalQuality - newQuality;87 impactValue = originalQuality - newQuality; 66 88 } 67 68 89 } 69 90 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r10368 r10469 120 120 <Compile Include="Plugin.cs" /> 121 121 <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" /> 122 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" /> 123 <Compile Include="SymbolicRegressionPruningAnalyzer.cs" /> 124 122 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" /> 123 <Compile Include="SymbolicRegressionPruningAnalyzer.cs" /> 124 <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" /> 125 125 <Compile Include="SingleObjective\SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs" /> 126 126 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs" /> … … 145 145 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs" /> 146 146 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" /> 147 <Compile Include="SymbolicRegressionPruningOperator.cs" /> 147 148 <Compile Include="SymbolicRegressionSolution.cs" /> 148 149 <Compile Include="SymbolicRegressionSolutionImpactValuesCalculator.cs" /> -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPruningAnalyzer.cs
r10378 r10469 1 using System; 1 #region License Information 2 3 /* HeuristicLab 4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 5 * 6 * This file is part of HeuristicLab. 7 * 8 * HeuristicLab is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * HeuristicLab is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #endregion 23 2 24 using HeuristicLab.Common; 3 25 using HeuristicLab.Core; 4 using HeuristicLab. Encodings.SymbolicExpressionTreeEncoding;26 using HeuristicLab.Parameters; 5 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 6 28 … … 9 31 [StorableClass] 10 32 public sealed class SymbolicRegressionPruningAnalyzer : SymbolicDataAnalysisSingleObjectivePruningAnalyzer { 33 private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator"; 34 private const string PruningOperatorParameterName = "PruningOperator"; 11 35 private SymbolicRegressionPruningAnalyzer(SymbolicRegressionPruningAnalyzer original, Cloner cloner) 12 36 : base(original, cloner) { … … 20 44 21 45 public SymbolicRegressionPruningAnalyzer() { 22 impactValuesCalculator = new SymbolicRegressionSolutionImpactValuesCalculator(); 23 } 24 25 protected override ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, 26 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double lowerEstimationLimit = Double.MinValue, 27 double upperEstimationLimit = Double.MaxValue) { 28 return new SymbolicRegressionModel(tree, interpreter, lowerEstimationLimit, upperEstimationLimit); 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())); 29 48 } 30 49 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionImpactValuesCalculator.cs
r9840 r10469 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 25 using HeuristicLab.Core; 24 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 28 26 29 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 30 [StorableClass] 31 [Item("SymbolicRegressionSolutionImpactValuesCalculator", "Calculate symbolic expression tree node impact values for regression problems.")] 27 32 public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator { 33 public SymbolicRegressionSolutionImpactValuesCalculator() { } 34 35 protected SymbolicRegressionSolutionImpactValuesCalculator(SymbolicRegressionSolutionImpactValuesCalculator original, Cloner cloner) 36 : base(original, cloner) { } 37 public override IDeepCloneable Clone(Cloner cloner) { 38 return new SymbolicRegressionSolutionImpactValuesCalculator(this, cloner); 39 } 40 41 [StorableConstructor] 42 protected SymbolicRegressionSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { } 28 43 public override double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows) { 29 44 var regressionModel = (ISymbolicRegressionModel)model; … … 34 49 35 50 public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN) { 51 double impactValue, replacementValue; 52 CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, originalQuality); 53 return impactValue; 54 } 55 56 public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, 57 IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, 58 double originalQuality = Double.NaN) { 36 59 var regressionModel = (ISymbolicRegressionModel)model; 37 60 var regressionProblemData = (IRegressionProblemData)problemData; … … 47 70 } 48 71 49 varreplacementValue = CalculateReplacementValue(regressionModel, node, regressionProblemData, rows);72 replacementValue = CalculateReplacementValue(regressionModel, node, regressionProblemData, rows); 50 73 var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue }; 51 74 … … 63 86 if (errorState != OnlineCalculatorError.None) newQuality = 0.0; 64 87 65 returnoriginalQuality - newQuality;88 impactValue = originalQuality - newQuality; 66 89 } 67 68 90 } 69 91 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs
r10428 r10469 5 5 using HeuristicLab.Core; 6 6 using HeuristicLab.Data; 7 using HeuristicLab. Encodings.SymbolicExpressionTreeEncoding;8 using HeuristicLab.Optimization ;7 using HeuristicLab.Operators; 8 using HeuristicLab.Optimization.Operators; 9 9 using HeuristicLab.Parameters; 10 10 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 14 14 [Item("SymbolicDataAnalysisSingleObjectivePruningAnalyzer", "An analyzer that prunes introns from trees in single objective symbolic data analysis problems.")] 15 15 public abstract class SymbolicDataAnalysisSingleObjectivePruningAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer { 16 #region parameter names 16 17 private const string ProblemDataParameterName = "ProblemData"; 17 private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";18 19 18 private const string UpdateIntervalParameterName = "UpdateInverval"; 20 19 private const string UpdateCounterParameterName = "UpdateCounter"; 21 22 20 private const string PopulationSliceParameterName = "PopulationSlice"; 23 21 private const string PruningProbabilityParameterName = "PruningProbability"; 24 25 private const string NumberOfPrunedSubtreesParameterName = "PrunedSubtrees"; 26 private const string NumberOfPrunedTreesParameterName = "PrunedTrees"; 27 22 private const string TotalNumberOfPrunedSubtreesParameterName = "Number of pruned subtrees"; 23 private const string TotalNumberOfPrunedTreesParameterName = "Number of pruned trees"; 28 24 private const string RandomParameterName = "Random"; 29 private const string EstimationLimitsParameterName = "EstimationLimits";30 31 25 private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes"; 32 26 private const string NodeImpactThresholdParameterName = "ImpactThreshold"; 33 34 private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition"; 35 36 private bool reentry; 37 [Storable] 38 protected ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator; 39 27 private const string PruningOperatorParameterName = "PruningOperator"; 28 private const string ResultsParameterName = "Results"; 29 #endregion 30 #region private members 31 private DataReducer prunedSubtreesReducer; 32 private DataReducer prunedTreesReducer; 33 private DataTableValuesCollector valuesCollector; 34 private ResultsCollector resultsCollector; 35 private EmptyOperator emptyOp; 36 #endregion 40 37 #region parameter properties 38 public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter { 39 get { return (IValueParameter<SymbolicDataAnalysisExpressionPruningOperator>)Parameters[PruningOperatorParameterName]; } 40 } 41 41 public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter { 42 42 get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; } … … 45 45 get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; } 46 46 } 47 public ILookupParameter<DoubleLimit> EstimationLimitsParameter {48 get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }49 }50 47 public ILookupParameter<IRandom> RandomParameter { 51 48 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } … … 53 50 private ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter { 54 51 get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; } 55 }56 public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {57 get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }58 }59 private ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {60 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }61 52 } 62 53 public IValueParameter<IntValue> UpdateIntervalParameter { … … 72 63 get { return (IValueParameter<DoubleValue>)Parameters[PruningProbabilityParameterName]; } 73 64 } 74 public IFixedValueParameter<DoubleValue> NumberOfPrunedSubtreesParameter {75 get { return (IFixedValueParameter<DoubleValue>)Parameters[NumberOfPrunedSubtreesParameterName]; }76 }77 public IFixedValueParameter<DoubleValue> NumberOfPrunedTreesParameter {78 get { return (IFixedValueParameter<DoubleValue>)Parameters[NumberOfPrunedTreesParameterName]; }79 }80 65 #endregion 81 66 #region properties 67 protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } } 82 68 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } } 83 protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } }84 protected ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } }85 69 protected IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } } 86 70 protected IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } } 87 71 protected DoubleRange PopulationSlice { get { return PopulationSliceParameter.Value; } } 88 72 protected DoubleValue PruningProbability { get { return PruningProbabilityParameter.Value; } } 89 protected DoubleValue PrunedSubtrees { get { return NumberOfPrunedSubtreesParameter.Value; } }90 protected DoubleValue PrunedTrees { get { return NumberOfPrunedTreesParameter.Value; } }91 protected DoubleLimit EstimationLimits { get { return EstimationLimitsParameter.ActualValue; } }92 73 protected IRandom Random { get { return RandomParameter.ActualValue; } } 93 74 protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } } 94 75 protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } } 95 76 #endregion 96 97 77 #region IStatefulItem members 98 78 public override void InitializeState() { 99 79 base.InitializeState(); 100 80 UpdateCounter.Value = 0; 101 PrunedSubtrees.Value = 0;102 PrunedTrees.Value = 0;103 81 } 104 82 public override void ClearState() { 105 83 base.ClearState(); 106 84 UpdateCounter.Value = 0; 107 PrunedSubtrees.Value = 0;108 PrunedTrees.Value = 0;109 85 } 110 86 #endregion … … 112 88 [StorableConstructor] 113 89 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(bool deserializing) : base(deserializing) { } 114 [StorableHook(HookType.AfterDeserialization)]115 private void AfterDeserialization() {116 if (!Parameters.ContainsKey(FitnessCalculationPartitionParameterName))117 Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, ""));118 }119 90 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner) 120 91 : base(original, cloner) { 121 impactValuesCalculator = original.impactValuesCalculator; 92 this.prunedSubtreesReducer = (DataReducer)original.prunedSubtreesReducer.Clone(); 93 this.prunedTreesReducer = (DataReducer)original.prunedTreesReducer.Clone(); 94 this.valuesCollector = (DataTableValuesCollector)original.valuesCollector.Clone(); 95 this.resultsCollector = (ResultsCollector)original.resultsCollector.Clone(); 122 96 } 123 97 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer() { 98 #region add parameters 124 99 Parameters.Add(new ValueParameter<DoubleRange>(PopulationSliceParameterName, new DoubleRange(0.75, 1))); 125 100 Parameters.Add(new ValueParameter<DoubleValue>(PruningProbabilityParameterName, new DoubleValue(0.5))); 126 // analyzer parameters127 101 Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1))); 128 102 Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called", new IntValue(0))); 129 103 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName)); 130 104 Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName)); 131 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));132 133 Parameters.Add(new FixedValueParameter<DoubleValue>(NumberOfPrunedSubtreesParameterName, new DoubleValue(0)));134 Parameters.Add(new FixedValueParameter<DoubleValue>(NumberOfPrunedTreesParameterName, new DoubleValue(0)));135 Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));136 105 Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, new DoubleValue(0.0))); 137 106 Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, new BoolValue(false))); 138 Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "")); 107 #endregion 108 } 109 110 private void InitializeOperators() { 111 prunedSubtreesReducer = new DataReducer(); 112 prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName; 113 prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values 114 prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter 115 prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName; 116 117 prunedTreesReducer = new DataReducer(); 118 prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName; 119 prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); 120 prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); 121 prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName; 122 123 valuesCollector = new DataTableValuesCollector(); 124 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName)); 125 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName)); 126 valuesCollector.DataTableParameter.ActualName = "Population pruning"; 127 128 resultsCollector = new ResultsCollector(); 129 resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning")); 130 resultsCollector.ResultsParameter.ActualName = ResultsParameterName; 131 132 emptyOp = new EmptyOperator(); 133 } 134 135 // 136 /// <summary> 137 /// Computes the closed interval bounding the portion of the population that is to be pruned. 138 /// </summary> 139 /// <returns>Returns an int range [start, end]</returns> 140 private IntRange GetSliceBounds() { 141 var count = ExecutionContext.Scope.SubScopes.Count; 142 var start = (int)Math.Round(PopulationSlice.Start * count); 143 var end = (int)Math.Round(PopulationSlice.End * count); 144 if (end > count) end = count; 145 146 if (start >= end) throw new ArgumentOutOfRangeException("Invalid PopulationSlice bounds."); 147 return new IntRange(start, end); 148 } 149 150 private IOperation CreatePruningOperation() { 151 var oc = new OperationCollection { Parallel = true }; 152 var range = GetSliceBounds(); 153 var qualities = Quality.Select(x => x.Value).ToArray(); 154 var indices = Enumerable.Range(0, qualities.Length).ToArray(); 155 Array.Sort(qualities, indices); 156 if (!Maximization.Value) Array.Reverse(indices); 157 158 var subscopes = ExecutionContext.Scope.SubScopes; 159 160 for (int i = 0; i < subscopes.Count; ++i) { 161 IOperator op; 162 if (range.Start <= i && i < range.End && Random.NextDouble() <= PruningProbability.Value) 163 op = PruningOperator; 164 else op = emptyOp; 165 var index = indices[i]; 166 var subscope = subscopes[index]; 167 oc.Add(ExecutionContext.CreateChildOperation(op, subscope)); 168 } 169 return oc; 139 170 } 140 171 141 172 public override IOperation Apply() { 142 if (reentry) { 143 UpdateCounter.Value++; 173 UpdateCounter.Value++; 174 if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply(); 175 UpdateCounter.Value = 0; 144 176 145 if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply(); 146 UpdateCounter.Value = 0; 177 if (prunedSubtreesReducer == null || prunedTreesReducer == null || valuesCollector == null || resultsCollector == null) { InitializeOperators(); } 147 178 148 var trees = SymbolicExpressionTreeParameter.ActualValue.ToList(); 149 var qualities = QualityParameter.ActualValue.ToList(); 179 var prune = CreatePruningOperation(); 180 var reducePrunedSubtrees = ExecutionContext.CreateChildOperation(prunedSubtreesReducer); 181 var reducePrunedTrees = ExecutionContext.CreateChildOperation(prunedTreesReducer); 182 var collectValues = ExecutionContext.CreateChildOperation(valuesCollector); 183 var collectResults = ExecutionContext.CreateChildOperation(resultsCollector); 150 184 151 var population = trees.Zip(qualities, (tree, quality) => new { Tree = tree, Quality = quality }).ToList(); 152 Func<double, double, int> compare = (a, b) => Maximization.Value ? a.CompareTo(b) : b.CompareTo(a); 153 population.Sort((a, b) => compare(a.Quality.Value, b.Quality.Value)); 154 155 var start = (int)Math.Round(PopulationSlice.Start * trees.Count); 156 var end = (int)Math.Round(PopulationSlice.End * trees.Count); 157 158 if (end == population.Count) end--; 159 160 if (start >= end || end >= population.Count) throw new Exception("Invalid PopulationSlice bounds."); 161 162 PrunedSubtrees.Value = 0; 163 PrunedTrees.Value = 0; 164 165 reentry = false; 166 167 var operations = new OperationCollection { Parallel = true }; 168 foreach (var p in population.Skip(start).Take(end)) { 169 if (Random.NextDouble() > PruningProbability.Value) continue; 170 var op = new SymbolicDataAnalysisExpressionPruningOperator { 171 Model = CreateModel(p.Tree, Interpreter, EstimationLimits.Lower, EstimationLimits.Upper), 172 ImpactsCalculator = impactValuesCalculator, 173 ProblemData = ProblemData, 174 Random = Random, 175 PruneOnlyZeroImpactNodes = PruneOnlyZeroImpactNodes.Value, 176 NodeImpactThreshold = NodeImpactThreshold.Value, 177 FitnessCalculationPartition = FitnessCalculationPartition 178 }; 179 operations.Add(ExecutionContext.CreateChildOperation(op, ExecutionContext.Scope)); 180 } 181 return new OperationCollection { operations, ExecutionContext.CreateOperation(this) }; 182 } 183 184 DataTable table; 185 186 if (ResultCollection.ContainsKey("Population Pruning")) { 187 table = (DataTable)ResultCollection["Population Pruning"].Value; 188 } else { 189 table = new DataTable("Population Pruning"); 190 table.Rows.Add(new DataRow("Pruned Trees") { VisualProperties = { StartIndexZero = true } }); 191 table.Rows.Add(new DataRow("Pruned Subtrees") { VisualProperties = { StartIndexZero = true } }); 192 ResultCollection.Add(new Result("Population Pruning", table)); 193 } 194 195 table.Rows["Pruned Trees"].Values.Add(PrunedTrees.Value); 196 table.Rows["Pruned Subtrees"].Values.Add(PrunedSubtrees.Value); 197 198 reentry = true; 199 200 return base.Apply(); 185 return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() }; 201 186 } 202 203 protected abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,204 double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue);205 187 } 206 188 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisImpactValuesCalculator.cs
r8946 r10469 1 1 using System.Collections.Generic; 2 using HeuristicLab.Core; 2 3 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 3 4 4 5 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 5 public interface ISymbolicDataAnalysisSolutionImpactValuesCalculator {6 public interface ISymbolicDataAnalysisSolutionImpactValuesCalculator : IItem { 6 7 double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows); 7 8 double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN); 9 void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, 10 IEnumerable<int> rows, out double impactValue, out double replacementValue, double originalQuality = double.NaN); 8 11 } 9 12 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs
r10417 r10469 34 34 [StorableClass] 35 35 [Item("SymbolicExpressionTreePruningOperator", "An operator that replaces introns with constant values in a symbolic expression tree.")] 36 public class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator { 37 private const string NumberOfPrunedSubtreesParameterName = "PrunedSubtrees"; 38 private const string NumberOfPrunedTreesParameterName = "PrunedTrees"; 36 public abstract class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator { 37 #region parameter names 38 private const string ProblemDataParameterName = "ProblemData"; 39 private const string SymbolicDataAnalysisModelParameterName = "SymbolicDataAnalysisModel"; 40 private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator"; 41 private const string PrunedSubtreesParameterName = "PrunedSubtrees"; 42 private const string PrunedTreesParameterName = "PrunedTrees"; 43 private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition"; 44 private const string NodeImpactThresholdParameterName = "ImpactThreshold"; 45 private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes"; 46 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; // the tree to be pruned 47 private const string QualityParameterName = "Quality"; // the quality 48 private const string EstimationLimitsParameterName = "EstimationLimits"; 49 private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter"; 50 #endregion 51 39 52 #region parameter properties 40 public ILookupParameter< DoubleValue> NumberOfPrunedSubtreesParameter {41 get { return (ILookupParameter< DoubleValue>)Parameters[NumberOfPrunedSubtreesParameterName]; }53 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 54 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 42 55 } 43 public ILookupParameter<DoubleValue> NumberOfPrunedTreesParameter { 44 get { return (ILookupParameter<DoubleValue>)Parameters[NumberOfPrunedTreesParameterName]; } 56 public ILookupParameter<DoubleValue> QualityParameter { 57 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 58 } 59 public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter { 60 get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; } 61 } 62 public IValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator> ImpactValuesCalculatorParameter { 63 get { return (IValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>)Parameters[ImpactValuesCalculatorParameterName]; } 64 } 65 public ILookupParameter<IntRange> FitnessCalculationPartitionParameter { 66 get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; } 67 } 68 public ILookupParameter<IntValue> PrunedSubtreesParameter { 69 get { return (ILookupParameter<IntValue>)Parameters[PrunedSubtreesParameterName]; } 70 } 71 public ILookupParameter<IntValue> PrunedTreesParameter { 72 get { return (ILookupParameter<IntValue>)Parameters[PrunedTreesParameterName]; } 73 } 74 public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter { 75 get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; } 76 } 77 public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter { 78 get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; } 79 } 80 public ILookupParameter<DoubleLimit> EstimationLimitsParameter { 81 get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 82 } 83 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter { 84 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; } 45 85 } 46 86 #endregion 47 87 #region properties 48 private DoubleValue PrunedSubtrees { get { return NumberOfPrunedSubtreesParameter.ActualValue; } } 49 private DoubleValue PrunedTrees { get { return NumberOfPrunedTreesParameter.ActualValue; } } 88 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } } 89 protected ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { get { return ImpactValuesCalculatorParameter.Value; } } 90 protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } } 91 protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } } 92 protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } } 93 protected ISymbolicExpressionTree SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } } 94 protected DoubleValue Quality { get { return QualityParameter.ActualValue; } } 95 protected DoubleLimit EstimationLimits { get { return EstimationLimitsParameter.ActualValue; } } 96 protected ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } } 50 97 #endregion 51 98 52 99 [StorableConstructor] 53 100 protected SymbolicDataAnalysisExpressionPruningOperator(bool deserializing) : base(deserializing) { } 54 public override IDeepCloneable Clone(Cloner cloner) { 55 return new SymbolicDataAnalysisExpressionPruningOperator(this, cloner); 101 protected SymbolicDataAnalysisExpressionPruningOperator(SymbolicDataAnalysisExpressionPruningOperator original, Cloner cloner) 102 : base(original, cloner) { } 103 104 protected SymbolicDataAnalysisExpressionPruningOperator() { 105 #region add parameters 106 Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName)); 107 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisModel>(SymbolicDataAnalysisModelParameterName)); 108 Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName)); 109 Parameters.Add(new LookupParameter<IntValue>(PrunedSubtreesParameterName, "A counter of how many subtrees were replaced.")); 110 Parameters.Add(new LookupParameter<IntValue>(PrunedTreesParameterName, "A counter of how many trees were pruned.")); 111 Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, "Specify whether or not only zero impact nodes should be pruned.")); 112 Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, "Specifies an impact value threshold below which nodes should be pruned.")); 113 Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName)); 114 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName)); 115 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName)); 116 Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName)); 117 #endregion 56 118 } 57 protected SymbolicDataAnalysisExpressionPruningOperator(SymbolicDataAnalysisExpressionPruningOperator original, Cloner cloner)58 : base(original, cloner) {59 }60 61 public SymbolicDataAnalysisExpressionPruningOperator() {62 Parameters.Add(new LookupParameter<DoubleValue>(NumberOfPrunedSubtreesParameterName));63 Parameters.Add(new LookupParameter<DoubleValue>(NumberOfPrunedTreesParameterName));64 }65 66 public ISymbolicDataAnalysisModel Model { get; set; }67 public IDataAnalysisProblemData ProblemData { get; set; }68 public ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactsCalculator { get; set; }69 70 public IntRange FitnessCalculationPartition { get; set; }71 public IRandom Random { get; set; }72 73 public bool PruneOnlyZeroImpactNodes { get; set; }74 public double NodeImpactThreshold { get; set; }75 76 119 public override IOperation Apply() { 77 int prunedSubtrees = 0; 78 79 var nodes = Model.SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); 120 var model = CreateModel(); 121 var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); 80 122 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size).ToList(); 81 123 82 for (int j = 0; j < nodes.Count; ++j) { 83 var node = nodes[j]; 124 var prunedSubtrees = 0; 125 var prunedTrees = 0; 126 127 double quality = Evaluate(model); 128 129 for (int i = 0; i < nodes.Count; ++i) { 130 var node = nodes[i]; 84 131 if (node is ConstantTreeNode) continue; 85 132 86 var impact = ImpactsCalculator.CalculateImpactValue(Model, node, ProblemData, rows); 133 double impactValue, replacementValue; 134 ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality); 87 135 88 if (PruneOnlyZeroImpactNodes) { 89 if (!impact.IsAlmost(0.0)) continue; 90 } else { 91 if (NodeImpactThreshold < impact) continue; 92 } 136 if (PruneOnlyZeroImpactNodes.Value && (!impactValue.IsAlmost(0.0))) continue; 137 else if (NodeImpactThreshold.Value < impactValue) continue; 93 138 94 var replacementValue = ImpactsCalculator.CalculateReplacementValue(Model, node, ProblemData, rows);95 139 var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue }; 96 140 ReplaceWithConstant(node, constantNode); 97 j += node.GetLength() - 1; // skip subtrees under the node that was folded 141 i += node.GetLength() - 1; // skip subtrees under the node that was folded 142 143 quality -= impactValue; 98 144 99 145 prunedSubtrees++; 100 146 } 101 147 102 if (prunedSubtrees > 0) {103 lock (PrunedSubtrees) { PrunedSubtrees.Value += prunedSubtrees; }104 lock (PrunedTrees) { PrunedTrees.Value += 1; }105 } 148 if (prunedSubtrees > 0) prunedTrees = 1; 149 PrunedSubtreesParameter.ActualValue = new IntValue(prunedSubtrees); 150 PrunedTreesParameter.ActualValue = new IntValue(prunedTrees); 151 106 152 return base.Apply(); 107 153 } … … 112 158 parent.InsertSubtree(i, replacement); 113 159 } 160 protected abstract ISymbolicDataAnalysisModel CreateModel(); 161 protected abstract double Evaluate(IDataAnalysisModel model); 114 162 } 115 163 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs
r9456 r10469 22 22 using System.Collections.Generic; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 27 26 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 27 public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator : ISymbolicDataAnalysisSolutionImpactValuesCalculator { 29 [StorableClass] 30 [Item("SymbolicDataAnalysisSolutionImpactValuesCalculator", "Calculates the impact values and replacements values for symbolic expression tree nodes.")] 31 public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator : Item, ISymbolicDataAnalysisSolutionImpactValuesCalculator { 32 protected SymbolicDataAnalysisSolutionImpactValuesCalculator() { } 33 34 protected SymbolicDataAnalysisSolutionImpactValuesCalculator(SymbolicDataAnalysisSolutionImpactValuesCalculator original, Cloner cloner) 35 : base(original, cloner) { } 36 [StorableConstructor] 37 protected SymbolicDataAnalysisSolutionImpactValuesCalculator(bool deserializing) : base(deserializing) { } 28 38 public abstract double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows); 29 39 public abstract double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN); 40 public abstract void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, double originalQuality = double.NaN); 30 41 31 42 protected static double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
Note: See TracChangeset
for help on using the changeset viewer.