Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3040 Moved Alglib+AutoDiff constant optimizer in own class and created base class to provide multiple constant-opt implementations.

File size: 3.9 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    }
47
48    protected override void UpdateModel(ISymbolicExpressionTree tree) {
49      var model = new SymbolicRegressionModel(Content.ProblemData.TargetVariable, tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
50      model.Scale(Content.ProblemData);
51      Content.Model = model;
52    }
53
54    protected override ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
55      const int constOptIterations = 50;
56      const int maxRepetitions = 100;
57      const double minimumImprovement = 1e-10;
58      var regressionProblemData = Content.ProblemData;
59      var model = Content.Model;
60      progress.CanBeStopped = true;
61      double prevResult = 0.0, improvement = 0.0;
62      var result = 0.0;
63      int reps = 0;
64
65      do {
66        prevResult = result;
67        tree = NonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData, regressionProblemData.TrainingIndices,
68          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
69          cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
70            double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
71            progress.ProgressValue = Math.Min(newProgressValue, 1.0);
72          });
73        result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
74          model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
75        reps++;
76        improvement = result - prevResult;
77      } while (improvement > minimumImprovement && reps < maxRepetitions &&
78               progress.ProgressState != ProgressState.StopRequested &&
79               progress.ProgressState != ProgressState.CancelRequested);
80      return tree;
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.