1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
6 | using HeuristicLab.Problems.DataAnalysis;
|
---|
7 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
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 UnitTests {
|
---|
14 | [TestClass]
|
---|
15 | public class PerformanceTest {
|
---|
16 | private static readonly int seed = 1234;
|
---|
17 | private static readonly int totalRows = 1000;
|
---|
18 | private static readonly int maxIterations = 10;
|
---|
19 | private static readonly int repetitions = 5;
|
---|
20 | private static readonly int maxTreeSize = 50;
|
---|
21 |
|
---|
22 | [TestMethod]
|
---|
23 | [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
|
---|
24 | [TestProperty("Time", "long")]
|
---|
25 | public void New_ConstantsOptimization_Tower_Algorithm() {
|
---|
26 | var twister = new MersenneTwister((uint)seed);
|
---|
27 | var problemData = new RegressionRealWorldInstanceProvider().LoadData(new Tower());
|
---|
28 | var rows = Enumerable.Range(0, totalRows);
|
---|
29 |
|
---|
30 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
31 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
32 |
|
---|
33 | var trees = CreateRandomTrees(twister, problemData.Dataset, grammar, 1000, 1, maxTreeSize, 0, 0);
|
---|
34 | foreach (SymbolicExpressionTree tree in trees) {
|
---|
35 | InitTree(tree, twister, problemData.AllowedInputVariables.ToList());
|
---|
36 | }
|
---|
37 |
|
---|
38 | Console.WriteLine("Random tree constants optimization performance of new method:");
|
---|
39 |
|
---|
40 | //warm up
|
---|
41 | for (int i = 0; i < trees.Length; i++) {
|
---|
42 | double quality = LMConstantsOptimizer.OptimizeConstants(trees[i], problemData, rows, true, maxIterations);
|
---|
43 | }
|
---|
44 |
|
---|
45 | Stopwatch watch = new Stopwatch();
|
---|
46 | for (int rep = 0; rep < repetitions; rep++) {
|
---|
47 | watch.Start();
|
---|
48 | for (int i = 0; i < trees.Length; i++) {
|
---|
49 | double quality = LMConstantsOptimizer.OptimizeConstants(trees[i], problemData, rows, true, maxIterations);
|
---|
50 | }
|
---|
51 | watch.Stop();
|
---|
52 | Console.WriteLine("Iteration " + rep + "\t\t" + " Elapsed time: \t" + watch.ElapsedMilliseconds + " ms \t\t" +
|
---|
53 | "Time per tree: " + watch.ElapsedMilliseconds / 1000.0 / trees.Length);
|
---|
54 | watch.Reset();
|
---|
55 | }
|
---|
56 | }
|
---|
57 | [TestMethod]
|
---|
58 | [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
|
---|
59 | [TestProperty("Time", "long")]
|
---|
60 | public void Old_ConstantsOptimization_Tower_Algorithm() {
|
---|
61 | var twister = new MersenneTwister((uint)seed);
|
---|
62 | var problemData = new RegressionRealWorldInstanceProvider().LoadData(new Tower());
|
---|
63 | var rows = Enumerable.Range(0, totalRows);
|
---|
64 |
|
---|
65 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
66 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
67 |
|
---|
68 | var trees = CreateRandomTrees(twister, problemData.Dataset, grammar, 1000, 1, maxTreeSize, 0, 0);
|
---|
69 | foreach (SymbolicExpressionTree tree in trees) {
|
---|
70 | InitTree(tree, twister, problemData.AllowedInputVariables.ToList());
|
---|
71 | }
|
---|
72 |
|
---|
73 | Console.WriteLine("Random tree constants optimization performance of existing method:");
|
---|
74 | var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
|
---|
75 |
|
---|
76 | //warm up
|
---|
77 | for (int i = 0; i < trees.Length; i++) {
|
---|
78 | double quality = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
|
---|
79 | interpreter, trees[i], problemData, rows, true, maxIterations);
|
---|
80 | }
|
---|
81 |
|
---|
82 | Stopwatch watch = new Stopwatch();
|
---|
83 | for (int rep = 0; rep < repetitions; rep++) {
|
---|
84 | watch.Start();
|
---|
85 | for (int i = 0; i < trees.Length; i++) {
|
---|
86 | double quality = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
|
---|
87 | interpreter, trees[i], problemData, rows, true, maxIterations);
|
---|
88 | }
|
---|
89 | watch.Stop();
|
---|
90 | Console.WriteLine("Iteration " + rep + "\t\t" + " Elapsed time: \t" + watch.ElapsedMilliseconds + " ms \t\t" +
|
---|
91 | "Time per tree: " + watch.ElapsedMilliseconds / 1000.0 / trees.Length);
|
---|
92 | watch.Reset();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 | public static ISymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, IDataset dataset, ISymbolicExpressionGrammar grammar, int popSize) {
|
---|
99 | return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public static ISymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, IDataset dataset, ISymbolicExpressionGrammar grammar,
|
---|
103 | int popSize, int minSize, int maxSize,
|
---|
104 | int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
105 | foreach (Variable variableSymbol in grammar.Symbols.OfType<Variable>()) {
|
---|
106 | variableSymbol.VariableNames = dataset.VariableNames;
|
---|
107 | }
|
---|
108 | ISymbolicExpressionTree[] randomTrees = new ISymbolicExpressionTree[popSize];
|
---|
109 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
110 | randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10);
|
---|
111 | }
|
---|
112 | return randomTrees;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public static void InitTree(ISymbolicExpressionTree tree, MersenneTwister twister, List<string> varNames) {
|
---|
116 | foreach (var node in tree.IterateNodesPostfix()) {
|
---|
117 | if (node is VariableTreeNode) {
|
---|
118 | var varNode = node as VariableTreeNode;
|
---|
119 | varNode.Weight = twister.NextDouble() * 20.0 - 10.0;
|
---|
120 | varNode.VariableName = varNames[twister.Next(varNames.Count)];
|
---|
121 | } else if (node is ConstantTreeNode) {
|
---|
122 | var constantNode = node as ConstantTreeNode;
|
---|
123 | constantNode.Value = twister.NextDouble() * 20.0 - 10.0;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | }
|
---|
129 | }
|
---|