Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17633 was 17633, checked in by pfleck, 4 years ago

#3040 Reenabled the old optimize button in the simplifier and added a new button for const opt with vectors.

File size: 5.4 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    }
49
50    protected override void UpdateModel(ISymbolicExpressionTree tree) {
51      var model = new SymbolicRegressionModel(Content.ProblemData.TargetVariable, tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
52      model.Scale(Content.ProblemData);
53      Content.Model = model;
54    }
55
56    protected override ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
57      const int constOptIterations = 50;
58      const int maxRepetitions = 100;
59      const double minimumImprovement = 1e-10;
60      var regressionProblemData = Content.ProblemData;
61      var model = Content.Model;
62      progress.CanBeStopped = true;
63      double prevResult = 0.0, improvement = 0.0;
64      var result = 0.0;
65      int reps = 0;
66
67      do {
68        prevResult = result;
69        tree = NonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData, regressionProblemData.TrainingIndices,
70          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
71          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
72            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
73            progress.ProgressValue = Math.Min(newProgressValue, 1.0);
74          });
75        result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
76          model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
77        reps++;
78        improvement = result - prevResult;
79      } while (improvement > minimumImprovement && reps < maxRepetitions &&
80               progress.ProgressState != ProgressState.StopRequested &&
81               progress.ProgressState != ProgressState.CancelRequested);
82      return tree;
83    }
84
85    protected override ISymbolicExpressionTree VectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
86      const int maxIterations = 1000;
87      var regressionProblemData = Content.ProblemData;
88      progress.CanBeStopped = true;
89
90      var learningRate = Math.Pow(10, (double)nudLearningRate.Value);
91
92      return TensorFlowConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData,
93        regressionProblemData.TrainingIndices,
94        applyLinearScaling: false, updateVariableWeights: true, maxIterations: maxIterations, learningRate: learningRate,
95        cancellationToken: cancellationToken,
96        progress: new SynchronousProgress<double>(cost => {
97          var newProgress = progress.ProgressValue + (1.0 / (maxIterations + 1));
98          progress.ProgressValue = Math.Min(newProgress, 1.0);
99          progress.Message = $"MSE: {cost}";
100        })
101      );
102    }
103
104    internal class SynchronousProgress<T> : IProgress<T> {
105      private readonly Action<T> callback;
106      public SynchronousProgress(Action<T> callback) {
107        this.callback = callback;
108      }
109      public void Report(T value) {
110        callback(value);
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.