Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2974_Constants_Optimization/UnitTests/ConstantsOptimizationTests.cs @ 16752

Last change on this file since 16752 was 16690, checked in by gkronber, 6 years ago

#2974: added code in the unit tests to make behaviour of new and old ConstOpt consistent to allow direct comparison of results (+fixed a small bug in the unit tests)

File size: 11.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
23using HeuristicLab.Problems.DataAnalysis;
24using HeuristicLab.Problems.DataAnalysis.Symbolic;
25using HeuristicLab.Problems.DataAnalysis.Symbolic.ConstantsOptimization;
26using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
27
28
29using HeuristicLab.Problems.Instances.DataAnalysis;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31
32namespace UnitTests {
33  [TestClass]
34  public class ConstantsOptimizationTests {
35    private static readonly int seed = 1234;
36
37
38    public static void CompareConstantsOptimizationResults(IRegressionProblemData problemData, ISymbolicExpressionTree tree) {
39      var applyLinearScaling = true;
40      var old_optimizedTree = (ISymbolicExpressionTree)tree.Clone();
41      var old_result = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
42        new SymbolicDataAnalysisExpressionTreeLinearInterpreter(),
43        old_optimizedTree, problemData, problemData.TrainingIndices, applyLinearScaling, maxIterations: 10);
44
45
46      var new_optimizedTree = (ISymbolicExpressionTree)tree.Clone();
47      var new_result = LMConstantsOptimizer.OptimizeConstants(new_optimizedTree, problemData.Dataset, problemData.TargetVariable, problemData.TrainingIndices, applyLinearScaling, maxIterations: 10);
48
49      // extract constants
50      var old_constants = Util.ExtractConstants(old_optimizedTree, applyLinearScaling);
51      var new_constants = Util.ExtractConstants(new_optimizedTree, applyLinearScaling);
52
53      {
54        // consistency with old ConstOpt style
55        var initalR2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
56          new SymbolicDataAnalysisExpressionTreeLinearInterpreter(),
57          tree, double.MinValue, double.MaxValue, problemData, problemData.TrainingIndices, applyLinearScaling);
58
59        // LMConstantsOptimizer returns 0 if optimization was not successful
60        if (initalR2 - new_result > 0.001) {
61          new_result = initalR2;
62          new_constants = old_constants;
63        }
64      }
65
66      //check R² values
67      Assert.AreEqual(old_result, new_result, 1E-6);
68
69      //check numeric values of constants
70      for (int i = 0; i < old_constants.Length; i++) {
71        if (old_constants[i] == 0) {
72          Assert.AreEqual(old_constants[i], new_constants[i], 1E-8); // check absolute error
73        } else {
74          Assert.AreEqual(0.0, 1.0 - new_constants[i] / old_constants[i], 1E-6); // check percentual error
75        }
76      }
77    }
78
79
80
81    [TestMethod]
82    [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
83    [TestProperty("Time", "short")]
84    public void ConstantsOptimizationTest_Nguyen01() {
85      var problemData = new NguyenFunctionOne(seed).GenerateRegressionData();
86      var modelTemplate = "({0})* CUBE(X) + ({1}) * SQR(X) + ({2}) * X";
87
88      string modelString;
89      object[] constants;
90      ISymbolicExpressionTree modelTree;
91
92      constants = new object[] { 1.0, 2.0, 3.0 };
93      modelString = string.Format(modelTemplate, constants);
94      modelTree = new InfixExpressionParser().Parse(modelString);
95      CompareConstantsOptimizationResults(problemData, modelTree);
96
97      constants = new object[] { 5.0, -2.0, 500.362 };
98      modelString = string.Format(modelTemplate, constants);
99      modelTree = new InfixExpressionParser().Parse(modelString);
100      CompareConstantsOptimizationResults(problemData, modelTree);
101
102      constants = new object[] { -6987.25, 1, -888 };
103      modelString = string.Format(modelTemplate, constants);
104      modelTree = new InfixExpressionParser().Parse(modelString);
105      CompareConstantsOptimizationResults(problemData, modelTree);
106    }
107
108    [TestMethod]
109    [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
110    [TestProperty("Time", "short")]
111    public void ConstantsOptimizationTest_Nguyen03() {
112      var problemData = new NguyenFunctionThree(seed).GenerateRegressionData();
113      var modelTemplate = "({0})* X*X*X*X*X + ({1}) * X*X*X*X + ({2}) * X*X*X + ({3}) * X*X + ({4}) * X + {5}";
114
115      string modelString;
116      object[] constants;
117      ISymbolicExpressionTree modelTree;
118
119      constants = new object[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
120      modelString = string.Format(modelTemplate, constants);
121      modelTree = new InfixExpressionParser().Parse(modelString);
122      CompareConstantsOptimizationResults(problemData, modelTree);
123
124      constants = new object[] { 5.0, -2.0, 500.362, -5646, 0.0001, 1234 };
125      modelString = string.Format(modelTemplate, constants);
126      modelTree = new InfixExpressionParser().Parse(modelString);
127      CompareConstantsOptimizationResults(problemData, modelTree);
128
129      constants = new object[] { -6987.25, 1, -888, +888, -1, 6987.25, 0, 25 };
130      modelString = string.Format(modelTemplate, constants);
131      modelTree = new InfixExpressionParser().Parse(modelString);
132      CompareConstantsOptimizationResults(problemData, modelTree);
133    }
134
135    [TestMethod]
136    [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
137    [TestProperty("Time", "short")]
138    public void ConstantsOptimizationTest_Nguyen05() {
139      var problemData = new NguyenFunctionFive(seed).GenerateRegressionData();
140      var modelTemplate = "({0}) * SIN(({1})*X*X) * COS(({2})*X) + ({3})";
141
142      string modelString;
143      object[] constants;
144      ISymbolicExpressionTree modelTree;
145
146      constants = new object[] { 1.0, 2.0, 3.0, 4.0 };
147      modelString = string.Format(modelTemplate, constants);
148      modelTree = new InfixExpressionParser().Parse(modelString);
149      CompareConstantsOptimizationResults(problemData, modelTree);
150
151      constants = new object[] { 5.0, -2.0, -3.0, 5.0 };
152      modelString = string.Format(modelTemplate, constants);
153      modelTree = new InfixExpressionParser().Parse(modelString);
154      CompareConstantsOptimizationResults(problemData, modelTree);
155
156      constants = new object[] { 0.5, 1, 1, 3 };
157      modelString = string.Format(modelTemplate, constants);
158      modelTree = new InfixExpressionParser().Parse(modelString);
159      CompareConstantsOptimizationResults(problemData, modelTree);
160    }
161
162    [TestMethod]
163    [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
164    [TestProperty("Time", "short")]
165    public void ConstantsOptimizationTest_Nguyen07() {
166      var problemData = new NguyenFunctionSeven(seed).GenerateRegressionData();
167      var modelTemplate = "({0}) * LOG(({1})*X + ({2})) + ({3}) * LOG(({4})*X*X + ({5})) + ({6})";
168
169      string modelString;
170      object[] constants;
171      ISymbolicExpressionTree modelTree;
172
173      constants = new object[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
174      modelString = string.Format(modelTemplate, constants);
175      modelTree = new InfixExpressionParser().Parse(modelString);
176      CompareConstantsOptimizationResults(problemData, modelTree);
177
178      constants = new object[] { 5.0, 2.0, 500.362, -5646, 0.0001, 1234, 421 };
179      modelString = string.Format(modelTemplate, constants);
180      modelTree = new InfixExpressionParser().Parse(modelString);
181      // This test fails not because of a problem in ConstOpt but because of the code in
182      // OnlinePearsonsRCalculator
183      //         double xVar = sxCalculator.PopulationVariance;
184      //         double yVar = syCalculator.PopulationVariance;
185      //         if (xVar.IsAlmost(0.0) || yVar.IsAlmost(0.0)) {
186      //           return 0.0;
187      //         } else { ...
188      // PopulationVariance might become close to zero, but the result of cov / (sqrt(xvar) * sqrt(yvar) might still be correct.
189      // Currently, we just return 0. Which means that we reset optimized constants to initial values
190      CompareConstantsOptimizationResults(problemData, modelTree);
191
192      constants = new object[] { -6987.25, 1, 888, +888, -1, 6987.25, 0, 25 };
193      modelString = string.Format(modelTemplate, constants);
194      modelTree = new InfixExpressionParser().Parse(modelString);
195      CompareConstantsOptimizationResults(problemData, modelTree);
196    }
197
198    [TestMethod]
199    [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
200    [TestProperty("Time", "short")]
201    public void ConstantsOptimizationTest_Nguyen08() {
202      var problemData = new NguyenFunctionEight(seed).GenerateRegressionData();
203      var modelTemplate = "({0})* SQRT(({1}) * X) + ({2})";
204
205      string modelString;
206      object[] constants;
207      ISymbolicExpressionTree modelTree;
208
209      constants = new object[] { 1.0, 2.0, 3.0 };
210      modelString = string.Format(modelTemplate, constants);
211      modelTree = new InfixExpressionParser().Parse(modelString);
212      CompareConstantsOptimizationResults(problemData, modelTree);
213
214      constants = new object[] { 5.0, 20.0, -500.362 };
215      modelString = string.Format(modelTemplate, constants);
216      modelTree = new InfixExpressionParser().Parse(modelString);
217      CompareConstantsOptimizationResults(problemData, modelTree);
218
219      constants = new object[] { -6987.25, 1, -888 };
220      modelString = string.Format(modelTemplate, constants);
221      modelTree = new InfixExpressionParser().Parse(modelString);
222      CompareConstantsOptimizationResults(problemData, modelTree);
223    }
224
225    [TestMethod]
226    [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
227    [TestProperty("Time", "short")]
228    public void ConstantsOptimizationTest_Keijzer05() {
229      var problemData = new KeijzerFunctionFive(seed).GenerateRegressionData();
230      var modelTemplate = "( ({0}) * X * Z ) / ( (({1}) * X + ({2})) * ({3}) * SQR(Y) ) + ({4})";
231
232      string modelString;
233      object[] constants;
234      ISymbolicExpressionTree modelTree;
235
236      constants = new object[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
237      modelString = string.Format(modelTemplate, constants);
238      modelTree = new InfixExpressionParser().Parse(modelString);
239      CompareConstantsOptimizationResults(problemData, modelTree);
240
241      constants = new object[] { 5.0, -2.0, 500.362, -5646, 0.0001 };
242      modelString = string.Format(modelTemplate, constants);
243      modelTree = new InfixExpressionParser().Parse(modelString);
244      CompareConstantsOptimizationResults(problemData, modelTree);
245
246      constants = new object[] { -6987.25, 1, -888, +888, -1, 6987.25, 0 };
247      modelString = string.Format(modelTemplate, constants);
248      modelTree = new InfixExpressionParser().Parse(modelString);
249      CompareConstantsOptimizationResults(problemData, modelTree);
250    }
251
252    [TestInitialize]
253    public void InitEnvironment() {
254      System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
255    }
256  }
257}
Note: See TracBrowser for help on using the repository browser.