Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/OnlineCalculatorPerformanceTest.cs @ 7835

Last change on this file since 7835 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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.Random;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace HeuristicLab.Problems.DataAnalysis.Tests {
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
37    /// <summary>
38    ///Gets or sets the test context which provides
39    ///information about and functionality for the current test run.
40    ///</summary>
41    public TestContext TestContext {
42      get { return testContextInstance; }
43      set { testContextInstance = value; }
44    }
45
46
47    [TestMethod]
48    public void OnlineAccuracyCalculatorPerformanceTest() {
49      TestCalculatorPerfomance(OnlineAccuracyCalculator.Calculate);
50    }
51    [TestMethod]
52    public void OnlineCovarianceCalculatorPerformanceTest() {
53      TestCalculatorPerfomance(OnlineCovarianceCalculator.Calculate);
54    }
55    [TestMethod]
56    public void OnlineMeanAbsolutePercentageErrorCalculatorPerformanceTest() {
57      TestCalculatorPerfomance(OnlineMeanAbsolutePercentageErrorCalculator.Calculate);
58    }
59
60    [TestMethod]
61    public void OnlineMeanSquaredErrorCalculatorPerformanceTest() {
62      TestCalculatorPerfomance(OnlineMeanSquaredErrorCalculator.Calculate);
63    }
64    [TestMethod]
65    public void OnlineNormalizedMeanSquaredErrorCalculatorPerformanceTest() {
66      TestCalculatorPerfomance(OnlineNormalizedMeanSquaredErrorCalculator.Calculate);
67    }
68    [TestMethod]
69    public void OnlinePearsonsRSquaredCalculatorPerformanceTest() {
70      TestCalculatorPerfomance(OnlinePearsonsRSquaredCalculator.Calculate);
71    }
72
73    private delegate double CalcateFunc(IEnumerable<double> estimated, IEnumerable<double> original, out OnlineCalculatorError errorState);
74    private void TestCalculatorPerfomance(CalcateFunc calculateFunc) {
75      var twister = new MersenneTwister(31415);
76      var dataset = CreateRandomDataset(twister, Rows, Columns);
77      OnlineCalculatorError errorState = OnlineCalculatorError.None; ;
78
79      Stopwatch watch = new Stopwatch();
80      watch.Start();
81      for (int i = 0; i < Repetitions; i++) {
82        double value = calculateFunc(dataset.GetDoubleValues("y"), dataset.GetDoubleValues("x0"), out errorState);
83      }
84      Assert.AreEqual(errorState, OnlineCalculatorError.None);
85      watch.Stop();
86
87      TestContext.WriteLine("");
88      TestContext.WriteLine("Calculated Rows per milisecond: {0}.", Rows * Repetitions * 1.0 / watch.ElapsedMilliseconds);
89    }
90
91    public static Dataset CreateRandomDataset(MersenneTwister twister, int rows, int columns) {
92      double[,] data = new double[rows, columns];
93      for (int i = 0; i < rows; i++) {
94        for (int j = 0; j < columns; j++) {
95          data[i, j] = twister.NextDouble() * 2.0 - 1.0;
96        }
97      }
98      IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString()));
99      Dataset ds = new Dataset(variableNames, data);
100      return ds;
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.