Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/GaussianProcessRegressionTest.cs @ 8448

Last change on this file since 8448 was 8371, checked in by gkronber, 12 years ago

#1902: worked on Gaussian Process algorithm

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