Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3040 Print MSE progress for constant opt in simplifier.

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