Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2990_VariableImpactBasedFeatureSelection/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/IntervalInterpreterTest.cs

Last change on this file was 16422, checked in by fholzing, 6 years ago

#2904: reintegrated branch

File size: 4.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using Microsoft.VisualStudio.TestTools.UnitTesting;
4
5namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
6  [TestClass]
7  public class IntervalInterpreterTest {
8    private IRegressionProblemData problemData;
9    private Dictionary<string, Interval> variableRanges;
10
11    [TestInitialize]
12    public void InitTest() {
13      double[,] arr = new double[4, 3];
14
15      arr[0, 0] = 3;
16      arr[0, 1] = 6;
17      arr[0, 2] = 2;
18      arr[1, 0] = 5;
19      arr[1, 1] = 2;
20      arr[1, 2] = 1;
21      arr[2, 0] = 8;
22      arr[2, 1] = 5;
23      arr[2, 2] = 0;
24      arr[3, 0] = 3;
25      arr[3, 1] = 4;
26      arr[3, 2] = 2;
27
28      var ds = new Dataset(new string[] { "x1", "x2", "y" }, arr);
29      problemData = (IRegressionProblemData)new RegressionProblemData(ds, new string[] { "x1", "x2" }, "y");
30
31      variableRanges = new Dictionary<string, Interval>();
32      variableRanges.Add("x1", new Interval(1, 10));
33      variableRanges.Add("x2", new Interval(4, 6));
34    }
35
36    private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null) {
37      var parser = new InfixExpressionParser();
38      var tree = parser.Parse(expression);
39      var interpreter = new IntervalInterpreter();
40      Interval result;
41      if (variableRanges == null)
42        result = interpreter.GetSymbolicExressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices);
43      else
44        result = interpreter.GetSymbolicExressionTreeInterval(tree, variableRanges);
45
46      Assert.AreEqual(expectedResult, result);
47    }
48
49
50    [TestMethod]
51    [TestCategory("Problems.DataAnalysis.Symbolic")]
52    [TestProperty("Time", "short")]
53    public void TestIntervalInterpreterAdd() {
54      EvaluateTest("x1 + x2", new Interval(5, 14));
55      EvaluateTest("x1 + x2", new Interval(5, 16), variableRanges);
56    }
57
58    [TestMethod]
59    [TestCategory("Problems.DataAnalysis.Symbolic")]
60    [TestProperty("Time", "short")]
61    public void TestIntervalInterpreterLogAdd() {
62      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(14)));
63      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(16)), variableRanges);
64    }
65
66    [TestMethod]
67    [TestCategory("Problems.DataAnalysis.Symbolic")]
68    [TestProperty("Time", "short")]
69    public void TestIntervalInterpreterLogAddMul() {
70      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(11), Math.Log(30)));
71      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(7), Math.Log(36)), variableRanges);
72    }
73
74    [TestMethod]
75    [TestCategory("Problems.DataAnalysis.Symbolic")]
76    [TestProperty("Time", "short")]
77    public void TestIntervalInterpreterSin() {
78      EvaluateTest("sin(x1+x2)", new Interval(-1, 1));
79      EvaluateTest("sin(x1+x2)", new Interval(-1, 1), variableRanges);
80      EvaluateTest("sin(1+2)", new Interval(Math.Sin(3), Math.Sin(3)));
81    }
82
83    [TestMethod]
84    [TestCategory("Problems.DataAnalysis.Symbolic")]
85    [TestProperty("Time", "short")]
86    public void TestIntervalInterpreterCos() {
87      EvaluateTest("cos(x1+x2)", new Interval(-1, 1));
88      EvaluateTest("cos(x1+x2)", new Interval(-1, 1), variableRanges);
89      EvaluateTest("cos(1+2)", new Interval(Math.Sin(3 - Math.PI / 2), Math.Sin(3 - Math.PI / 2)));
90    }
91
92    [TestMethod]
93    [TestCategory("Problems.DataAnalysis.Symbolic")]
94    [TestProperty("Time", "short")]
95    public void TestIntervalInterpreterExp() {
96      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-3), Math.Exp(6)));
97      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-5), Math.Exp(6)), variableRanges);
98    }
99
100    [TestMethod]
101    [TestCategory("Problems.DataAnalysis.Symbolic")]
102    [TestProperty("Time", "short")]
103    public void TestIntervalInterpreterExpRoot() {
104      EvaluateTest("exp(root(x1*x2, 2))", new Interval(Math.Exp(Math.Sqrt(6)), Math.Exp(Math.Sqrt(48))));
105      EvaluateTest("exp(root(x1*x2, 2))", new Interval(Math.Exp(Math.Sqrt(4)), Math.Exp(Math.Sqrt(60))), variableRanges);
106    }
107
108    [TestMethod]
109    [TestCategory("Problems.DataAnalysis.Symbolic")]
110    [TestProperty("Time", "short")]
111    public void TestIntervalInterpreterPower() {
112      EvaluateTest("pow(x1, 2)", new Interval(Math.Pow(3, 1), Math.Pow(8, 3)));
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.