Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicDataAnalysisSolutionPruningOptimizer.cs @ 9963

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

#1772: Merged changes from the trunk and other branches. Added new ExtendedSymbolicExpressionTreeCanvas control for the visual exploration of tree genealogies. Reorganized some files and folders.

File size: 5.4 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
31
32namespace HeuristicLab.EvolutionaryTracking {
33  [Item("SymbolicDataAnalysisSolutionPruningOptimizer", "An operator which automatically removes nodes that have a negative impact from the tree model, optimizing the remaining constants.")]
34  [StorableClass]
35  public class SymbolicDataAnalysisSolutionPruningOptimizer : SingleSuccessorOperator, IAnalyzer {
36    private const string ResultCollectionParameterName = "Results";
37    private const string TrainingBestSolutionParameterName = "Best training solution";
38
39    #region parameter properties
40    public ILookupParameter<ResultCollection> ResultCollectionParameter {
41      get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
42    }
43    #endregion
44    #region properties
45    public ResultCollection ResultCollection {
46      get { return ResultCollectionParameter.ActualValue; }
47    }
48    #endregion
49
50    [StorableConstructor]
51    protected SymbolicDataAnalysisSolutionPruningOptimizer(bool deserializing) : base(deserializing) { }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new SymbolicDataAnalysisSolutionPruningOptimizer(this, cloner);
55    }
56
57    protected SymbolicDataAnalysisSolutionPruningOptimizer(SymbolicDataAnalysisSolutionPruningOptimizer original, Cloner cloner)
58      : base(original, cloner) {
59    }
60
61    public SymbolicDataAnalysisSolutionPruningOptimizer() {
62      Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName));
63    }
64
65    public override IOperation Apply() {
66      var solution = (ISymbolicRegressionSolution)ResultCollection[TrainingBestSolutionParameterName].Value;
67      ResultCollection[TrainingBestSolutionParameterName].Value = PruneAndOptimizeSolution(solution);
68      return base.Apply();
69    }
70
71    public ISymbolicDataAnalysisSolution PruneAndOptimizeSolution(ISymbolicRegressionSolution solution) {
72      return PruneAndOptimizeRegressionSolution(solution);
73    }
74
75    private static ISymbolicRegressionSolution PruneAndOptimizeRegressionSolution(ISymbolicRegressionSolution solution) {
76      var calculator = new SymbolicRegressionSolutionImpactValuesCalculator();
77      var model = solution.Model;
78      var problemData = solution.ProblemData;
79      // get tree levels and iterate each level from the bottom up
80      var root = model.SymbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0);
81      var levels = root.IterateNodesBreadth().GroupBy(root.GetBranchLevel).OrderByDescending(g => g.Key);
82
83      foreach (var level in levels) {
84        bool stop = false;
85        while (!stop) {
86          var tuple = level.Select(n => new { Node = n, Impact = calculator.CalculateImpactValue(model, n, problemData, problemData.TrainingIndices) }).OrderBy(x => x.Impact).First();
87          var node = tuple.Node;
88          var impact = tuple.Impact;
89          if (impact < 0) {
90            var replacementValue = calculator.CalculateReplacementValue(model, node, problemData,
91              problemData.TrainingIndices);
92            var constantNode = MakeConstantTreeNode(replacementValue);
93            var parent = node.Parent;
94            var index = parent.IndexOfSubtree(node);
95            parent.RemoveSubtree(index);
96            parent.InsertSubtree(index, constantNode);
97            SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(model.Interpreter,
98              model.SymbolicExpressionTree, problemData, problemData.TrainingIndices, true, 50,
99              model.UpperEstimationLimit,
100              model.LowerEstimationLimit);
101            continue;
102          }
103          stop = true;
104        }
105      }
106      var newSolution = (ISymbolicRegressionSolution)model.CreateRegressionSolution(problemData);
107      return newSolution.TrainingRSquared > solution.TrainingRSquared ? newSolution : solution;
108    }
109
110    public bool EnabledByDefault {
111      get { return true; }
112    }
113
114    private static ConstantTreeNode MakeConstantTreeNode(double value) {
115      var constant = new Constant { MinValue = value - 1, MaxValue = value + 1 };
116      var constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();
117      constantTreeNode.Value = value;
118      return constantTreeNode;
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.