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
RevLine 
[3915]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3915]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;
[17472]23using System.Threading;
[6256]24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[15371]25using HeuristicLab.MainForm;
[5699]26using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
[3915]27
[5699]28namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views {
29  public partial class InteractiveSymbolicRegressionSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
[5717]30    public new SymbolicRegressionSolution Content {
31      get { return (SymbolicRegressionSolution)base.Content; }
32      set { base.Content = value; }
33    }
34
[5699]35    public InteractiveSymbolicRegressionSolutionSimplifierView()
[15371]36      : base(new SymbolicRegressionSolutionImpactValuesCalculator()) {
[5699]37      InitializeComponent();
38      this.Caption = "Interactive Regression Solution Simplifier";
[3915]39    }
40
[17456]41    protected override void SetEnabledStateOfControls() {
42      base.SetEnabledStateOfControls();
43
44      var tree = Content?.Model?.SymbolicExpressionTree;
[17633]45      btnOptimizeConstants.Enabled = tree != null && NonlinearLeastSquaresConstantOptimizationEvaluator.CanOptimizeConstants(tree);
46      btnVectorOptimizeConstants.Enabled = tree != null && TensorFlowConstantOptimizationEvaluator.CanOptimizeConstants(tree);
47      nudLearningRate.Enabled = tree != null && TensorFlowConstantOptimizationEvaluator.CanOptimizeConstants(tree);
[17786]48      btnUnrollingVectorOptimizeConstants.Enabled = tree != null && VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.CanOptimizeConstants(tree);
[18239]49#if INCLUDE_DIFFSHARP
[17786]50      btnDiffSharpOptimizeConstants.Enabled = tree != null && NonlinearLeastSquaresVectorConstantOptimizationEvaluator.CanOptimizeConstants(tree);
[18239]51#endif
[17456]52    }
53
[5717]54    protected override void UpdateModel(ISymbolicExpressionTree tree) {
[13941]55      var model = new SymbolicRegressionModel(Content.ProblemData.TargetVariable, tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
[8972]56      model.Scale(Content.ProblemData);
[5818]57      Content.Model = model;
[3915]58    }
59
[17472]60    protected override ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
[15371]61      const int constOptIterations = 50;
[17456]62      const int maxRepetitions = 100;
63      const double minimumImprovement = 1e-10;
[15400]64      var regressionProblemData = Content.ProblemData;
65      var model = Content.Model;
[16798]66      progress.CanBeStopped = true;
[17456]67      double prevResult = 0.0, improvement = 0.0;
[16798]68      var result = 0.0;
69      int reps = 0;
70
[17633]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);
[18238]78            progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
[17633]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) {
[17502]91      const int maxIterations = 1000;
[17633]92      var regressionProblemData = Content.ProblemData;
93      progress.CanBeStopped = true;
94
95      var learningRate = Math.Pow(10, (double)nudLearningRate.Value);
96
[17502]97      return TensorFlowConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData,
98        regressionProblemData.TrainingIndices,
[18240]99        applyLinearScaling: true, updateVariableWeights: true, maxIterations: maxIterations, learningRate: learningRate,
[17502]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      );
[10492]107    }
[17502]108
[17786]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,
[18239]125          regressionProblemData,
126          regressionProblemData.TrainingIndices,
[18240]127          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
[17786]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);
[18238]131            progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
[17786]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
[18239]155#if INCLUDE_DIFFSHARP
[17786]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);
[18238]163            progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
[17786]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);
[18239]172#endif
[17786]173      return tree;
174    }
175
176
[17502]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    }
[3915]186  }
187}
Note: See TracBrowser for help on using the repository browser.