Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/3.3/Tests/SupportVectorMachineTest.cs @ 6788

Last change on this file since 6788 was 6553, checked in by gkronber, 13 years ago

#1581: created a unit test for SVM performance

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