1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Problems.DataAnalysis;
|
---|
8 | using HeuristicLab.Random;
|
---|
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
|
---|
12 | //[TestClass()]
|
---|
13 | public class MctsSymbolicRegressionTest {
|
---|
14 | private const int Seed = 1234;
|
---|
15 | private IRandom rand;
|
---|
16 |
|
---|
17 | private const double SuccessThreshold = 0.99999;
|
---|
18 |
|
---|
19 | [TestInitialize]
|
---|
20 | public void InitTest() {
|
---|
21 | Console.Write("init called... ");
|
---|
22 | rand = new FastRandom(Seed);
|
---|
23 | }
|
---|
24 |
|
---|
25 | #region test structure search (no constants)
|
---|
26 | [TestMethod]
|
---|
27 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
28 | [TestProperty("Time", "short")]
|
---|
29 | public void MctsSymbReg_NoConstants_Nguyen1() {
|
---|
30 | // x³ + x² + x
|
---|
31 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
32 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 ")));
|
---|
33 | TestGrammarEnumeration(regProblem);
|
---|
34 | }
|
---|
35 | [TestMethod]
|
---|
36 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
37 | [TestProperty("Time", "short")]
|
---|
38 | public void MctsSymbReg_NoConstants_Nguyen2() {
|
---|
39 | // x^4 + x³ + x² + x
|
---|
40 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
41 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 ")));
|
---|
42 | TestGrammarEnumeration(regProblem);
|
---|
43 | }
|
---|
44 | [TestMethod]
|
---|
45 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
46 | [TestProperty("Time", "short")]
|
---|
47 | public void MctsSymbReg_NoConstants_Nguyen3() {
|
---|
48 | // x^5 + x^4 + x³ + x² + x
|
---|
49 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
50 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F3 ")));
|
---|
51 | TestGrammarEnumeration(regProblem);
|
---|
52 | }
|
---|
53 | [TestMethod]
|
---|
54 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
55 | [TestProperty("Time", "short")]
|
---|
56 | public void MctsSymbReg_NoConstants_Nguyen4() {
|
---|
57 | // x^6 + x^5 + x^4 + x³ + x² + x
|
---|
58 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
59 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F4 ")));
|
---|
60 | TestGrammarEnumeration(regProblem);
|
---|
61 | }
|
---|
62 |
|
---|
63 | [TestMethod]
|
---|
64 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
65 | [TestProperty("Time", "short")]
|
---|
66 | public void MctsSymbReg_NoConstants_Nguyen7() {
|
---|
67 | // log(x + 1) + log(x² + 1)
|
---|
68 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
69 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
|
---|
70 | TestGrammarEnumeration(regProblem);
|
---|
71 | }
|
---|
72 |
|
---|
73 | [TestMethod]
|
---|
74 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
75 | [TestProperty("Time", "short")]
|
---|
76 | public void MctsSymbReg_NoConstants_Poly10_Part1() {
|
---|
77 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
78 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
79 |
|
---|
80 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
81 | // Y' = X1*X2 + X3*X4 + X5*X6
|
---|
82 | // simplify problem by changing target
|
---|
83 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
84 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
85 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
86 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
87 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
88 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
89 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
90 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
91 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
92 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
93 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
94 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
95 | for (int i = 0; i < ys.Length; i++) {
|
---|
96 | ys[i] -= x1[i] * x7[i] * x9[i];
|
---|
97 | ys[i] -= x3[i] * x6[i] * x10[i];
|
---|
98 | }
|
---|
99 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
100 |
|
---|
101 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
102 |
|
---|
103 | TestGrammarEnumeration(modifiedProblemData);
|
---|
104 | }
|
---|
105 |
|
---|
106 | [TestMethod]
|
---|
107 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
108 | [TestProperty("Time", "short")]
|
---|
109 | public void MctsSymbReg_NoConstants_Poly10_Part2() {
|
---|
110 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
111 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
112 |
|
---|
113 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
114 | // Y' = X1*X7*X9 + X3*X6*X10
|
---|
115 | // simplify problem by changing target
|
---|
116 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
117 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
118 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
119 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
120 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
121 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
122 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
123 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
124 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
125 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
126 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
127 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
128 | for (int i = 0; i < ys.Length; i++) {
|
---|
129 | ys[i] -= x1[i] * x2[i];
|
---|
130 | ys[i] -= x3[i] * x4[i];
|
---|
131 | ys[i] -= x5[i] * x6[i];
|
---|
132 | }
|
---|
133 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
134 |
|
---|
135 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
136 |
|
---|
137 | TestGrammarEnumeration(modifiedProblemData);
|
---|
138 | }
|
---|
139 |
|
---|
140 | [TestMethod]
|
---|
141 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
142 | [TestProperty("Time", "short")]
|
---|
143 | public void MctsSymbReg_NoConstants_Poly10_Part3() {
|
---|
144 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
145 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
146 |
|
---|
147 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
148 | // Y' = X1*X2 + X1*X7*X9
|
---|
149 | // simplify problem by changing target
|
---|
150 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
151 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
152 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
153 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
154 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
155 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
156 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
157 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
158 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
159 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
160 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
161 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
162 | for (int i = 0; i < ys.Length; i++) {
|
---|
163 | ys[i] -= x3[i] * x4[i];
|
---|
164 | ys[i] -= x5[i] * x6[i];
|
---|
165 | ys[i] -= x3[i] * x6[i] * x10[i];
|
---|
166 | }
|
---|
167 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
168 |
|
---|
169 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
170 |
|
---|
171 | TestGrammarEnumeration(modifiedProblemData);
|
---|
172 | }
|
---|
173 |
|
---|
174 | [TestMethod]
|
---|
175 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
176 | [TestProperty("Time", "short")]
|
---|
177 | public void MctsSymbReg_NoConstants_Poly10_Part4() {
|
---|
178 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
179 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
180 |
|
---|
181 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
182 | // Y' = X3*X4 + X5*X6 + X3*X6*X10
|
---|
183 | // simplify problem by changing target
|
---|
184 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
185 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
186 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
187 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
188 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
189 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
190 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
191 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
192 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
193 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
194 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
195 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
196 | for (int i = 0; i < ys.Length; i++) {
|
---|
197 | ys[i] -= x1[i] * x2[i];
|
---|
198 | ys[i] -= x1[i] * x7[i] * x9[i];
|
---|
199 | }
|
---|
200 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
201 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
202 |
|
---|
203 |
|
---|
204 | TestGrammarEnumeration(modifiedProblemData);
|
---|
205 | }
|
---|
206 |
|
---|
207 | [TestMethod]
|
---|
208 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
209 | [TestProperty("Time", "short")]
|
---|
210 | public void MctsSymbReg_NoConstants_Poly10_Part5() {
|
---|
211 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
212 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
213 |
|
---|
214 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
215 | // Y' = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9
|
---|
216 | // simplify problem by changing target
|
---|
217 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
218 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
219 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
220 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
221 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
222 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
223 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
224 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
225 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
226 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
227 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
228 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
229 | for (int i = 0; i < ys.Length; i++) {
|
---|
230 | ys[i] -= x3[i] * x6[i] * x10[i];
|
---|
231 | }
|
---|
232 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
233 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
234 |
|
---|
235 |
|
---|
236 | TestGrammarEnumeration(modifiedProblemData);
|
---|
237 | }
|
---|
238 |
|
---|
239 | [TestMethod]
|
---|
240 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
241 | [TestProperty("Time", "short")]
|
---|
242 | public void MctsSymbReg_NoConstants_Poly10_Part6() {
|
---|
243 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
244 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
245 |
|
---|
246 | // Y = X1*X2 + X3*X4 + X5*X6 + X1*X7*X9 + X3*X6*X10
|
---|
247 | // Y' = X1*X2 + X3*X4 + X5*X6 + X3*X6*X10
|
---|
248 | // simplify problem by changing target
|
---|
249 | var ds = ((Dataset)regProblem.Dataset).ToModifiable();
|
---|
250 | var ys = ds.GetDoubleValues("Y").ToArray();
|
---|
251 | var x1 = ds.GetDoubleValues("X1").ToArray();
|
---|
252 | var x2 = ds.GetDoubleValues("X2").ToArray();
|
---|
253 | var x3 = ds.GetDoubleValues("X3").ToArray();
|
---|
254 | var x4 = ds.GetDoubleValues("X4").ToArray();
|
---|
255 | var x5 = ds.GetDoubleValues("X5").ToArray();
|
---|
256 | var x6 = ds.GetDoubleValues("X6").ToArray();
|
---|
257 | var x7 = ds.GetDoubleValues("X7").ToArray();
|
---|
258 | var x8 = ds.GetDoubleValues("X8").ToArray();
|
---|
259 | var x9 = ds.GetDoubleValues("X9").ToArray();
|
---|
260 | var x10 = ds.GetDoubleValues("X10").ToArray();
|
---|
261 | for (int i = 0; i < ys.Length; i++) {
|
---|
262 | ys[i] -= x1[i] * x7[i] * x9[i];
|
---|
263 | }
|
---|
264 | ds.ReplaceVariable("Y", ys.ToList());
|
---|
265 | var modifiedProblemData = new RegressionProblemData(ds, regProblem.AllowedInputVariables, regProblem.TargetVariable);
|
---|
266 |
|
---|
267 | TestGrammarEnumeration(modifiedProblemData);
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | [TestMethod]
|
---|
272 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
273 | [TestProperty("Time", "long")]
|
---|
274 | public void MctsSymbReg_NoConstants_Poly10_250rows() {
|
---|
275 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(Seed);
|
---|
276 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
|
---|
277 | regProblem.TrainingPartition.Start = 0;
|
---|
278 | regProblem.TrainingPartition.End = regProblem.Dataset.Rows;
|
---|
279 | regProblem.TestPartition.Start = 0;
|
---|
280 | regProblem.TestPartition.End = 2;
|
---|
281 | TestGrammarEnumeration(regProblem);
|
---|
282 | }
|
---|
283 |
|
---|
284 | [TestMethod]
|
---|
285 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
286 | [TestProperty("Time", "long")]
|
---|
287 | public void MctsSymbReg_NoConstants_Poly10_10000rows() {
|
---|
288 | // as poly-10 but more rows
|
---|
289 | var x1 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
290 | var x2 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
291 | var x3 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
292 | var x4 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
293 | var x5 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
294 | var x6 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
295 | var x7 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
296 | var x8 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
297 | var x9 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
298 | var x10 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
|
---|
299 | var ys = new List<double>();
|
---|
300 | for (int i = 0; i < x1.Count; i++) {
|
---|
301 | ys.Add(x1[i] * x2[i] + x3[i] * x4[i] + x5[i] * x6[i] + x1[i] * x7[i] * x9[i] + x3[i] * x6[i] * x10[i]);
|
---|
302 | }
|
---|
303 |
|
---|
304 | var ds = new Dataset(new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "y" },
|
---|
305 | new[] { x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, ys });
|
---|
306 |
|
---|
307 |
|
---|
308 | var problemData = new RegressionProblemData(ds, new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" }, "y");
|
---|
309 |
|
---|
310 | problemData.TrainingPartition.Start = 0;
|
---|
311 | problemData.TrainingPartition.End = problemData.Dataset.Rows;
|
---|
312 | problemData.TestPartition.Start = 0;
|
---|
313 | problemData.TestPartition.End = 2; // must not be empty
|
---|
314 |
|
---|
315 |
|
---|
316 | TestGrammarEnumeration(problemData);
|
---|
317 | }
|
---|
318 |
|
---|
319 | [TestMethod]
|
---|
320 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
321 | [TestProperty("Time", "short")]
|
---|
322 | public void MctsSymbReg_NoConstants_TwoVars() {
|
---|
323 |
|
---|
324 | // y = x1 + x2 + x1*x2 + x1*x2*x2 + x1*x1*x2
|
---|
325 | var x1 = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
326 | var x2 = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
327 | var ys = x1.Zip(x2, (x1i, x2i) => x1i + x2i + x1i * x2i + x1i * x2i * x2i + x1i * x1i * x2i).ToList();
|
---|
328 |
|
---|
329 | var ds = new Dataset(new string[] { "a", "b", "y" }, new[] { x1, x2, ys });
|
---|
330 | var problemData = new RegressionProblemData(ds, new string[] { "a", "b" }, "y");
|
---|
331 |
|
---|
332 | TestGrammarEnumeration(problemData);
|
---|
333 | }
|
---|
334 |
|
---|
335 | [TestMethod]
|
---|
336 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
337 | [TestProperty("Time", "short")]
|
---|
338 | public void MctsSymbReg_NoConstants_Misleading() {
|
---|
339 |
|
---|
340 | // y = a + baaaaa (the effect of the second term should be very small)
|
---|
341 | // the alg will quickly find that a has big effect and will search below a
|
---|
342 | // since we prevent a + a... the algorithm must find the correct expression via a + b...
|
---|
343 | // however b has a small effect so the branch might not be identified as relevant
|
---|
344 | var @as = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
345 | var bs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
346 | var cs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() * 1.0e-3).ToList();
|
---|
347 | var ds = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
348 | var es = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
|
---|
349 | var ys = new double[@as.Count];
|
---|
350 | for (int i = 0; i < ys.Length; i++)
|
---|
351 | ys[i] = @as[i] + bs[i] + @as[i] * bs[i] * cs[i];
|
---|
352 |
|
---|
353 | var dataset = new Dataset(new string[] { "a", "b", "c", "d", "e", "y" }, new[] { @as, bs, cs, ds, es, ys.ToList() });
|
---|
354 |
|
---|
355 | var problemData = new RegressionProblemData(dataset, new string[] { "a", "b", "c", "d", "e" }, "y");
|
---|
356 |
|
---|
357 | TestGrammarEnumeration(problemData);
|
---|
358 | }
|
---|
359 | #endregion
|
---|
360 |
|
---|
361 | #region restricted structure but including numeric constants
|
---|
362 |
|
---|
363 | [TestMethod]
|
---|
364 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
365 | [TestProperty("Time", "short")]
|
---|
366 | public void MctsSymbRegKeijzer7() {
|
---|
367 | // ln(x)
|
---|
368 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
369 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 7 f(")));
|
---|
370 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
371 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
372 | TestGrammarEnumeration(regProblem);
|
---|
373 | }
|
---|
374 |
|
---|
375 |
|
---|
376 | [TestMethod]
|
---|
377 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
378 | [TestProperty("Time", "short")]
|
---|
379 | public void MctsSymbRegBenchmarkNguyen5() {
|
---|
380 | // sin(x²)cos(x) - 1
|
---|
381 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
|
---|
382 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F5 ")));
|
---|
383 | TestGrammarEnumeration(regProblem);
|
---|
384 | }
|
---|
385 |
|
---|
386 | [TestMethod]
|
---|
387 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
388 | [TestProperty("Time", "short")]
|
---|
389 | public void MctsSymbRegBenchmarkNguyen6() {
|
---|
390 | // sin(x) + sin(x + x²)
|
---|
391 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
|
---|
392 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F6 ")));
|
---|
393 | TestGrammarEnumeration(regProblem);
|
---|
394 | }
|
---|
395 |
|
---|
396 | [TestMethod]
|
---|
397 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
398 | [TestProperty("Time", "short")]
|
---|
399 | public void MctsSymbRegBenchmarkNguyen7() {
|
---|
400 | // log(x + 1) + log(x² + 1)
|
---|
401 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
402 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
|
---|
403 | TestGrammarEnumeration(regProblem);
|
---|
404 | }
|
---|
405 | [TestMethod]
|
---|
406 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
407 | [TestProperty("Time", "short")]
|
---|
408 | public void MctsSymbRegBenchmarkNguyen8() {
|
---|
409 | // Sqrt(x)
|
---|
410 | // = x ^ 0.5
|
---|
411 | // = exp(0.5 * log(x))
|
---|
412 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
413 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F8 ")));
|
---|
414 | TestGrammarEnumeration(regProblem);
|
---|
415 | }
|
---|
416 |
|
---|
417 | // [TestMethod]
|
---|
418 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
419 | [TestProperty("Time", "short")]
|
---|
420 | public void MctsSymbRegBenchmarkNguyen9() {
|
---|
421 | // sin(x) + sin(y²)
|
---|
422 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
|
---|
423 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F9 ")));
|
---|
424 | TestGrammarEnumeration(regProblem);
|
---|
425 | }
|
---|
426 |
|
---|
427 | // [TestMethod]
|
---|
428 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
429 | [TestProperty("Time", "short")]
|
---|
430 | public void MctsSymbRegBenchmarkNguyen10() {
|
---|
431 | // 2sin(x)cos(y)
|
---|
432 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider();
|
---|
433 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F10 ")));
|
---|
434 | TestGrammarEnumeration(regProblem);
|
---|
435 | }
|
---|
436 |
|
---|
437 | [TestMethod]
|
---|
438 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
439 | [TestProperty("Time", "short")]
|
---|
440 | public void MctsSymbRegBenchmarkNguyen11() {
|
---|
441 | // x ^ y , x > 0, y > 0
|
---|
442 | // = exp(y * log(x))
|
---|
443 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
444 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F11 ")));
|
---|
445 | TestGrammarEnumeration(regProblem);
|
---|
446 | }
|
---|
447 | [TestMethod]
|
---|
448 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
449 | [TestProperty("Time", "short")]
|
---|
450 | public void MctsSymbRegBenchmarkNguyen12() {
|
---|
451 | // x^4 - x³ + y²/2 - y
|
---|
452 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
|
---|
453 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F12 ")));
|
---|
454 | TestGrammarEnumeration(regProblem);
|
---|
455 | }
|
---|
456 |
|
---|
457 | #endregion
|
---|
458 |
|
---|
459 | #region keijzer
|
---|
460 | [TestMethod]
|
---|
461 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
462 | [TestProperty("Time", "long")]
|
---|
463 | public void MctsSymbRegBenchmarkKeijzer5() {
|
---|
464 | // (30 * x * z) / ((x - 10) * y²)
|
---|
465 | // = 30 x z / (xy² - y²)
|
---|
466 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
467 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 5 f(")));
|
---|
468 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
469 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
470 | TestGrammarEnumeration(regProblem);
|
---|
471 | }
|
---|
472 |
|
---|
473 | [TestMethod]
|
---|
474 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
475 | [TestProperty("Time", "short")]
|
---|
476 | public void MctsSymbRegBenchmarkKeijzer6() {
|
---|
477 | // Keijzer 6 f(x) = Sum(1 / i) From 1 to X , x \in [0..120]
|
---|
478 | // we can only approximate this
|
---|
479 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
480 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 6 f(")));
|
---|
481 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
482 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
483 | TestGrammarEnumeration(regProblem);
|
---|
484 | }
|
---|
485 |
|
---|
486 | [TestMethod]
|
---|
487 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
488 | [TestProperty("Time", "short")]
|
---|
489 | public void MctsSymbRegBenchmarkKeijzer8() {
|
---|
490 | // sqrt(x)
|
---|
491 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
492 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 8 f(")));
|
---|
493 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
494 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
495 | TestGrammarEnumeration(regProblem);
|
---|
496 | }
|
---|
497 |
|
---|
498 | [TestMethod]
|
---|
499 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
500 | [TestProperty("Time", "short")]
|
---|
501 | public void MctsSymbRegBenchmarkKeijzer9() {
|
---|
502 | // arcsinh(x) i.e. ln(x + sqrt(x² + 1))
|
---|
503 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
504 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 9 f(")));
|
---|
505 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
506 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
507 | TestGrammarEnumeration(regProblem);
|
---|
508 | }
|
---|
509 |
|
---|
510 | [TestMethod]
|
---|
511 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
512 | [TestProperty("Time", "short")]
|
---|
513 | public void MctsSymbRegBenchmarkKeijzer11() {
|
---|
514 | // xy + sin( (x-1) (y-1) )
|
---|
515 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider();
|
---|
516 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 11 f(")));
|
---|
517 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
518 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
519 | TestGrammarEnumeration(regProblem);
|
---|
520 | }
|
---|
521 |
|
---|
522 | [TestMethod]
|
---|
523 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
524 | [TestProperty("Time", "short")]
|
---|
525 | public void MctsSymbRegBenchmarkKeijzer12() {
|
---|
526 | // x^4 - x³ + y² / 2 - y, same as Nguyen 12
|
---|
527 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
528 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 12 f(")));
|
---|
529 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
530 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
531 | TestGrammarEnumeration(regProblem);
|
---|
532 | }
|
---|
533 |
|
---|
534 | [TestMethod]
|
---|
535 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
536 | [TestProperty("Time", "short")]
|
---|
537 | public void MctsSymbRegBenchmarkKeijzer14() {
|
---|
538 | // 8 / (2 + x² + y²)
|
---|
539 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
540 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 14 f(")));
|
---|
541 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
542 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
543 | TestGrammarEnumeration(regProblem);
|
---|
544 | }
|
---|
545 |
|
---|
546 | [TestMethod]
|
---|
547 | [TestCategory("Algorithms.DataAnalysis")]
|
---|
548 | [TestProperty("Time", "short")]
|
---|
549 | public void MctsSymbRegBenchmarkKeijzer15() {
|
---|
550 | // x³ / 5 + y³ / 2 - y - x
|
---|
551 | var provider = new HeuristicLab.Problems.Instances.DataAnalysis.KeijzerInstanceProvider(Seed);
|
---|
552 | var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Keijzer 15 f(")));
|
---|
553 | // some Keijzer problem instances have very large test partitions (here we are not concerened about test performance)
|
---|
554 | if (regProblem.TestPartition.End - regProblem.TestPartition.Start > 1000) regProblem.TestPartition.End = regProblem.TestPartition.Start + 1000;
|
---|
555 | TestGrammarEnumeration(regProblem);
|
---|
556 | }
|
---|
557 | #endregion
|
---|
558 |
|
---|
559 | private void TestGrammarEnumeration(IRegressionProblemData problemData)
|
---|
560 | {
|
---|
561 | // Configure algorithm and problem
|
---|
562 | var alg = new GrammarEnumerationAlgorithm();
|
---|
563 | var problem = new RegressionProblem();
|
---|
564 | alg.Problem = problem;
|
---|
565 | problem.ProblemData = problemData;
|
---|
566 |
|
---|
567 | // Run
|
---|
568 | alg.Start();
|
---|
569 |
|
---|
570 | // Evaluate results
|
---|
571 | var eps = 1.0 - SuccessThreshold;
|
---|
572 |
|
---|
573 | // Check if algorithm terminated correctly
|
---|
574 | Assert.IsTrue(alg.Results.ContainsKey("Best solution quality (training)"), "No training solution returned!");
|
---|
575 | Assert.IsTrue(alg.Results.ContainsKey("Best solution quality (test)"), "No test solution returned!");
|
---|
576 |
|
---|
577 | // Check resultss
|
---|
578 | Assert.AreEqual(1.0, ((DoubleValue)alg.Results["Best solution quality (training)"].Value).Value, eps, "Training quality too low!");
|
---|
579 | Assert.AreEqual(1.0, ((DoubleValue)alg.Results["Best solution quality (test)"].Value).Value, eps, "Test quality too low!");
|
---|
580 | }
|
---|
581 | }
|
---|
582 | }
|
---|