[17738] | 1 | using HeuristicLab.Algorithms.DataAnalysis.FastFunctionExtraction;
|
---|
| 2 | using HeuristicLab.Common;
|
---|
| 3 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 5 | using System;
|
---|
| 6 | using System.Linq;
|
---|
| 7 |
|
---|
| 8 | namespace UnitTests {
|
---|
| 9 |
|
---|
| 10 | [TestClass]
|
---|
| 11 | public class FFXModelTests {
|
---|
| 12 | private static readonly IDataset defaultDataset = new Dataset(new string[] { "x1", "x2", "x3", "y" }, new double[,] {
|
---|
| 13 | { 1, 1, 3, 7 },
|
---|
| 14 | { 2, 3, 1, 12 },
|
---|
| 15 | { 3, -3, 0, 0 },
|
---|
| 16 | { 4, -2, 1, 8 },
|
---|
| 17 | { 5, -8, 5, 2 }
|
---|
| 18 | });
|
---|
| 19 |
|
---|
| 20 | [TestMethod]
|
---|
| 21 | public void Simulate_MultipleNominatorTerms() {
|
---|
| 22 | double[] expr(double[] x1, double[] x2) {
|
---|
| 23 | return Enumerable.Range(0, x1.Length).Select(i =>
|
---|
| 24 | (-0.5 + 1.5 * Math.Pow(x1[i], 2) + 0.5 * Math.Abs(x2[i]))
|
---|
| 25 | ).ToArray();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | var data = new RegressionProblemData(defaultDataset, new string[] { "x1", "x2" }, "y");
|
---|
| 30 | var basisFunctions = new (double, IBasisFunction)[2];
|
---|
| 31 |
|
---|
| 32 | // model: -0.5 + 1.5 * x1^2 + 0.5* abs(x2)
|
---|
| 33 | double intercept = -0.5;
|
---|
| 34 | basisFunctions[0] = (1.5, new SimpleBasisFunction("x1", 2, NonlinearOperator.None, true));
|
---|
| 35 | basisFunctions[1] = (0.5, new SimpleBasisFunction("x2", 1, NonlinearOperator.Abs, true));
|
---|
| 36 | var model = new FFXModel(intercept, basisFunctions);
|
---|
| 37 |
|
---|
| 38 | var expectedVals = expr(
|
---|
| 39 | data.Dataset.GetDoubleValues("x1").ToArray(),
|
---|
| 40 | data.Dataset.GetDoubleValues("x2").ToArray()
|
---|
| 41 | );
|
---|
| 42 | var calculatedVals = model.Simulate(data, data.AllIndices);
|
---|
| 43 | for (int i = 0; i < calculatedVals.Length; i++) {
|
---|
| 44 | Assert.IsTrue(expectedVals[i].IsAlmost(calculatedVals[i]));
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | [TestMethod]
|
---|
| 49 | public void Simulate_MultipleDenominatorTerms() {
|
---|
| 50 | double[] expr(double[] x1, double[] x2, double[] x3) {
|
---|
| 51 | return Enumerable.Range(0, x1.Length).Select(i =>
|
---|
| 52 | (-0.5 + 1.5 * Math.Abs(x1[i])) / (1 + 1.5 * Math.Pow(x2[i], 2) - 0.3 * x3[i])
|
---|
| 53 | ).ToArray();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | var data = new RegressionProblemData(defaultDataset, new string[] { "x1", "x2", "x3" }, "y");
|
---|
| 57 | var basisFunctions = new (double, IBasisFunction)[3];
|
---|
| 58 |
|
---|
| 59 | // model: (-0.5 + 1.5 * abs(x1)) / (1 + 1.5 * x2^2 - 0.3 * x3)
|
---|
| 60 | double intercept = -0.5;
|
---|
| 61 | basisFunctions[0] = (1.5, new SimpleBasisFunction("x1", 1, NonlinearOperator.Abs, true));
|
---|
| 62 | basisFunctions[1] = (1.5, new SimpleBasisFunction("x2", 2, NonlinearOperator.None, false));
|
---|
| 63 | basisFunctions[2] = (-0.3, new SimpleBasisFunction("x3", 1, NonlinearOperator.None, false));
|
---|
| 64 |
|
---|
| 65 | var model = new FFXModel(intercept, basisFunctions);
|
---|
| 66 |
|
---|
| 67 | var calculatedVals = model.Simulate(data, data.AllIndices);
|
---|
| 68 | var expectedVals = expr(
|
---|
| 69 | data.Dataset.GetDoubleValues("x1").ToArray(),
|
---|
| 70 | data.Dataset.GetDoubleValues("x2").ToArray(),
|
---|
| 71 | data.Dataset.GetDoubleValues("x3").ToArray()
|
---|
| 72 | ).ToArray();
|
---|
| 73 | for (int i = 0; i < expectedVals.Length; i++) {
|
---|
| 74 | Assert.IsTrue(expectedVals[i].IsAlmost(calculatedVals[i]));
|
---|
| 75 | }
|
---|
| 76 | // TODO
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|