Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs @ 17786

Last change on this file since 17786 was 17786, checked in by pfleck, 3 years ago

#3040 Worked in DiffSharp for constant-opt.

File size: 9.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
23using System.Threading;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.MainForm;
26using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views {
29  public partial class InteractiveSymbolicRegressionSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
30    public new SymbolicRegressionSolution Content {
31      get { return (SymbolicRegressionSolution)base.Content; }
32      set { base.Content = value; }
33    }
34
35    public InteractiveSymbolicRegressionSolutionSimplifierView()
36      : base(new SymbolicRegressionSolutionImpactValuesCalculator()) {
37      InitializeComponent();
38      this.Caption = "Interactive Regression Solution Simplifier";
39    }
40
41    protected override void SetEnabledStateOfControls() {
42      base.SetEnabledStateOfControls();
43
44      var tree = Content?.Model?.SymbolicExpressionTree;
45      btnOptimizeConstants.Enabled = tree != null && NonlinearLeastSquaresConstantOptimizationEvaluator.CanOptimizeConstants(tree);
46      btnVectorOptimizeConstants.Enabled = tree != null && TensorFlowConstantOptimizationEvaluator.CanOptimizeConstants(tree);
47      nudLearningRate.Enabled = tree != null && TensorFlowConstantOptimizationEvaluator.CanOptimizeConstants(tree);
48      btnUnrollingVectorOptimizeConstants.Enabled = tree != null && VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.CanOptimizeConstants(tree);
49      btnDiffSharpOptimizeConstants.Enabled = tree != null && NonlinearLeastSquaresVectorConstantOptimizationEvaluator.CanOptimizeConstants(tree);
50    }
51
52    protected override void UpdateModel(ISymbolicExpressionTree tree) {
53      var model = new SymbolicRegressionModel(Content.ProblemData.TargetVariable, tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
54      model.Scale(Content.ProblemData);
55      Content.Model = model;
56    }
57
58    protected override ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
59      const int constOptIterations = 50;
60      const int maxRepetitions = 100;
61      const double minimumImprovement = 1e-10;
62      var regressionProblemData = Content.ProblemData;
63      var model = Content.Model;
64      progress.CanBeStopped = true;
65      double prevResult = 0.0, improvement = 0.0;
66      var result = 0.0;
67      int reps = 0;
68
69      do {
70        prevResult = result;
71        tree = NonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData, regressionProblemData.TrainingIndices,
72          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
73          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
74            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
75            progress.ProgressValue = Math.Min(newProgressValue, 1.0);
76          });
77        result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
78          model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
79        reps++;
80        improvement = result - prevResult;
81      } while (improvement > minimumImprovement && reps < maxRepetitions &&
82               progress.ProgressState != ProgressState.StopRequested &&
83               progress.ProgressState != ProgressState.CancelRequested);
84      return tree;
85    }
86
87    protected override ISymbolicExpressionTree VectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
88      const int maxIterations = 1000;
89      var regressionProblemData = Content.ProblemData;
90      progress.CanBeStopped = true;
91
92      var learningRate = Math.Pow(10, (double)nudLearningRate.Value);
93
94      return TensorFlowConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData,
95        regressionProblemData.TrainingIndices,
96        applyLinearScaling: false, updateVariableWeights: true, maxIterations: maxIterations, learningRate: learningRate,
97        cancellationToken: cancellationToken,
98        progress: new SynchronousProgress<double>(cost => {
99          var newProgress = progress.ProgressValue + (1.0 / (maxIterations + 1));
100          progress.ProgressValue = Math.Min(newProgress, 1.0);
101          progress.Message = $"MSE: {cost}";
102        })
103      );
104    }
105
106    protected override ISymbolicExpressionTree UnrollingVectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
107      const int constOptIterations = 50;
108      const int maxRepetitions = 100;
109      const double minimumImprovement = 1e-10;
110      var regressionProblemData = Content.ProblemData;
111      var model = Content.Model;
112      progress.CanBeStopped = true;
113      double prevResult = 0.0, improvement = 0.0;
114      var result = 0.0;
115      int reps = 0;
116      var interpreter = new SymbolicDataAnalysisExpressionTreeVectorInterpreter();
117
118      do {
119        prevResult = result;
120        tree = VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree(
121          tree, interpreter,
122          regressionProblemData, regressionProblemData.TrainingIndices,
123          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
124          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
125            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
126            progress.ProgressValue = Math.Min(newProgressValue, 1.0);
127          });
128        result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
129          model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
130        reps++;
131        improvement = result - prevResult;
132      } while (improvement > minimumImprovement && reps < maxRepetitions &&
133               progress.ProgressState != ProgressState.StopRequested &&
134               progress.ProgressState != ProgressState.CancelRequested);
135      return tree;
136    }
137
138
139    protected override ISymbolicExpressionTree DiffSharpVectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
140      const int constOptIterations = 50;
141      const int maxRepetitions = 100;
142      const double minimumImprovement = 1e-10;
143      var regressionProblemData = Content.ProblemData;
144      var model = Content.Model;
145      progress.CanBeStopped = true;
146      double prevResult = 0.0, improvement = 0.0;
147      var result = 0.0;
148      int reps = 0;
149
150      do {
151        prevResult = result;
152        tree = NonlinearLeastSquaresVectorConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData, regressionProblemData.TrainingIndices,
153          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
154          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
155            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
156            progress.ProgressValue = Math.Min(newProgressValue, 1.0);
157          });
158        result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
159          model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
160        reps++;
161        improvement = result - prevResult;
162      } while (improvement > minimumImprovement && reps < maxRepetitions &&
163               progress.ProgressState != ProgressState.StopRequested &&
164               progress.ProgressState != ProgressState.CancelRequested);
165      return tree;
166    }
167
168
169    internal class SynchronousProgress<T> : IProgress<T> {
170      private readonly Action<T> callback;
171      public SynchronousProgress(Action<T> callback) {
172        this.callback = callback;
173      }
174      public void Report(T value) {
175        callback(value);
176      }
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.