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 |
|
---|
22 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
23 | using HeuristicLab.Problems.DataAnalysis;
|
---|
24 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
25 | using HeuristicLab.Problems.DataAnalysis.Symbolic.ConstantsOptimization;
|
---|
26 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
27 |
|
---|
28 |
|
---|
29 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
31 |
|
---|
32 | namespace 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 | //check R² values
|
---|
50 | //Assert.AreEqual(old_result, new_result);
|
---|
51 |
|
---|
52 | //check numeric values of constants
|
---|
53 | var old_constants = Util.ExtractConstants(old_optimizedTree, applyLinearScaling);
|
---|
54 | var new_constants = Util.ExtractConstants(new_optimizedTree, applyLinearScaling);
|
---|
55 | //Assert.IsTrue(old_constants.SequenceEqual(new_constants));
|
---|
56 |
|
---|
57 | for (int i = 0; i < old_constants.Length; i++) {
|
---|
58 | Assert.AreEqual(old_constants[i], new_constants[i], 0.00000001);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | [TestMethod]
|
---|
65 | [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
|
---|
66 | [TestProperty("Time", "short")]
|
---|
67 | public void ConstantsOptimizationTest_Nguyen01() {
|
---|
68 | var problemData = new NguyenFunctionOne(seed).GenerateRegressionData();
|
---|
69 | var modelTemplate = "({0})* CUBE(X) + ({1}) * SQR(X) + ({2}) * X";
|
---|
70 |
|
---|
71 | string modelString;
|
---|
72 | object[] constants;
|
---|
73 | ISymbolicExpressionTree modelTree;
|
---|
74 |
|
---|
75 | constants = new object[] { 1.0, 2.0, 3.0 };
|
---|
76 | modelString = string.Format(modelTemplate, constants);
|
---|
77 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
78 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
79 |
|
---|
80 | constants = new object[] { 5.0, -2.0, 500.362 };
|
---|
81 | modelString = string.Format(modelTemplate, constants);
|
---|
82 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
83 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
84 |
|
---|
85 | constants = new object[] { -6987.25, 1, -888 };
|
---|
86 | modelString = string.Format(modelTemplate, constants);
|
---|
87 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
88 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
89 | }
|
---|
90 |
|
---|
91 | [TestMethod]
|
---|
92 | [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
|
---|
93 | [TestProperty("Time", "short")]
|
---|
94 | public void ConstantsOptimizationTest_Nguyen03() {
|
---|
95 | var problemData = new NguyenFunctionThree(seed).GenerateRegressionData();
|
---|
96 | var modelTemplate = "({0})* X*X*X*X*X + ({1}) * X*X*X*X + ({2}) * X*X*X + ({3}) * X*X + ({4}) * X + {5}";
|
---|
97 |
|
---|
98 | string modelString;
|
---|
99 | object[] constants;
|
---|
100 | ISymbolicExpressionTree modelTree;
|
---|
101 |
|
---|
102 | constants = new object[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
|
---|
103 | modelString = string.Format(modelTemplate, constants);
|
---|
104 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
105 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
106 |
|
---|
107 | constants = new object[] { 5.0, -2.0, 500.362, -5646, 0.0001, 1234 };
|
---|
108 | modelString = string.Format(modelTemplate, constants);
|
---|
109 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
110 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
111 |
|
---|
112 | constants = new object[] { -6987.25, 1, -888, +888, -1, 6987.25, 0, 25 };
|
---|
113 | modelString = string.Format(modelTemplate, constants);
|
---|
114 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
115 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
116 | }
|
---|
117 |
|
---|
118 | [TestMethod]
|
---|
119 | [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
|
---|
120 | [TestProperty("Time", "short")]
|
---|
121 | public void ConstantsOptimizationTest_Nguyen05() {
|
---|
122 | var problemData = new NguyenFunctionFive(seed).GenerateRegressionData();
|
---|
123 | var modelTemplate = "({0}) * SIN(({1})*X*X) * COS(({2})*X) + ({3})";
|
---|
124 |
|
---|
125 | string modelString;
|
---|
126 | object[] constants;
|
---|
127 | ISymbolicExpressionTree modelTree;
|
---|
128 |
|
---|
129 | constants = new object[] { 1.0, 2.0, 3.0, 4.0 };
|
---|
130 | modelString = string.Format(modelTemplate, constants);
|
---|
131 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
132 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
133 |
|
---|
134 | constants = new object[] { 5.0, -2.0, -3.0, 5.0 };
|
---|
135 | modelString = string.Format(modelTemplate, constants);
|
---|
136 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
137 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
138 |
|
---|
139 | constants = new object[] { 0.5, 1, 1, 3 };
|
---|
140 | modelString = string.Format(modelTemplate, constants);
|
---|
141 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
142 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
143 | }
|
---|
144 |
|
---|
145 | [TestMethod]
|
---|
146 | [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
|
---|
147 | [TestProperty("Time", "short")]
|
---|
148 | public void ConstantsOptimizationTest_Nguyen07() {
|
---|
149 | var problemData = new NguyenFunctionFive(seed).GenerateRegressionData();
|
---|
150 | var modelTemplate = "({0}) * LOG(({1})*X + ({2})) + ({3}) * LOG(({4})*X*X + ({5})) + ({6})";
|
---|
151 |
|
---|
152 | string modelString;
|
---|
153 | object[] constants;
|
---|
154 | ISymbolicExpressionTree modelTree;
|
---|
155 |
|
---|
156 | constants = new object[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
|
---|
157 | modelString = string.Format(modelTemplate, constants);
|
---|
158 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
159 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
160 |
|
---|
161 | constants = new object[] { 5.0, 2.0, 500.362, -5646, 0.0001, 1234, 421 };
|
---|
162 | modelString = string.Format(modelTemplate, constants);
|
---|
163 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
164 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
165 |
|
---|
166 | constants = new object[] { -6987.25, 1, 888, +888, -1, 6987.25, 0, 25 };
|
---|
167 | modelString = string.Format(modelTemplate, constants);
|
---|
168 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
169 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
170 | }
|
---|
171 |
|
---|
172 | [TestMethod]
|
---|
173 | [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
|
---|
174 | [TestProperty("Time", "short")]
|
---|
175 | public void ConstantsOptimizationTest_Nguyen08() {
|
---|
176 | var problemData = new NguyenFunctionEight(seed).GenerateRegressionData();
|
---|
177 | var modelTemplate = "({0})* SQRT(({1}) * X) + ({2})";
|
---|
178 |
|
---|
179 | string modelString;
|
---|
180 | object[] constants;
|
---|
181 | ISymbolicExpressionTree modelTree;
|
---|
182 |
|
---|
183 | constants = new object[] { 1.0, 2.0, 3.0 };
|
---|
184 | modelString = string.Format(modelTemplate, constants);
|
---|
185 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
186 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
187 |
|
---|
188 | constants = new object[] { 5.0, 20.0, -500.362 };
|
---|
189 | modelString = string.Format(modelTemplate, constants);
|
---|
190 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
191 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
192 |
|
---|
193 | constants = new object[] { -6987.25, 1, -888 };
|
---|
194 | modelString = string.Format(modelTemplate, constants);
|
---|
195 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
196 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
197 | }
|
---|
198 |
|
---|
199 | [TestMethod]
|
---|
200 | [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
|
---|
201 | [TestProperty("Time", "short")]
|
---|
202 | public void ConstantsOptimizationTest_Keijzer05() {
|
---|
203 | var problemData = new KeijzerFunctionFive(seed).GenerateRegressionData();
|
---|
204 | var modelTemplate = "( ({0}) * X * Z ) / ( (({1}) * X + ({2})) * ({3}) * SQR(Y) ) + ({4})";
|
---|
205 |
|
---|
206 | string modelString;
|
---|
207 | object[] constants;
|
---|
208 | ISymbolicExpressionTree modelTree;
|
---|
209 |
|
---|
210 | constants = new object[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
|
---|
211 | modelString = string.Format(modelTemplate, constants);
|
---|
212 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
213 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
214 |
|
---|
215 | constants = new object[] { 5.0, -2.0, 500.362, -5646, 0.0001 };
|
---|
216 | modelString = string.Format(modelTemplate, constants);
|
---|
217 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
218 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
219 |
|
---|
220 | constants = new object[] { -6987.25, 1, -888, +888, -1, 6987.25, 0 };
|
---|
221 | modelString = string.Format(modelTemplate, constants);
|
---|
222 | modelTree = new InfixExpressionParser().Parse(modelString);
|
---|
223 | CompareConstantsOptimizationResults(problemData, modelTree);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|