Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/OnlineCalculatorPerformanceTest.cs @ 16046

Last change on this file since 16046 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    /// <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]
47    [TestCategory("Problems.DataAnalysis")]
48    [TestProperty("Time", "medium")]
49    public void OnlineAccuracyCalculatorPerformanceTest() {
50      TestCalculatorPerfomance(OnlineAccuracyCalculator.Calculate);
51    }
52    [TestMethod]
53    [TestCategory("Problems.DataAnalysis")]
54    [TestProperty("Time", "medium")]
55    public void OnlineCovarianceCalculatorPerformanceTest() {
56      TestCalculatorPerfomance(OnlineCovarianceCalculator.Calculate);
57    }
58    [TestMethod]
59    [TestCategory("Problems.DataAnalysis")]
60    [TestProperty("Time", "medium")]
61    public void OnlineMeanAbsolutePercentageErrorCalculatorPerformanceTest() {
62      TestCalculatorPerfomance(OnlineMeanAbsolutePercentageErrorCalculator.Calculate);
63    }
64
65    [TestMethod]
66    [TestCategory("Problems.DataAnalysis")]
67    [TestProperty("Time", "medium")]
68    public void OnlineMeanSquaredErrorCalculatorPerformanceTest() {
69      TestCalculatorPerfomance(OnlineMeanSquaredErrorCalculator.Calculate);
70    }
71    [TestMethod]
72    [TestCategory("Problems.DataAnalysis")]
73    [TestProperty("Time", "medium")]
74    public void OnlineNormalizedMeanSquaredErrorCalculatorPerformanceTest() {
75      TestCalculatorPerfomance(OnlineNormalizedMeanSquaredErrorCalculator.Calculate);
76    }
77    [TestMethod]
78    [TestCategory("Problems.DataAnalysis")]
79    [TestProperty("Time", "medium")]
80    public void OnlinePearsonsRSquaredCalculatorPerformanceTest() {
81      TestCalculatorPerfomance(OnlinePearsonsRCalculator.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);
88      OnlineCalculatorError errorState = OnlineCalculatorError.None; ;
89
90      Stopwatch watch = new Stopwatch();
91      watch.Start();
92      for (int i = 0; i < Repetitions; i++) {
93        double value = calculateFunc(dataset.GetDoubleValues("y"), dataset.GetDoubleValues("x0"), out errorState);
94      }
95      Assert.AreEqual(errorState, OnlineCalculatorError.None);
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.