Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicDataAnalysisRegressionSolutionPruningOptimizer.cs @ 10429

Last change on this file since 10429 was 10214, checked in by bburlacu, 11 years ago

#1772: Added HeuristicLab.Problems.DataAnalysis.Symbolic.Regression project

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
26
27namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
28  [Item("SymbolicDataAnalysisSolutionPruningOptimizer", "An operator which automatically removes nodes that have a negative impact from the tree model, optimizing the remaining constants.")]
29  [StorableClass]
30  public class SymbolicDataAnalysisRegressionSolutionPruningOptimizer : SymbolicDataAnalysisSolutionPruningOptimizer {
31    public override ISymbolicDataAnalysisSolution PruneAndOptimizeSolution(ISymbolicDataAnalysisSolution solution) {
32      var regressionSolution = (ISymbolicRegressionSolution)solution;
33      return PruneAndOptimizeRegressionSolution(regressionSolution);
34    }
35    /// <summary>
36    /// This method will walk all the levels of the symbolic regression solution model root starting from the deepest level and:
37    /// - it calculates the impact value of every originalNode on that level
38    /// - it prunes (replaces with a constant) the originalNode with the lowest negative impact value (0 or positive impacts are left unchanged)
39    /// - when no more nodes can be pruned, it moves on the upper level in the tree
40    /// - if the pruned and optimized solution is worse than the original solution (it can happen sometimes), then the original solution is returned
41    /// </summary>
42    /// <param name="solution"></param>
43    /// <returns></returns>
44    private ISymbolicRegressionSolution PruneAndOptimizeRegressionSolution(ISymbolicRegressionSolution solution) {
45      var calculator = new SymbolicRegressionSolutionImpactValuesCalculator();
46      var model = (ISymbolicRegressionModel)solution.Model;
47      var problemData = solution.ProblemData;
48      // get tree levels and iterate each level from the bottom up
49      var root = model.SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0);
50      var levels = root.IterateNodesBreadth().GroupBy(root.GetBranchLevel).OrderByDescending(g => g.Key);
51
52      OptimizeConstants(solution); // even if there are no negative impacts we still optimize the solution
53
54      foreach (var level in levels) {
55        var nodes = level.ToArray();
56        double minImpact;
57        do {
58          minImpact = 0.0;
59          int minImpactIndex = -1;
60          for (int i = 0; i < nodes.Length; ++i) {
61            if (nodes[i] is ConstantTreeNode) continue;
62            var impact = calculator.CalculateImpactValue(model, nodes[i], problemData, problemData.TrainingIndices);
63            if (impact < minImpact) {
64              minImpact = impact;
65              minImpactIndex = i;
66            }
67          }
68          if (minImpact >= 0) continue;
69          var node = nodes[minImpactIndex];
70          var replacementValue = calculator.CalculateReplacementValue(model, node, problemData, problemData.TrainingIndices);
71          var constantNode = MakeConstantTreeNode(replacementValue);
72          ReplaceWithConstantNode(node, constantNode);
73          nodes[minImpactIndex] = constantNode;
74          OptimizeConstants(solution);
75        } while (minImpact < 0);
76      }
77
78      var newSolution = (ISymbolicRegressionSolution)model.CreateRegressionSolution(problemData);
79      return newSolution.TrainingRSquared > solution.TrainingRSquared ? newSolution : solution;
80    }
81
82    private static void OptimizeConstants(ISymbolicRegressionSolution solution) {
83      var model = solution.Model;
84      var problemData = solution.ProblemData;
85      SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(model.Interpreter, model.SymbolicExpressionTree, problemData, problemData.TrainingIndices, true, 50);
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.