Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2974: First stable version of new CoOp.

File size: 5.9 KB
RevLine 
[16461]1using System;
[16500]2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
[16461]5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[16500]6using HeuristicLab.Problems.DataAnalysis;
[16461]7using HeuristicLab.Problems.DataAnalysis.Symbolic;
[16507]8using HeuristicLab.Problems.DataAnalysis.Symbolic.ConstantsOptimization;
[16461]9using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
[16500]10using HeuristicLab.Problems.Instances.DataAnalysis;
11using HeuristicLab.Random;
[16461]12using Microsoft.VisualStudio.TestTools.UnitTesting;
13
14namespace UnitTests {
15  [TestClass]
16  public class PerformanceTest {
[16500]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;
[16461]22
23    [TestMethod]
24    [TestCategory("Problems.DataAnalysis.Symbolic.Regression")]
25    [TestProperty("Time", "long")]
[16507]26    public static void New_ConstantsOptimization_Tower_Algorithm() {
[16500]27      var twister = new MersenneTwister((uint)seed);
28      var problemData = new RegressionRealWorldInstanceProvider().LoadData(new Tower());
29      var rows = Enumerable.Range(0, totalRows);
[16461]30
[16500]31      var grammar = new TypeCoherentExpressionGrammar();
[16461]32      grammar.ConfigureAsDefaultRegressionGrammar();
33
[16500]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      }
[16461]38
[16500]39      Console.WriteLine("Random tree constants optimization performance of new method:");
[16461]40
[16500]41      //warm up
42      for (int i = 0; i < trees.Length; i++) {
[16507]43        if (!trees[i].IterateNodesPrefix().OfType<VariableTreeNode>().Any()) Debugger.Break();
44        double quality = LMConstantsOptimizer.OptimizeConstants(trees[i], problemData.Dataset,problemData.TargetVariable, rows, true, maxIterations);
[16500]45      }
[16461]46
[16500]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++) {
[16507]51          double quality = LMConstantsOptimizer.OptimizeConstants(trees[i], problemData.Dataset, problemData.TargetVariable, rows, true, maxIterations);
[16500]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);
[16461]66
[16500]67      var grammar = new TypeCoherentExpressionGrammar();
68      grammar.ConfigureAsDefaultRegressionGrammar();
[16461]69
[16500]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      }
[16461]74
[16500]75      Console.WriteLine("Random tree constants optimization performance of existing method:");
76      var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
[16461]77
[16500]78      //warm up
79      for (int i = 0; i < trees.Length; i++) {
[16507]80        if (!trees[i].IterateNodesPrefix().OfType<VariableTreeNode>().Any()) Debugger.Break();
[16500]81        double quality = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
82          interpreter, trees[i], problemData, rows, true, maxIterations);
83      }
[16461]84
[16500]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    }
[16461]98
99
100
[16500]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    }
[16461]104
[16500]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;
[16461]116    }
117
[16500]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        }
[16461]128      }
129    }
130
131  }
132}
Note: See TracBrowser for help on using the repository browser.