[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;
|
---|
[10469] | 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
| 33 | [StorableClass]
|
---|
| 34 | [Item("SymbolicClassificationPruningOperator", "An operator which prunes symbolic classificaton trees.")]
|
---|
| 35 | public class SymbolicClassificationPruningOperator : SymbolicDataAnalysisExpressionPruningOperator {
|
---|
| 36 | private const string ModelCreatorParameterName = "ModelCreator";
|
---|
[12720] | 37 | private const string EvaluatorParameterName = "Evaluator";
|
---|
[10469] | 38 |
|
---|
| 39 | #region parameter properties
|
---|
| 40 | public ILookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
|
---|
| 41 | get { return (ILookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
|
---|
| 42 | }
|
---|
[12720] | 43 |
|
---|
| 44 | public ILookupParameter<ISymbolicClassificationSingleObjectiveEvaluator> EvaluatorParameter {
|
---|
| 45 | get {
|
---|
| 46 | return (ILookupParameter<ISymbolicClassificationSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName];
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
[10469] | 49 | #endregion
|
---|
| 50 |
|
---|
[12358] | 51 | protected SymbolicClassificationPruningOperator(SymbolicClassificationPruningOperator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 52 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationPruningOperator(this, cloner); }
|
---|
[11025] | 53 |
|
---|
[10469] | 54 | [StorableConstructor]
|
---|
| 55 | protected SymbolicClassificationPruningOperator(bool deserializing) : base(deserializing) { }
|
---|
| 56 |
|
---|
[12189] | 57 | public SymbolicClassificationPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator)
|
---|
| 58 | : base(impactValuesCalculator) {
|
---|
[10469] | 59 | Parameters.Add(new LookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName));
|
---|
[12720] | 60 | Parameters.Add(new LookupParameter<ISymbolicClassificationSingleObjectiveEvaluator>(EvaluatorParameterName));
|
---|
[10469] | 61 | }
|
---|
| 62 |
|
---|
[12744] | 63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 64 | private void AfterDeserialization() {
|
---|
| 65 | // BackwardsCompatibility3.3
|
---|
| 66 | #region Backwards compatible code, remove with 3.4
|
---|
| 67 | base.ImpactValuesCalculator = new SymbolicClassificationSolutionImpactValuesCalculator();
|
---|
| 68 | if (!Parameters.ContainsKey(EvaluatorParameterName)) {
|
---|
| 69 | Parameters.Add(new LookupParameter<ISymbolicClassificationSingleObjectiveEvaluator>(EvaluatorParameterName));
|
---|
| 70 | }
|
---|
| 71 | #endregion
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[12189] | 74 | protected override ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits) {
|
---|
| 75 | var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(tree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
|
---|
| 76 | var classificationProblemData = (IClassificationProblemData)problemData;
|
---|
| 77 | var rows = classificationProblemData.TrainingIndices;
|
---|
| 78 | model.RecalculateModelParameters(classificationProblemData, rows);
|
---|
[10469] | 79 | return model;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | protected override double Evaluate(IDataAnalysisModel model) {
|
---|
[12720] | 83 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
| 84 | var classificationModel = (ISymbolicClassificationModel)model;
|
---|
[12358] | 85 | var classificationProblemData = (IClassificationProblemData)ProblemDataParameter.ActualValue;
|
---|
| 86 | var rows = Enumerable.Range(FitnessCalculationPartitionParameter.ActualValue.Start, FitnessCalculationPartitionParameter.ActualValue.Size);
|
---|
[12720] | 87 | return evaluator.Evaluate(this.ExecutionContext, classificationModel.SymbolicExpressionTree, classificationProblemData, rows);
|
---|
[12189] | 88 | }
|
---|
| 89 |
|
---|
| 90 | public static ISymbolicExpressionTree Prune(ISymbolicExpressionTree tree, ISymbolicClassificationModelCreator modelCreator,
|
---|
| 91 | SymbolicClassificationSolutionImpactValuesCalculator impactValuesCalculator, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
| 92 | IClassificationProblemData problemData, DoubleLimit estimationLimits, IEnumerable<int> rows,
|
---|
| 93 | double nodeImpactThreshold = 0.0, bool pruneOnlyZeroImpactNodes = false) {
|
---|
| 94 | var clonedTree = (ISymbolicExpressionTree)tree.Clone();
|
---|
| 95 | var model = modelCreator.CreateSymbolicClassificationModel(clonedTree, interpreter, estimationLimits.Lower, estimationLimits.Upper);
|
---|
| 96 |
|
---|
[12461] | 97 | var nodes = clonedTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
|
---|
[12720] | 98 | double qualityForImpactsCalculation = double.NaN;
|
---|
[12189] | 99 |
|
---|
| 100 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
| 101 | var node = nodes[i];
|
---|
| 102 | if (node is ConstantTreeNode) continue;
|
---|
| 103 |
|
---|
[12720] | 104 | double impactValue, replacementValue, newQualityForImpactsCalculation;
|
---|
| 105 | impactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, out newQualityForImpactsCalculation, qualityForImpactsCalculation);
|
---|
[12189] | 106 |
|
---|
[12358] | 107 | if (pruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
|
---|
| 108 | if (!pruneOnlyZeroImpactNodes && impactValue > nodeImpactThreshold) continue;
|
---|
[12189] | 109 |
|
---|
| 110 | var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
|
---|
| 111 | constantNode.Value = replacementValue;
|
---|
| 112 |
|
---|
| 113 | ReplaceWithConstant(node, constantNode);
|
---|
| 114 | i += node.GetLength() - 1; // skip subtrees under the node that was folded
|
---|
| 115 |
|
---|
[12720] | 116 | qualityForImpactsCalculation = newQualityForImpactsCalculation;
|
---|
[12189] | 117 | }
|
---|
| 118 | return model.SymbolicExpressionTree;
|
---|
| 119 | }
|
---|
[10469] | 120 | }
|
---|
| 121 | }
|
---|