Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2974_Constants_Optimization/UnitTests/PerformanceTest.cs @ 17687

Last change on this file since 17687 was 16507, checked in by mkommend, 6 years ago

#2974: First stable version of new CoOp.

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