Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18240 was 18240, checked in by pfleck, 2 years ago

#3040 smaller fixes and some code cleanup

File size: 9.6 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#if INCLUDE_DIFFSHARP
50      btnDiffSharpOptimizeConstants.Enabled = tree != null && NonlinearLeastSquaresVectorConstantOptimizationEvaluator.CanOptimizeConstants(tree);
51#endif
52    }
53
54    protected override void UpdateModel(ISymbolicExpressionTree tree) {
55      var model = new SymbolicRegressionModel(Content.ProblemData.TargetVariable, tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
56      model.Scale(Content.ProblemData);
57      Content.Model = model;
58    }
59
60    protected override ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
61      const int constOptIterations = 50;
62      const int maxRepetitions = 100;
63      const double minimumImprovement = 1e-10;
64      var regressionProblemData = Content.ProblemData;
65      var model = Content.Model;
66      progress.CanBeStopped = true;
67      double prevResult = 0.0, improvement = 0.0;
68      var result = 0.0;
69      int reps = 0;
70
71      do {
72        prevResult = result;
73        tree = NonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData, regressionProblemData.TrainingIndices,
74          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
75          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
76            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
77            progress.ProgressValue = Math.Min(newProgressValue, 1.0);
78            progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
79          });
80        result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
81          model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
82        reps++;
83        improvement = result - prevResult;
84      } while (improvement > minimumImprovement && reps < maxRepetitions &&
85               progress.ProgressState != ProgressState.StopRequested &&
86               progress.ProgressState != ProgressState.CancelRequested);
87      return tree;
88    }
89
90    protected override ISymbolicExpressionTree VectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
91      const int maxIterations = 1000;
92      var regressionProblemData = Content.ProblemData;
93      progress.CanBeStopped = true;
94
95      var learningRate = Math.Pow(10, (double)nudLearningRate.Value);
96
97      return TensorFlowConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData,
98        regressionProblemData.TrainingIndices,
99        applyLinearScaling: true, updateVariableWeights: true, maxIterations: maxIterations, learningRate: learningRate,
100        cancellationToken: cancellationToken,
101        progress: new SynchronousProgress<double>(cost => {
102          var newProgress = progress.ProgressValue + (1.0 / (maxIterations + 1));
103          progress.ProgressValue = Math.Min(newProgress, 1.0);
104          progress.Message = $"MSE: {cost}";
105        })
106      );
107    }
108
109    protected override ISymbolicExpressionTree UnrollingVectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
110      const int constOptIterations = 50;
111      const int maxRepetitions = 100;
112      const double minimumImprovement = 1e-10;
113      var regressionProblemData = Content.ProblemData;
114      var model = Content.Model;
115      progress.CanBeStopped = true;
116      double prevResult = 0.0, improvement = 0.0;
117      var result = 0.0;
118      int reps = 0;
119      var interpreter = new SymbolicDataAnalysisExpressionTreeVectorInterpreter();
120
121      do {
122        prevResult = result;
123        tree = VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree(
124          tree, interpreter,
125          regressionProblemData,
126          regressionProblemData.TrainingIndices,
127          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
128          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
129            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
130            progress.ProgressValue = Math.Min(newProgressValue, 1.0);
131            progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
132          });
133        result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
134          model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
135        reps++;
136        improvement = result - prevResult;
137      } while (improvement > minimumImprovement && reps < maxRepetitions &&
138               progress.ProgressState != ProgressState.StopRequested &&
139               progress.ProgressState != ProgressState.CancelRequested);
140      return tree;
141    }
142
143
144    protected override ISymbolicExpressionTree DiffSharpVectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
145      const int constOptIterations = 50;
146      const int maxRepetitions = 100;
147      const double minimumImprovement = 1e-10;
148      var regressionProblemData = Content.ProblemData;
149      var model = Content.Model;
150      progress.CanBeStopped = true;
151      double prevResult = 0.0, improvement = 0.0;
152      var result = 0.0;
153      int reps = 0;
154
155#if INCLUDE_DIFFSHARP
156      do {
157        prevResult = result;
158        tree = NonlinearLeastSquaresVectorConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData, regressionProblemData.TrainingIndices,
159          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
160          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
161            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
162            progress.ProgressValue = Math.Min(newProgressValue, 1.0);
163            progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
164          });
165        result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
166          model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
167        reps++;
168        improvement = result - prevResult;
169      } while (improvement > minimumImprovement && reps < maxRepetitions &&
170               progress.ProgressState != ProgressState.StopRequested &&
171               progress.ProgressState != ProgressState.CancelRequested);
172#endif
173      return tree;
174    }
175
176
177    internal class SynchronousProgress<T> : IProgress<T> {
178      private readonly Action<T> callback;
179      public SynchronousProgress(Action<T> callback) {
180        this.callback = callback;
181      }
182      public void Report(T value) {
183        callback(value);
184      }
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.