1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Threading;
|
---|
4 | using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Problems.DataAnalysis;
|
---|
8 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
9 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
10 | using HeuristicLab.Random;
|
---|
11 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
12 |
|
---|
13 | namespace 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 | }
|
---|