Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2789_MathNetNumerics-Exploration/Test/UnitTest1.cs @ 16674

Last change on this file since 16674 was 15469, checked in by gkronber, 7 years ago

#2789 trying to get SBART to work correctly.

File size: 5.4 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Linq;
4using HeuristicLab.Algorithms.DataAnalysis.Experimental;
5using HeuristicLab.Algorithms.GeneticAlgorithm;
6using HeuristicLab.Common;
7using HeuristicLab.Problems.DataAnalysis;
8using HeuristicLab.SequentialEngine;
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10
11namespace Test {
12  [TestClass]
13  public class UnitTest1 {
14    [TestMethod]
15    public void TestMethod1() {
16      var ga = new GeneticAlgorithm();
17      var symbReg = new HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.SymbolicRegressionSingleObjectiveProblem();
18      var eval = new SymbolicRegressionConstantOptimizationEvaluator();
19      symbReg.EvaluatorParameter.Value = eval;
20      ga.Engine = new SequentialEngine();
21      ga.Seed.Value = 1234;
22      ga.Problem = symbReg;
23
24      ga.Start();
25
26    }
27  }
28
29  [TestClass]
30  public class PenalizedRegressionSplineTests {
31    [TestMethod]
32    public void TestPenalizedRegressionSplinesCrossValidation() {
33      var xs = HeuristicLab.Common.SequenceGenerator.GenerateSteps(-3.5, 3.5, 0.02, includeEnd: true).ToArray();
34      var ys = xs.Select(xi => alglib.normaldistr.normaldistribution(xi)); // 1.0 / (Math.Sqrt(2 * Math.PI) * Math.Exp(-0.5 * xi * xi))).ToArray();
35
36      alglib.hqrndstate state;
37      alglib.hqrndseed(1234, 5678, out state);
38      var ys_noise = ys.Select(yi => yi + alglib.hqrndnormal(state) * 0.01).ToArray();
39
40      double bestRho = -15;
41      double best_loo_rmse = double.PositiveInfinity;
42      var clock = new Stopwatch();
43      clock.Start();
44      int iters = 0;
45      for (int rho = -15; rho <= 15; rho++) {
46        double loo_rmse;
47        double avgTrainRmse;
48        double[] predictions;
49        Splines.CalculatePenalizedRegressionSpline(xs, ys_noise, rho, "y", new string[] { "x" }, out avgTrainRmse, out loo_rmse, out predictions);
50        iters++;
51        Console.WriteLine("{0} {1} {2}", rho, avgTrainRmse, loo_rmse);
52        if (loo_rmse < best_loo_rmse) {
53          best_loo_rmse = loo_rmse;
54          bestRho = rho;
55        }
56      }
57      clock.Stop();
58      Console.WriteLine("Best rho {0}, RMSE (LOO): {1}, ms/run {2}", bestRho, best_loo_rmse, clock.ElapsedMilliseconds / (double)iters);
59    }
60
61    [TestMethod]
62    public void TestReinschSplineCrossValidation() {
63      var xs = HeuristicLab.Common.SequenceGenerator.GenerateSteps(-3.5, 3.5, 0.02, includeEnd: true).ToList();
64      var ys = xs.Select(xi => alglib.normaldistr.normaldistribution(xi)); // 1.0 / (Math.Sqrt(2 * Math.PI) * Math.Exp(-0.5 * xi * xi))).ToArray();
65
66      alglib.hqrndstate state;
67      alglib.hqrndseed(1234, 5678, out state);
68      var ys_noise = ys.Select(yi => yi + alglib.hqrndnormal(state) * 0.01).ToList();
69
70      var tol = ys_noise.StandardDeviation();
71
72      var ds = new Dataset(new string[] { "x", "y" }, new[] { xs, ys_noise });
73      var rows = Enumerable.Range(0, xs.Count);
74
75      var bestTol = double.PositiveInfinity;
76      var bestLooRmse = double.PositiveInfinity;
77      var clock = new Stopwatch();
78      clock.Start();
79      var iters = 0;
80      while (tol > 0.0001) {
81        double loo_rmse;
82        double avgTrainRmse;
83        var model = Splines.CalculateSmoothingSplineReinsch(
84          xs.ToArray(), ys_noise.ToArray(), tol, "y", new string[] { "x" }, out avgTrainRmse, out loo_rmse);
85        var y_pred = model.GetEstimatedValues(ds, rows);
86
87        Console.WriteLine("{0} {1} {2}", tol, avgTrainRmse, loo_rmse);
88        if (loo_rmse < bestLooRmse) {
89          bestLooRmse = loo_rmse;
90          bestTol = tol;
91        }
92        tol *= 0.8;
93        iters++;
94      }
95      clock.Stop();
96      Console.WriteLine("Best tolerance {0}, RMSE (LOO): {1}, ms/run {2}", bestTol, bestLooRmse, clock.ElapsedMilliseconds / (double)iters);
97    }
98
99    [TestMethod]
100    public void TestReinschSplineAutomaticTolerance() {
101      var xs = HeuristicLab.Common.SequenceGenerator.GenerateSteps(-3.5, 3.5, 0.02, includeEnd: true).ToList();
102      var ys = xs.Select(xi => alglib.normaldistr.normaldistribution(xi)); // 1.0 / (Math.Sqrt(2 * Math.PI) * Math.Exp(-0.5 * xi * xi))).ToArray();
103
104      alglib.hqrndstate state;
105      alglib.hqrndseed(1234, 5678, out state);
106      var ys_noise = ys.Select(yi => yi + alglib.hqrndnormal(state) * 0.01).ToList();
107
108      double optTol;
109      double looRMSE;
110      Splines.CalculateSmoothingSplineReinsch(xs.ToArray(), ys_noise.ToArray(), new string[] { "x" }, "y", out optTol, out looRMSE);
111
112      Console.WriteLine("Best tolerance {0}, RMSE (LOO): {1}", optTol, looRMSE);
113    }
114
115    [TestMethod]
116    public void TestGAM() {
117      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider();
118      var problemData = provider.LoadData(provider.GetDataDescriptors().First(dd => dd.Name.Contains("Poly")));
119      // var provider = new HeuristicLab.Problems.Instances.DataAnalysis.RegressionRealWorldInstanceProvider();
120      // var problemData = provider.LoadData(provider.GetDataDescriptors().First(dd => dd.Name.Contains("Chem")));
121
122      var gam = new GAM();
123      gam.MaxIterations = 10;
124      gam.MaxInteractions = 3;
125      gam.Problem.ProblemData = problemData;
126      gam.Start();
127
128      var solution = (IRegressionSolution)gam.Results["Ensemble solution"].Value;
129
130      Console.WriteLine("RMSE (train) {0}", solution.TrainingRootMeanSquaredError);
131      Console.WriteLine("RMSE (test) {0}", solution.TestRootMeanSquaredError);
132    }
133
134  }
135}
Note: See TracBrowser for help on using the repository browser.