[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 |
|
---|
| 22 | using System;
|
---|
[17472] | 23 | using System.Threading;
|
---|
[6256] | 24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[15371] | 25 | using HeuristicLab.MainForm;
|
---|
[5699] | 26 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
|
---|
[3915] | 27 |
|
---|
[5699] | 28 | namespace 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,
|
---|
[18239] | 99 | //new int[]{ 0, 1 },
|
---|
| 100 | applyLinearScaling: false, updateVariableWeights: true, maxIterations: maxIterations, learningRate: learningRate,
|
---|
[17502] | 101 | cancellationToken: cancellationToken,
|
---|
| 102 | progress: new SynchronousProgress<double>(cost => {
|
---|
| 103 | var newProgress = progress.ProgressValue + (1.0 / (maxIterations + 1));
|
---|
| 104 | progress.ProgressValue = Math.Min(newProgress, 1.0);
|
---|
| 105 | progress.Message = $"MSE: {cost}";
|
---|
| 106 | })
|
---|
| 107 | );
|
---|
[10492] | 108 | }
|
---|
[17502] | 109 |
|
---|
[17786] | 110 | protected override ISymbolicExpressionTree UnrollingVectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
|
---|
| 111 | const int constOptIterations = 50;
|
---|
| 112 | const int maxRepetitions = 100;
|
---|
| 113 | const double minimumImprovement = 1e-10;
|
---|
| 114 | var regressionProblemData = Content.ProblemData;
|
---|
| 115 | var model = Content.Model;
|
---|
| 116 | progress.CanBeStopped = true;
|
---|
| 117 | double prevResult = 0.0, improvement = 0.0;
|
---|
| 118 | var result = 0.0;
|
---|
| 119 | int reps = 0;
|
---|
| 120 | var interpreter = new SymbolicDataAnalysisExpressionTreeVectorInterpreter();
|
---|
| 121 |
|
---|
| 122 | do {
|
---|
| 123 | prevResult = result;
|
---|
| 124 | tree = VectorUnrollingNonlinearLeastSquaresConstantOptimizationEvaluator.OptimizeTree(
|
---|
| 125 | tree, interpreter,
|
---|
[18239] | 126 | regressionProblemData,
|
---|
| 127 | regressionProblemData.TrainingIndices,
|
---|
| 128 | //new int[] { 0, 1 },
|
---|
| 129 | applyLinearScaling: false, maxIterations: constOptIterations, updateVariableWeights: true,
|
---|
[17786] | 130 | cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
|
---|
| 131 | double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
|
---|
| 132 | progress.ProgressValue = Math.Min(newProgressValue, 1.0);
|
---|
[18238] | 133 | progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
|
---|
[17786] | 134 | });
|
---|
| 135 | result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
|
---|
| 136 | model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
|
---|
| 137 | reps++;
|
---|
| 138 | improvement = result - prevResult;
|
---|
| 139 | } while (improvement > minimumImprovement && reps < maxRepetitions &&
|
---|
| 140 | progress.ProgressState != ProgressState.StopRequested &&
|
---|
| 141 | progress.ProgressState != ProgressState.CancelRequested);
|
---|
| 142 | return tree;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | protected override ISymbolicExpressionTree DiffSharpVectorOptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
|
---|
| 147 | const int constOptIterations = 50;
|
---|
| 148 | const int maxRepetitions = 100;
|
---|
| 149 | const double minimumImprovement = 1e-10;
|
---|
| 150 | var regressionProblemData = Content.ProblemData;
|
---|
| 151 | var model = Content.Model;
|
---|
| 152 | progress.CanBeStopped = true;
|
---|
| 153 | double prevResult = 0.0, improvement = 0.0;
|
---|
| 154 | var result = 0.0;
|
---|
| 155 | int reps = 0;
|
---|
| 156 |
|
---|
[18239] | 157 | #if INCLUDE_DIFFSHARP
|
---|
[17786] | 158 | do {
|
---|
| 159 | prevResult = result;
|
---|
| 160 | tree = NonlinearLeastSquaresVectorConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData, regressionProblemData.TrainingIndices,
|
---|
| 161 | applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true,
|
---|
| 162 | cancellationToken: cancellationToken, iterationCallback: (args, func, obj) => {
|
---|
| 163 | double newProgressValue = progress.ProgressValue + (1.0 / (constOptIterations + 2) / maxRepetitions); // (constOptIterations + 2) iterations are reported
|
---|
| 164 | progress.ProgressValue = Math.Min(newProgressValue, 1.0);
|
---|
[18238] | 165 | progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
|
---|
[17786] | 166 | });
|
---|
| 167 | result = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(model.Interpreter, tree,
|
---|
| 168 | model.LowerEstimationLimit, model.UpperEstimationLimit, regressionProblemData, regressionProblemData.TrainingIndices, applyLinearScaling: true);
|
---|
| 169 | reps++;
|
---|
| 170 | improvement = result - prevResult;
|
---|
| 171 | } while (improvement > minimumImprovement && reps < maxRepetitions &&
|
---|
| 172 | progress.ProgressState != ProgressState.StopRequested &&
|
---|
| 173 | progress.ProgressState != ProgressState.CancelRequested);
|
---|
[18239] | 174 | #endif
|
---|
[17786] | 175 | return tree;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
[17502] | 179 | internal class SynchronousProgress<T> : IProgress<T> {
|
---|
| 180 | private readonly Action<T> callback;
|
---|
| 181 | public SynchronousProgress(Action<T> callback) {
|
---|
| 182 | this.callback = callback;
|
---|
| 183 | }
|
---|
| 184 | public void Report(T value) {
|
---|
| 185 | callback(value);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
[3915] | 188 | }
|
---|
| 189 | }
|
---|