Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/OnlineCalculatorPerformanceTest.cs @ 10878

Last change on this file since 10878 was 9885, checked in by mkommend, 11 years ago

#2088: Merged all changesets regarding the unit test restructuring in the stable branch.

File size: 4.5 KB
RevLine 
[5952]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5952]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.Random;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
[9885]28namespace HeuristicLab.Problems.DataAnalysis.Tests {
[5952]29  [TestClass]
30  public class OnlineCalculatorPerformanceTest {
31    private const int Rows = 5000;
32    private const int Columns = 2;
33    private const int Repetitions = 10000;
34
35    private TestContext testContextInstance;
36    /// <summary>
37    ///Gets or sets the test context which provides
38    ///information about and functionality for the current test run.
39    ///</summary>
40    public TestContext TestContext {
41      get { return testContextInstance; }
42      set { testContextInstance = value; }
43    }
44
45
46    [TestMethod]
[9885]47    [TestCategory("Problems.DataAnalysis")]
48    [TestProperty("Time", "medium")]
[5952]49    public void OnlineAccuracyCalculatorPerformanceTest() {
50      TestCalculatorPerfomance(OnlineAccuracyCalculator.Calculate);
51    }
52    [TestMethod]
[9885]53    [TestCategory("Problems.DataAnalysis")]
54    [TestProperty("Time", "medium")]
[5952]55    public void OnlineCovarianceCalculatorPerformanceTest() {
56      TestCalculatorPerfomance(OnlineCovarianceCalculator.Calculate);
57    }
58    [TestMethod]
[9885]59    [TestCategory("Problems.DataAnalysis")]
60    [TestProperty("Time", "medium")]
[5952]61    public void OnlineMeanAbsolutePercentageErrorCalculatorPerformanceTest() {
62      TestCalculatorPerfomance(OnlineMeanAbsolutePercentageErrorCalculator.Calculate);
63    }
64
65    [TestMethod]
[9885]66    [TestCategory("Problems.DataAnalysis")]
67    [TestProperty("Time", "medium")]
[5952]68    public void OnlineMeanSquaredErrorCalculatorPerformanceTest() {
69      TestCalculatorPerfomance(OnlineMeanSquaredErrorCalculator.Calculate);
70    }
71    [TestMethod]
[9885]72    [TestCategory("Problems.DataAnalysis")]
73    [TestProperty("Time", "medium")]
[5952]74    public void OnlineNormalizedMeanSquaredErrorCalculatorPerformanceTest() {
75      TestCalculatorPerfomance(OnlineNormalizedMeanSquaredErrorCalculator.Calculate);
76    }
77    [TestMethod]
[9885]78    [TestCategory("Problems.DataAnalysis")]
79    [TestProperty("Time", "medium")]
[5952]80    public void OnlinePearsonsRSquaredCalculatorPerformanceTest() {
81      TestCalculatorPerfomance(OnlinePearsonsRSquaredCalculator.Calculate);
82    }
83
84    private delegate double CalcateFunc(IEnumerable<double> estimated, IEnumerable<double> original, out OnlineCalculatorError errorState);
85    private void TestCalculatorPerfomance(CalcateFunc calculateFunc) {
86      var twister = new MersenneTwister(31415);
87      var dataset = CreateRandomDataset(twister, Rows, Columns);
[5963]88      OnlineCalculatorError errorState = OnlineCalculatorError.None; ;
[5952]89
90      Stopwatch watch = new Stopwatch();
91      watch.Start();
92      for (int i = 0; i < Repetitions; i++) {
[6740]93        double value = calculateFunc(dataset.GetDoubleValues("y"), dataset.GetDoubleValues("x0"), out errorState);
[5952]94      }
[5963]95      Assert.AreEqual(errorState, OnlineCalculatorError.None);
[5952]96      watch.Stop();
97
98      TestContext.WriteLine("");
99      TestContext.WriteLine("Calculated Rows per milisecond: {0}.", Rows * Repetitions * 1.0 / watch.ElapsedMilliseconds);
100    }
101
102    public static Dataset CreateRandomDataset(MersenneTwister twister, int rows, int columns) {
103      double[,] data = new double[rows, columns];
104      for (int i = 0; i < rows; i++) {
105        for (int j = 0; j < columns; j++) {
106          data[i, j] = twister.NextDouble() * 2.0 - 1.0;
107        }
108      }
109      IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString()));
110      Dataset ds = new Dataset(variableNames, data);
111      return ds;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.