Changeset 11025
- Timestamp:
- 06/18/14 15:27:38 (11 years ago)
- Location:
- trunk/sources
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationPruningOperator.cs
r10469 r11025 1 using System.Linq; 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 24 using System.Linq; 2 25 using HeuristicLab.Common; 3 26 using HeuristicLab.Core; 4 using HeuristicLab.Data;5 27 using HeuristicLab.Parameters; 6 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 12 34 private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator"; 13 35 private const string ModelCreatorParameterName = "ModelCreator"; 14 private const string ApplyLinearScalingParmameterName = "ApplyLinearScaling";15 36 16 37 #region parameter properties … … 18 39 get { return (ILookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; } 19 40 } 20 21 public ILookupParameter<BoolValue> ApplyLinearScalingParameter {22 get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParmameterName]; }23 }24 #endregion25 #region properties26 private ISymbolicClassificationModelCreator ModelCreator { get { return ModelCreatorParameter.ActualValue; } }27 private BoolValue ApplyLinearScaling { get { return ApplyLinearScalingParameter.ActualValue; } }28 41 #endregion 29 42 … … 31 44 : base(original, cloner) { 32 45 } 46 33 47 public override IDeepCloneable Clone(Cloner cloner) { 34 48 return new SymbolicClassificationPruningOperator(this, cloner); … … 44 58 45 59 protected override ISymbolicDataAnalysisModel CreateModel() { 46 var model = ModelCreator.CreateSymbolicClassificationModel(SymbolicExpressionTree, Interpreter, EstimationLimits.Lower, EstimationLimits.Upper); 47 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size); 60 var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(SymbolicExpressionTree, Interpreter, EstimationLimits.Lower, EstimationLimits.Upper); 48 61 var problemData = (IClassificationProblemData)ProblemData; 62 var rows = problemData.TrainingIndices; 49 63 model.RecalculateModelParameters(problemData, rows); 50 64 return model; … … 54 68 var classificationModel = (IClassificationModel)model; 55 69 var classificationProblemData = (IClassificationProblemData)ProblemData; 56 var trainingIndices = ProblemData.TrainingIndices.ToList();70 var trainingIndices = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size).ToArray(); 57 71 var estimatedValues = classificationModel.GetEstimatedClassValues(ProblemData.Dataset, trainingIndices); 58 72 var targetValues = ProblemData.Dataset.GetDoubleValues(classificationProblemData.TargetVariable, trainingIndices); 59 73 OnlineCalculatorError errorState; 60 var quality = Online PearsonsRSquaredCalculator.Calculate(targetValues, estimatedValues, out errorState);74 var quality = OnlineAccuracyCalculator.Calculate(targetValues, estimatedValues, out errorState); 61 75 if (errorState != OnlineCalculatorError.None) return double.NaN; 62 76 return quality; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPruningOperator.cs
r10469 r11025 1 using System.Linq; 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 24 using System.Linq; 2 25 using HeuristicLab.Common; 3 26 using HeuristicLab.Core; … … 10 33 public class SymbolicRegressionPruningOperator : SymbolicDataAnalysisExpressionPruningOperator { 11 34 private const string ImpactValuesCalculatorParameterName = "ImpactValuesCalculator"; 12 private const string ImpactValuesCalculatorParameterDescription = "The impact values calculator to be used for figuring out the node impacts.";13 14 private const string EvaluatorParameterName = "Evaluator";15 16 public ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {17 get { return (ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }18 }19 35 20 36 protected SymbolicRegressionPruningOperator(SymbolicRegressionPruningOperator original, Cloner cloner) … … 30 46 public SymbolicRegressionPruningOperator() { 31 47 var impactValuesCalculator = new SymbolicRegressionSolutionImpactValuesCalculator(); 32 Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, ImpactValuesCalculatorParameterDescription, impactValuesCalculator)); 33 Parameters.Add(new LookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>(EvaluatorParameterName)); 48 Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, "The impact values calculator to be used for figuring out the node impacts.", impactValuesCalculator)); 34 49 } 35 50 … … 41 56 var regressionModel = (IRegressionModel)model; 42 57 var regressionProblemData = (IRegressionProblemData)ProblemData; 43 var trainingIndices = ProblemData.TrainingIndices.ToList();58 var trainingIndices = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size).ToArray(); 44 59 var estimatedValues = regressionModel.GetEstimatedValues(ProblemData.Dataset, trainingIndices); // also bounds the values 45 60 var targetValues = ProblemData.Dataset.GetDoubleValues(regressionProblemData.TargetVariable, trainingIndices); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs
r11013 r11025 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 24 using System; 2 25 using System.Linq; 3 26 using HeuristicLab.Analysis; … … 29 52 private const string PopulationSizeParameterName = "PopulationSize"; 30 53 #endregion 54 31 55 #region private members 32 56 private DataReducer prunedSubtreesReducer; … … 36 60 private EmptyOperator emptyOp; 37 61 #endregion 62 38 63 #region parameter properties 39 64 public IValueParameter<SymbolicDataAnalysisExpressionPruningOperator> PruningOperatorParameter { … … 49 74 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 50 75 } 51 private ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {52 get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }53 }54 76 public IValueParameter<IntValue> UpdateIntervalParameter { 55 77 get { return (IValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; } … … 68 90 } 69 91 #endregion 92 70 93 #region properties 71 94 protected SymbolicDataAnalysisExpressionPruningOperator PruningOperator { get { return PruningOperatorParameter.Value; } } 72 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }73 95 protected IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } } 74 96 protected IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } } 75 97 protected DoubleRange PopulationSlice { get { return PopulationSliceParameter.Value; } } 76 98 protected DoubleValue PruningProbability { get { return PruningProbabilityParameter.Value; } } 77 protected IRandom Random { get { return RandomParameter.ActualValue; } } 78 protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } } 79 protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } } 80 #endregion 99 100 protected bool PruneOnlyZeroImpactNodes { 101 get { return PruneOnlyZeroImpactNodesParameter.Value.Value; } 102 set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; } 103 } 104 protected double NodeImpactThreshold { 105 get { return NodeImpactThresholdParameter.Value.Value; } 106 set { NodeImpactThresholdParameter.Value.Value = value; } 107 } 108 #endregion 109 81 110 #region IStatefulItem members 82 111 public override void InitializeState() { … … 92 121 [StorableConstructor] 93 122 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(bool deserializing) : base(deserializing) { } 123 94 124 protected SymbolicDataAnalysisSingleObjectivePruningAnalyzer(SymbolicDataAnalysisSingleObjectivePruningAnalyzer original, Cloner cloner) 95 125 : base(original, cloner) { … … 125 155 } 126 156 127 private void InitializeOperators() {128 prunedSubtreesReducer = new DataReducer();129 prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName;130 prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values131 prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter132 prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName;133 134 prunedTreesReducer = new DataReducer();135 prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName;136 prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);137 prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);138 prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName;139 140 valuesCollector = new DataTableValuesCollector();141 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName));142 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName));143 valuesCollector.DataTableParameter.ActualName = "Population pruning";144 145 resultsCollector = new ResultsCollector();146 resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning"));147 resultsCollector.ResultsParameter.ActualName = ResultsParameterName;148 149 emptyOp = new EmptyOperator();150 }151 152 157 // 153 158 /// <summary> … … 176 181 177 182 var subscopes = ExecutionContext.Scope.SubScopes; 183 var random = RandomParameter.ActualValue; 178 184 179 185 for (int i = 0; i < subscopes.Count; ++i) { 180 186 IOperator op; 181 if (range.Start <= i && i < range.End && Random.NextDouble() <= PruningProbability.Value)187 if (range.Start <= i && i < range.End && random.NextDouble() <= PruningProbability.Value) 182 188 op = PruningOperator; 183 189 else op = emptyOp; … … 204 210 return new OperationCollection { prune, reducePrunedSubtrees, reducePrunedTrees, collectValues, collectResults, base.Apply() }; 205 211 } 212 213 private void InitializeOperators() { 214 prunedSubtreesReducer = new DataReducer(); 215 prunedSubtreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedSubtreesParameter.ActualName; 216 prunedSubtreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); // sum all the pruned subtrees parameter values 217 prunedSubtreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter 218 prunedSubtreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedSubtreesParameterName; 219 220 prunedTreesReducer = new DataReducer(); 221 prunedTreesReducer.ParameterToReduce.ActualName = PruningOperator.PrunedTreesParameter.ActualName; 222 prunedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum); 223 prunedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); 224 prunedTreesReducer.TargetParameter.ActualName = TotalNumberOfPrunedTreesParameterName; 225 226 valuesCollector = new DataTableValuesCollector(); 227 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedSubtreesParameterName)); 228 valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>(TotalNumberOfPrunedTreesParameterName)); 229 valuesCollector.DataTableParameter.ActualName = "Population pruning"; 230 231 resultsCollector = new ResultsCollector(); 232 resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Population pruning")); 233 resultsCollector.ResultsParameter.ActualName = ResultsParameterName; 234 235 emptyOp = new EmptyOperator(); 236 } 206 237 } 207 238 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionPruningOperator.cs
r10469 r11025 85 85 } 86 86 #endregion 87 87 88 #region properties 88 89 protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } } 89 90 protected ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { get { return ImpactValuesCalculatorParameter.Value; } } 90 91 protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } } 91 protected BoolValue PruneOnlyZeroImpactNodes { get { return PruneOnlyZeroImpactNodesParameter.Value; } } 92 protected DoubleValue NodeImpactThreshold { get { return NodeImpactThresholdParameter.Value; } } 92 protected bool PruneOnlyZeroImpactNodes { 93 get { return PruneOnlyZeroImpactNodesParameter.Value.Value; } 94 set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; } 95 } 96 protected double NodeImpactThreshold { 97 get { return NodeImpactThresholdParameter.Value.Value; } 98 set { NodeImpactThresholdParameter.Value.Value = value; } 99 } 93 100 protected ISymbolicExpressionTree SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } } 94 101 protected DoubleValue Quality { get { return QualityParameter.ActualValue; } } … … 117 124 #endregion 118 125 } 126 127 protected abstract ISymbolicDataAnalysisModel CreateModel(); 128 129 protected abstract double Evaluate(IDataAnalysisModel model); 130 119 131 public override IOperation Apply() { 120 132 var model = CreateModel(); 121 133 var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); 122 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size).ToList(); 123 134 var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size); 124 135 var prunedSubtrees = 0; 125 136 var prunedTrees = 0; … … 134 145 ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality); 135 146 136 if (PruneOnlyZeroImpactNodes.Value && (!impactValue.IsAlmost(0.0))) continue; 137 else if (NodeImpactThreshold.Value < impactValue) continue; 147 if (PruneOnlyZeroImpactNodes) { 148 if (!impactValue.IsAlmost(0.0)) continue; 149 } else if (NodeImpactThreshold < impactValue) { 150 continue; 151 } 138 152 139 var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue }; 153 var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode(); 154 constantNode.Value = replacementValue; 155 140 156 ReplaceWithConstant(node, constantNode); 141 157 i += node.GetLength() - 1; // skip subtrees under the node that was folded … … 152 168 return base.Apply(); 153 169 } 170 154 171 private static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) { 155 172 var parent = original.Parent; … … 158 175 parent.InsertSubtree(i, replacement); 159 176 } 160 protected abstract ISymbolicDataAnalysisModel CreateModel();161 protected abstract double Evaluate(IDataAnalysisModel model);162 177 } 163 178 }
Note: See TracChangeset
for help on using the changeset viewer.