Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs @ 13661

Last change on this file since 13661 was 13661, checked in by gkronber, 8 years ago

#2581: updated unit test to compile with r13658

File size: 24.8 KB
Line 
1using System;
2using System.Diagnostics.Contracts;
3using System.Linq;
4using System.Threading;
5using HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression;
6using HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression.Policies;
7using HeuristicLab.Data;
8using HeuristicLab.Optimization;
9using HeuristicLab.Problems.DataAnalysis;
10using HeuristicLab.Tests;
11using Microsoft.VisualStudio.TestTools.UnitTesting;
12
13namespace HeuristicLab.Algorithms.DataAnalysis {
14  [TestClass()]
15  public class MctsSymbolicRegressionTest {
16    #region number of solutions
17    // the algorithm should visits each solution only once
18    [TestMethod]
19    [TestCategory("Algorithms.DataAnalysis")]
20    [TestProperty("Time", "short")]
21    public void MctsSymbRegNumberOfSolutionsOneVariable() {
22      // this problem has only one variable
23      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
24      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 ")));
25      {
26        // possible solutions with max one variable reference:
27        // x
28        // log(x)
29        // exp(x)
30        // 1/x
31        TestMctsNumberOfSolutions(regProblem, 1, 4);
32      }
33      {
34        // possible solutions with max two variable references:
35        // TODO: equal terms should not be allowed (see ConstraintHandler)
36        // x
37        // log(x)
38        // exp(x)
39        // 1/x
40        //              -- 4
41        // x * x
42        // x * log(x)
43        // x * exp(x)
44        // x * 1/x
45        // x + x                                        ?
46        // x + log(x)
47        // x + exp(x)
48        // x + 1/x
49        //              -- 8
50        // log(x) * log(x)
51        // log(x) * exp(x)
52        // log(x) * 1/x
53        // log(x) + log(x)                              ?
54        // log(x) + exp(x)                              ?
55        // log(x) + 1/x
56        //              -- 6
57        // exp(x) * exp(x)
58        // exp(x) * 1/x
59        // exp(x) + exp(x)                              ?
60        // exp(x) + 1/x
61        //              -- 4
62        // 1/x * 1/x
63        // 1/x + 1/x                                    ?
64        //              -- 2
65        // log(x+x)                                     ?
66        // log(x*x)
67        // exp(x*x)
68        // 1/(x+x)                                      ?
69        // 1/(x*x)
70        //              -- 5
71
72
73        TestMctsNumberOfSolutions(regProblem, 2, 29);
74      }
75      {
76        // possible solutions with max three variable references:
77        // without log and inv
78        // x
79        // exp(x)
80        //              -- 2
81        // x * x
82        // x + x                                            ?
83        // x * exp(x)
84        // x + exp(x)
85        // exp(x) * exp(x)
86        // exp(x) + exp(x)                                  ?
87        // exp(x*x)
88        //              -- 7
89        // x * x * x
90        // x + x * x                                       
91        // x + x + x                                        ?
92        // x * x * exp(x)
93        // x + x * exp(x)                                   
94        // x + x + exp(x)                                   ?
95        // exp(x) + x*x
96        // exp(x) + x*exp(x)                               
97        // x + exp(x) * exp(x)                             
98        // x + exp(x) + exp(x)                              ?
99        // x * exp(x) * exp(x)
100        // x * exp(x*x)
101        // x + exp(x*x)
102        //              -- 13
103
104        // exp(x) * exp(x) * exp(x)
105        // exp(x) + exp(x) * exp(x)                         
106        // exp(x) + exp(x) + exp(x)                         ?
107        //              -- 3
108
109        // exp(x)   * exp(x*x)
110        // exp(x)   + exp(x*x)
111        //              -- 2
112        // exp(x*x*x)
113        //              -- 1
114        TestMctsNumberOfSolutions(regProblem, 3, 2 + 7 + 13 + 3 + 2 + 1, allowLog: false, allowInv: false);
115      }
116      {
117        // possible solutions with max 4 variable references:
118        // without exp, log and inv
119        // x       
120        // x*x
121        // x+x                                             ?
122        // x*x*x
123        // x+x*x
124        // x+x+x                                           ?
125        // x*x*x*x
126        // x+x*x*x
127        // x*x+x*x                                         ?
128        // x+x+x*x                                         ?
129        // x+x+x+x                                         ?
130
131        TestMctsNumberOfSolutions(regProblem, 4, 11, allowLog: false, allowInv: false, allowExp: false);
132      }
133      {
134        // possible solutions with max 5 variable references:
135        // without exp, log and inv
136        // x       
137        // xx
138        // x+x                                             ?
139        // xxx
140        // x+xx
141        // x+x+x                                           ?
142        // xxxx
143        // x+xxx
144        // xx+xx                                           ?
145        // x+x+xx                                          ?
146        // x+x+x+x                                         ?
147        // xxxxx
148        // x+xxxx
149        // xx+xxx
150        // x+x+xxx                                         ?
151        // x+xx+xx                                         ?
152        // x+x+x+xx                                        ?
153        // x+x+x+x+x                                       ?
154        TestMctsNumberOfSolutions(regProblem, 5, 18, allowLog: false, allowInv: false, allowExp: false);
155      }
156    }
157
158    [TestMethod]
159    [TestCategory("Algorithms.DataAnalysis")]
160    [TestProperty("Time", "short")]
161    public void MctsSymbRegNumberOfSolutionsTwoVariables() {
162      // this problem has only two input variables
163      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
164      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F9 ")));
165      {
166        // possible solutions with max one variable reference:
167        // x
168        // log(x)
169        // exp(x)
170        // 1/x
171        // y
172        // log(y)
173        // exp(y)
174        // 1/y
175        TestMctsNumberOfSolutions(regProblem, 1, 8);
176      }
177      {
178        // possible solutions with max one variable reference:
179        // without log and inv
180
181        // x
182        // exp(x)
183        // y
184        // exp(y)
185        TestMctsNumberOfSolutions(regProblem, 1, 4, allowLog: false, allowInv: false);
186      }
187      {
188        // possible solutions with max two variable references:
189        // without log and inv
190
191        // x
192        // y
193        // exp(x)
194        // exp(y)
195        //                  -- 4
196        // x (*|+) x
197        // x (*|+) exp(x)
198        // x (*|+) y
199        // x (*|+) exp(y)
200        //                  -- 8
201        // exp(x) (*|+) exp(x)
202        // exp(x) (*|+) exp(y)
203        //                  -- 4
204        // y (*|+) y
205        // y (*|+) exp(x)
206        // y (*|+) exp(y)
207        //                  -- 6
208        // exp(y) (*|+) exp(y)
209        //                  -- 2
210        //
211        // exp(x*x)
212        // exp(x*y)
213        // exp(y*y)
214        //                  -- 3
215
216        TestMctsNumberOfSolutions(regProblem, 2, 4 + 8 + 4 + 6 + 2 + 3, allowLog: false, allowInv: false);
217      }
218
219      {
220        // possible solutions with max two variable references:
221        // without exp and sum
222        // x
223        // y
224        // log(x)
225        // log(y)
226        // inv(x)
227        // inv(y)
228        //              -- 6
229        // x * x
230        // x * y
231        // x * log(x)
232        // x * log(y)
233        // x * inv(x)
234        // x * inv(y)
235        //              -- 6
236        // log(x) * log(x)
237        // log(x) * log(y)
238        // log(x) * inv(x)
239        // log(x) * inv(y)
240        //              -- 4
241        // inv(x) * inv(x)
242        // inv(x) * inv(y)
243        //              -- 2
244        // y * y
245        // y * log(x)
246        // y * log(y)
247        // y * inv(x)
248        // y * inv(y)
249        //              -- 5
250        // log(y) * log(y)
251        // log(y) * inv(x)
252        // log(y) * inv(y)
253        //              -- 3
254        // inv(y) * inv(y)
255        //              -- 1
256        // log(x*x)
257        // log(x*y)
258        // log(y*y)
259
260        // inv(x*x)
261        // inv(x*y)
262        // inv(y*y)
263        //             -- 6
264        // log(x+x)
265        // log(x+y)
266        // log(y+y)
267
268        // inv(x+x)
269        // inv(x+y)
270        // inv(y+y)
271        //             -- 6
272        TestMctsNumberOfSolutions(regProblem, 2, 6 + 6 + 4 + 2 + 5 + 3 + 1 + 6 + 6, allowExp: false, allowSum: false);
273      }
274    }
275    #endregion
276
277
278    #region Nguyen
279    [TestMethod]
280    [TestCategory("Algorithms.DataAnalysis")]
281    [TestProperty("Time", "short")]
282    public void MctsSymbRegBenchmarkNguyen1() {
283      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
284      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 ")));
285      TestMcts(regProblem);
286    }
287    [TestMethod]
288    [TestCategory("Algorithms.DataAnalysis")]
289    [TestProperty("Time", "short")]
290    public void MctsSymbRegBenchmarkNguyen2() {
291      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
292      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 ")));
293      TestMcts(regProblem);
294    }
295    [TestMethod]
296    [TestCategory("Algorithms.DataAnalysis")]
297    [TestProperty("Time", "short")]
298    public void MctsSymbRegBenchmarkNguyen3() {
299      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
300      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F3 ")));
301      TestMcts(regProblem, successThreshold: 0.99);
302    }
303    [TestMethod]
304    [TestCategory("Algorithms.DataAnalysis")]
305    [TestProperty("Time", "short")]
306    public void MctsSymbRegBenchmarkNguyen4() {
307      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
308      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F4 ")));
309      TestMcts(regProblem);
310    }
311    [TestMethod]
312    [TestCategory("Algorithms.DataAnalysis")]
313    [TestProperty("Time", "short")]
314    public void MctsSymbRegBenchmarkNguyen5() {
315      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
316      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F5 ")));
317      TestMcts(regProblem);
318    }
319    [TestMethod]
320    [TestCategory("Algorithms.DataAnalysis")]
321    [TestProperty("Time", "short")]
322    public void MctsSymbRegBenchmarkNguyen6() {
323      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
324      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F6 ")));
325      TestMcts(regProblem);
326    }
327    [TestMethod]
328    [TestCategory("Algorithms.DataAnalysis")]
329    [TestProperty("Time", "short")]
330    public void MctsSymbRegBenchmarkNguyen7() {
331      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
332      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
333      TestMcts(regProblem);
334    }
335    [TestMethod]
336    [TestCategory("Algorithms.DataAnalysis")]
337    [TestProperty("Time", "short")]
338    public void MctsSymbRegBenchmarkNguyen8() {
339      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
340      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F8 ")));
341      TestMcts(regProblem, successThreshold: 0.99);
342    }
343    [TestMethod]
344    [TestCategory("Algorithms.DataAnalysis")]
345    [TestProperty("Time", "short")]
346    public void MctsSymbRegBenchmarkNguyen9() {
347      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
348      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F9 ")));
349      TestMcts(regProblem, iterations: 10000);
350    }
351    [TestMethod]
352    [TestCategory("Algorithms.DataAnalysis")]
353    [TestProperty("Time", "short")]
354    public void MctsSymbRegBenchmarkNguyen10() {
355      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
356      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F10 ")));
357      TestMcts(regProblem);
358    }
359    [TestMethod]
360    [TestCategory("Algorithms.DataAnalysis")]
361    [TestProperty("Time", "short")]
362    public void MctsSymbRegBenchmarkNguyen11() {
363      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
364      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F11 ")));
365      TestMcts(regProblem, 10000, 0.95); // cannot solve exactly in 10000 iterations
366    }
367    [TestMethod]
368    [TestCategory("Algorithms.DataAnalysis")]
369    [TestProperty("Time", "short")]
370    public void MctsSymbRegBenchmarkNguyen12() {
371      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
372      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F12 ")));
373      TestMcts(regProblem, iterations: 10000, successThreshold: 0.99, allowLog: false, allowExp: false, allowInv: false);
374    }
375
376    #endregion
377
378    #region keijzer
379    [TestMethod]
380    [TestCategory("Algorithms.DataAnalysis")]
381    [TestProperty("Time", "long")]
382    public void MctsSymbRegBenchmarkKeijzer5() {
383      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
384      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 5 f(")));
385      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
386      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
387      TestMcts(regProblem, iterations: 10000, allowExp: false, allowLog: false, allowSum: false, successThreshold: 0.99);
388    }
389
390    [TestMethod]
391    [TestCategory("Algorithms.DataAnalysis")]
392    [TestProperty("Time", "short")]
393    public void MctsSymbRegBenchmarkKeijzer6() {
394      // Keijzer 6 f(x) = Sum(1 / i) From 1 to X
395      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
396      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 6 f(")));
397      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
398      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
399      TestMcts(regProblem, allowLog: false, allowExp: false, successThreshold: 0.995); // cannot solve
400    }
401
402    [TestMethod]
403    [TestCategory("Algorithms.DataAnalysis")]
404    [TestProperty("Time", "short")]
405    public void MctsSymbRegBenchmarkKeijzer7() {
406      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
407      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 7 f(")));
408      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
409      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
410      TestMcts(regProblem);
411    }
412
413    [TestMethod]
414    [TestCategory("Algorithms.DataAnalysis")]
415    [TestProperty("Time", "short")]
416    public void MctsSymbRegBenchmarkKeijzer8() {
417      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
418      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 8 f(")));
419      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
420      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
421      TestMcts(regProblem);
422    }
423
424    [TestMethod]
425    [TestCategory("Algorithms.DataAnalysis")]
426    [TestProperty("Time", "short")]
427    public void MctsSymbRegBenchmarkKeijzer9() {
428      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
429      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 9 f(")));
430      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
431      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
432      TestMcts(regProblem);
433    }
434
435    /*
436     * cannot solve this yet x^y
437    [TestMethod]
438    [TestCategory("Algorithms.DataAnalysis")]
439    [TestProperty("Time", "short")]
440    public void MctsSymbRegBenchmarkKeijzer10() {
441      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
442      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 10 f(")));
443      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
444      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
445      TestMcts(regProblem, iterations: 10000, successThreshold: 0.99);
446    }
447     */
448
449    /* cannot solve this yet
450     * xy + sin( (x-1) (y-1) )
451    [TestMethod]
452    [TestCategory("Algorithms.DataAnalysis")]
453    [TestProperty("Time", "short")]
454    public void MctsSymbRegBenchmarkKeijzer11() {
455      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
456      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 11 f(")));
457      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
458      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
459      TestMcts(regProblem, successThreshold: 0.99); // cannot solve this yet
460    }
461     */
462    [TestMethod]
463    [TestCategory("Algorithms.DataAnalysis")]
464    [TestProperty("Time", "short")]
465    public void MctsSymbRegBenchmarkKeijzer12() {
466      // sames a Nguyen 12
467      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
468      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 12 f(")));
469      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
470      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
471      TestMcts(regProblem, iterations: 10000, allowLog: false, allowExp: false, allowInv: false, successThreshold: 0.99); // cannot solve exactly in 10000 iterations
472    }
473    [TestMethod]
474    [TestCategory("Algorithms.DataAnalysis")]
475    [TestProperty("Time", "short")]
476    public void MctsSymbRegBenchmarkKeijzer14() {
477      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
478      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 14 f(")));
479      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
480      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
481      TestMcts(regProblem);
482    }
483    [TestMethod]
484    [TestCategory("Algorithms.DataAnalysis")]
485    [TestProperty("Time", "short")]
486    public void MctsSymbRegBenchmarkKeijzer15() {
487      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
488      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 15 f(")));
489      // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
490      if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
491      TestMcts(regProblem, iterations: 10000, allowLog: false, allowExp: false, allowInv: false);
492    }
493    #endregion
494
495    private void TestMcts(IRegressionProblemData problemData, int iterations = 2000, double successThreshold = 0.999,
496      bool allowExp = true,
497      bool allowLog = true,
498      bool allowInv = true,
499      bool allowSum = true
500      ) {
501      var mctsSymbReg = new MctsSymbolicRegressionAlgorithm();
502      var regProblem = new RegressionProblem();
503      regProblem.ProblemDataParameter.Value = problemData;
504      #region Algorithm Configuration
505      mctsSymbReg.Problem = regProblem;
506      mctsSymbReg.Iterations = iterations;
507      mctsSymbReg.MaxVariableReferences = 10;
508      var ucbPolicy = new Ucb();
509      ucbPolicy.C = 2;
510      mctsSymbReg.Policy = ucbPolicy;
511      mctsSymbReg.SetSeedRandomly = false;
512      mctsSymbReg.Seed = 1234;
513      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("exp")), allowExp);
514      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("log")), allowLog);
515      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("1 /")), allowInv);
516      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("sum of multiple")), allowSum);
517      #endregion
518      RunAlgorithm(mctsSymbReg);
519
520      Console.WriteLine(mctsSymbReg.ExecutionTime);
521      // R² >= 0.999
522      // using arequal to get output of the actual value
523      var eps = 1.0 - successThreshold;
524      Assert.AreEqual(1.0, ((DoubleValue)mctsSymbReg.Results["Best solution quality (train)"].Value).Value, eps);
525      Assert.AreEqual(1.0, ((DoubleValue)mctsSymbReg.Results["Best solution quality (test)"].Value).Value, eps);
526    }
527
528    private void TestMctsNumberOfSolutions(IRegressionProblemData problemData, int maxNumberOfVariables, int expectedNumberOfSolutions,
529      bool allowProd = true,
530      bool allowExp = true,
531      bool allowLog = true,
532      bool allowInv = true,
533      bool allowSum = true
534  ) {
535      var mctsSymbReg = new MctsSymbolicRegressionAlgorithm();
536      var regProblem = new RegressionProblem();
537      regProblem.ProblemDataParameter.Value = problemData;
538      #region Algorithm Configuration
539
540      mctsSymbReg.SetSeedRandomly = false;
541      mctsSymbReg.Seed = 1234;
542      mctsSymbReg.Problem = regProblem;
543      mctsSymbReg.Iterations = int.MaxValue; // stopping when all solutions have been enumerated
544      mctsSymbReg.MaxVariableReferences = maxNumberOfVariables;
545      var ucbPolicy = new Ucb();
546      ucbPolicy.C = 1000; // essentially breadth first search
547      mctsSymbReg.Policy = ucbPolicy;
548      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.StartsWith("prod")), allowProd);
549      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("exp")), allowExp);
550      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("log")), allowLog);
551      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("1 /")), allowInv);
552      mctsSymbReg.AllowedFactors.SetItemCheckedState(mctsSymbReg.AllowedFactors.Single(s => s.Value.Contains("sum of multiple")), allowSum);
553      #endregion
554      RunAlgorithm(mctsSymbReg);
555
556      Console.WriteLine(mctsSymbReg.ExecutionTime);
557      Assert.AreEqual(expectedNumberOfSolutions, ((IntValue)mctsSymbReg.Results["Iterations"].Value).Value);
558    }
559
560
561    // same as in SamplesUtil
562    private void RunAlgorithm(IAlgorithm a) {
563      var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
564      Exception ex = null;
565      a.Stopped += (src, e) => { trigger.Set(); };
566      a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
567      a.Prepare();
568      a.Start();
569      trigger.WaitOne();
570
571      Assert.AreEqual(ex, null);
572    }
573
574  }
575}
Note: See TracBrowser for help on using the repository browser.