Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/OnlineCalculatorPerformanceTest.cs @ 11001

Last change on this file since 11001 was 7915, checked in by mkommend, 12 years ago

#1777: Extracted unit tests into a separate solution.

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Diagnostics;
24using System.Linq;
25using HeuristicLab.Problems.DataAnalysis;
26using HeuristicLab.Random;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28
29namespace HeuristicLab.Problems.DataAnalysis_34.Tests {
30  [TestClass]
31  public class OnlineCalculatorPerformanceTest {
32    private const int Rows = 5000;
33    private const int Columns = 2;
34    private const int Repetitions = 10000;
35
36    private TestContext testContextInstance;
37
38    /// <summary>
39    ///Gets or sets the test context which provides
40    ///information about and functionality for the current test run.
41    ///</summary>
42    public TestContext TestContext {
43      get { return testContextInstance; }
44      set { testContextInstance = value; }
45    }
46
47
48    [TestMethod]
49    public void OnlineAccuracyCalculatorPerformanceTest() {
50      TestCalculatorPerfomance(OnlineAccuracyCalculator.Calculate);
51    }
52    [TestMethod]
53    public void OnlineCovarianceCalculatorPerformanceTest() {
54      TestCalculatorPerfomance(OnlineCovarianceCalculator.Calculate);
55    }
56    [TestMethod]
57    public void OnlineMeanAbsolutePercentageErrorCalculatorPerformanceTest() {
58      TestCalculatorPerfomance(OnlineMeanAbsolutePercentageErrorCalculator.Calculate);
59    }
60
61    [TestMethod]
62    public void OnlineMeanSquaredErrorCalculatorPerformanceTest() {
63      TestCalculatorPerfomance(OnlineMeanSquaredErrorCalculator.Calculate);
64    }
65    [TestMethod]
66    public void OnlineNormalizedMeanSquaredErrorCalculatorPerformanceTest() {
67      TestCalculatorPerfomance(OnlineNormalizedMeanSquaredErrorCalculator.Calculate);
68    }
69    [TestMethod]
70    public void OnlinePearsonsRSquaredCalculatorPerformanceTest() {
71      TestCalculatorPerfomance(OnlinePearsonsRSquaredCalculator.Calculate);
72    }
73
74    private delegate double CalcateFunc(IEnumerable<double> estimated, IEnumerable<double> original, out OnlineCalculatorError errorState);
75    private void TestCalculatorPerfomance(CalcateFunc calculateFunc) {
76      var twister = new MersenneTwister(31415);
77      var dataset = CreateRandomDataset(twister, Rows, Columns);
78      OnlineCalculatorError errorState = OnlineCalculatorError.None; ;
79
80      Stopwatch watch = new Stopwatch();
81      watch.Start();
82      for (int i = 0; i < Repetitions; i++) {
83        double value = calculateFunc(dataset.GetDoubleValues("y"), dataset.GetDoubleValues("x0"), out errorState);
84      }
85      Assert.AreEqual(errorState, OnlineCalculatorError.None);
86      watch.Stop();
87
88      TestContext.WriteLine("");
89      TestContext.WriteLine("Calculated Rows per milisecond: {0}.", Rows * Repetitions * 1.0 / watch.ElapsedMilliseconds);
90    }
91
92    public static Dataset CreateRandomDataset(MersenneTwister twister, int rows, int columns) {
93      double[,] data = new double[rows, columns];
94      for (int i = 0; i < rows; i++) {
95        for (int j = 0; j < columns; j++) {
96          data[i, j] = twister.NextDouble() * 2.0 - 1.0;
97        }
98      }
99      IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString()));
100      Dataset ds = new Dataset(variableNames, data);
101      return ds;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.