[10368] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[12012] | 4 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10368] | 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;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.Operators;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 34 | [StorableClass]
|
---|
| 35 | [Item("SymbolicExpressionTreePruningOperator", "An operator that replaces introns with constant values in a symbolic expression tree.")]
|
---|
[12378] | 36 | public abstract class SymbolicDataAnalysisExpressionPruningOperator : SingleSuccessorOperator, ISymbolicExpressionTreeOperator {
|
---|
[10469] | 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";
|
---|
[12378] | 43 | private const string PrunedNodesParameterName = "PrunedNodes";
|
---|
[10469] | 44 | private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
|
---|
| 45 | private const string NodeImpactThresholdParameterName = "ImpactThreshold";
|
---|
| 46 | private const string PruneOnlyZeroImpactNodesParameterName = "PruneOnlyZeroImpactNodes";
|
---|
| 47 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; // the tree to be pruned
|
---|
| 48 | private const string QualityParameterName = "Quality"; // the quality
|
---|
| 49 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
| 50 | private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
| 51 | #endregion
|
---|
| 52 |
|
---|
[10368] | 53 | #region parameter properties
|
---|
[10469] | 54 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 55 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
[10368] | 56 | }
|
---|
[10469] | 57 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 58 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
[10368] | 59 | }
|
---|
[10469] | 60 | public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
|
---|
| 61 | get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
| 62 | }
|
---|
| 63 | public IValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator> ImpactValuesCalculatorParameter {
|
---|
| 64 | get { return (IValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>)Parameters[ImpactValuesCalculatorParameterName]; }
|
---|
| 65 | }
|
---|
| 66 | public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
|
---|
| 67 | get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
|
---|
| 68 | }
|
---|
| 69 | public ILookupParameter<IntValue> PrunedSubtreesParameter {
|
---|
| 70 | get { return (ILookupParameter<IntValue>)Parameters[PrunedSubtreesParameterName]; }
|
---|
| 71 | }
|
---|
| 72 | public ILookupParameter<IntValue> PrunedTreesParameter {
|
---|
| 73 | get { return (ILookupParameter<IntValue>)Parameters[PrunedTreesParameterName]; }
|
---|
| 74 | }
|
---|
[12378] | 75 | public ILookupParameter<IntValue> PrunedNodesParameter {
|
---|
| 76 | get { return (ILookupParameter<IntValue>)Parameters[PrunedNodesParameterName]; }
|
---|
| 77 | }
|
---|
[10469] | 78 | public IFixedValueParameter<DoubleValue> NodeImpactThresholdParameter {
|
---|
| 79 | get { return (IFixedValueParameter<DoubleValue>)Parameters[NodeImpactThresholdParameterName]; }
|
---|
| 80 | }
|
---|
| 81 | public IFixedValueParameter<BoolValue> PruneOnlyZeroImpactNodesParameter {
|
---|
| 82 | get { return (IFixedValueParameter<BoolValue>)Parameters[PruneOnlyZeroImpactNodesParameterName]; }
|
---|
| 83 | }
|
---|
| 84 | public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
| 85 | get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
| 86 | }
|
---|
| 87 | public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
|
---|
| 88 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
|
---|
| 89 | }
|
---|
[10368] | 90 | #endregion
|
---|
[11025] | 91 |
|
---|
[10368] | 92 | #region properties
|
---|
[12378] | 93 | public ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator {
|
---|
| 94 | get { return ImpactValuesCalculatorParameter.Value; }
|
---|
| 95 | set { ImpactValuesCalculatorParameter.Value = value; }
|
---|
| 96 | }
|
---|
| 97 | public bool PruneOnlyZeroImpactNodes {
|
---|
[11025] | 98 | get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }
|
---|
| 99 | set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }
|
---|
| 100 | }
|
---|
[12378] | 101 | public double NodeImpactThreshold {
|
---|
[11025] | 102 | get { return NodeImpactThresholdParameter.Value.Value; }
|
---|
| 103 | set { NodeImpactThresholdParameter.Value.Value = value; }
|
---|
| 104 | }
|
---|
[10368] | 105 | #endregion
|
---|
[10417] | 106 |
|
---|
| 107 | [StorableConstructor]
|
---|
| 108 | protected SymbolicDataAnalysisExpressionPruningOperator(bool deserializing) : base(deserializing) { }
|
---|
| 109 | protected SymbolicDataAnalysisExpressionPruningOperator(SymbolicDataAnalysisExpressionPruningOperator original, Cloner cloner)
|
---|
[10469] | 110 | : base(original, cloner) { }
|
---|
[10368] | 111 |
|
---|
[12189] | 112 | protected SymbolicDataAnalysisExpressionPruningOperator(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactValuesCalculator) {
|
---|
[10469] | 113 | #region add parameters
|
---|
| 114 | Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName));
|
---|
| 115 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisModel>(SymbolicDataAnalysisModelParameterName));
|
---|
| 116 | Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName));
|
---|
[12378] | 117 | Parameters.Add(new LookupParameter<IntValue>(PrunedNodesParameterName, "A counter of how many nodes were pruned."));
|
---|
[10469] | 118 | Parameters.Add(new LookupParameter<IntValue>(PrunedSubtreesParameterName, "A counter of how many subtrees were replaced."));
|
---|
| 119 | Parameters.Add(new LookupParameter<IntValue>(PrunedTreesParameterName, "A counter of how many trees were pruned."));
|
---|
| 120 | Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, "Specify whether or not only zero impact nodes should be pruned."));
|
---|
| 121 | Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, "Specifies an impact value threshold below which nodes should be pruned."));
|
---|
| 122 | Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
|
---|
| 123 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
|
---|
| 124 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
|
---|
| 125 | Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName));
|
---|
[12189] | 126 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisSolutionImpactValuesCalculator>(ImpactValuesCalculatorParameterName, impactValuesCalculator));
|
---|
[10469] | 127 | #endregion
|
---|
[10368] | 128 | }
|
---|
[11025] | 129 |
|
---|
[12189] | 130 | protected abstract ISymbolicDataAnalysisModel CreateModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData problemData, DoubleLimit estimationLimits);
|
---|
[11025] | 131 |
|
---|
| 132 | protected abstract double Evaluate(IDataAnalysisModel model);
|
---|
| 133 |
|
---|
[10469] | 134 | public override IOperation Apply() {
|
---|
[12378] | 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);
|
---|
[10469] | 144 | var prunedSubtrees = 0;
|
---|
| 145 | var prunedTrees = 0;
|
---|
[12378] | 146 | var prunedNodes = 0;
|
---|
[10414] | 147 |
|
---|
[10469] | 148 | double quality = Evaluate(model);
|
---|
[10368] | 149 |
|
---|
[10469] | 150 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
| 151 | var node = nodes[i];
|
---|
[10368] | 152 | if (node is ConstantTreeNode) continue;
|
---|
| 153 |
|
---|
[10469] | 154 | double impactValue, replacementValue;
|
---|
[12378] | 155 | ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, problemData, rows, out impactValue, out replacementValue, quality);
|
---|
[10368] | 156 |
|
---|
[12378] | 157 | if (PruneOnlyZeroImpactNodes && !impactValue.IsAlmost(0.0)) continue;
|
---|
| 158 | if (!PruneOnlyZeroImpactNodes && impactValue > NodeImpactThreshold) continue;
|
---|
[10368] | 159 |
|
---|
[11025] | 160 | var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
|
---|
| 161 | constantNode.Value = replacementValue;
|
---|
| 162 |
|
---|
[12378] | 163 | var length = node.GetLength();
|
---|
[10368] | 164 | ReplaceWithConstant(node, constantNode);
|
---|
[12378] | 165 | i += length - 1; // skip subtrees under the node that was folded
|
---|
[10368] | 166 |
|
---|
[10469] | 167 | quality -= impactValue;
|
---|
[10368] | 168 | prunedSubtrees++;
|
---|
[12378] | 169 | prunedNodes += length;
|
---|
[10368] | 170 | }
|
---|
| 171 |
|
---|
[10469] | 172 | if (prunedSubtrees > 0) prunedTrees = 1;
|
---|
| 173 | PrunedSubtreesParameter.ActualValue = new IntValue(prunedSubtrees);
|
---|
| 174 | PrunedTreesParameter.ActualValue = new IntValue(prunedTrees);
|
---|
[12378] | 175 | PrunedNodesParameter.ActualValue = new IntValue(prunedNodes);
|
---|
| 176 | QualityParameter.ActualValue.Value = quality;
|
---|
[10469] | 177 |
|
---|
[10368] | 178 | return base.Apply();
|
---|
| 179 | }
|
---|
[11025] | 180 |
|
---|
[12189] | 181 | protected static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
|
---|
[10368] | 182 | var parent = original.Parent;
|
---|
| 183 | var i = parent.IndexOfSubtree(original);
|
---|
| 184 | parent.RemoveSubtree(i);
|
---|
| 185 | parent.InsertSubtree(i, replacement);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|