Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2581:

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