Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs @ 15762

Last change on this file since 15762 was 15746, checked in by lkammere, 7 years ago

#2886: Refactor grammar enumeration alg.

File size: 30.9 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Linq;
4using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
5using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Optimization;
10using HeuristicLab.Problems.DataAnalysis;
11using HeuristicLab.Random;
12using Microsoft.VisualStudio.TestTools.UnitTesting;
13
14namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
15  [TestClass]
16  public class MctsSymbolicRegressionTest {
17    private const int Seed = 1234;
18    private IRandom rand;
19
20    private const double SuccessThreshold = 0.9999999;
21
22    private GrammarEnumerationAlgorithm alg;
23    private RegressionProblem problem;
24
25    [TestInitialize]
26    public void InitTest() {
27      rand = new FastRandom(Seed);
28
29      alg = new GrammarEnumerationAlgorithm();
30      problem = new RegressionProblem();
31      alg.Problem = problem;
32      alg.GuiUpdateInterval = int.MaxValue;
33    }
34
35    [TestCleanup]
36    public void Cleanup() {
37      if (alg.BestTrainingSentence != null) {
38        Console.WriteLine("Training: " + alg.Grammar.PostfixToInfixParser(alg.BestTrainingSentence));
39      }
40    }
41
42
43    private void EvaluateGrammarEnumeration() {
44      // Evaluate results
45      var eps = 1.0 - SuccessThreshold;
46
47      // Check if algorithm terminated correctly
48      Assert.IsTrue(alg.Results.ContainsKey("Best solution (Training)"), "No training solution returned!");
49
50      // Check resultss
51      Assert.AreEqual(1.0, ((IRegressionSolution)alg.Results["Best solution (Training)"].Value).TestRSquared, eps, "Test quality too low!");
52    }
53
54
55    #region test structure search (no constants)
56    [TestMethod]
57    [TestProperty("Goal", "structure search")]
58    public void MctsSymbReg_NoConstants_Nguyen1() {
59      // x³ + x² + x
60      alg.MaxTreeSize = 15;
61      Console.WriteLine(alg.MaxTreeSize);
62      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
63      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 ")));
64      alg.Problem.ProblemData = regProblem;
65
66      alg.Start();
67
68      TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First();
69      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
70      TerminalSymbol addSymbol = alg.Grammar.Addition;
71
72      SymbolString targetSolution = new SymbolString(new[] {
73        varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol,
74        varSymbol, varSymbol, mulSymbol, addSymbol,
75        varSymbol, addSymbol
76      });
77
78      int targetSolutionHash = alg.Grammar.CalcHashCode(targetSolution);
79      int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence);
80
81      Assert.IsTrue(alg.DistinctSentences.ContainsKey(actualSolutionHash), "Actual solution was not generated!");
82
83      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
84
85      // Evaluate
86      EvaluateGrammarEnumeration();
87    }
88
89    [TestMethod]
90    [TestProperty("Goal", "structure search")]
91    public void MctsSymbReg_NoConstants_Nguyen2() {
92      // x^4 + x³ + x² + x
93      alg.MaxTreeSize = 25;
94      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
95      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 ")));
96      alg.Problem.ProblemData = regProblem;
97
98      alg.Start();
99
100      TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First();
101      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
102      TerminalSymbol addSymbol = alg.Grammar.Addition;
103
104      SymbolString targetSolution = new SymbolString(new[] {
105        varSymbol, varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, mulSymbol,
106        varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, addSymbol,
107        varSymbol, varSymbol, mulSymbol, addSymbol,
108        varSymbol, addSymbol
109      });
110
111      int targetSolutionHash = alg.Grammar.CalcHashCode(targetSolution);
112      int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence);
113
114      var targetSolutionStr = alg.Grammar.PostfixToInfixParser(targetSolution).ToString();
115      var foundTargetSolutionStr = alg.Grammar.PostfixToInfixParser(alg.DistinctSentences[targetSolutionHash]).ToString();
116      var actualSolutionStr = alg.Grammar.PostfixToInfixParser(alg.DistinctSentences[targetSolutionHash]).ToString();
117
118      Assert.IsTrue(alg.DistinctSentences.ContainsKey(targetSolutionHash), "Target solution was not generated!");
119
120      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
121
122      // Evaluate
123      EvaluateGrammarEnumeration();
124    }
125
126    [TestMethod]
127    [TestProperty("Goal", "structure search")]
128    public void MctsSymbReg_NoConstants_Poly10() {
129      alg.MaxTreeSize = 10;
130      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
131      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly")));
132      alg.Problem.ProblemData = regProblem;
133
134      alg.Start();
135
136      Assert.AreEqual(alg.DistinctSentences.Count(), 110);
137
138      // Evaluate
139      // EvaluateGrammarEnumeration();
140    }
141
142    [TestMethod]
143    [TestProperty("Goal", "structure search")]
144    public void MctsSymbReg_NoConstants_15() {
145      alg.MaxTreeSize = 5;
146      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
147      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("15")));
148      alg.Problem.ProblemData = regProblem;
149
150      alg.Start();
151
152      Assert.AreEqual(alg.DistinctSentences.Count(), 16);
153
154      // Evaluate
155      // EvaluateGrammarEnumeration();
156    }
157
158    /*  NEXT UP
159    [TestMethod]
160    [TestProperty("Goal", "structure search")]
161    public void MctsSymbReg_NoConstants_Nguyen3() {
162      // x^5 + x^4 + x³ + x² + x
163      alg.MaxTreeSize = 25;
164      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
165      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F3 ")));
166      TestGrammarEnumeration(regProblem);
167    }
168    [TestMethod]
169    [TestProperty("Goal", "structure search")]
170    public void MctsSymbReg_NoConstants_Nguyen4() {
171
172      // x^6 + x^5 + x^4 + x³ + x² + x
173      alg.MaxTreeSize = 30;
174      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
175      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F4 ")));
176      TestGrammarEnumeration(regProblem);
177    } */
178
179
180    #endregion
181
182    #region TODO
183
184#if false
185    [TestMethod]
186    [TestCategory("Algorithms.DataAnalysis")]
187    [TestProperty("Time", "short")]
188    public void MctsSymbReg_NoConstants_Nguyen7() {
189      // log(x + 1) + log(x² + 1)
190      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
191      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
192      TestGrammarEnumeration(regProblem);
193    }
194
195    [TestMethod]
196    [TestCategory("Algorithms.DataAnalysis")]
197    [TestProperty("Time", "short")]
198    public void MctsSymbReg_NoConstants_Poly10_Part1() {
199      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
200      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
201
202      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
203      //  Y' = X1*X2 + X3*X4 + X5*X6
204      // simplify problem by changing target
205      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
206      var ys = ds.GetDoubleValues("Y").ToArray();
207      var x1 = ds.GetDoubleValues("X1").ToArray();
208      var x2 = ds.GetDoubleValues("X2").ToArray();
209      var x3 = ds.GetDoubleValues("X3").ToArray();
210      var x4 = ds.GetDoubleValues("X4").ToArray();
211      var x5 = ds.GetDoubleValues("X5").ToArray();
212      var x6 = ds.GetDoubleValues("X6").ToArray();
213      var x7 = ds.GetDoubleValues("X7").ToArray();
214      var x8 = ds.GetDoubleValues("X8").ToArray();
215      var x9 = ds.GetDoubleValues("X9").ToArray();
216      var x10 = ds.GetDoubleValues("X10").ToArray();
217      for (int i = 0; i < ys.Length; i++) {
218        ys[i] -= x1[i] * x7[i] * x9[i];
219        ys[i] -= x3[i] * x6[i] * x10[i];
220      }
221      ds.ReplaceVariable("Y", ys.ToList());
222
223      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
224
225      TestGrammarEnumeration(modifiedProblemData);
226    }
227
228    [TestMethod]
229    [TestCategory("Algorithms.DataAnalysis")]
230    [TestProperty("Time", "short")]
231    public void MctsSymbReg_NoConstants_Poly10_Part2() {
232      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
233      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
234
235      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
236      //  Y' = X1*X7*X9 + X3*X6*X10
237      // simplify problem by changing target
238      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
239      var ys = ds.GetDoubleValues("Y").ToArray();
240      var x1 = ds.GetDoubleValues("X1").ToArray();
241      var x2 = ds.GetDoubleValues("X2").ToArray();
242      var x3 = ds.GetDoubleValues("X3").ToArray();
243      var x4 = ds.GetDoubleValues("X4").ToArray();
244      var x5 = ds.GetDoubleValues("X5").ToArray();
245      var x6 = ds.GetDoubleValues("X6").ToArray();
246      var x7 = ds.GetDoubleValues("X7").ToArray();
247      var x8 = ds.GetDoubleValues("X8").ToArray();
248      var x9 = ds.GetDoubleValues("X9").ToArray();
249      var x10 = ds.GetDoubleValues("X10").ToArray();
250      for (int i = 0; i < ys.Length; i++) {
251        ys[i] -= x1[i] * x2[i];
252        ys[i] -= x3[i] * x4[i];
253        ys[i] -= x5[i] * x6[i];
254      }
255      ds.ReplaceVariable("Y", ys.ToList());
256
257      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
258
259      TestGrammarEnumeration(modifiedProblemData);
260    }
261
262    [TestMethod]
263    [TestCategory("Algorithms.DataAnalysis")]
264    [TestProperty("Time", "short")]
265    public void MctsSymbReg_NoConstants_Poly10_Part3() {
266      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
267      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
268
269      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
270      //  Y' = X1*X2 + X1*X7*X9
271      // simplify problem by changing target
272      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
273      var ys = ds.GetDoubleValues("Y").ToArray();
274      var x1 = ds.GetDoubleValues("X1").ToArray();
275      var x2 = ds.GetDoubleValues("X2").ToArray();
276      var x3 = ds.GetDoubleValues("X3").ToArray();
277      var x4 = ds.GetDoubleValues("X4").ToArray();
278      var x5 = ds.GetDoubleValues("X5").ToArray();
279      var x6 = ds.GetDoubleValues("X6").ToArray();
280      var x7 = ds.GetDoubleValues("X7").ToArray();
281      var x8 = ds.GetDoubleValues("X8").ToArray();
282      var x9 = ds.GetDoubleValues("X9").ToArray();
283      var x10 = ds.GetDoubleValues("X10").ToArray();
284      for (int i = 0; i < ys.Length; i++) {
285        ys[i] -= x3[i] * x4[i];
286        ys[i] -= x5[i] * x6[i];
287        ys[i] -= x3[i] * x6[i] * x10[i];
288      }
289      ds.ReplaceVariable("Y", ys.ToList());
290
291      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
292
293      TestGrammarEnumeration(modifiedProblemData);
294    }
295
296    [TestMethod]
297    [TestCategory("Algorithms.DataAnalysis")]
298    [TestProperty("Time", "short")]
299    public void MctsSymbReg_NoConstants_Poly10_Part4() {
300      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
301      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
302
303      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
304      //  Y' = X3*X4 + X5*X6 + X3*X6*X10
305      // simplify problem by changing target
306      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
307      var ys = ds.GetDoubleValues("Y").ToArray();
308      var x1 = ds.GetDoubleValues("X1").ToArray();
309      var x2 = ds.GetDoubleValues("X2").ToArray();
310      var x3 = ds.GetDoubleValues("X3").ToArray();
311      var x4 = ds.GetDoubleValues("X4").ToArray();
312      var x5 = ds.GetDoubleValues("X5").ToArray();
313      var x6 = ds.GetDoubleValues("X6").ToArray();
314      var x7 = ds.GetDoubleValues("X7").ToArray();
315      var x8 = ds.GetDoubleValues("X8").ToArray();
316      var x9 = ds.GetDoubleValues("X9").ToArray();
317      var x10 = ds.GetDoubleValues("X10").ToArray();
318      for (int i = 0; i < ys.Length; i++) {
319        ys[i] -= x1[i] * x2[i];
320        ys[i] -= x1[i] * x7[i] * x9[i];
321      }
322      ds.ReplaceVariable("Y", ys.ToList());
323      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
324
325
326      TestGrammarEnumeration(modifiedProblemData);
327    }
328
329    [TestMethod]
330    [TestCategory("Algorithms.DataAnalysis")]
331    [TestProperty("Time", "short")]
332    public void MctsSymbReg_NoConstants_Poly10_Part5() {
333      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
334      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
335
336      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
337      //  Y' = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9
338      // simplify problem by changing target
339      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
340      var ys = ds.GetDoubleValues("Y").ToArray();
341      var x1 = ds.GetDoubleValues("X1").ToArray();
342      var x2 = ds.GetDoubleValues("X2").ToArray();
343      var x3 = ds.GetDoubleValues("X3").ToArray();
344      var x4 = ds.GetDoubleValues("X4").ToArray();
345      var x5 = ds.GetDoubleValues("X5").ToArray();
346      var x6 = ds.GetDoubleValues("X6").ToArray();
347      var x7 = ds.GetDoubleValues("X7").ToArray();
348      var x8 = ds.GetDoubleValues("X8").ToArray();
349      var x9 = ds.GetDoubleValues("X9").ToArray();
350      var x10 = ds.GetDoubleValues("X10").ToArray();
351      for (int i = 0; i < ys.Length; i++) {
352        ys[i] -= x3[i] * x6[i] * x10[i];
353      }
354      ds.ReplaceVariable("Y", ys.ToList());
355      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
356
357
358      TestGrammarEnumeration(modifiedProblemData);
359    }
360
361    [TestMethod]
362    [TestCategory("Algorithms.DataAnalysis")]
363    [TestProperty("Time", "short")]
364    public void MctsSymbReg_NoConstants_Poly10_Part6() {
365      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
366      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
367
368      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
369      //  Y' = X1*X2 + X3*X4 + X5*X6 + X3*X6*X10
370      // simplify problem by changing target
371      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
372      var ys = ds.GetDoubleValues("Y").ToArray();
373      var x1 = ds.GetDoubleValues("X1").ToArray();
374      var x2 = ds.GetDoubleValues("X2").ToArray();
375      var x3 = ds.GetDoubleValues("X3").ToArray();
376      var x4 = ds.GetDoubleValues("X4").ToArray();
377      var x5 = ds.GetDoubleValues("X5").ToArray();
378      var x6 = ds.GetDoubleValues("X6").ToArray();
379      var x7 = ds.GetDoubleValues("X7").ToArray();
380      var x8 = ds.GetDoubleValues("X8").ToArray();
381      var x9 = ds.GetDoubleValues("X9").ToArray();
382      var x10 = ds.GetDoubleValues("X10").ToArray();
383      for (int i = 0; i < ys.Length; i++) {
384        ys[i] -= x1[i] * x7[i] * x9[i];
385      }
386      ds.ReplaceVariable("Y", ys.ToList());
387      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
388
389      TestGrammarEnumeration(modifiedProblemData);
390    }
391
392
393    [TestMethod]
394    [TestCategory("Algorithms.DataAnalysis")]
395    [TestProperty("Time", "long")]
396    public void MctsSymbReg_NoConstants_Poly10_250rows() {
397      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
398      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
399      regProblem.TrainingPartition.Start = 0;
400      regProblem.TrainingPartition.End = regProblem.Dataset.Rows;
401      regProblem.TestPartition.Start = 0;
402      regProblem.TestPartition.End = 2;
403      TestGrammarEnumeration(regProblem);
404    }
405
406    [TestMethod]
407    [TestCategory("Algorithms.DataAnalysis")]
408    [TestProperty("Time", "long")]
409    public void MctsSymbReg_NoConstants_Poly10_10000rows() {
410      // as poly-10 but more rows
411      var x1 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
412      var x2 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
413      var x3 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
414      var x4 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
415      var x5 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
416      var x6 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
417      var x7 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
418      var x8 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
419      var x9 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
420      var x10 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
421      var ys = new List<double>();
422      for (int i = 0; i < x1.Count; i++) {
423        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]);
424      }
425
426      var ds = new Dataset(new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "y" },
427        new[] { x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, ys });
428
429
430      var problemData = new RegressionProblemData(ds, new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" }, "y");
431
432      problemData.TrainingPartition.Start = 0;
433      problemData.TrainingPartition.End = problemData.Dataset.Rows;
434      problemData.TestPartition.Start = 0;
435      problemData.TestPartition.End = 2; // must not be empty
436
437
438      TestGrammarEnumeration(problemData);
439    }
440
441    [TestMethod]
442    [TestCategory("Algorithms.DataAnalysis")]
443    [TestProperty("Time", "short")]
444    public void MctsSymbReg_NoConstants_TwoVars() {
445
446      // y = x1 + x2 + x1*x2 + x1*x2*x2 + x1*x1*x2
447      var x1 = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
448      var x2 = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
449      var ys = x1.Zip(x2, (x1i, x2i) => x1i + x2i + x1i * x2i + x1i * x2i * x2i + x1i * x1i * x2i).ToList();
450
451      var ds = new Dataset(new string[] { "a", "b", "y" }, new[] { x1, x2, ys });
452      var problemData = new RegressionProblemData(ds, new string[] { "a", "b" }, "y");
453
454      TestGrammarEnumeration(problemData);
455    }
456
457    [TestMethod]
458    [TestCategory("Algorithms.DataAnalysis")]
459    [TestProperty("Time", "short")]
460    public void MctsSymbReg_NoConstants_Misleading() {
461
462      // y = a + baaaaa (the effect of the second term should be very small)
463      // the alg will quickly find that a has big effect and will search below a
464      // since we prevent a + a... the algorithm must find the correct expression via a + b...
465      // however b has a small effect so the branch might not be identified as relevant
466      var @as = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
467      var bs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
468      var cs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() * 1.0e-3).ToList();
469      var ds = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
470      var es = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
471      var ys = new double[@as.Count];
472      for (int i = 0; i < ys.Length; i++)
473        ys[i] = @as[i] + bs[i] + @as[i] * bs[i] * cs[i];
474
475      var dataset = new Dataset(new string[] { "a", "b", "c", "d", "e", "y" }, new[] { @as, bs, cs, ds, es, ys.ToList() });
476
477      var problemData = new RegressionProblemData(dataset, new string[] { "a", "b", "c", "d", "e" }, "y");
478
479      TestGrammarEnumeration(problemData);
480    }
481
482    #region restricted structure but including numeric constants
483
484    [TestMethod]
485    [TestCategory("Algorithms.DataAnalysis")]
486    [TestProperty("Time", "short")]
487    public void MctsSymbRegKeijzer7() {
488      // ln(x)
489      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
490      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 7 f(")));
491      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
492      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
493      TestGrammarEnumeration(regProblem);
494    }
495
496   
497    [TestMethod]
498    [TestCategory("Algorithms.DataAnalysis")]
499    [TestProperty("Time", "short")]
500    public void MctsSymbRegBenchmarkNguyen5() {
501      // sin(x²)cos(x) - 1
502      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
503      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F5 ")));
504      TestGrammarEnumeration(regProblem);
505    }
506
507    [TestMethod]
508    [TestCategory("Algorithms.DataAnalysis")]
509    [TestProperty("Time", "short")]
510    public void MctsSymbRegBenchmarkNguyen6() {
511      // sin(x) + sin(x + x²)
512      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
513      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F6 ")));
514      TestGrammarEnumeration(regProblem);
515    }
516   
517    [TestMethod]
518    [TestCategory("Algorithms.DataAnalysis")]
519    [TestProperty("Time", "short")]
520    public void MctsSymbRegBenchmarkNguyen7() {
521      //  log(x + 1) + log(x² + 1)
522      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
523      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
524      TestGrammarEnumeration(regProblem);
525    }
526    [TestMethod]
527    [TestCategory("Algorithms.DataAnalysis")]
528    [TestProperty("Time", "short")]
529    public void MctsSymbRegBenchmarkNguyen8() {
530      // Sqrt(x)
531      // = x ^ 0.5
532      // = exp(0.5 * log(x))
533      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
534      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F8 ")));
535      TestGrammarEnumeration(regProblem);
536    }
537   
538    // [TestMethod]
539    [TestCategory("Algorithms.DataAnalysis")]
540    [TestProperty("Time", "short")]
541    public void MctsSymbRegBenchmarkNguyen9() {
542      //  sin(x) + sin(y²)
543      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
544      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F9 ")));
545      TestGrammarEnumeration(regProblem);
546    }
547
548    // [TestMethod]
549    [TestCategory("Algorithms.DataAnalysis")]
550    [TestProperty("Time", "short")]
551    public void MctsSymbRegBenchmarkNguyen10() {
552      // 2sin(x)cos(y)
553      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
554      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F10 ")));
555      TestGrammarEnumeration(regProblem);
556    }
557   
558    [TestMethod]
559    [TestCategory("Algorithms.DataAnalysis")]
560    [TestProperty("Time", "short")]
561    public void MctsSymbRegBenchmarkNguyen11() {
562      // x ^ y  , x > 0, y > 0   
563      // = exp(y * log(x))
564      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
565      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F11 ")));
566      TestGrammarEnumeration(regProblem);
567    }
568    [TestMethod]
569    [TestCategory("Algorithms.DataAnalysis")]
570    [TestProperty("Time", "short")]
571    public void MctsSymbRegBenchmarkNguyen12() {
572      // x^4 - x³ + y²/2 - y
573      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
574      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F12 ")));
575      TestGrammarEnumeration(regProblem);
576    }
577
578    #endregion
579
580    #region keijzer
581    [TestMethod]
582    [TestCategory("Algorithms.DataAnalysis")]
583    [TestProperty("Time", "long")]
584    public void MctsSymbRegBenchmarkKeijzer5() {
585      // (30 * x * z) / ((x - 10)  * y²)
586      // = 30 x z / (xy² - y²)
587      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
588      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 5 f(")));
589      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
590      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
591      TestGrammarEnumeration(regProblem);
592    }
593
594    [TestMethod]
595    [TestCategory("Algorithms.DataAnalysis")]
596    [TestProperty("Time", "short")]
597    public void MctsSymbRegBenchmarkKeijzer6() {
598      // Keijzer 6 f(x) = Sum(1 / i) From 1 to X  , x \in [0..120]
599      // we can only approximate this
600      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
601      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 6 f(")));
602      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
603      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
604      TestGrammarEnumeration(regProblem);
605    }
606
607    [TestMethod]
608    [TestCategory("Algorithms.DataAnalysis")]
609    [TestProperty("Time", "short")]
610    public void MctsSymbRegBenchmarkKeijzer8() {
611      // sqrt(x)
612      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
613      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 8 f(")));
614      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
615      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
616      TestGrammarEnumeration(regProblem);
617    }
618
619    [TestMethod]
620    [TestCategory("Algorithms.DataAnalysis")]
621    [TestProperty("Time", "short")]
622    public void MctsSymbRegBenchmarkKeijzer9() {
623      // arcsinh(x)  i.e. ln(x + sqrt(x² + 1))
624      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
625      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 9 f(")));
626      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
627      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
628      TestGrammarEnumeration(regProblem);
629    }
630
631    [TestMethod]
632    [TestCategory("Algorithms.DataAnalysis")]
633    [TestProperty("Time", "short")]
634    public void MctsSymbRegBenchmarkKeijzer11() {
635      // xy + sin( (x-1) (y-1) )
636      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
637      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 11 f(")));
638      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
639      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
640      TestGrammarEnumeration(regProblem);
641    }
642
643    [TestMethod]
644    [TestCategory("Algorithms.DataAnalysis")]
645    [TestProperty("Time", "short")]
646    public void MctsSymbRegBenchmarkKeijzer12() {
647      // x^4 - x³ + y² / 2 - y,  same as Nguyen 12             
648      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
649      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 12 f(")));
650      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
651      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
652      TestGrammarEnumeration(regProblem);
653    }
654
655    [TestMethod]
656    [TestCategory("Algorithms.DataAnalysis")]
657    [TestProperty("Time", "short")]
658    public void MctsSymbRegBenchmarkKeijzer14() {
659      // 8 / (2 + x² + y²)
660      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
661      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 14 f(")));
662      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
663      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
664      TestGrammarEnumeration(regProblem);
665    }
666
667    [TestMethod]
668    [TestCategory("Algorithms.DataAnalysis")]
669    [TestProperty("Time", "short")]
670    public void MctsSymbRegBenchmarkKeijzer15() {
671      // x³ / 5 + y³ / 2 - y - x
672      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
673      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 15 f(")));
674      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
675      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
676      TestGrammarEnumeration(regProblem);
677    }
678    #endregion
679
680#endif
681    #endregion
682  }
683}
Note: See TracBrowser for help on using the repository browser.