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