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 |
|
---|
22 | using System;
|
---|
23 | using System.Threading;
|
---|
24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
25 | using HeuristicLab.MainForm;
|
---|
26 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
|
---|
27 |
|
---|
28 | namespace 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 | #if INCLUDE_DIFFSHARP
|
---|
50 | btnDiffSharpOptimizeConstants.Enabled = tree != null && NonlinearLeastSquaresVectorConstantOptimizationEvaluator.CanOptimizeConstants(tree);
|
---|
51 | #endif
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected override void UpdateModel(ISymbolicExpressionTree tree) {
|
---|
55 | var model = new SymbolicRegressionModel(Content.ProblemData.TargetVariable, tree, Content.Model.Interpreter, Content.Model.LowerEstimationLimit, Content.Model.UpperEstimationLimit);
|
---|
56 | model.Scale(Content.ProblemData);
|
---|
57 | Content.Model = model;
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected override ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, CancellationToken cancellationToken, IProgress progress) {
|
---|
61 | const int constOptIterations = 50;
|
---|
62 | const int maxRepetitions = 100;
|
---|
63 | const double minimumImprovement = 1e-10;
|
---|
64 | var regressionProblemData = Content.ProblemData;
|
---|
65 | var model = Content.Model;
|
---|
66 | progress.CanBeStopped = true;
|
---|
67 | double prevResult = 0.0, improvement = 0.0;
|
---|
68 | var result = 0.0;
|
---|
69 | int reps = 0;
|
---|
70 |
|
---|
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);
|
---|
78 | progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
|
---|
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) {
|
---|
91 | const int maxIterations = 1000;
|
---|
92 | var regressionProblemData = Content.ProblemData;
|
---|
93 | progress.CanBeStopped = true;
|
---|
94 |
|
---|
95 | var learningRate = Math.Pow(10, (double)nudLearningRate.Value);
|
---|
96 |
|
---|
97 | return TensorFlowConstantOptimizationEvaluator.OptimizeTree(tree, regressionProblemData,
|
---|
98 | regressionProblemData.TrainingIndices,
|
---|
99 | //new int[]{ 0, 1 },
|
---|
100 | applyLinearScaling: false, updateVariableWeights: true, maxIterations: maxIterations, learningRate: learningRate,
|
---|
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 | );
|
---|
108 | }
|
---|
109 |
|
---|
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,
|
---|
126 | regressionProblemData,
|
---|
127 | regressionProblemData.TrainingIndices,
|
---|
128 | //new int[] { 0, 1 },
|
---|
129 | applyLinearScaling: false, maxIterations: constOptIterations, updateVariableWeights: true,
|
---|
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);
|
---|
133 | progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
|
---|
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 |
|
---|
157 | #if INCLUDE_DIFFSHARP
|
---|
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);
|
---|
165 | progress.Message = $"MSE: { func / regressionProblemData.TrainingPartition.Size }";
|
---|
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);
|
---|
174 | #endif
|
---|
175 | return tree;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
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 | }
|
---|
188 | }
|
---|
189 | }
|
---|