[10368] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[12009] | 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.")]
|
---|
[11145] | 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 |
|
---|
[10368] | 52 | #region parameter properties
|
---|
[11145] | 53 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 54 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
[10368] | 55 | }
|
---|
[11145] | 56 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 57 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
[10368] | 58 | }
|
---|
[11145] | 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]; }
|
---|
| 85 | }
|
---|
[10368] | 86 | #endregion
|
---|
[11145] | 87 |
|
---|
[10368] | 88 | #region properties
|
---|
[11145] | 89 | protected IDataAnalysisProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } }
|
---|
| 90 | protected ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactValuesCalculator { get { return ImpactValuesCalculatorParameter.Value; } }
|
---|
| 91 | protected IntRange FitnessCalculationPartition { get { return FitnessCalculationPartitionParameter.ActualValue; } }
|
---|
| 92 | protected bool PruneOnlyZeroImpactNodes {
|
---|
| 93 | get { return PruneOnlyZeroImpactNodesParameter.Value.Value; }
|
---|
| 94 | set { PruneOnlyZeroImpactNodesParameter.Value.Value = value; }
|
---|
[10368] | 95 | }
|
---|
[11145] | 96 | protected double NodeImpactThreshold {
|
---|
| 97 | get { return NodeImpactThresholdParameter.Value.Value; }
|
---|
| 98 | set { NodeImpactThresholdParameter.Value.Value = value; }
|
---|
[10368] | 99 | }
|
---|
[11145] | 100 | protected ISymbolicExpressionTree SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } }
|
---|
| 101 | protected DoubleValue Quality { get { return QualityParameter.ActualValue; } }
|
---|
| 102 | protected DoubleLimit EstimationLimits { get { return EstimationLimitsParameter.ActualValue; } }
|
---|
| 103 | protected ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get { return InterpreterParameter.ActualValue; } }
|
---|
| 104 | #endregion
|
---|
[10368] | 105 |
|
---|
[11145] | 106 | [StorableConstructor]
|
---|
| 107 | protected SymbolicDataAnalysisExpressionPruningOperator(bool deserializing) : base(deserializing) { }
|
---|
| 108 | protected SymbolicDataAnalysisExpressionPruningOperator(SymbolicDataAnalysisExpressionPruningOperator original, Cloner cloner)
|
---|
| 109 | : base(original, cloner) { }
|
---|
| 110 |
|
---|
| 111 | protected SymbolicDataAnalysisExpressionPruningOperator() {
|
---|
| 112 | #region add parameters
|
---|
| 113 | Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName));
|
---|
| 114 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisModel>(SymbolicDataAnalysisModelParameterName));
|
---|
| 115 | Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName));
|
---|
| 116 | Parameters.Add(new LookupParameter<IntValue>(PrunedSubtreesParameterName, "A counter of how many subtrees were replaced."));
|
---|
| 117 | Parameters.Add(new LookupParameter<IntValue>(PrunedTreesParameterName, "A counter of how many trees were pruned."));
|
---|
| 118 | Parameters.Add(new FixedValueParameter<BoolValue>(PruneOnlyZeroImpactNodesParameterName, "Specify whether or not only zero impact nodes should be pruned."));
|
---|
| 119 | Parameters.Add(new FixedValueParameter<DoubleValue>(NodeImpactThresholdParameterName, "Specifies an impact value threshold below which nodes should be pruned."));
|
---|
| 120 | Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
|
---|
| 121 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
|
---|
| 122 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
|
---|
| 123 | Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName));
|
---|
| 124 | #endregion
|
---|
[10368] | 125 | }
|
---|
| 126 |
|
---|
[11145] | 127 | protected abstract ISymbolicDataAnalysisModel CreateModel();
|
---|
[10368] | 128 |
|
---|
[11145] | 129 | protected abstract double Evaluate(IDataAnalysisModel model);
|
---|
[10368] | 130 |
|
---|
| 131 | public override IOperation Apply() {
|
---|
[11145] | 132 | var model = CreateModel();
|
---|
| 133 | var nodes = SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList();
|
---|
| 134 | var rows = Enumerable.Range(FitnessCalculationPartition.Start, FitnessCalculationPartition.Size);
|
---|
| 135 | var prunedSubtrees = 0;
|
---|
| 136 | var prunedTrees = 0;
|
---|
[10368] | 137 |
|
---|
[11145] | 138 | double quality = Evaluate(model);
|
---|
[10368] | 139 |
|
---|
[11145] | 140 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
| 141 | var node = nodes[i];
|
---|
[10368] | 142 | if (node is ConstantTreeNode) continue;
|
---|
| 143 |
|
---|
[11145] | 144 | double impactValue, replacementValue;
|
---|
| 145 | ImpactValuesCalculator.CalculateImpactAndReplacementValues(model, node, ProblemData, rows, out impactValue, out replacementValue, quality);
|
---|
[10368] | 146 |
|
---|
| 147 | if (PruneOnlyZeroImpactNodes) {
|
---|
[11145] | 148 | if (!impactValue.IsAlmost(0.0)) continue;
|
---|
| 149 | } else if (NodeImpactThreshold < impactValue) {
|
---|
| 150 | continue;
|
---|
[10368] | 151 | }
|
---|
| 152 |
|
---|
[11145] | 153 | var constantNode = (ConstantTreeNode)node.Grammar.GetSymbol("Constant").CreateTreeNode();
|
---|
| 154 | constantNode.Value = replacementValue;
|
---|
| 155 |
|
---|
[10368] | 156 | ReplaceWithConstant(node, constantNode);
|
---|
[11145] | 157 | i += node.GetLength() - 1; // skip subtrees under the node that was folded
|
---|
[10368] | 158 |
|
---|
[11145] | 159 | quality -= impactValue;
|
---|
| 160 |
|
---|
[10368] | 161 | prunedSubtrees++;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[11145] | 164 | if (prunedSubtrees > 0) prunedTrees = 1;
|
---|
| 165 | PrunedSubtreesParameter.ActualValue = new IntValue(prunedSubtrees);
|
---|
| 166 | PrunedTreesParameter.ActualValue = new IntValue(prunedTrees);
|
---|
| 167 |
|
---|
[10368] | 168 | return base.Apply();
|
---|
| 169 | }
|
---|
[11145] | 170 |
|
---|
[10368] | 171 | private static void ReplaceWithConstant(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement) {
|
---|
| 172 | var parent = original.Parent;
|
---|
| 173 | var i = parent.IndexOfSubtree(original);
|
---|
| 174 | parent.RemoveSubtree(i);
|
---|
| 175 | parent.InsertSubtree(i, replacement);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|