Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionImpactValuesCalculator.cs @ 16731

Last change on this file since 16731 was 16434, checked in by mkommend, 6 years ago

#2910: Merged r15871 into stable.

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
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 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
79    protected IEnumerable<double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
80      IDataset dataset, IEnumerable<int> rows) {
81      //optimization: constant nodes return always the same value
82      ConstantTreeNode constantNode = node as ConstantTreeNode;
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());
99
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
107      }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.