[11025] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[12012] | 4 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11025] | 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 |
|
---|
[12189] | 24 | using System.Collections.Generic;
|
---|
[11025] | 25 | using System.Linq;
|
---|
[10469] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[12189] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[12720] | 29 | using HeuristicLab.Parameters;
|
---|
[10469] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
| 33 | [StorableClass]
|
---|
| 34 | [Item("SymbolicRegressionPruningOperator", "An operator which prunes symbolic regression trees.")]
|
---|
| 35 | public class SymbolicRegressionPruningOperator : SymbolicDataAnalysisExpressionPruningOperator {
|
---|
[12720] | 36 | private const string EvaluatorParameterName = "Evaluator";
|
---|
| 37 |
|
---|
| 38 | #region parameter properties
|
---|
| 39 | public ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {
|
---|
| 40 | get { return (ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }
|
---|
| 41 | }
|
---|
| 42 | #endregion
|
---|
| 43 |
|
---|
[10469] | 44 | protected SymbolicRegressionPruningOperator(SymbolicRegressionPruningOperator original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) {
|
---|
| 46 | }
|
---|
| 47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 48 | return new SymbolicRegressionPruningOperator(this, cloner);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | [StorableConstructor]
|
---|
| 52 | protected SymbolicRegressionPruningOperator(bool deserializing) : base(deserializing) { }
|
---|
| 53 |
|
---|
[12189] | 54 | public SymbolicRegressionPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator)
|
---|
| 55 | : base(impactValuesCalculator) {
|
---|
[12720] | 56 | Parameters.Add(new LookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>(EvaluatorParameterName));
|
---|
[10469] | 57 | }
|
---|
| 58 |
|
---|
[12744] | 59 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 60 | private void AfterDeserialization() {
|
---|
| 61 | // BackwardsCompatibility3.3
|
---|
| 62 | #region Backwards compatible code, remove with 3.4
|
---|
| 63 | base.ImpactValuesCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();
|
---|
| 64 | if (!Parameters.ContainsKey(EvaluatorParameterName)) {
|
---|
| 65 | Parameters.Add(new LookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>(EvaluatorParameterName));
|
---|
| 66 | }
|
---|
| 67 | #endregion
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[12189] | 70 | protected override ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {
|
---|
| 71 | return new SymbolicRegressionModel(tree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
|
---|
[10469] | 72 | }
|
---|
| 73 |
|
---|
| 74 | protected override double Evaluate(IDataAnalysisModel model) {
|
---|
[12720] | 75 | var regressionModel = (ISymbolicRegressionModel)model;
|
---|
[12358] | 76 | var regressionProblemData = (IRegressionProblemData)ProblemDataParameter.ActualValue;
|
---|
[12720] | 77 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
| 78 | var fitnessEvaluationPartition = FitnessCalculationPartitionParameter.ActualValue;
|
---|
| 79 | var rows = Enumerable.Range(fitnessEvaluationPartition.Start, fitnessEvaluationPartition.Size);
|
---|
| 80 | return evaluator.Evaluate(this.ExecutionContext, regressionModel.SymbolicExpressionTree, regressionProblemData, rows);
|
---|
[12189] | 81 | }
|
---|
| 82 |
|
---|
| 83 | public static ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, SymbolicRegressionSolutionImpactValuesCalculator impactValuesCalculator, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IRegressionProblemData problemData, DoubleLimit estimationLimits, IEnumerable<int> rows, double nodeImpactThreshold = 0.0, bool pruneOnlyZeroImpactNodes = false) {
|
---|
| 84 | var clonedTree = (ISymbolicExpressionTree)tree.Clone();
|
---|
| 85 | var model = new SymbolicRegressionModel(clonedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
|
---|
[12461] | 86 | var nodes = clonedTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList(); // skip the nodes corresponding to the ProgramRootSymbol and the StartSymbol
|
---|
[12189] | 87 |
|
---|
[12720] | 88 | double qualityForImpactsCalculation = double.NaN; // pass a NaN value initially so the impact calculator will calculate the quality
|
---|
| 89 |
|
---|
[12189] | 90 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
| 91 | var node = nodes[i];
|
---|
| 92 | if (node is ConstantTreeNode) continue;
|
---|
| 93 |
|
---|
| 94 | double impactValue, replacementValue;
|
---|
[12720] | 95 | double newQualityForImpactsCalculation;
|
---|
| 96 | impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, out newQualityForImpactsCalculation, qualityForImpactsCalculation);
|
---|
[12189] | 97 |
|
---|
[12358] | 98 | if (pruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
|
---|
| 99 | if (!pruneOnlyZeroImpactNodes && impactValue > nodeImpactThreshold) continue;
|
---|
[12189] | 100 |
|
---|
| 101 | var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
|
---|
| 102 | constantNode.Value = replacementValue;
|
---|
| 103 |
|
---|
| 104 | ReplaceWithConstant(node, constantNode);
|
---|
| 105 | i += node.GetLength() - 1; // skip subtrees under the node that was folded
|
---|
| 106 |
|
---|
[12720] | 107 | qualityForImpactsCalculation = newQualityForImpactsCalculation;
|
---|
[12189] | 108 | }
|
---|
| 109 | return model.SymbolicExpressionTree;
|
---|
| 110 | }
|
---|
[10469] | 111 | }
|
---|
| 112 | }
|
---|