1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Problems.DataAnalysis;
|
---|
9 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
10 | using HeuristicLab.Random;
|
---|
11 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
|
---|
14 | [TestClass]
|
---|
15 | public class MctsSymbolicRegressionTest {
|
---|
16 | private const int Seed = 1234;
|
---|
17 | private IRandom rand;
|
---|
18 |
|
---|
19 | private const double SuccessThreshold = 0.9999999;
|
---|
20 |
|
---|
21 | private GrammarEnumerationAlgorithm alg;
|
---|
22 | private RegressionProblem problem;
|
---|
23 |
|
---|
24 | [TestInitialize]
|
---|
25 | public void InitTest() {
|
---|
26 | rand = new FastRandom(Seed);
|
---|
27 |
|
---|
28 | alg = new GrammarEnumerationAlgorithm();
|
---|
29 | problem = new RegressionProblem();
|
---|
30 | alg.Problem = problem;
|
---|
31 | alg.GuiUpdateInterval = int.MaxValue;
|
---|
32 | foreach (IGrammarEnumerationAnalyzer grammarEnumerationAnalyzer in alg.Analyzers) {
|
---|
33 | alg.Analyzers.SetItemCheckedState(grammarEnumerationAnalyzer, grammarEnumerationAnalyzer is RSquaredEvaluator);
|
---|
34 | }
|
---|
35 | alg.SearchDataStructure = StorageType.PriorityQueue;
|
---|
36 | }
|
---|
37 |
|
---|
38 | [TestCleanup]
|
---|
39 | public void Cleanup() {
|
---|
40 | if (alg.BestTrainingSentence != null) {
|
---|
41 | Console.WriteLine("Training: " + alg.Grammar.ToInfixString(alg.BestTrainingSentence));
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private void EvaluateGrammarEnumeration() {
|
---|
46 | // Evaluate results
|
---|
47 | var eps = 1.0 - SuccessThreshold;
|
---|
48 |
|
---|
49 | // Check if algorithm terminated correctly
|
---|
50 | Assert.IsTrue(alg.Results.ContainsKey("Best solution (Training)"), "No training solution returned!");
|
---|
51 |
|
---|
52 | // Check resultss
|
---|
53 | Assert.AreEqual(1.0, ((IRegressionSolution)alg.Results["Best solution (Training)"].Value).TestRSquared, eps, "Test quality too low!");
|
---|
54 | }
|
---|
55 |
|
---|
56 | [TestMethod]
|
---|
57 | [TestProperty("Goal", "structure search")]
|
---|
58 | public void NoConstants_Nguyen1() {
|
---|
59 | // x³ + x² + x
|
---|
60 | alg.Evaluator.OptimizeConstants = false;
|
---|
61 | alg.MaxComplexity = 6;
|
---|
62 | alg.Problem.ProblemData = new NguyenFunctionOne(Seed).GenerateRegressionData();
|
---|
63 | alg.SearchDataStructureSize = (int)1e6;
|
---|
64 |
|
---|
65 | alg.Start();
|
---|
66 |
|
---|
67 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
68 | TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
|
---|
69 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
70 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
71 |
|
---|
72 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
73 | constSymbol, varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, mulSymbol,
|
---|
74 | constSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, addSymbol,
|
---|
75 | constSymbol, varSymbol, mulSymbol, addSymbol,
|
---|
76 | constSymbol, addSymbol
|
---|
77 | });
|
---|
78 |
|
---|
79 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
80 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
81 |
|
---|
82 | //Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
83 |
|
---|
84 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
85 |
|
---|
86 | // Evaluate
|
---|
87 | EvaluateGrammarEnumeration();
|
---|
88 | }
|
---|
89 |
|
---|
90 | // Too "large" target model for now...
|
---|
91 | //[TestMethod]
|
---|
92 | [TestProperty("Goal", "structure search")]
|
---|
93 | public void NoConstants_Nguyen2() {
|
---|
94 | // x^4 + x³ + x² + x
|
---|
95 | alg.MaxComplexity = 11;
|
---|
96 | alg.Problem.ProblemData = new NguyenFunctionTwo(Seed).GenerateRegressionData();
|
---|
97 |
|
---|
98 | alg.Start();
|
---|
99 | EvaluateGrammarEnumeration();
|
---|
100 | }
|
---|
101 |
|
---|
102 | // Too "large" target model for now...
|
---|
103 | //[TestMethod]
|
---|
104 | [TestProperty("Goal", "structure search")]
|
---|
105 | public void NoConstants_Nguyen3() {
|
---|
106 | // x^5 + x^4 + x^3 + x^2 + x
|
---|
107 | alg.MaxComplexity = 32;
|
---|
108 | alg.Problem.ProblemData = new NguyenFunctionThree(Seed).GenerateRegressionData();
|
---|
109 |
|
---|
110 | alg.Start();
|
---|
111 |
|
---|
112 | EvaluateGrammarEnumeration();
|
---|
113 | }
|
---|
114 |
|
---|
115 | [TestMethod]
|
---|
116 | [TestProperty("Goal", "structure search")]
|
---|
117 | public void NoConstants_Nguyen6() {
|
---|
118 | // sin(x) + sin(x + x²)
|
---|
119 | alg.Evaluator.OptimizeConstants = false;
|
---|
120 | alg.MaxComplexity = 4;
|
---|
121 | alg.Problem.ProblemData = new NguyenFunctionSix(Seed).GenerateRegressionData();
|
---|
122 | alg.SearchDataStructureSize = (int)1e6;
|
---|
123 |
|
---|
124 | alg.Start();
|
---|
125 |
|
---|
126 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
127 | TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
|
---|
128 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
129 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
130 | TerminalSymbol sinSymbol = alg.Grammar.Sin;
|
---|
131 |
|
---|
132 | // c * sin(c x + c) + c * sin(c * x * x + c * x) + c
|
---|
133 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
134 | varSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol,
|
---|
135 | varSymbol, varSymbol, mulSymbol, constSymbol, mulSymbol, varSymbol, constSymbol, mulSymbol, addSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, addSymbol,
|
---|
136 | constSymbol, addSymbol
|
---|
137 | });
|
---|
138 |
|
---|
139 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
140 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
141 |
|
---|
142 | //Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
143 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
144 |
|
---|
145 | EvaluateGrammarEnumeration();
|
---|
146 | }
|
---|
147 |
|
---|
148 | [TestMethod]
|
---|
149 | [TestProperty("Goal", "structure search")]
|
---|
150 | public void NoConstants_Nguyen9() {
|
---|
151 | // sin(x) + sin(y²)
|
---|
152 | alg.Evaluator.OptimizeConstants = false;
|
---|
153 | alg.MaxComplexity = 3;
|
---|
154 | alg.Problem.ProblemData = new NguyenFunctionNine(Seed).GenerateRegressionData();
|
---|
155 | alg.SearchDataStructureSize = (int)1e6;
|
---|
156 |
|
---|
157 | alg.Start();
|
---|
158 |
|
---|
159 | TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First(v => v.StringRepresentation == "X");
|
---|
160 | TerminalSymbol ySymbol = alg.Grammar.VarTerminals.First(v => v.StringRepresentation == "Y");
|
---|
161 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
162 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
163 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
164 | TerminalSymbol sinSymbol = alg.Grammar.Sin;
|
---|
165 |
|
---|
166 | // c*sin(c*x + c) + c*sin(c*y*y + c) + c
|
---|
167 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
168 | xSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol,
|
---|
169 | ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, addSymbol,
|
---|
170 | constSymbol, addSymbol
|
---|
171 | });
|
---|
172 |
|
---|
173 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
174 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
175 |
|
---|
176 | //Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
177 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
178 |
|
---|
179 | EvaluateGrammarEnumeration();
|
---|
180 | }
|
---|
181 |
|
---|
182 | // Too much variables for now...
|
---|
183 | //[TestMethod]
|
---|
184 | [TestProperty("Goal", "structure search")]
|
---|
185 | public void MctsSymbReg_NoConstants_Poly10() {
|
---|
186 | alg.MaxComplexity = 10;
|
---|
187 | alg.Problem.ProblemData = new PolyTen(Seed).GenerateRegressionData();
|
---|
188 |
|
---|
189 | alg.Start();
|
---|
190 | EvaluateGrammarEnumeration();
|
---|
191 | }
|
---|
192 |
|
---|
193 | [TestMethod]
|
---|
194 | [TestProperty("Goal", "structure search")]
|
---|
195 | public void NoConstants_Inverse() {
|
---|
196 | // x / (log(x)*x + x)
|
---|
197 | alg.Evaluator.OptimizeConstants = false;
|
---|
198 | alg.MaxComplexity = 4;
|
---|
199 |
|
---|
200 | var x = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() + 1.1).ToList();
|
---|
201 | var y = x.Select(xi => xi / (Math.Log(xi) * xi + xi)).ToList();
|
---|
202 | alg.Problem.ProblemData = new RegressionProblemData(new Dataset(new List<string>() { "x", "y" }, new List<IList>() { x, y }), "x".ToEnumerable(), "y");
|
---|
203 | alg.SearchDataStructureSize = (int)1e7;
|
---|
204 |
|
---|
205 | alg.Start();
|
---|
206 | EvaluateGrammarEnumeration();
|
---|
207 | }
|
---|
208 |
|
---|
209 | [TestMethod]
|
---|
210 | [TestProperty("Goal", "structure search + const op")]
|
---|
211 | public void Constants_Nguyen7() {
|
---|
212 | // log(x+1) + log(x*x + 1)
|
---|
213 | alg.MaxComplexity = 4;
|
---|
214 | alg.Evaluator.OptimizeConstants = true;
|
---|
215 | alg.Problem.ProblemData = new NguyenFunctionSeven().GenerateRegressionData();
|
---|
216 |
|
---|
217 | alg.Start();
|
---|
218 |
|
---|
219 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
220 | TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
|
---|
221 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
222 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
223 | TerminalSymbol logSymbol = alg.Grammar.Log;
|
---|
224 |
|
---|
225 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
226 | varSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, logSymbol, constSymbol, mulSymbol,
|
---|
227 | varSymbol, varSymbol, mulSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, logSymbol, constSymbol, mulSymbol, addSymbol,
|
---|
228 | constSymbol, addSymbol
|
---|
229 | });
|
---|
230 |
|
---|
231 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
232 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
233 |
|
---|
234 | Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
235 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
236 |
|
---|
237 | // Evaluate
|
---|
238 | EvaluateGrammarEnumeration();
|
---|
239 | }
|
---|
240 |
|
---|
241 | [TestMethod]
|
---|
242 | [TestProperty("Goal", "structure search + const op")]
|
---|
243 | public void Constants_Nguyen12() {
|
---|
244 | // x*x*x*x - x*x*x + y*y/2 -y
|
---|
245 | alg.MaxComplexity = 10;
|
---|
246 | alg.Evaluator.OptimizeConstants = true;
|
---|
247 | alg.Problem.ProblemData = new NguyenFunctionTwelve().GenerateRegressionData();
|
---|
248 |
|
---|
249 | alg.Start();
|
---|
250 |
|
---|
251 | // Evaluate
|
---|
252 | EvaluateGrammarEnumeration();
|
---|
253 | }
|
---|
254 |
|
---|
255 | [TestMethod]
|
---|
256 | [TestProperty("Goal", "sinnus const op")]
|
---|
257 | public void Constants_Keijzer3() {
|
---|
258 | // 0.3*x*sin(2*pi*x)
|
---|
259 | alg.MaxComplexity = 2;
|
---|
260 | alg.Problem.ProblemData = new KeijzerFunctionThree().GenerateRegressionData();
|
---|
261 | alg.SearchDataStructureSize = (int)1e7;
|
---|
262 |
|
---|
263 | alg.Start();
|
---|
264 |
|
---|
265 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
266 | TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
|
---|
267 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
268 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
269 |
|
---|
270 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
271 | constSymbol, varSymbol, mulSymbol, constSymbol, addSymbol, alg.Grammar.Sin,
|
---|
272 | varSymbol, mulSymbol,
|
---|
273 | constSymbol, mulSymbol,
|
---|
274 | constSymbol, addSymbol
|
---|
275 | });
|
---|
276 |
|
---|
277 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
278 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
279 |
|
---|
280 | //Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
281 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
282 |
|
---|
283 | // Evaluate
|
---|
284 | EvaluateGrammarEnumeration();
|
---|
285 | }
|
---|
286 |
|
---|
287 | [TestMethod]
|
---|
288 | [TestProperty("Goal", "structure search + const op")]
|
---|
289 | public void Constants_Keijzer5() {
|
---|
290 | // (30*x*z) / ((x - 10)*y*y)
|
---|
291 | alg.MaxComplexity = 5;
|
---|
292 | alg.Evaluator.OptimizeConstants = true;
|
---|
293 | alg.Problem.ProblemData = new KeijzerFunctionFive().GenerateRegressionData();
|
---|
294 | alg.SearchDataStructureSize = (int)1e7;
|
---|
295 |
|
---|
296 | alg.Start();
|
---|
297 |
|
---|
298 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
299 | TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First(s => s.StringRepresentation == "X");
|
---|
300 | TerminalSymbol ySymbol = alg.Grammar.VarTerminals.First(s => s.StringRepresentation == "Y");
|
---|
301 | TerminalSymbol zSymbol = alg.Grammar.VarTerminals.First(s => s.StringRepresentation == "Z");
|
---|
302 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
303 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
304 | TerminalSymbol invSymbol = alg.Grammar.Inv;
|
---|
305 |
|
---|
306 | // 30 * x * z * 1/(x*y*y - 10*y*y)
|
---|
307 | // --> x z * c * x y * y * c * y y * c * + c + inv c +
|
---|
308 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
309 | xSymbol, zSymbol, mulSymbol, constSymbol, mulSymbol,
|
---|
310 | xSymbol, ySymbol, mulSymbol, ySymbol, mulSymbol, constSymbol, mulSymbol,
|
---|
311 | ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol, constSymbol, addSymbol, invSymbol,
|
---|
312 | constSymbol, addSymbol
|
---|
313 | });
|
---|
314 |
|
---|
315 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
316 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
317 |
|
---|
318 | //Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
319 |
|
---|
320 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
321 |
|
---|
322 | // Evaluate
|
---|
323 | EvaluateGrammarEnumeration();
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | [TestMethod]
|
---|
328 | [TestProperty("Goal", "structure search + const op")]
|
---|
329 | public void Constants_Keijzer12() {
|
---|
330 | // x*x*x*x - x*x*x + y*y/2 - y
|
---|
331 | alg.MaxComplexity = 10;
|
---|
332 | alg.Problem.ProblemData = new KeijzerFunctionTwelve().GenerateRegressionData();
|
---|
333 | alg.SearchDataStructureSize = (int)1e7;
|
---|
334 |
|
---|
335 | alg.Start();
|
---|
336 |
|
---|
337 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
338 | TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First();
|
---|
339 | TerminalSymbol ySymbol = alg.Grammar.VarTerminals.Last();
|
---|
340 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
341 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
342 |
|
---|
343 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
344 | xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol,
|
---|
345 | xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
|
---|
346 | ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
|
---|
347 | ySymbol, constSymbol, mulSymbol, addSymbol,
|
---|
348 | constSymbol, addSymbol
|
---|
349 | });
|
---|
350 |
|
---|
351 | var x = alg.Grammar.ToInfixString(targetSolution);
|
---|
352 |
|
---|
353 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
354 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
355 |
|
---|
356 | //Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
357 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
358 |
|
---|
359 | // Evaluate
|
---|
360 | EvaluateGrammarEnumeration();
|
---|
361 | }
|
---|
362 |
|
---|
363 | [TestMethod]
|
---|
364 | [TestProperty("Goal", "structure search + const op")]
|
---|
365 | public void Constants_Keijzer14() {
|
---|
366 | // 8 / (2 + x*x + y*y)
|
---|
367 | alg.MaxComplexity = 4;
|
---|
368 | alg.Evaluator.OptimizeConstants = true;
|
---|
369 | alg.Problem.ProblemData = new KeijzerFunctionFourteen().GenerateRegressionData();
|
---|
370 | alg.SearchDataStructureSize = (int)1e7;
|
---|
371 |
|
---|
372 | alg.Start();
|
---|
373 |
|
---|
374 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
375 | TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First();
|
---|
376 | TerminalSymbol ySymbol = alg.Grammar.VarTerminals.Last();
|
---|
377 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
378 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
379 | TerminalSymbol divSymbol = alg.Grammar.Inv;
|
---|
380 |
|
---|
381 |
|
---|
382 | // x x mul c mul y y mul c mul add const add inv const mul const add
|
---|
383 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
384 | xSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol,
|
---|
385 | ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
|
---|
386 | constSymbol, addSymbol, divSymbol,
|
---|
387 | constSymbol, mulSymbol,
|
---|
388 | constSymbol, addSymbol
|
---|
389 | });
|
---|
390 |
|
---|
391 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
392 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
393 |
|
---|
394 | //Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
395 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
396 |
|
---|
397 | // Evaluate
|
---|
398 | EvaluateGrammarEnumeration();
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | [TestMethod]
|
---|
403 | [TestProperty("Goal", "structure search + const op")]
|
---|
404 | public void Constants_Keijzer15() {
|
---|
405 | // x*x*x / 5 + y*y*y / 2 - y - x
|
---|
406 | alg.MaxComplexity = 8;
|
---|
407 | alg.Evaluator.OptimizeConstants = true;
|
---|
408 | alg.Problem.ProblemData = new KeijzerFunctionFifteen().GenerateRegressionData();
|
---|
409 |
|
---|
410 | alg.Start();
|
---|
411 |
|
---|
412 | TerminalSymbol constSymbol = alg.Grammar.Const;
|
---|
413 | TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First();
|
---|
414 | TerminalSymbol ySymbol = alg.Grammar.VarTerminals.Last();
|
---|
415 | TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
|
---|
416 | TerminalSymbol addSymbol = alg.Grammar.Addition;
|
---|
417 |
|
---|
418 | // x x * x * const * y y * y * const * + y const * + x const * const +
|
---|
419 | SymbolList targetSolution = new SymbolList(new[] {
|
---|
420 | xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol,
|
---|
421 | ySymbol, ySymbol, mulSymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
|
---|
422 | ySymbol, constSymbol, mulSymbol, addSymbol,
|
---|
423 | xSymbol, constSymbol, mulSymbol, addSymbol,
|
---|
424 | constSymbol, addSymbol
|
---|
425 | });
|
---|
426 |
|
---|
427 | int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
|
---|
428 | int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
|
---|
429 |
|
---|
430 | Assert.IsTrue(alg.DistinctSentencesComplexity.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
|
---|
431 | Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
|
---|
432 |
|
---|
433 | // Evaluate
|
---|
434 | EvaluateGrammarEnumeration();
|
---|
435 | }
|
---|
436 |
|
---|
437 | [TestMethod]
|
---|
438 | [TestProperty("Goal", "Poly-10 derivatives")]
|
---|
439 | public void MctsSymbReg_NoConstants_Poly10_Part1() {
|
---|
440 | alg.MaxComplexity = 12;
|
---|
441 | alg.Evaluator.OptimizeConstants = false;
|
---|
442 | var regProblem = new PolyTen(123).GenerateRegressionData();
|
---|
443 |
|
---|
444 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
445 | // Y' = X1*X2 + X3*X4 + X5*X6
|
---|
446 | // simplify problem by changing target
|
---|
447 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
448 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
449 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
450 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
451 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
452 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
453 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
454 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
455 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
456 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
457 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
458 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
459 | for (int i = 0; i < ys.Length; i++) {
|
---|
460 | //ys[i] -= x1[i] * x7[i] * x9[i];
|
---|
461 | //ys[i] -= x3[i] * x6[i] * x10[i];
|
---|
462 | }
|
---|
463 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
464 |
|
---|
465 | alg.Problem.ProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
466 |
|
---|
467 | alg.Start();
|
---|
468 |
|
---|
469 | EvaluateGrammarEnumeration();
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 |
|
---|
474 |
|
---|
475 | #if false
|
---|
476 |
|
---|
477 | [TestMethod]
|
---|
478 | [TestProperty("Goal", "structure search")]
|
---|
479 | public void MctsSymbReg_NoConstants_15() {
|
---|
480 | alg.MaxTreeSize = 5;
|
---|
481 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
482 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("15")));
|
---|
483 | alg.Problem.ProblemData = regProblem;
|
---|
484 |
|
---|
485 | alg.Start();
|
---|
486 | EvaluateGrammarEnumeration();
|
---|
487 | }
|
---|
488 |
|
---|
489 |
|
---|
490 | [TestMethod]
|
---|
491 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
492 | [TestProperty("Time", "short")]
|
---|
493 | public void MctsSymbReg_NoConstants_Nguyen7() {
|
---|
494 | // log(x + 1) + log(x² + 1)
|
---|
495 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
496 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
|
---|
497 | TestGrammarEnumeration(regProblem);
|
---|
498 | }
|
---|
499 |
|
---|
500 | [TestMethod]
|
---|
501 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
502 | [TestProperty("Time", "short")]
|
---|
503 | public void MctsSymbReg_NoConstants_Poly10_Part1() {
|
---|
504 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
505 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
506 |
|
---|
507 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
508 | // Y' = X1*X2 + X3*X4 + X5*X6
|
---|
509 | // simplify problem by changing target
|
---|
510 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
511 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
512 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
513 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
514 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
515 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
516 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
517 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
518 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
519 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
520 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
521 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
522 | for (int i = 0; i < ys.Length; i++) {
|
---|
523 | ys[i] -= x1[i] * x7[i] * x9[i];
|
---|
524 | ys[i] -= x3[i] * x6[i] * x10[i];
|
---|
525 | }
|
---|
526 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
527 |
|
---|
528 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
529 |
|
---|
530 | TestGrammarEnumeration(modifiedProblemData);
|
---|
531 | }
|
---|
532 |
|
---|
533 | [TestMethod]
|
---|
534 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
535 | [TestProperty("Time", "short")]
|
---|
536 | public void MctsSymbReg_NoConstants_Poly10_Part2() {
|
---|
537 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
538 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
539 |
|
---|
540 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
541 | // Y' = X1*X7*X9 + X3*X6*X10
|
---|
542 | // simplify problem by changing target
|
---|
543 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
544 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
545 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
546 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
547 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
548 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
549 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
550 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
551 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
552 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
553 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
554 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
555 | for (int i = 0; i < ys.Length; i++) {
|
---|
556 | ys[i] -= x1[i] * x2[i];
|
---|
557 | ys[i] -= x3[i] * x4[i];
|
---|
558 | ys[i] -= x5[i] * x6[i];
|
---|
559 | }
|
---|
560 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
561 |
|
---|
562 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
563 |
|
---|
564 | TestGrammarEnumeration(modifiedProblemData);
|
---|
565 | }
|
---|
566 |
|
---|
567 | [TestMethod]
|
---|
568 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
569 | [TestProperty("Time", "short")]
|
---|
570 | public void MctsSymbReg_NoConstants_Poly10_Part3() {
|
---|
571 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
572 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
573 |
|
---|
574 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
575 | // Y' = X1*X2 + X1*X7*X9
|
---|
576 | // simplify problem by changing target
|
---|
577 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
578 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
579 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
580 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
581 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
582 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
583 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
584 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
585 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
586 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
587 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
588 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
589 | for (int i = 0; i < ys.Length; i++) {
|
---|
590 | ys[i] -= x3[i] * x4[i];
|
---|
591 | ys[i] -= x5[i] * x6[i];
|
---|
592 | ys[i] -= x3[i] * x6[i] * x10[i];
|
---|
593 | }
|
---|
594 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
595 |
|
---|
596 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
597 |
|
---|
598 | TestGrammarEnumeration(modifiedProblemData);
|
---|
599 | }
|
---|
600 |
|
---|
601 | [TestMethod]
|
---|
602 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
603 | [TestProperty("Time", "short")]
|
---|
604 | public void MctsSymbReg_NoConstants_Poly10_Part4() {
|
---|
605 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
606 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
607 |
|
---|
608 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
609 | // Y' = X3*X4 + X5*X6 + X3*X6*X10
|
---|
610 | // simplify problem by changing target
|
---|
611 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
612 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
613 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
614 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
615 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
616 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
617 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
618 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
619 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
620 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
621 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
622 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
623 | for (int i = 0; i < ys.Length; i++) {
|
---|
624 | ys[i] -= x1[i] * x2[i];
|
---|
625 | ys[i] -= x1[i] * x7[i] * x9[i];
|
---|
626 | }
|
---|
627 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
628 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
629 |
|
---|
630 |
|
---|
631 | TestGrammarEnumeration(modifiedProblemData);
|
---|
632 | }
|
---|
633 |
|
---|
634 | [TestMethod]
|
---|
635 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
636 | [TestProperty("Time", "short")]
|
---|
637 | public void MctsSymbReg_NoConstants_Poly10_Part5() {
|
---|
638 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
639 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
640 |
|
---|
641 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
642 | // Y' = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9
|
---|
643 | // simplify problem by changing target
|
---|
644 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
645 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
646 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
647 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
648 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
649 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
650 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
651 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
652 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
653 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
654 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
655 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
656 | for (int i = 0; i < ys.Length; i++) {
|
---|
657 | ys[i] -= x3[i] * x6[i] * x10[i];
|
---|
658 | }
|
---|
659 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
660 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
661 |
|
---|
662 |
|
---|
663 | TestGrammarEnumeration(modifiedProblemData);
|
---|
664 | }
|
---|
665 |
|
---|
666 | [TestMethod]
|
---|
667 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
668 | [TestProperty("Time", "short")]
|
---|
669 | public void MctsSymbReg_NoConstants_Poly10_Part6() {
|
---|
670 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
671 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
672 |
|
---|
673 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
674 | // Y' = X1*X2 + X3*X4 + X5*X6 + X3*X6*X10
|
---|
675 | // simplify problem by changing target
|
---|
676 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
677 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
678 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
679 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
680 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
681 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
682 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
683 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
684 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
685 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
686 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
687 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
688 | for (int i = 0; i < ys.Length; i++) {
|
---|
689 | ys[i] -= x1[i] * x7[i] * x9[i];
|
---|
690 | }
|
---|
691 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
692 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
693 |
|
---|
694 | TestGrammarEnumeration(modifiedProblemData);
|
---|
695 | }
|
---|
696 |
|
---|
697 |
|
---|
698 | [TestMethod]
|
---|
699 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
700 | [TestProperty("Time", "long")]
|
---|
701 | public void MctsSymbReg_NoConstants_Poly10_250rows() {
|
---|
702 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
703 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
704 | regProblem.TrainingPartition.Start = 0;
|
---|
705 | regProblem.TrainingPartition.End = regProblem.Dataset.Rows;
|
---|
706 | regProblem.TestPartition.Start = 0;
|
---|
707 | regProblem.TestPartition.End = 2;
|
---|
708 | TestGrammarEnumeration(regProblem);
|
---|
709 | }
|
---|
710 |
|
---|
711 | [TestMethod]
|
---|
712 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
713 | [TestProperty("Time", "long")]
|
---|
714 | public void MctsSymbReg_NoConstants_Poly10_10000rows() {
|
---|
715 | // as poly-10 but more rows
|
---|
716 | var x1 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
717 | var x2 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
718 | var x3 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
719 | var x4 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
720 | var x5 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
721 | var x6 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
722 | var x7 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
723 | var x8 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
724 | var x9 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
725 | var x10 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
726 | var ys = new List<double>();
|
---|
727 | for (int i = 0; i < x1.Count; i++) {
|
---|
728 | ys.Add(x1[i] * x2[i] + x3[i] * x4[i] + x5[i] * x6[i] + x1[i] * x7[i] * x9[i] + x3[i] * x6[i] * x10[i]);
|
---|
729 | }
|
---|
730 |
|
---|
731 | var ds = new Dataset(new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "y" },
|
---|
732 | new[] { x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, ys });
|
---|
733 |
|
---|
734 |
|
---|
735 | var problemData = new RegressionProblemData(ds, new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" }, "y");
|
---|
736 |
|
---|
737 | problemData.TrainingPartition.Start = 0;
|
---|
738 | problemData.TrainingPartition.End = problemData.Dataset.Rows;
|
---|
739 | problemData.TestPartition.Start = 0;
|
---|
740 | problemData.TestPartition.End = 2; // must not be empty
|
---|
741 |
|
---|
742 |
|
---|
743 | TestGrammarEnumeration(problemData);
|
---|
744 | }
|
---|
745 |
|
---|
746 | [TestMethod]
|
---|
747 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
748 | [TestProperty("Time", "short")]
|
---|
749 | public void MctsSymbReg_NoConstants_TwoVars() {
|
---|
750 |
|
---|
751 | // y = x1 + x2 + x1*x2 + x1*x2*x2 + x1*x1*x2
|
---|
752 | var x1 = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
753 | var x2 = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
754 | var ys = x1.Zip(x2, (x1i, x2i) => x1i + x2i + x1i * x2i + x1i * x2i * x2i + x1i * x1i * x2i).ToList();
|
---|
755 |
|
---|
756 | var ds = new Dataset(new string[] { "a", "b", "y" }, new[] { x1, x2, ys });
|
---|
757 | var problemData = new RegressionProblemData(ds, new string[] { "a", "b" }, "y");
|
---|
758 |
|
---|
759 | TestGrammarEnumeration(problemData);
|
---|
760 | }
|
---|
761 |
|
---|
762 | [TestMethod]
|
---|
763 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
764 | [TestProperty("Time", "short")]
|
---|
765 | public void MctsSymbReg_NoConstants_Misleading() {
|
---|
766 |
|
---|
767 | // y = a + baaaaa (the effect of the second term should be very small)
|
---|
768 | // the alg will quickly find that a has big effect and will search below a
|
---|
769 | // since we prevent a + a... the algorithm must find the correct expression via a + b...
|
---|
770 | // however b has a small effect so the branch might not be identified as relevant
|
---|
771 | var @as = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
772 | var bs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
773 | var cs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() * 1.0e-3).ToList();
|
---|
774 | var ds = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
775 | var es = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
776 | var ys = new double[@as.Count];
|
---|
777 | for (int i = 0; i < ys.Length; i++)
|
---|
778 | ys[i] = @as[i] + bs[i] + @as[i] * bs[i] * cs[i];
|
---|
779 |
|
---|
780 | var dataset = new Dataset(new string[] { "a", "b", "c", "d", "e", "y" }, new[] { @as, bs, cs, ds, es, ys.ToList() });
|
---|
781 |
|
---|
782 | var problemData = new RegressionProblemData(dataset, new string[] { "a", "b", "c", "d", "e" }, "y");
|
---|
783 |
|
---|
784 | TestGrammarEnumeration(problemData);
|
---|
785 | }
|
---|
786 |
|
---|
787 | [TestMethod]
|
---|
788 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
789 | [TestProperty("Time", "short")]
|
---|
790 | public void MctsSymbRegKeijzer7() {
|
---|
791 | // ln(x)
|
---|
792 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
793 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 7 f(")));
|
---|
794 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
795 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
796 | TestGrammarEnumeration(regProblem);
|
---|
797 | }
|
---|
798 |
|
---|
799 |
|
---|
800 | [TestMethod]
|
---|
801 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
802 | [TestProperty("Time", "short")]
|
---|
803 | public void MctsSymbRegBenchmarkNguyen5() {
|
---|
804 | // sin(x²)cos(x) - 1
|
---|
805 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
|
---|
806 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F5 ")));
|
---|
807 | TestGrammarEnumeration(regProblem);
|
---|
808 | }
|
---|
809 |
|
---|
810 | [TestMethod]
|
---|
811 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
812 | [TestProperty("Time", "short")]
|
---|
813 | public void MctsSymbRegBenchmarkNguyen6() {
|
---|
814 | // sin(x) + sin(x + x²)
|
---|
815 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
|
---|
816 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F6 ")));
|
---|
817 | TestGrammarEnumeration(regProblem);
|
---|
818 | }
|
---|
819 |
|
---|
820 | [TestMethod]
|
---|
821 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
822 | [TestProperty("Time", "short")]
|
---|
823 | public void MctsSymbRegBenchmarkNguyen7() {
|
---|
824 | // log(x + 1) + log(x² + 1)
|
---|
825 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
826 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
|
---|
827 | TestGrammarEnumeration(regProblem);
|
---|
828 | }
|
---|
829 | [TestMethod]
|
---|
830 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
831 | [TestProperty("Time", "short")]
|
---|
832 | public void MctsSymbRegBenchmarkNguyen8() {
|
---|
833 | // Sqrt(x)
|
---|
834 | // = x ^ 0.5
|
---|
835 | // = exp(0.5 * log(x))
|
---|
836 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
837 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F8 ")));
|
---|
838 | TestGrammarEnumeration(regProblem);
|
---|
839 | }
|
---|
840 |
|
---|
841 | // [TestMethod]
|
---|
842 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
843 | [TestProperty("Time", "short")]
|
---|
844 | public void MctsSymbRegBenchmarkNguyen9() {
|
---|
845 | // sin(x) + sin(y²)
|
---|
846 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
|
---|
847 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F9 ")));
|
---|
848 | TestGrammarEnumeration(regProblem);
|
---|
849 | }
|
---|
850 |
|
---|
851 | // [TestMethod]
|
---|
852 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
853 | [TestProperty("Time", "short")]
|
---|
854 | public void MctsSymbRegBenchmarkNguyen10() {
|
---|
855 | // 2sin(x)cos(y)
|
---|
856 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
|
---|
857 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F10 ")));
|
---|
858 | TestGrammarEnumeration(regProblem);
|
---|
859 | }
|
---|
860 |
|
---|
861 | [TestMethod]
|
---|
862 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
863 | [TestProperty("Time", "short")]
|
---|
864 | public void MctsSymbRegBenchmarkNguyen11() {
|
---|
865 | // x ^ y , x > 0, y > 0
|
---|
866 | // = exp(y * log(x))
|
---|
867 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
868 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F11 ")));
|
---|
869 | TestGrammarEnumeration(regProblem);
|
---|
870 | }
|
---|
871 | [TestMethod]
|
---|
872 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
873 | [TestProperty("Time", "short")]
|
---|
874 | public void MctsSymbRegBenchmarkNguyen12() {
|
---|
875 | // x^4 - x³ + y²/2 - y
|
---|
876 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
877 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F12 ")));
|
---|
878 | TestGrammarEnumeration(regProblem);
|
---|
879 | }
|
---|
880 |
|
---|
881 | [TestMethod]
|
---|
882 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
883 | [TestProperty("Time", "long")]
|
---|
884 | public void MctsSymbRegBenchmarkKeijzer5() {
|
---|
885 | // (30 * x * z) / ((x - 10) * y²)
|
---|
886 | // = 30 x z / (xy² - y²)
|
---|
887 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
888 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 5 f(")));
|
---|
889 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
890 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
891 | TestGrammarEnumeration(regProblem);
|
---|
892 | }
|
---|
893 |
|
---|
894 | [TestMethod]
|
---|
895 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
896 | [TestProperty("Time", "short")]
|
---|
897 | public void MctsSymbRegBenchmarkKeijzer6() {
|
---|
898 | // Keijzer 6 f(x) = Sum(1 / i) From 1 to X , x \in [0..120]
|
---|
899 | // we can only approximate this
|
---|
900 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
901 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 6 f(")));
|
---|
902 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
903 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
904 | TestGrammarEnumeration(regProblem);
|
---|
905 | }
|
---|
906 |
|
---|
907 | [TestMethod]
|
---|
908 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
909 | [TestProperty("Time", "short")]
|
---|
910 | public void MctsSymbRegBenchmarkKeijzer8() {
|
---|
911 | // sqrt(x)
|
---|
912 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
913 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 8 f(")));
|
---|
914 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
915 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
916 | TestGrammarEnumeration(regProblem);
|
---|
917 | }
|
---|
918 |
|
---|
919 | [TestMethod]
|
---|
920 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
921 | [TestProperty("Time", "short")]
|
---|
922 | public void MctsSymbRegBenchmarkKeijzer9() {
|
---|
923 | // arcsinh(x) i.e. ln(x + sqrt(x² + 1))
|
---|
924 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
925 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 9 f(")));
|
---|
926 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
927 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
928 | TestGrammarEnumeration(regProblem);
|
---|
929 | }
|
---|
930 |
|
---|
931 | [TestMethod]
|
---|
932 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
933 | [TestProperty("Time", "short")]
|
---|
934 | public void MctsSymbRegBenchmarkKeijzer11() {
|
---|
935 | // xy + sin( (x-1) (y-1) )
|
---|
936 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
|
---|
937 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 11 f(")));
|
---|
938 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
939 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
940 | TestGrammarEnumeration(regProblem);
|
---|
941 | }
|
---|
942 |
|
---|
943 | [TestMethod]
|
---|
944 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
945 | [TestProperty("Time", "short")]
|
---|
946 | public void MctsSymbRegBenchmarkKeijzer12() {
|
---|
947 | // x^4 - x³ + y² / 2 - y, same as Nguyen 12
|
---|
948 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
949 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 12 f(")));
|
---|
950 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
951 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
952 | TestGrammarEnumeration(regProblem);
|
---|
953 | }
|
---|
954 |
|
---|
955 | [TestMethod]
|
---|
956 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
957 | [TestProperty("Time", "short")]
|
---|
958 | public void MctsSymbRegBenchmarkKeijzer14() {
|
---|
959 | // 8 / (2 + x² + y²)
|
---|
960 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
961 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 14 f(")));
|
---|
962 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
963 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
964 | TestGrammarEnumeration(regProblem);
|
---|
965 | }
|
---|
966 |
|
---|
967 | [TestMethod]
|
---|
968 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
969 | [TestProperty("Time", "short")]
|
---|
970 | public void MctsSymbRegBenchmarkKeijzer15() {
|
---|
971 | // x³ / 5 + y³ / 2 - y - x
|
---|
972 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
973 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 15 f(")));
|
---|
974 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
975 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
976 | TestGrammarEnumeration(regProblem);
|
---|
977 | }
|
---|
978 | #endif
|
---|
979 | }
|
---|
980 | }
|
---|