Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MCTS-SymbReg-2796/Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs @ 15416

Last change on this file since 15416 was 15416, checked in by gkronber, 6 years ago

#2796 worked on MCTS for symbreg

File size: 49.4 KB
Line 
1using System;
2using System.Linq;
3using System.Threading;
4using HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression.Policies;
5using HeuristicLab.Data;
6using HeuristicLab.Optimization;
7using HeuristicLab.Problems.DataAnalysis;
8using HeuristicLab.Problems.Instances.DataAnalysis;
9using HeuristicLab.Random;
10using Microsoft.VisualStudio.TestTools.UnitTesting;
11
12namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
13  [TestClass()]
14  public class MctsSymbolicRegressionTest {
15    #region expression hashing
16    [TestMethod]
17    [TestCategory("Algorithms.DataAnalysis")]
18    [TestProperty("Time", "short")]
19    public void ExprHashTest() {
20      int nParams;
21      byte[] code;
22
23      {
24        // addition of variables
25        var codeGen = new CodeGenerator();
26        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
27        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
28        codeGen.Emit1(OpCodes.Add);
29        codeGen.Emit1(OpCodes.Exit);
30        codeGen.GetCode(out code, out nParams);
31        var h1 = ExprHash.GetHash(code, nParams);
32
33        codeGen = new CodeGenerator();
34        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
35        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
36        codeGen.Emit1(OpCodes.Add);
37        codeGen.Emit1(OpCodes.Exit);
38        codeGen.GetCode(out code, out nParams);
39        var h2 = ExprHash.GetHash(code, nParams);
40
41        Assert.AreEqual(h1, h2);
42      }
43
44      {
45        // multiplication of variables
46        var codeGen = new CodeGenerator();
47        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
48        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
49        codeGen.Emit1(OpCodes.Mul);
50        codeGen.Emit1(OpCodes.Exit);
51        codeGen.GetCode(out code, out nParams);
52        var h1 = ExprHash.GetHash(code, nParams);
53
54        codeGen = new CodeGenerator();
55        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
56        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
57        codeGen.Emit1(OpCodes.Mul);
58        codeGen.Emit1(OpCodes.Exit);
59        codeGen.GetCode(out code, out nParams);
60        var h2 = ExprHash.GetHash(code, nParams);
61
62        Assert.AreEqual(h1, h2);
63      }
64
65      {
66        // distributivity
67        var codeGen = new CodeGenerator();
68        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
69        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
70        codeGen.Emit1(OpCodes.Add);
71        codeGen.Emit2(OpCodes.LoadVar, 3);
72        codeGen.Emit1(OpCodes.Mul);
73        codeGen.Emit1(OpCodes.Exit);
74        codeGen.GetCode(out code, out nParams);
75        var h1 = ExprHash.GetHash(code, nParams);
76
77        codeGen = new CodeGenerator();
78        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
79        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 3);
80        codeGen.Emit1(OpCodes.Mul);
81        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
82        codeGen.Emit2(OpCodes.LoadVar, 3);
83        codeGen.Emit1(OpCodes.Mul);
84        codeGen.Emit1(OpCodes.Add);
85        codeGen.Emit1(OpCodes.Exit);
86        codeGen.GetCode(out code, out nParams);
87        var h2 = ExprHash.GetHash(code, nParams);
88
89        Assert.AreEqual(h1, h2);
90      }
91
92
93      {
94        // div
95        var codeGen = new CodeGenerator();
96        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
97        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
98        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
99        codeGen.Emit1(OpCodes.Inv);
100        codeGen.Emit1(OpCodes.Exit);
101        codeGen.GetCode(out code, out nParams);
102        var h1 = ExprHash.GetHash(code, nParams);
103
104        codeGen = new CodeGenerator();
105        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
106        codeGen.Emit1(OpCodes.Inv);
107        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
108        codeGen.Emit1(OpCodes.Inv);
109        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
110        codeGen.Emit1(OpCodes.Exit);
111        codeGen.GetCode(out code, out nParams);
112        var h2 = ExprHash.GetHash(code, nParams);
113
114        Assert.AreEqual(h1, h2);
115      }
116      {
117        // exp
118        var codeGen = new CodeGenerator();
119        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
120        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
121        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add);
122        codeGen.Emit1(OpCodes.Exp);
123        codeGen.Emit1(OpCodes.Exit);
124        codeGen.GetCode(out code, out nParams);
125        var h1 = ExprHash.GetHash(code, nParams);
126
127        codeGen = new CodeGenerator();
128        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
129        codeGen.Emit1(OpCodes.Exp);
130        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
131        codeGen.Emit1(OpCodes.Exp);
132        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
133        codeGen.GetCode(out code, out nParams);
134        codeGen.Emit1(OpCodes.Exit);
135        var h2 = ExprHash.GetHash(code, nParams);
136
137        Assert.AreEqual(h1, h2);
138      }
139      {
140        // log
141        var codeGen = new CodeGenerator();
142        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
143        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
144        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
145        codeGen.Emit1(OpCodes.Log);
146        codeGen.Emit1(OpCodes.Exit);
147        codeGen.GetCode(out code, out nParams);
148        var h1 = ExprHash.GetHash(code, nParams);
149
150        codeGen = new CodeGenerator();
151        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
152        codeGen.Emit1(OpCodes.Log);
153        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 2);
154        codeGen.Emit1(OpCodes.Log);
155        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add);
156        codeGen.Emit1(OpCodes.Exit);
157        codeGen.GetCode(out code, out nParams);
158        var h2 = ExprHash.GetHash(code, nParams);
159
160        Assert.AreEqual(h1, h2);
161      }
162
163      {
164        // x1 + x1 is equivalent to x1
165        var codeGen = new CodeGenerator();
166        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
167        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
168        codeGen.Emit1(OpCodes.Add);
169        codeGen.Emit1(OpCodes.Exit);
170        codeGen.GetCode(out code, out nParams);
171        var h1 = ExprHash.GetHash(code, nParams);
172
173        codeGen = new CodeGenerator();
174        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
175        codeGen.Emit1(OpCodes.Exit);
176        codeGen.GetCode(out code, out nParams);
177        var h2 = ExprHash.GetHash(code, nParams);
178
179        Assert.AreEqual(h1, h2);
180      }
181      {
182        // c1*x1 + c2*x1 is equivalent to c3*x1
183        var codeGen = new CodeGenerator();
184        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN);
185        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
186        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
187
188        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN);
189        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
190        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
191
192        codeGen.Emit1(OpCodes.Add);
193        codeGen.Emit1(OpCodes.Exit);
194        codeGen.GetCode(out code, out nParams);
195        var h1 = ExprHash.GetHash(code, nParams);
196
197        codeGen = new CodeGenerator();
198        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN);
199        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
200        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
201        codeGen.Emit1(OpCodes.Exit);
202        codeGen.GetCode(out code, out nParams);
203        var h2 = ExprHash.GetHash(code, nParams);
204
205        Assert.AreEqual(h1, h2);
206      }
207
208      {
209        var codeGen = new CodeGenerator();
210        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0);
211        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1);
212        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN);
213        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
214        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
215
216        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN);
217        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
218        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
219
220        codeGen.Emit1(OpCodes.Add);
221
222        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
223        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add);
224
225        codeGen.Emit1(OpCodes.Exit);
226        codeGen.GetCode(out code, out nParams);
227        var h1 = ExprHash.GetHash(code, nParams);
228
229        codeGen = new CodeGenerator();
230        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0);
231        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1);
232        codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN);
233        codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1);
234        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
235        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul);
236        codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add);
237        codeGen.Emit1(OpCodes.Exit);
238        codeGen.GetCode(out code, out nParams);
239        var h2 = ExprHash.GetHash(code, nParams);
240
241        Assert.AreEqual(h1, h2);
242      }
243    }
244    #endregion
245
246    #region number of solutions
247    // the algorithm should visits each solution only once
248    [TestMethod]
249    [TestCategory("Algorithms.DataAnalysis")]
250    [TestProperty("Time", "short")]
251    public void MctsSymbRegNumberOfSolutionsOneVariable() {
252      // this problem has only one variable
253      var provider = new NguyenInstanceProvider();
254      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 ")));
255      {
256        // possible solutions with max one variable reference:
257        // x
258        // log(x)
259        // exp(x)
260        // 1/x
261        TestMctsNumberOfSolutions(regProblem, 1, 4);
262      }
263      {
264        // possible solutions with max two variable references:
265        // TODO: equal terms should not be allowed (see ConstraintHandler)
266        // x
267        // log(x)
268        // exp(x)
269        // 1/x
270        //              -- 4
271        // x * x
272        // x * log(x)
273        // x * exp(x)
274        // x * 1/x
275        // x + x                                        ?
276        // x + log(x)
277        // x + exp(x)
278        // x + 1/x
279        //              -- 8
280        // log(x) * log(x)
281        // log(x) * exp(x)
282        // log(x) * 1/x
283        // log(x) + log(x)                              ?
284        // log(x) + exp(x)                              ?
285        // log(x) + 1/x
286        //              -- 6
287        // exp(x) * exp(x)
288        // exp(x) * 1/x
289        // exp(x) + exp(x)                              ?
290        // exp(x) + 1/x
291        //              -- 4
292        // 1/x * 1/x
293        // 1/x + 1/x                                    ?
294        //              -- 2
295        // log(x+x)                                     ?
296        // log(x*x)
297        // exp(x*x)
298        // 1/(x+x)                                      ?
299        // 1/(x*x)
300        //              -- 5
301
302
303        TestMctsNumberOfSolutions(regProblem, 2, 29);
304      }
305      {
306        // possible solutions with max three variable references:
307        // without log and inv
308        // x
309        // exp(x)
310        //              -- 2
311        // x * x
312        // x + x                                            ?
313        // x * exp(x)
314        // x + exp(x)
315        // exp(x) * exp(x)
316        // exp(x) + exp(x)                                  ?
317        // exp(x*x)
318        //              -- 7
319        // x * x * x
320        // x + x * x                                       
321        // x + x + x                                        ?
322        // x * x * exp(x)
323        // x + x * exp(x)                                   
324        // x + x + exp(x)                                   ?
325        // exp(x) + x*x
326        // exp(x) + x*exp(x)                               
327        // x + exp(x) * exp(x)                             
328        // x + exp(x) + exp(x)                              ?
329        // x * exp(x) * exp(x)
330        // x * exp(x*x)
331        // x + exp(x*x)
332        //              -- 13
333
334        // exp(x) * exp(x) * exp(x)
335        // exp(x) + exp(x) * exp(x)                         
336        // exp(x) + exp(x) + exp(x)                         ?
337        //              -- 3
338
339        // exp(x)   * exp(x*x)
340        // exp(x)   + exp(x*x)
341        //              -- 2
342        // exp(x*x*x)
343        //              -- 1
344        TestMctsNumberOfSolutions(regProblem, 3, 2 + 7 + 13 + 3 + 2 + 1, allowLog: false, allowInv: false);
345      }
346      {
347        // possible solutions with max 4 variable references:
348        // without exp, log and inv
349        // x       
350        // x*x
351        // x+x                                             ?
352        // x*x*x
353        // x+x*x
354        // x+x+x                                           ?
355        // x*x*x*x
356        // x+x*x*x
357        // x*x+x*x                                         ?
358        // x+x+x*x                                         ?
359        // x+x+x+x                                         ?
360
361        TestMctsNumberOfSolutions(regProblem, 4, 11, allowLog: false, allowInv: false, allowExp: false);
362      }
363      {
364        // possible solutions with max 5 variable references:
365        // without exp, log and inv
366        // x       
367        // xx
368        // x+x                                             ?
369        // xxx
370        // x+xx
371        // x+x+x                                           ?
372        // xxxx
373        // x+xxx
374        // xx+xx                                           ?
375        // x+x+xx                                          ?
376        // x+x+x+x                                         ?
377        // xxxxx
378        // x+xxxx
379        // xx+xxx
380        // x+x+xxx                                         ?
381        // x+xx+xx                                         ?
382        // x+x+x+xx                                        ?
383        // x+x+x+x+x                                       ?
384        TestMctsNumberOfSolutions(regProblem, 5, 18, allowLog: false, allowInv: false, allowExp: false);
385      }
386    }
387
388    [TestMethod]
389    [TestCategory("Algorithms.DataAnalysis")]
390    [TestProperty("Time", "short")]
391    public void MctsSymbRegNumberOfSolutionsTwoVariables() {
392      // this problem has only two input variables
393      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
394      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F9 ")));
395      {
396        // possible solutions with max one variable reference:
397        // x
398        // log(x)
399        // exp(x)
400        // 1/x
401        // y
402        // log(y)
403        // exp(y)
404        // 1/y
405        TestMctsNumberOfSolutions(regProblem, 1, 8);
406      }
407      {
408        // possible solutions with max one variable reference:
409        // without log and inv
410
411        // x
412        // exp(x)
413        // y
414        // exp(y)
415        TestMctsNumberOfSolutions(regProblem, 1, 4, allowLog: false, allowInv: false);
416      }
417      {
418        // possible solutions with max two variable references:
419        // without log and inv
420
421        // x
422        // y
423        // exp(x)
424        // exp(y)
425        //                  -- 4
426        // x (*|+) x
427        // x (*|+) exp(x)
428        // x (*|+) y
429        // x (*|+) exp(y)
430        //                  -- 8
431        // exp(x) (*|+) exp(x)
432        // exp(x) (*|+) exp(y)
433        //                  -- 4
434        // y (*|+) y
435        // y (*|+) exp(x)
436        // y (*|+) exp(y)
437        //                  -- 6
438        // exp(y) (*|+) exp(y)
439        //                  -- 2
440        //
441        // exp(x*x)
442        // exp(x*y)
443        // exp(y*y)
444        //                  -- 3
445
446        TestMctsNumberOfSolutions(regProblem, 2, 4 + 8 + 4 + 6 + 2 + 3, allowLog: false, allowInv: false);
447      }
448
449      {
450        // possible solutions with max two variable references:
451        // without exp and sum
452        // x
453        // y
454        // log(x)
455        // log(y)
456        // inv(x)
457        // inv(y)
458        //              -- 6
459        // x * x
460        // x * y
461        // x * log(x)
462        // x * log(y)
463        // x * inv(x)
464        // x * inv(y)
465        //              -- 6
466        // log(x) * log(x)
467        // log(x) * log(y)
468        // log(x) * inv(x)
469        // log(x) * inv(y)
470        //              -- 4
471        // inv(x) * inv(x)
472        // inv(x) * inv(y)
473        //              -- 2
474        // y * y
475        // y * log(x)
476        // y * log(y)
477        // y * inv(x)
478        // y * inv(y)
479        //              -- 5
480        // log(y) * log(y)
481        // log(y) * inv(x)
482        // log(y) * inv(y)
483        //              -- 3
484        // inv(y) * inv(y)
485        //              -- 1
486        // log(x*x)
487        // log(x*y)
488        // log(y*y)
489
490        // inv(x*x)
491        // inv(x*y)
492        // inv(y*y)
493        //             -- 6
494        // log(x+x)
495        // log(x+y)
496        // log(y+y)
497
498        // inv(x+x)
499        // inv(x+y)
500        // inv(y+y)
501        //             -- 6
502        TestMctsNumberOfSolutions(regProblem, 2, 6 + 6 + 4 + 2 + 5 + 3 + 1 + 6 + 6, allowExp: false, allowSum: false);
503      }
504    }
505    #endregion
506
507
508    #region test structure search (no constants)
509    [TestMethod]
510    [TestCategory("Algorithms.DataAnalysis")]
511    [TestProperty("Time", "short")]
512    public void MctsSymbReg_NoConstants_Nguyen1() {
513      // x³ + x² + x
514      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
515      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 ")));
516      TestMctsWithoutConstants(regProblem, nVarRefs: 10, allowExp: false, allowLog: false, allowInv: false);
517    }
518    [TestMethod]
519    [TestCategory("Algorithms.DataAnalysis")]
520    [TestProperty("Time", "short")]
521    public void MctsSymbReg_NoConstants_Nguyen2() {
522      // x^4 + x³ + x² + x
523      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
524      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 ")));
525      TestMctsWithoutConstants(regProblem, allowExp: false, allowLog: false, allowInv: false);
526    }
527    [TestMethod]
528    [TestCategory("Algorithms.DataAnalysis")]
529    [TestProperty("Time", "short")]
530    public void MctsSymbReg_NoConstants_Nguyen3() {
531      // x^5 + x^4 + x³ + x² + x
532      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
533      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F3 ")));
534      TestMctsWithoutConstants(regProblem, nVarRefs: 15, iterations: 1000000, allowExp: false, allowLog: false, allowInv: false);
535    }
536    [TestMethod]
537    [TestCategory("Algorithms.DataAnalysis")]
538    [TestProperty("Time", "short")]
539    public void MctsSymbReg_NoConstants_Nguyen4() {
540      // x^6 + x^5 + x^4 + x³ + x² + x
541      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
542      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F4 ")));
543      TestMctsWithoutConstants(regProblem, nVarRefs: 25, iterations: 1000000, allowExp: false, allowLog: false, allowInv: false);
544    }
545
546    [TestMethod]
547    [TestCategory("Algorithms.DataAnalysis")]
548    [TestProperty("Time", "short")]
549    public void MctsSymbReg_NoConstants_Nguyen7() {
550      // log(x + 1) + log(x² + 1)
551      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
552      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
553      TestMctsWithoutConstants(regProblem, nVarRefs: 10, iterations: 100000, allowExp: false, allowLog: true, allowInv: false);
554    }
555
556    [TestMethod]
557    [TestCategory("Algorithms.DataAnalysis")]
558    [TestProperty("Time", "short")]
559    public void MctsSymbReg_NoConstants_Poly10_Part1() {
560      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
561      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
562
563      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
564      //  Y' = X1*X2 + X3*X4 + X5*X6
565      // simplify problem by changing target
566      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
567      var ys = ds.GetDoubleValues("Y").ToArray();
568      var x1 = ds.GetDoubleValues("X1").ToArray();
569      var x2 = ds.GetDoubleValues("X2").ToArray();
570      var x3 = ds.GetDoubleValues("X3").ToArray();
571      var x4 = ds.GetDoubleValues("X4").ToArray();
572      var x5 = ds.GetDoubleValues("X5").ToArray();
573      var x6 = ds.GetDoubleValues("X6").ToArray();
574      var x7 = ds.GetDoubleValues("X7").ToArray();
575      var x8 = ds.GetDoubleValues("X8").ToArray();
576      var x9 = ds.GetDoubleValues("X9").ToArray();
577      var x10 = ds.GetDoubleValues("X10").ToArray();
578      for (int i = 0; i < ys.Length; i++) {
579        ys[i] -= x1[i] * x7[i] * x9[i];
580        ys[i] -= x3[i] * x6[i] * x10[i];
581      }
582      ds.ReplaceVariable("Y", ys.ToList());
583
584      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
585
586
587      TestMctsWithoutConstants(modifiedProblemData, nVarRefs: 15, iterations: 100000, allowExp: false, allowLog: false, allowInv: false);
588    }
589
590    [TestMethod]
591    [TestCategory("Algorithms.DataAnalysis")]
592    [TestProperty("Time", "short")]
593    public void MctsSymbReg_NoConstants_Poly10_Part2() {
594      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
595      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
596
597      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
598      //  Y' = X1*X7*X9 + X3*X6*X10
599      // simplify problem by changing target
600      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
601      var ys = ds.GetDoubleValues("Y").ToArray();
602      var x1 = ds.GetDoubleValues("X1").ToArray();
603      var x2 = ds.GetDoubleValues("X2").ToArray();
604      var x3 = ds.GetDoubleValues("X3").ToArray();
605      var x4 = ds.GetDoubleValues("X4").ToArray();
606      var x5 = ds.GetDoubleValues("X5").ToArray();
607      var x6 = ds.GetDoubleValues("X6").ToArray();
608      var x7 = ds.GetDoubleValues("X7").ToArray();
609      var x8 = ds.GetDoubleValues("X8").ToArray();
610      var x9 = ds.GetDoubleValues("X9").ToArray();
611      var x10 = ds.GetDoubleValues("X10").ToArray();
612      for (int i = 0; i < ys.Length; i++) {
613        ys[i] -= x1[i] * x2[i];
614        ys[i] -= x3[i] * x4[i];
615        ys[i] -= x5[i] * x6[i];
616      }
617      ds.ReplaceVariable("Y", ys.ToList());
618
619      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
620
621
622      TestMctsWithoutConstants(modifiedProblemData, nVarRefs: 15, iterations: 100000, allowExp: false, allowLog: false, allowInv: false);
623    }
624
625    [TestMethod]
626    [TestCategory("Algorithms.DataAnalysis")]
627    [TestProperty("Time", "short")]
628    public void MctsSymbReg_NoConstants_Poly10_Part3() {
629      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
630      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
631
632      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
633      //  Y' = X1*X2 + X1*X7*X9
634      // simplify problem by changing target
635      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
636      var ys = ds.GetDoubleValues("Y").ToArray();
637      var x1 = ds.GetDoubleValues("X1").ToArray();
638      var x2 = ds.GetDoubleValues("X2").ToArray();
639      var x3 = ds.GetDoubleValues("X3").ToArray();
640      var x4 = ds.GetDoubleValues("X4").ToArray();
641      var x5 = ds.GetDoubleValues("X5").ToArray();
642      var x6 = ds.GetDoubleValues("X6").ToArray();
643      var x7 = ds.GetDoubleValues("X7").ToArray();
644      var x8 = ds.GetDoubleValues("X8").ToArray();
645      var x9 = ds.GetDoubleValues("X9").ToArray();
646      var x10 = ds.GetDoubleValues("X10").ToArray();
647      for (int i = 0; i < ys.Length; i++) {
648        ys[i] -= x3[i] * x4[i];
649        ys[i] -= x5[i] * x6[i];
650        ys[i] -= x3[i] * x6[i] * x10[i];
651      }
652      ds.ReplaceVariable("Y", ys.ToList());
653
654      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
655
656
657      TestMctsWithoutConstants(modifiedProblemData, nVarRefs: 15, iterations: 100000, allowExp: false, allowLog: false, allowInv: false);
658    }
659
660    [TestMethod]
661    [TestCategory("Algorithms.DataAnalysis")]
662    [TestProperty("Time", "short")]
663    public void MctsSymbReg_NoConstants_Poly10_Part4() {
664      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
665      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
666
667      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
668      //  Y' = X3*X4 + X5*X6 + X3*X6*X10
669      // simplify problem by changing target
670      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
671      var ys = ds.GetDoubleValues("Y").ToArray();
672      var x1 = ds.GetDoubleValues("X1").ToArray();
673      var x2 = ds.GetDoubleValues("X2").ToArray();
674      var x3 = ds.GetDoubleValues("X3").ToArray();
675      var x4 = ds.GetDoubleValues("X4").ToArray();
676      var x5 = ds.GetDoubleValues("X5").ToArray();
677      var x6 = ds.GetDoubleValues("X6").ToArray();
678      var x7 = ds.GetDoubleValues("X7").ToArray();
679      var x8 = ds.GetDoubleValues("X8").ToArray();
680      var x9 = ds.GetDoubleValues("X9").ToArray();
681      var x10 = ds.GetDoubleValues("X10").ToArray();
682      for (int i = 0; i < ys.Length; i++) {
683        ys[i] -= x1[i] * x2[i];
684        ys[i] -= x1[i] * x7[i] * x9[i];
685      }
686      ds.ReplaceVariable("Y", ys.ToList());
687      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
688
689
690      TestMctsWithoutConstants(modifiedProblemData, nVarRefs: 15, iterations: 100000, allowExp: false, allowLog: false, allowInv: false);
691    }
692
693    [TestMethod]
694    [TestCategory("Algorithms.DataAnalysis")]
695    [TestProperty("Time", "short")]
696    public void MctsSymbReg_NoConstants_Poly10_Part5() {
697      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
698      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
699
700      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
701      //  Y' = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9
702      // simplify problem by changing target
703      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
704      var ys = ds.GetDoubleValues("Y").ToArray();
705      var x1 = ds.GetDoubleValues("X1").ToArray();
706      var x2 = ds.GetDoubleValues("X2").ToArray();
707      var x3 = ds.GetDoubleValues("X3").ToArray();
708      var x4 = ds.GetDoubleValues("X4").ToArray();
709      var x5 = ds.GetDoubleValues("X5").ToArray();
710      var x6 = ds.GetDoubleValues("X6").ToArray();
711      var x7 = ds.GetDoubleValues("X7").ToArray();
712      var x8 = ds.GetDoubleValues("X8").ToArray();
713      var x9 = ds.GetDoubleValues("X9").ToArray();
714      var x10 = ds.GetDoubleValues("X10").ToArray();
715      for (int i = 0; i < ys.Length; i++) {
716        ys[i] -= x3[i] * x6[i] * x10[i];
717      }
718      ds.ReplaceVariable("Y", ys.ToList());
719      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
720
721
722      TestMctsWithoutConstants(modifiedProblemData, nVarRefs: 15, iterations: 100000, allowExp: false, allowLog: false, allowInv: false);
723    }
724
725    [TestMethod]
726    [TestCategory("Algorithms.DataAnalysis")]
727    [TestProperty("Time", "short")]
728    public void MctsSymbReg_NoConstants_Poly10_Part6() {
729      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
730      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
731
732      //  Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
733      //  Y' = X1*X2 + X3*X4 + X5*X6 + X3*X6*X10
734      // simplify problem by changing target
735      var ds = ((Dataset)regProblem.Dataset).ToModifiable();
736      var ys = ds.GetDoubleValues("Y").ToArray();
737      var x1 = ds.GetDoubleValues("X1").ToArray();
738      var x2 = ds.GetDoubleValues("X2").ToArray();
739      var x3 = ds.GetDoubleValues("X3").ToArray();
740      var x4 = ds.GetDoubleValues("X4").ToArray();
741      var x5 = ds.GetDoubleValues("X5").ToArray();
742      var x6 = ds.GetDoubleValues("X6").ToArray();
743      var x7 = ds.GetDoubleValues("X7").ToArray();
744      var x8 = ds.GetDoubleValues("X8").ToArray();
745      var x9 = ds.GetDoubleValues("X9").ToArray();
746      var x10 = ds.GetDoubleValues("X10").ToArray();
747      for (int i = 0; i < ys.Length; i++) {
748        ys[i] -= x1[i] * x7[i] * x9[i];
749      }
750      ds.ReplaceVariable("Y", ys.ToList());
751      var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
752
753
754      TestMctsWithoutConstants(modifiedProblemData, nVarRefs: 9, iterations: 100000, allowExp: false, allowLog: false, allowInv: false);
755    }
756
757
758    [TestMethod]
759    [TestCategory("Algorithms.DataAnalysis")]
760    [TestProperty("Time", "short")]
761    public void MctsSymbReg_NoConstants_Poly10() {
762      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
763      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
764      TestMctsWithoutConstants(regProblem, nVarRefs: 15, iterations: 200000, allowExp: false, allowLog: false, allowInv: false);
765    }
766
767    [TestMethod]
768    [TestCategory("Algorithms.DataAnalysis")]
769    [TestProperty("Time", "short")]
770    public void MctsSymbReg_NoConstants_TwoVars() {
771
772      // y = x1 + x2 + x1*x2 + x1*x2*x2 + x1*x1*x2
773      var rand = new FastRandom(1234);
774      var x1 = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
775      var x2 = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
776      var ys = x1.Zip(x2, (x1i, x2i) => x1i + x2i + x1i * x2i + x1i * x2i * x2i + x1i * x1i * x2i).ToList();
777
778      var ds = new Dataset(new string[] { "a", "b", "y" }, new[] { x1, x2, ys });
779
780      var problemData = new RegressionProblemData(ds, new string[] { "a", "b" }, "y");
781
782
783      TestMctsWithoutConstants(problemData, nVarRefs: 10, iterations: 10000, allowExp: false, allowLog: false, allowInv: false);
784    }
785
786    [TestMethod]
787    [TestCategory("Algorithms.DataAnalysis")]
788    [TestProperty("Time", "short")]
789    public void MctsSymbReg_NoConstants_Misleading() {
790
791      // y = a + baaaaa (the effect of the second term should be very small)
792      // the alg will quickly find that a has big effect and will search below a
793      // since we prevent a + a... the algorithm must find the correct expression via a + b...
794      // however b has a small effect so the branch might not be identified as relevant
795
796      var rand = new FastRandom(1234);
797      var @as = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
798      var bs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
799      var cs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() *1.0e-3).ToList();
800      var ds = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() ).ToList();
801      var es = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() ).ToList();
802      var ys = new double[@as.Count];
803      for(int i=0;i<ys.Length;i++)
804        ys[i] = @as[i] + bs[i] + @as[i]*bs[i]*cs[i];
805
806      var dataset = new Dataset(new string[] { "a", "b", "c", "d", "e", "y" }, new[] { @as, bs, cs, ds, es, ys.ToList() });
807
808      var problemData = new RegressionProblemData(dataset, new string[] { "a", "b","c","d","e" }, "y");
809
810
811      TestMctsWithoutConstants(problemData, nVarRefs: 10, iterations: 10000, allowExp: false, allowLog: false, allowInv: false);
812    }
813    #endregion
814
815    #region restricted structure but including numeric constants
816
817    [TestMethod]
818    [TestCategory("Algorithms.DataAnalysis")]
819    [TestProperty("Time", "short")]
820    public void MctsSymbRegKeijzer7() {
821      // ln(x)
822      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(seed: 1234);
823      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 7 f(")));
824      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
825      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
826      TestMcts(regProblem, allowExp: false, allowLog: true, allowInv: false);
827    }
828
829    /*
830    // [TestMethod]
831    [TestCategory("Algorithms.DataAnalysis")]
832    [TestProperty("Time", "short")]
833    public void MctsSymbRegBenchmarkNguyen5() {
834      // sin(x²)cos(x) - 1
835      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
836      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F5 ")));
837      TestMcts(regProblem);
838    }
839    // [TestMethod]
840    [TestCategory("Algorithms.DataAnalysis")]
841    [TestProperty("Time", "short")]
842    public void MctsSymbRegBenchmarkNguyen6() {
843      // sin(x) + sin(x + x²)
844      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
845      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F6 ")));
846      TestMcts(regProblem);
847    }
848    */
849    [TestMethod]
850    [TestCategory("Algorithms.DataAnalysis")]
851    [TestProperty("Time", "short")]
852    public void MctsSymbRegBenchmarkNguyen7() {
853      //  log(x + 1) + log(x² + 1)
854      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
855      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
856      TestMcts(regProblem, maxVariableReferences:5, allowExp: false, allowLog: true, allowInv: false);
857    }
858    [TestMethod]
859    [TestCategory("Algorithms.DataAnalysis")]
860    [TestProperty("Time", "short")]
861    public void MctsSymbRegBenchmarkNguyen8() {
862      // Sqrt(x)
863      // = x ^ 0.5
864      // = exp(0.5 * log(x))
865      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
866      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F8 ")));
867      TestMcts(regProblem, maxVariableReferences: 5, allowExp: true, allowLog: true, allowInv: false);
868    }
869    /*
870    // [TestMethod]
871    [TestCategory("Algorithms.DataAnalysis")]
872    [TestProperty("Time", "short")]
873    public void MctsSymbRegBenchmarkNguyen9() {
874      //  sin(x) + sin(y²)
875      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
876      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F9 ")));
877      TestMcts(regProblem);
878    }
879    // [TestMethod]
880    [TestCategory("Algorithms.DataAnalysis")]
881    [TestProperty("Time", "short")]
882    public void MctsSymbRegBenchmarkNguyen10() {
883      // 2sin(x)cos(y)
884      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
885      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F10 ")));
886      TestMcts(regProblem);
887    }
888    */
889    [TestMethod]
890    [TestCategory("Algorithms.DataAnalysis")]
891    [TestProperty("Time", "short")]
892    public void MctsSymbRegBenchmarkNguyen11() {
893      // x ^ y  , x > 0, y > 0   
894      // = exp(y * log(x))
895      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
896      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F11 ")));
897      TestMcts(regProblem, maxVariableReferences: 5, allowExp: true, allowLog: true, allowInv: false);
898    }
899    [TestMethod]
900    [TestCategory("Algorithms.DataAnalysis")]
901    [TestProperty("Time", "short")]
902    public void MctsSymbRegBenchmarkNguyen12() {
903      // x^4 - x³ + y²/2 - y
904      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
905      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F12 ")));
906      TestMcts(regProblem, maxVariableReferences: 20, allowExp: false, allowLog: false, allowInv: false);
907    }
908
909    #endregion
910
911    #region keijzer
912    [TestMethod]
913    [TestCategory("Algorithms.DataAnalysis")]
914    [TestProperty("Time", "long")]
915    public void MctsSymbRegBenchmarkKeijzer5() {
916      // (30 * x * z) / ((x - 10)  * y²)
917      // = 30 x z / (xy² - y²)
918      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(seed: 1234);
919      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 5 f(")));
920      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
921      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
922      TestMcts(regProblem, maxVariableReferences: 20, allowExp: false, allowLog: false, allowInv: true);
923    }
924
925    [TestMethod]
926    [TestCategory("Algorithms.DataAnalysis")]
927    [TestProperty("Time", "short")]
928    public void MctsSymbRegBenchmarkKeijzer6() {
929      // Keijzer 6 f(x) = Sum(1 / i) From 1 to X  , x \in [0..120]
930      // we can only approximate this
931      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(seed: 1234);
932      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 6 f(")));
933      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
934      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
935      TestMcts(regProblem, maxVariableReferences: 20, allowExp: false, allowLog: false, allowInv: true);
936    }
937
938
939    [TestMethod]
940    [TestCategory("Algorithms.DataAnalysis")]
941    [TestProperty("Time", "short")]
942    public void MctsSymbRegBenchmarkKeijzer8() {
943      // sqrt(x)
944      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(seed: 1234);
945      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 8 f(")));
946      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
947      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
948      TestMcts(regProblem, maxVariableReferences: 5, allowExp: true, allowLog: true, allowInv: false);
949    }
950
951    [TestMethod]
952    [TestCategory("Algorithms.DataAnalysis")]
953    [TestProperty("Time", "short")]
954    public void MctsSymbRegBenchmarkKeijzer9() {
955      // arcsinh(x)  i.e. ln(x + sqrt(x² + 1))
956      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(seed: 1234);
957      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 9 f(")));
958      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
959      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
960      TestMcts(regProblem, maxVariableReferences: 5, allowExp: true, allowLog: true, allowInv: false);
961    }
962
963    /*
964    [TestMethod]
965    [TestCategory("Algorithms.DataAnalysis")]
966    [TestProperty("Time", "short")]
967    public void MctsSymbRegBenchmarkKeijzer11() {
968      // xy + sin( (x-1) (y-1) )
969      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
970      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 11 f(")));
971      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
972      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
973      TestMcts(regProblem, successThreshold: 0.99); // cannot solve this yet
974    }
975     */
976    [TestMethod]
977    [TestCategory("Algorithms.DataAnalysis")]
978    [TestProperty("Time", "short")]
979    public void MctsSymbRegBenchmarkKeijzer12() {
980      // x^4 - x³ + y² / 2 - y,  same as Nguyen 12             
981      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(seed: 1234);
982      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 12 f(")));
983      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
984      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
985      TestMcts(regProblem, maxVariableReferences: 15, allowExp: false, allowLog: false, allowInv: false);
986    }
987    [TestMethod]
988    [TestCategory("Algorithms.DataAnalysis")]
989    [TestProperty("Time", "short")]
990    public void MctsSymbRegBenchmarkKeijzer14() {
991      // 8 / (2 + x² + y²)
992      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(seed: 1234);
993      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 14 f(")));
994      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
995      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
996      TestMcts(regProblem, maxVariableReferences: 10, allowExp: false, allowLog: false, allowInv: true);
997    }
998    [TestMethod]
999    [TestCategory("Algorithms.DataAnalysis")]
1000    [TestProperty("Time", "short")]
1001    public void MctsSymbRegBenchmarkKeijzer15() {
1002      // x³ / 5 + y³ / 2 - y - x
1003      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(seed: 1234);
1004      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 15 f(")));
1005      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
1006      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
1007      TestMcts(regProblem, maxVariableReferences: 10, allowExp: false, allowLog: false, allowInv: false);
1008    }
1009    #endregion
1010
1011    private void TestMcts(IRegressionProblemData problemData,
1012      int iterations = 20000,
1013      double successThreshold = 0.99999,
1014      int maxVariableReferences = 5,
1015      bool allowExp = true,
1016      bool allowLog = true,
1017      bool allowInv = true,
1018      bool allowSum = true
1019      ) {
1020      var mctsSymbReg = new MctsSymbolicRegressionAlgorithm();
1021      var regProblem = new RegressionProblem();
1022      regProblem.ProblemDataParameter.Value = problemData;
1023      #region Algorithm Configuration
1024      mctsSymbReg.Problem = regProblem;
1025      mctsSymbReg.Iterations = iterations;
1026      mctsSymbReg.MaxVariableReferences = maxVariableReferences;
1027
1028      mctsSymbReg.SetSeedRandomly = false;
1029      mctsSymbReg.Seed = 1234;
1030      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("exp")), allowExp);
1031      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("log")), allowLog);
1032      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("1 /")), allowInv);
1033      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("t1(x) + t2(x) + ... ")), allowSum);
1034
1035      mctsSymbReg.ScaleVariables = true;
1036      mctsSymbReg.ConstantOptimizationIterations = 0;
1037
1038      #endregion
1039      RunAlgorithm(mctsSymbReg);
1040
1041      Console.WriteLine(mctsSymbReg.ExecutionTime);
1042      var eps = 1.0 - successThreshold;
1043      Assert.AreEqual(1.0, ((DoubleValue)mctsSymbReg.Results["Best solution quality (train)"].Value).Value, eps);
1044      Assert.AreEqual(1.0, ((DoubleValue)mctsSymbReg.Results["Best solution quality (test)"].Value).Value, eps);
1045    }
1046
1047
1048    private void TestMctsWithoutConstants(IRegressionProblemData problemData,
1049      int nVarRefs = 10,
1050      int iterations = 200000, double successThreshold = 0.99999,
1051      bool allowExp = true,
1052      bool allowLog = true,
1053      bool allowInv = true,
1054      bool allowSum = true
1055      ) {
1056      var mctsSymbReg = new MctsSymbolicRegressionAlgorithm();
1057      var regProblem = new RegressionProblem();
1058      regProblem.ProblemDataParameter.Value = problemData;
1059      #region Algorithm Configuration
1060      mctsSymbReg.Problem = regProblem;
1061      mctsSymbReg.Iterations = iterations;
1062      mctsSymbReg.MaxVariableReferences = nVarRefs;
1063      mctsSymbReg.SetSeedRandomly = false;
1064      mctsSymbReg.Seed = 1234;
1065      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("exp")), allowExp);
1066      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("log")), allowLog);
1067      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("1 /")), allowInv);
1068      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("t1(x) + t2(x) + ... ")), allowSum);
1069
1070      // no constants
1071      mctsSymbReg.ScaleVariables = false;
1072      mctsSymbReg.ConstantOptimizationIterations = -1;
1073
1074      // random policy
1075      // var epsPolicy = new EpsilonGreedy();
1076      // epsPolicy.Eps = 1.0;
1077      // mctsSymbReg.Policy = epsPolicy;
1078
1079      // UCB tuned
1080      // var ucbTuned = new UcbTuned();
1081      // ucbTuned.C = 1.5;
1082      // mctsSymbReg.Policy = ucbTuned;
1083
1084
1085      #endregion
1086      RunAlgorithm(mctsSymbReg);
1087
1088      Console.WriteLine(mctsSymbReg.ExecutionTime);
1089      var eps = 1.0 - successThreshold;
1090      Assert.AreEqual(1.0, ((DoubleValue)mctsSymbReg.Results["Best solution quality (train)"].Value).Value, eps);
1091      Assert.AreEqual(1.0, ((DoubleValue)mctsSymbReg.Results["Best solution quality (test)"].Value).Value, eps);
1092    }
1093
1094    private void TestMctsNumberOfSolutions(IRegressionProblemData problemData, int maxNumberOfVariables, int expectedNumberOfSolutions,
1095      bool allowProd = true,
1096      bool allowExp = true,
1097      bool allowLog = true,
1098      bool allowInv = true,
1099      bool allowSum = true
1100  ) {
1101      var mctsSymbReg = new MctsSymbolicRegressionAlgorithm();
1102      var regProblem = new RegressionProblem();
1103      regProblem.ProblemDataParameter.Value = problemData;
1104      #region Algorithm Configuration
1105
1106      mctsSymbReg.SetSeedRandomly = false;
1107      mctsSymbReg.Seed = 1234;
1108      mctsSymbReg.Problem = regProblem;
1109      mctsSymbReg.Iterations = int.MaxValue; // stopping when all solutions have been enumerated
1110      mctsSymbReg.MaxVariableReferences = maxNumberOfVariables;
1111      var ucbPolicy = new Ucb();
1112      ucbPolicy.C = 1000; // essentially breadth first search
1113      mctsSymbReg.Policy = ucbPolicy;
1114      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.StartsWith("x * y * ...")), allowProd);
1115      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("exp(c * x * y ...)")), allowExp);
1116      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("log(c + c1 x + c2 x + ...)")), allowLog);
1117      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("1 / (1 + c1 x + c2 x + ...)")), allowInv);
1118      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("t1(x) + t2(x) + ... ")), allowSum);
1119      #endregion
1120      RunAlgorithm(mctsSymbReg);
1121
1122      Console.WriteLine(mctsSymbReg.ExecutionTime);
1123      Assert.AreEqual(expectedNumberOfSolutions, ((IntValue)mctsSymbReg.Results["Iterations"].Value).Value);
1124    }
1125
1126
1127    // same as in SamplesUtil
1128    private void RunAlgorithm(IAlgorithm a) {
1129      var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
1130      Exception ex = null;
1131      a.Stopped += (src, e) => { trigger.Set(); };
1132      a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
1133      a.Prepare();
1134      a.Start();
1135      trigger.WaitOne();
1136
1137      Assert.AreEqual(ex, null);
1138    }
1139
1140  }
1141}
Note: See TracBrowser for help on using the repository browser.