Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2893_BNLR/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs @ 16755

Last change on this file since 16755 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableClass]
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(bool deserializing) : base(deserializing) { }
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      if (double.IsNaN(qualityForImpactsCalculation))
43        qualityForImpactsCalculation = CalculateQualityForImpacts(model, problemData, rows);
44
45      var cloner = new Cloner();
46      var tempModel = cloner.Clone(model);
47      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);
48
49      var tempModelParentNode = tempModelNode.Parent;
50      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
51
52      double bestReplacementValue = 0.0;
53      double bestImpactValue = double.PositiveInfinity;
54      newQualityForImpactsCalculation = qualityForImpactsCalculation; // initialize
55      // try the potentially reasonable replacement values and use the best one
56      foreach (var repValue in CalculateReplacementValues(node, model.SymbolicExpressionTree, model.Interpreter, problemData.Dataset, problemData.TrainingIndices)) {
57        tempModelParentNode.RemoveSubtree(i);
58
59        var constantNode = new ConstantTreeNode(new Constant()) { Value = repValue };
60        tempModelParentNode.InsertSubtree(i, constantNode);
61
62        newQualityForImpactsCalculation = CalculateQualityForImpacts(tempModel, problemData, rows);
63
64        impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
65        if (impactValue < bestImpactValue) {
66          bestImpactValue = impactValue;
67          bestReplacementValue = repValue;
68        }
69      }
70
71      replacementValue = bestReplacementValue;
72      impactValue = bestImpactValue;
73    }
74
75    protected abstract double CalculateQualityForImpacts(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IEnumerable<int> rows);
76
77    protected IEnumerable<double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
78      IDataset dataset, IEnumerable<int> rows) {
79      //optimization: constant nodes return always the same value
80      ConstantTreeNode constantNode = node as ConstantTreeNode;
81      BinaryFactorVariableTreeNode binaryFactorNode = node as BinaryFactorVariableTreeNode;
82      FactorVariableTreeNode factorNode = node as FactorVariableTreeNode;
83      if (constantNode != null) {
84        yield return constantNode.Value;
85      } else if (binaryFactorNode != null) {
86        // valid replacements are either all off or all on
87        yield return 0;
88        yield return 1;
89      } else if (factorNode != null) {
90        foreach (var w in factorNode.Weights) yield return w;
91        yield return 0.0;
92      } else {
93        var rootSymbol = new ProgramRootSymbol().CreateTreeNode();
94        var startSymbol = new StartSymbol().CreateTreeNode();
95        rootSymbol.AddSubtree(startSymbol);
96        startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
97
98        var tempTree = new SymbolicExpressionTree(rootSymbol);
99        // clone ADFs of source tree
100        for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
101          tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
102        }
103        yield return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median();
104        yield return interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Average(); // TODO perf
105      }
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.