Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/Test/AlgorithmPerformanceTest.cs @ 16175

Last change on this file since 16175 was 16157, checked in by bburlacu, 6 years ago

#2886: Update IGrammarEnumerationEvaluator interface (add Evaluate method accepting an ISymbolicExpressionTree for the case when the constants have already been optimized in the tree, add boolean OptimizeConstants flag), small refactor in GrammarEnumeration/GrammarEnumerationAlgorithm.cs, add unit tests

File size: 3.2 KB
Line 
1using System;
2using System.Linq;
3using System.Threading;
4using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Problems.DataAnalysis;
8using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
9using HeuristicLab.Problems.Instances.DataAnalysis;
10using HeuristicLab.Random;
11using Microsoft.VisualStudio.TestTools.UnitTesting;
12
13namespace Test {
14  [TestClass]
15  public class AlgorithmPerformanceTest {
16    private const int Seed = 1234;
17    private IRandom rand;
18
19    private const double SuccessThreshold = 0.9999999;
20
21    private GrammarEnumerationAlgorithm alg;
22    private RegressionProblem problem;
23
24    [TestInitialize]
25    public void InitTest() {
26      rand = new FastRandom(Seed);
27      alg = new GrammarEnumerationAlgorithm();
28      //var provider = new PennMLRegressionInstanceProvider();
29      //var id = provider.GetDataDescriptors().Single(x => x.Name == "1027_ESL"); // something not randomly generated
30      //problem = new RegressionProblem { ProblemData = provider.LoadData(id) };
31      problem = new RegressionProblem { ProblemData = new KeijzerFunctionEleven(Seed).GenerateRegressionData() };
32      alg.Problem = problem;
33      alg.GuiUpdateInterval = int.MaxValue;
34      alg.SearchDataStructure = StorageType.Stack;
35      alg.SearchDataStructureSize = (int)1e5;
36
37      alg.Stopped += (sender, args) => mutex.Set();
38      alg.Started += (sender, args) => mutex.Reset();
39    }
40
41    private readonly ManualResetEvent mutex = new ManualResetEvent(false);
42
43    [TestMethod]
44    [TestProperty("Performance", "Phrase expansion speed for 2 variables.")]
45    public void AlgorithmPerformance() {
46      int reps = 5;
47      foreach (var st in Enum.GetValues(typeof(StorageType)).Cast<StorageType>()) {
48        alg.SearchDataStructure = st;
49        double avgExpansionSpeed = 0;
50        for (int i = 0; i < reps; ++i) {
51          alg.Prepare();
52          alg.Start();
53          mutex.WaitOne();
54          var expansionSpeed = (IntValue)alg.Results["Expansions per second"].Value;
55          avgExpansionSpeed += expansionSpeed.Value;
56        }
57
58        var distinct = (IntValue)alg.Results["Distinct Sentences"].Value;
59        var sentences = (IntValue)alg.Results["Generated Sentences"].Value;
60        var expansions = (IntValue)alg.Results["Phrase Expansions"].Value;
61        var archived = (IntValue)alg.Results["Generated/Archived Phrases"].Value;
62        var overwritten = (IntValue)alg.Results["Sentences overwritten"].Value;
63        var best = (SymbolicRegressionSolution)alg.Results["Best solution (Training)"].Value;
64        Console.WriteLine(st);
65        Console.WriteLine($"Generated/archived phrases: {archived.Value}");
66        Console.WriteLine($"Expansions per second: {avgExpansionSpeed / reps}");
67        Console.WriteLine($"Generated sentences: {sentences.Value}");
68        Console.WriteLine($"Distinct sentences: {distinct.Value}");
69        Console.WriteLine($"Overwritten sentences: {overwritten.Value}");
70        Console.WriteLine($"Train R2: {best.TrainingRSquared}");
71        Console.WriteLine($"Test R2: {best.TestRSquared}");
72        Console.WriteLine();
73      }
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.