[3915] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3915] | 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[6256] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[5699] | 27 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
|
---|
[3915] | 28 |
|
---|
[5699] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views {
|
---|
| 30 | public partial class InteractiveSymbolicRegressionSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
|
---|
| 31 | private readonly ConstantTreeNode constantNode;
|
---|
| 32 | private readonly SymbolicExpressionTree tempTree;
|
---|
[5717] | 33 |
|
---|
| 34 | public new SymbolicRegressionSolution Content {
|
---|
| 35 | get { return (SymbolicRegressionSolution)base.Content; }
|
---|
| 36 | set { base.Content = value; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[5699] | 39 | public InteractiveSymbolicRegressionSolutionSimplifierView()
|
---|
| 40 | : base() {
|
---|
| 41 | InitializeComponent();
|
---|
| 42 | this.Caption = "Interactive Regression Solution Simplifier";
|
---|
[3915] | 43 |
|
---|
[5699] | 44 | constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
|
---|
| 45 | ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
|
---|
| 46 | ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
|
---|
[5736] | 47 | root.AddSubtree(start);
|
---|
[5699] | 48 | tempTree = new SymbolicExpressionTree(root);
|
---|
[3915] | 49 | }
|
---|
| 50 |
|
---|
[5717] | 51 | protected override void UpdateModel(ISymbolicExpressionTree tree) {
|
---|
[8639] | 52 | var model = new SymbolicRegressionModel(tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
|
---|
[8664] | 53 | SymbolicRegressionModel.Scale(model, Content.ProblemData, Content.ProblemData.TargetVariable);
|
---|
[5818] | 54 | Content.Model = model;
|
---|
[3915] | 55 | }
|
---|
| 56 |
|
---|
[5717] | 57 | protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree) {
|
---|
[5699] | 58 | Dictionary<ISymbolicExpressionTreeNode, double> replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
[5993] | 59 | foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
|
---|
| 60 | replacementValues[node] = CalculateReplacementValue(node, tree);
|
---|
[5699] | 61 | }
|
---|
| 62 | return replacementValues;
|
---|
[3915] | 63 | }
|
---|
| 64 |
|
---|
[5717] | 65 | protected override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {
|
---|
| 66 | var interpreter = Content.Model.Interpreter;
|
---|
| 67 | var dataset = Content.ProblemData.Dataset;
|
---|
[8139] | 68 | var rows = Content.ProblemData.TrainingIndices;
|
---|
[5717] | 69 | string targetVariable = Content.ProblemData.TargetVariable;
|
---|
[5699] | 70 | Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
[5736] | 71 | List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
|
---|
[8727] | 72 | var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit).ToArray();
|
---|
[6740] | 73 | var targetValues = dataset.GetDoubleValues(targetVariable, rows);
|
---|
[5942] | 74 | OnlineCalculatorError errorState;
|
---|
| 75 | double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);
|
---|
| 76 | if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
|
---|
[3915] | 77 |
|
---|
[5699] | 78 | foreach (ISymbolicExpressionTreeNode node in nodes) {
|
---|
| 79 | var parent = node.Parent;
|
---|
[5993] | 80 | constantNode.Value = CalculateReplacementValue(node, tree);
|
---|
[5699] | 81 | ISymbolicExpressionTreeNode replacementNode = constantNode;
|
---|
| 82 | SwitchNode(parent, node, replacementNode);
|
---|
[8727] | 83 | var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
|
---|
[5942] | 84 | double newR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);
|
---|
| 85 | if (errorState != OnlineCalculatorError.None) newR2 = 0.0;
|
---|
[3915] | 86 |
|
---|
[5717] | 87 | // impact = 0 if no change
|
---|
| 88 | // impact < 0 if new solution is better
|
---|
| 89 | // impact > 0 if new solution is worse
|
---|
| 90 | impactValues[node] = originalR2 - newR2;
|
---|
[5699] | 91 | SwitchNode(parent, replacementNode, node);
|
---|
[3915] | 92 | }
|
---|
[5699] | 93 | return impactValues;
|
---|
[3915] | 94 | }
|
---|
| 95 |
|
---|
[5993] | 96 | private double CalculateReplacementValue(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {
|
---|
| 97 | // remove old ADFs
|
---|
[6803] | 98 | while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);
|
---|
[5993] | 99 | // clone ADFs of source tree
|
---|
[6803] | 100 | for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {
|
---|
[5993] | 101 | tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
|
---|
| 102 | }
|
---|
[5736] | 103 | var start = tempTree.Root.GetSubtree(0);
|
---|
[6803] | 104 | while (start.SubtreeCount > 0) start.RemoveSubtree(0);
|
---|
[5736] | 105 | start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());
|
---|
[5717] | 106 | var interpreter = Content.Model.Interpreter;
|
---|
[8139] | 107 | var rows = Content.ProblemData.TrainingIndices;
|
---|
[8727] | 108 | return interpreter.GetSymbolicExpressionTreeValues(tempTree, Content.ProblemData.Dataset, rows)
|
---|
| 109 | .LimitToRange(Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit)
|
---|
| 110 | .Median();
|
---|
[3915] | 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
[5699] | 114 | private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
|
---|
[6803] | 115 | for (int i = 0; i < root.SubtreeCount; i++) {
|
---|
[5736] | 116 | if (root.GetSubtree(i) == oldBranch) {
|
---|
| 117 | root.RemoveSubtree(i);
|
---|
| 118 | root.InsertSubtree(i, newBranch);
|
---|
[3915] | 119 | return;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
[6256] | 123 |
|
---|
[8730] | 124 | protected override void OnModelChanged() {
|
---|
| 125 | base.OnModelChanged();
|
---|
[8736] | 126 | if (Content != null)
|
---|
| 127 | btnOptimizeConstants.Enabled =
|
---|
| 128 | SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(Content.Model.SymbolicExpressionTree);
|
---|
| 129 | else
|
---|
| 130 | btnOptimizeConstants.Enabled = false;
|
---|
[8730] | 131 | }
|
---|
| 132 | protected override void OnContentChanged() {
|
---|
| 133 | base.OnContentChanged();
|
---|
| 134 | base.OnModelChanged();
|
---|
[8736] | 135 | if (Content != null)
|
---|
| 136 | btnOptimizeConstants.Enabled =
|
---|
| 137 | SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(Content.Model.SymbolicExpressionTree);
|
---|
| 138 | else
|
---|
| 139 | btnOptimizeConstants.Enabled = false;
|
---|
[8730] | 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
[6256] | 143 | protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
|
---|
[8664] | 144 | var model = Content.Model;
|
---|
| 145 | SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(Content.Model.Interpreter, Content.Model.SymbolicExpressionTree, Content.ProblemData, Content.ProblemData.TrainingIndices,
|
---|
[8704] | 146 | applyLinearScaling: true, maxIterations: 50, upperEstimationLimit: model.UpperEstimationLimit, lowerEstimationLimit: model.LowerEstimationLimit);
|
---|
[6256] | 147 | UpdateModel(Content.Model.SymbolicExpressionTree);
|
---|
| 148 | }
|
---|
[3915] | 149 | }
|
---|
| 150 | }
|
---|