Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3022-FastFunctionExtraction/UnitTests/BasisFunctionUtilsTests.cs @ 18156

Last change on this file since 18156 was 17779, checked in by gkronber, 4 years ago

#3022: made a few changes while reviewing the code.

File size: 8.6 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Algorithms.DataAnalysis.FastFunctionExtraction;
4using HeuristicLab.Data;
5using HeuristicLab.Problems.DataAnalysis;
6using Microsoft.VisualStudio.TestTools.UnitTesting;
7
8namespace UnitTests {
9
10    [TestClass]
11    public class BasisFunctionUtilsTests {
12        private static readonly HashSet<double> defaultExponents = new HashSet<double> { 0.5, 1.0, 2.0 };
13        private static readonly HashSet<NonlinearOperator> defaultNonlinFuncs = new HashSet<NonlinearOperator> { NonlinearOperator.Abs, NonlinearOperator.Log, NonlinearOperator.Sin, NonlinearOperator.Cos };
14        private static readonly Approach defaultApproach = new Approach(false, false, false, false, false, defaultExponents, defaultNonlinFuncs, 10, 1, 0.2, 0.8, 5);
15        private static readonly IDataset defaultDataset = new Dataset(new string[] { "x1", "x2", "x3", "y" }, new double[,] {
16            { 1, 1, -3, 7 },
17            { 2, 3, -1, 12 },
18            { 3, -3, 0, 0 },
19            { 4, -2, 1, -8 },
20            { 15, -8, 5, 2 }
21        });
22
23        [TestMethod]
24        public void CreateBasisFunctions_ZeroFeatures_AllFalse() {
25            var data = new RegressionProblemData(defaultDataset, new string[] { }, "y");
26            var basisFunctions = BFUtils.CreateBasisFunctions(data, defaultApproach);
27            Assert.AreEqual(basisFunctions.Count(), 0);
28        }
29
30        [TestMethod]
31        public void CreateBasisFunctions_TwoFeatures_All_False() {
32            var data = new RegressionProblemData(defaultDataset, new string[] { "x1", "x2" }, "y");
33            var basisFunctions = BFUtils.CreateBasisFunctions(data, defaultApproach);
34            Assert.AreEqual(basisFunctions.Count(), 2);
35            var bf = basisFunctions.Last();
36            Assert.AreEqual(bf, new SimpleBasisFunction("x2"));
37        }
38
39        [TestMethod]
40        public void CreateBasisFunctions_TwoFeatures_AllowExponents() {
41            var appr = defaultApproach;
42            appr.AllowExp = true;
43            appr.Exponents = defaultExponents;
44            var data = new RegressionProblemData(defaultDataset, new string[] { "x1", "x2" }, "y");
45            var basisFunctions = BFUtils.CreateBasisFunctions(data, appr);
46            // -1 because you sqrt("x2") is not valid
47            Assert.AreEqual(2 * appr.Exponents.Count - 1, basisFunctions.Count());
48            var bf = basisFunctions.Last();
49            Assert.AreEqual(bf, new SimpleBasisFunction("x2", exponent: defaultExponents.Last()));
50        }
51
52        [TestMethod]
53        public void CreateSimpleBases_NoFeatures() {
54            var data = new RegressionProblemData(defaultDataset, new string[] { }, "y");
55            var bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1, 2, 0.5 }, new HashSet<NonlinearOperator> { NonlinearOperator.Abs }).ToArray();
56            // num_features * num_exponents * num_nonlinearOperators
57            Assert.AreEqual(0 * 3 * 1, bfs.Length);
58        }
59
60        [TestMethod]
61        public void CreateSimpleBases_NoExponents() {
62            var data = new RegressionProblemData(defaultDataset, new string[] { "x1" }, "y");
63            var bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { }, new HashSet<NonlinearOperator> { NonlinearOperator.None }).ToArray();
64            // num_features * num_exponents * num_nonlinearOperators
65            Assert.AreEqual(1 * 0 * 1, bfs.Length);
66
67            bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1 }, new HashSet<NonlinearOperator> { }).ToArray();
68            Assert.AreEqual(1 * 1 * 0, bfs.Length);
69
70            bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1 }, new HashSet<NonlinearOperator> { NonlinearOperator.None }).ToArray();
71            Assert.AreEqual(1 * 1 * 1, bfs.Length);
72            Assert.AreEqual(bfs.First(), new SimpleBasisFunction(feature: "x1", exponent: 1, op: NonlinearOperator.None, isNominator: true));
73
74            bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 2, 3 }, new HashSet<NonlinearOperator> { NonlinearOperator.None, NonlinearOperator.Log }).ToArray();
75            Assert.AreEqual(1 * 2 * 2, bfs.Length);
76            Assert.AreEqual(bfs.First(), new SimpleBasisFunction(feature: "x1", exponent: 2, op: NonlinearOperator.None, isNominator: true));
77        }
78
79        [TestMethod]
80        public void CreateSimpleBases_NoNonlinearOperator() {
81            var data = new RegressionProblemData(defaultDataset, new string[] { "x1", "x2" }, "y");
82            var bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1, 2, 3 }, new HashSet<NonlinearOperator> { }).ToArray();
83            // num_features * num_exponents * num_nonlinearOperators
84            Assert.AreEqual(2 * 3 * 0, bfs.Length);
85        }
86
87        [TestMethod]
88        public void CreateSimpleBases_ManyFeatures() {
89            var data = new RegressionProblemData(defaultDataset, new string[] { "x1", "x2" }, "y");
90            var bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1, 2, 3 }, new HashSet<NonlinearOperator> { NonlinearOperator.None }).ToArray();
91            // num_features * num_exponents * (num_operators + 1)
92            Assert.AreEqual(2 * 3 * 1, bfs.Length);
93        }
94
95        [TestMethod]
96        public void CreateHingeBases() {
97            var data = new RegressionProblemData(defaultDataset, new string[] { "x1", "x2" }, "y");
98            var bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1, 2 }, new HashSet<NonlinearOperator> { NonlinearOperator.None, NonlinearOperator.Sin }).ToArray();
99
100            var numSimpleBasisFuncs = bfs.Length;
101            int num_thrs = 2;
102            var hingeBases = BFUtils.CreateHingeBases(data, bfs, 0, 1, num_thrs).ToArray();
103            Assert.AreEqual(2 * numSimpleBasisFuncs * num_thrs, hingeBases.Length);
104        }
105
106        [TestMethod]
107        public void CreateHingeBases_CheckOtherBFValues() {
108            var data = new RegressionProblemData(defaultDataset, new string[] { "x3" }, "y");
109            var bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1 }, new HashSet<NonlinearOperator> { NonlinearOperator.None }).ToArray();
110            Assert.AreEqual(1, bfs.Length);
111
112            int num_thrs = 2;
113            var partition = new IntRange(0, data.Dataset.Rows);
114            var hingeBases = BFUtils.CreateHingeBases(data, bfs, 0, 1, num_thrs, partition).ToArray();
115            Assert.AreEqual(2 * 1 * num_thrs, hingeBases.Length);
116            foreach(var hingeBase in hingeBases) {
117                Assert.AreEqual(hingeBase.Feature, "x3");
118                Assert.AreEqual(hingeBase.Exponent, 1);
119                Assert.AreEqual(hingeBase.IsDenominator, true);
120            }
121        }
122
123        [TestMethod]
124        public void CreateHingeBases_CheckArbitraryStartThrAndEndThr() {
125            var data = new RegressionProblemData(defaultDataset, new string[] { "x3" }, "y");
126            var bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1 }, new HashSet<NonlinearOperator> { NonlinearOperator.None }).ToArray();
127            Assert.AreEqual(1, bfs.Length);
128
129            int num_thrs = 2;
130            var partition = new IntRange(0, data.Dataset.Rows);
131            var hingeBases = BFUtils.CreateHingeBases(data, bfs, 0.5, 0.75, num_thrs, partition).ToArray();
132            Assert.AreEqual(2 * num_thrs, hingeBases.Length);
133            Assert.AreEqual(hingeBases.Count(hingeBase => hingeBase.Threshold == 1), 2);
134            Assert.AreEqual(hingeBases.Count(hingeBase => hingeBase.Threshold == 3), 2);
135        }
136
137        [TestMethod]
138        public void CreateHingeBases_CheckManyThrs() {
139            var data = new RegressionProblemData(defaultDataset, new string[] { "x3" }, "y");
140            var bfs = BFUtils.CreateSimpleBases(data, new HashSet<double> { 1 }, new HashSet<NonlinearOperator> { NonlinearOperator.None }).ToArray();
141            Assert.AreEqual(1, bfs.Length);
142
143            int num_thrs = 9;
144            var partition = new IntRange(0, data.Dataset.Rows);
145
146            // i.e. from -11 to 13
147            double start_thr = -1;
148            double end_thr = 2;
149
150            var hingeBases = BFUtils.CreateHingeBases(data, bfs, start_thr, end_thr, num_thrs, partition).ToArray();
151            Assert.AreEqual(2 * num_thrs, hingeBases.Length);
152            Assert.AreEqual(hingeBases.Count(hingeBase => hingeBase.Threshold == -11), 2);
153            Assert.AreEqual(hingeBases.Count(hingeBase => hingeBase.Threshold == -8), 2);
154            Assert.AreEqual(hingeBases.Count(hingeBase => hingeBase.Threshold == 13), 2);
155        }
156    }
157}
Note: See TracBrowser for help on using the repository browser.