[8409] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8409] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
[14826] | 23 | using System.Linq;
|
---|
[8409] | 24 | using HeuristicLab.Common;
|
---|
[10469] | 25 | using HeuristicLab.Core;
|
---|
[8409] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[8409] | 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[16565] | 30 | [StorableType("E725708A-508E-47DC-B667-DAA569FD1DC2")]
|
---|
[10469] | 31 | [Item("SymbolicDataAnalysisSolutionImpactValuesCalculator", "Calculates the impact values and replacements values for symbolic expression tree nodes.")]
|
---|
| 32 | public abstract class SymbolicDataAnalysisSolutionImpactValuesCalculator : Item, ISymbolicDataAnalysisSolutionImpactValuesCalculator {
|
---|
| 33 | protected SymbolicDataAnalysisSolutionImpactValuesCalculator() { }
|
---|
| 34 | protected SymbolicDataAnalysisSolutionImpactValuesCalculator(SymbolicDataAnalysisSolutionImpactValuesCalculator original, Cloner cloner)
|
---|
| 35 | : base(original, cloner) { }
|
---|
| 36 | [StorableConstructor]
|
---|
[16565] | 37 | protected SymbolicDataAnalysisSolutionImpactValuesCalculator(StorableConstructorFlag _) : base(_) { }
|
---|
[8409] | 38 |
|
---|
[15371] | 39 | public virtual void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows,
|
---|
| 40 | out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
|
---|
| 41 | double qualityForImpactsCalculation = double.NaN) {
|
---|
| 42 |
|
---|
| 43 | var cloner = new Cloner();
|
---|
| 44 | var tempModel = cloner.Clone(model);
|
---|
[15871] | 45 |
|
---|
| 46 | if (double.IsNaN(qualityForImpactsCalculation)) {
|
---|
| 47 | qualityForImpactsCalculation = CalculateQualityForImpacts(tempModel, problemData, rows);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[15371] | 50 | var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
|
---|
| 51 | var tempModelParentNode = tempModelNode.Parent;
|
---|
| 52 | int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
|
---|
| 53 |
|
---|
| 54 | double bestReplacementValue = 0.0;
|
---|
| 55 | double bestImpactValue = double.PositiveInfinity;
|
---|
| 56 | newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize
|
---|
| 57 | // try the potentially reasonable replacement values and use the best one
|
---|
[15871] | 58 | foreach (var repValue in CalculateReplacementValues(node, model.SymbolicExpressionTree, model.Interpreter, problemData.Dataset, rows)) {
|
---|
[15371] | 59 | tempModelParentNode.RemoveSubtree(i);
|
---|
| 60 |
|
---|
| 61 | var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };
|
---|
| 62 | tempModelParentNode.InsertSubtree(i, constantNode);
|
---|
| 63 |
|
---|
| 64 | newQualityForImpactsCalculation = CalculateQualityForImpacts(tempModel, problemData, rows);
|
---|
| 65 |
|
---|
| 66 | impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
|
---|
| 67 | if (impactValue < bestImpactValue) {
|
---|
| 68 | bestImpactValue = impactValue;
|
---|
| 69 | bestReplacementValue = repValue;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | replacementValue = bestReplacementValue;
|
---|
| 74 | impactValue = bestImpactValue;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | protected abstract double CalculateQualityForImpacts(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IEnumerable<int> rows);
|
---|
| 78 |
|
---|
[14826] | 79 | protected IEnumerable<double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
[12509] | 80 | IDataset dataset, IEnumerable<int> rows) {
|
---|
[8946] | 81 | //optimization: constant nodes return always the same value
|
---|
| 82 | ConstantTreeNode constantNode = node as ConstantTreeNode;
|
---|
[14826] | 83 | BinaryFactorVariableTreeNode binaryFactorNode = node as BinaryFactorVariableTreeNode;
|
---|
| 84 | FactorVariableTreeNode factorNode = node as FactorVariableTreeNode;
|
---|
| 85 | if (constantNode != null) {
|
---|
| 86 | yield return constantNode.Value;
|
---|
| 87 | } else if (binaryFactorNode != null) {
|
---|
| 88 | // valid replacements are either all off or all on
|
---|
| 89 | yield return 0;
|
---|
| 90 | yield return 1;
|
---|
| 91 | } else if (factorNode != null) {
|
---|
| 92 | foreach (var w in factorNode.Weights) yield return w;
|
---|
| 93 | yield return 0.0;
|
---|
| 94 | } else {
|
---|
| 95 | var rootSymbol = new ProgramRootSymbol().CreateTreeNode();
|
---|
| 96 | var startSymbol = new StartSymbol().CreateTreeNode();
|
---|
| 97 | rootSymbol.AddSubtree(startSymbol);
|
---|
| 98 | startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
|
---|
[8409] | 99 |
|
---|
[14826] | 100 | var tempTree = new SymbolicExpressionTree(rootSymbol);
|
---|
| 101 | // clone ADFs of source tree
|
---|
| 102 | for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
|
---|
| 103 | tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
|
---|
| 104 | }
|
---|
| 105 | yield return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median();
|
---|
| 106 | yield return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Average(); // TODO perf
|
---|
[8946] | 107 | }
|
---|
[8409] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|