Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/SupportVectorMachineTest.cs @ 14648

Last change on this file since 14648 was 12870, checked in by gkronber, 9 years ago

#2434 fixed compile error in unit tests using CV

File size: 3.6 KB
RevLine 
[6553]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6553]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;
[9764]23using System.Collections.Generic;
[6553]24using System.Linq;
25using System.Threading;
[9764]26using HeuristicLab.Algorithms.DataAnalysis;
[6553]27using HeuristicLab.Common;
[9764]28using HeuristicLab.Core;
29using HeuristicLab.Problems.DataAnalysis;
[6553]30using Microsoft.VisualStudio.TestTools.UnitTesting;
31
[9764]32namespace HeuristicLab.Tests {
[6553]33  [TestClass]
34  public class SupportVectorMachineTest {
35    public SupportVectorMachineTest() { }
36
37    private TestContext testContextInstance;
38
39    /// <summary>
40    ///Gets or sets the test context which provides
41    ///information about and functionality for the current test run.
42    ///</summary>
43    public TestContext TestContext {
44      get {
45        return testContextInstance;
46      }
47      set {
48        testContextInstance = value;
49      }
50    }
51
52    private EventWaitHandle trigger = new AutoResetEvent(false);
53    private Exception ex;
54
55    [TestMethod]
[9783]56    [TestCategory("Algorithms.DataAnalysis")]
57    [TestProperty("Time", "medium")]
[6553]58    public void SupportVectorMachinePerformanceTest() {
59      ex = null;
60
61      var cv = new CrossValidation();
62      cv.Algorithm = new SupportVectorRegression();
63      var rand = new HeuristicLab.Random.MersenneTwister();
64      double[,] data = GenerateData(1000, rand);
65      List<string> variables = new List<string>() { "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "y" };
66      Dataset ds = new Dataset(variables, data);
67      cv.Problem.ProblemDataParameter.ActualValue = new RegressionProblemData(ds, variables.Take(10), variables.Last());
[12870]68      cv.Folds = 5;
[6553]69      cv.SamplesStart.Value = 0;
70      cv.SamplesEnd.Value = 999;
71
72      cv.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(cv_ExceptionOccurred);
73      cv.Stopped += new EventHandler(cv_Stopped);
74
75      cv.Prepare();
76      cv.Start();
77      trigger.WaitOne();
78      if (ex != null) throw ex;
79
80      TestContext.WriteLine("Runtime: {0}", cv.ExecutionTime.ToString());
81
82    }
83
84    // poly-10: y = x1 x2 + x3 x4 + x5 x6 + x1 x7 x9 + x3 x6 x10
85    private double[,] GenerateData(int n, IRandom random) {
86      double[,] data = new double[n, 11];
87      for (int i = 0; i < n; i++) {
88        for (int c = 0; c < 10; c++) {
89          data[i, c] = random.NextDouble() * 2.0 - 1.0;
90        }
91        data[i, 10] =
92          data[i, 0] * data[i, 1] +
93          data[i, 2] * data[i, 3] +
94          data[i, 4] * data[i, 5] +
95          data[i, 0] * data[i, 6] * data[i, 8] +
96          data[i, 2] * data[i, 5] * data[i, 9];
97      }
98      return data;
99    }
100
101    private void cv_ExceptionOccurred(object sender, EventArgs<Exception> e) {
102      ex = e.Value;
103    }
104
105    private void cv_Stopped(object sender, EventArgs e) {
106      trigger.Set();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.