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