1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HEAL.Attic;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
30 | [StorableType("E725708A-508E-47DC-B667-DAA569FD1DC2")]
|
---|
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]
|
---|
37 | protected SymbolicDataAnalysisSolutionImpactValuesCalculator(StorableConstructorFlag _) : base(_) { }
|
---|
38 |
|
---|
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);
|
---|
45 |
|
---|
46 | if (double.IsNaN(qualityForImpactsCalculation)) {
|
---|
47 | qualityForImpactsCalculation = CalculateQualityForImpacts(tempModel, problemData, rows);
|
---|
48 | }
|
---|
49 |
|
---|
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
|
---|
58 | foreach (var repValue in CalculateReplacementValues(node, model.SymbolicExpressionTree, model.Interpreter, problemData.Dataset, rows)) {
|
---|
59 | tempModelParentNode.RemoveSubtree(i);
|
---|
60 |
|
---|
61 | var numberNode = new NumberTreeNode(new Number()) { Value = repValue };
|
---|
62 | tempModelParentNode.InsertSubtree(i, numberNode);
|
---|
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 |
|
---|
79 | protected IEnumerable<double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
80 | IDataset dataset, IEnumerable<int> rows) {
|
---|
81 | var numberNode = node as INumericTreeNode;
|
---|
82 | BinaryFactorVariableTreeNode binaryFactorNode = node as BinaryFactorVariableTreeNode;
|
---|
83 | FactorVariableTreeNode factorNode = node as FactorVariableTreeNode;
|
---|
84 | if (numberNode != null) {
|
---|
85 | yield return numberNode.Value;
|
---|
86 | } else if (binaryFactorNode != null) {
|
---|
87 | // valid replacements are either all off or all on
|
---|
88 | yield return 0;
|
---|
89 | yield return 1;
|
---|
90 | } else if (factorNode != null) {
|
---|
91 | foreach (var w in factorNode.Weights) yield return w;
|
---|
92 | yield return 0.0;
|
---|
93 | } else {
|
---|
94 | var rootSymbol = new ProgramRootSymbol().CreateTreeNode();
|
---|
95 | var startSymbol = new StartSymbol().CreateTreeNode();
|
---|
96 | rootSymbol.AddSubtree(startSymbol);
|
---|
97 | startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
|
---|
98 |
|
---|
99 | var tempTree = new SymbolicExpressionTree(rootSymbol);
|
---|
100 | // clone ADFs of source tree
|
---|
101 | for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
|
---|
102 | tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
|
---|
103 | }
|
---|
104 | yield return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median();
|
---|
105 | yield return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Average(); // TODO perf
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|