source: branches/2994-AutoDiffForIntervals/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/AutoDiffIntervalInterpreterTest.cs @ 17288

Last change on this file since 17288 was 17288, checked in by gkronber, 3 years ago

#2994: fixed a bug in a test case

File size: 9.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
7  [TestClass]
8  public class AutoDiffIntervalInterpreterTest {
9    private IRegressionProblemData problemData;
10    private IDictionary<string, Interval> variableRanges;
11
12    [TestInitialize]
13    public void InitTest() {
14      double[,] arr = new double[4, 3];
15
16      arr[0, 0] = 3;
17      arr[0, 1] = 6;
18      arr[0, 2] = 2;
19      arr[1, 0] = 5;
20      arr[1, 1] = 2;
21      arr[1, 2] = 1;
22      arr[2, 0] = 8;
23      arr[2, 1] = 5;
24      arr[2, 2] = 0;
25      arr[3, 0] = 3;
26      arr[3, 1] = 4;
27      arr[3, 2] = 2;
28
29      // intervals for dataset
30      // x1: 3 .. 8
31      // x2: 2 .. 6
32
33      var ds = new Dataset(new string[] { "x1", "x2", "y" }, arr);
34      problemData = (IRegressionProblemData)new RegressionProblemData(ds, new string[] { "x1", "x2" }, "y");
35
36
37      variableRanges = new Dictionary<string, Interval>();
38      variableRanges.Add("x1", new Interval(1, 10));
39      variableRanges.Add("x2", new Interval(4, 6));
40    }
41
42    private void EvaluateTest(string expression, Interval expectedResult, IDictionary<string, Interval> variableRanges = null, double lowerDelta = 1e-6, double upperDelta = 1e-6) {
43      var parser = new InfixExpressionParser();
44      var tree = parser.Parse(expression);
45      var interpreter = new IntervalEvaluator();
46      Interval result;
47      if (variableRanges == null) variableRanges = problemData.VariableRanges.GetIntervals();
48      result = interpreter.Evaluate(tree, variableRanges, new ISymbolicExpressionTreeNode[0], out double[] _, out double[] __);
49
50      Assert.AreEqual(expectedResult.LowerBound, result.LowerBound, lowerDelta);
51      Assert.AreEqual(expectedResult.UpperBound, result.UpperBound, upperDelta);
52    }
53
54
55    [TestMethod]
56    [TestCategory("Problems.DataAnalysis.Symbolic")]
57    [TestProperty("Time", "short")]
58    public void TestIntervalInterpreterAdd() {
59      EvaluateTest("x1 + x2", new Interval(5, 14));
60      EvaluateTest("x1 + x2", new Interval(5, 16), variableRanges);
61    }
62
63    [TestMethod]
64    [TestCategory("Problems.DataAnalysis.Symbolic")]
65    [TestProperty("Time", "short")]
66    public void TestIntervalInterpreterLogAdd() {
67      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(14)));
68      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(16)), variableRanges);
69    }
70
71    [TestMethod]
72    [TestCategory("Problems.DataAnalysis.Symbolic")]
73    [TestProperty("Time", "short")]
74    public void TestIntervalInterpreterLogAddMul() {
75      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(11), Math.Log(30)));
76      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(7), Math.Log(36)), variableRanges);
77    }
78
79    [TestMethod]
80    [TestCategory("Problems.DataAnalysis.Symbolic")]
81    [TestProperty("Time", "short")]
82    public void TestIntervalInterpreterSin() {
83      EvaluateTest("sin(x1+x2)", new Interval(-1, 1));
84      EvaluateTest("sin(x1+x2)", new Interval(-1, 1), variableRanges);
85      EvaluateTest("sin(1+2)", new Interval(Math.Sin(3), Math.Sin(3)));
86
87      var localVarRanges = new Dictionary<string, Interval>();
88      localVarRanges.Add("x1", new Interval(-1, 1));
89      localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
90      localVarRanges.Add("x3", new Interval(0, Math.PI / 2));
91      localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI));
92      localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4));
93
94      EvaluateTest("sin(x1)", new Interval(Math.Sin(-1), Math.Sin(1)), localVarRanges, 1E-8, 1E-8);
95      EvaluateTest("sin(x2)", new Interval(-1, 0), localVarRanges, 1E-8, 1E-8);
96      EvaluateTest("sin(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
97      EvaluateTest("sin(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8);
98      EvaluateTest("sin(x5)", new Interval(Math.Sin(Math.PI / 4), 1), localVarRanges, 1E-8, 1E-8);
99    }
100
101    [TestMethod]
102    [TestCategory("Problems.DataAnalysis.Symbolic")]
103    [TestProperty("Time", "short")]
104    public void TestIntervalInterpreterCos() {
105      EvaluateTest("cos(x1+x2)", new Interval(-1, 1));
106      EvaluateTest("cos(x1+x2)", new Interval(-1, 1), variableRanges);
107      EvaluateTest("cos(1+2)", new Interval(Math.Cos(3), Math.Cos(3)));
108
109      var localVarRanges = new Dictionary<string, Interval>();
110      localVarRanges.Add("x1", new Interval(-1, 1));
111      localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
112      localVarRanges.Add("x3", new Interval(0, Math.PI / 2));
113      localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI));
114      localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4));
115
116      EvaluateTest("cos(x1)", new Interval(Math.Cos(-1), 1), localVarRanges, 1E-8, 1E-8);
117      EvaluateTest("cos(x2)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
118      EvaluateTest("cos(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
119      EvaluateTest("cos(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8);
120      EvaluateTest("cos(x5)", new Interval(Math.Cos(Math.PI * 3.0 / 4), Math.Cos(Math.PI / 4)), localVarRanges, 1E-8, 1E-8);
121
122    }
123
124    [TestMethod]
125    [TestCategory("Problems.DataAnalysis.Symbolic")]
126    [TestProperty("Time", "short")]
127    public void TestIntervalInterpreterTan() {
128      throw new NotSupportedException();
129      // critical values:
130      // lim tan(x) = -inf for x => -pi/2
131      // lim tan(x) = +inf for x =>  pi/2
132      var variableRanges = new Dictionary<string, Interval>();
133      variableRanges.Add("x1", new Interval(-1, 1));
134      variableRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
135      variableRanges.Add("x3", new Interval(0, Math.PI / 2));
136      variableRanges.Add("x4", new Interval(-Math.PI, Math.PI));
137
138      EvaluateTest("tan(x1)", new Interval(Math.Tan(-1), Math.Tan(1)), variableRanges, 1E-8, 1E-8);
139      EvaluateTest("tan(x2)", new Interval(double.NegativeInfinity, 0), variableRanges, 0, 1E-8);
140      EvaluateTest("tan(x3)", new Interval(0, 8.16588936419192E+15), variableRanges, 0, 1E6); // actually upper bound should be infinity.
141      EvaluateTest("tan(x4)", new Interval(double.NegativeInfinity, double.PositiveInfinity), variableRanges);
142    }
143
144    [TestMethod]
145    [TestCategory("Problems.DataAnalysis.Symbolic")]
146    [TestProperty("Time", "short")]
147    public void TestIntervalInterpreterTanh() {
148      // critical values:
149      // lim tanh(x) = -1 for x => -inf
150      // lim tanh(x) =  1 for x =>  inf
151      var variableRanges = new Dictionary<string, Interval>();
152      variableRanges.Add("x1", new Interval(-1, 1));
153      variableRanges.Add("x2", new Interval(double.NegativeInfinity, 0));
154      variableRanges.Add("x3", new Interval(0, double.PositiveInfinity));
155
156      EvaluateTest("tanh(x1)", new Interval(Math.Tanh(-1), Math.Tanh(1)), variableRanges);
157      EvaluateTest("tanh(x2)", new Interval(-1, 0), variableRanges);
158      EvaluateTest("tanh(x3)", new Interval(0, 1), variableRanges);
159    }
160
161
162    [TestMethod]
163    [TestCategory("Problems.DataAnalysis.Symbolic")]
164    [TestProperty("Time", "short")]
165    public void TestIntervalInterpreterExp() {
166      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-3), Math.Exp(6)));
167      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-5), Math.Exp(6)), variableRanges);
168    }
169
170    [TestMethod]
171    [TestCategory("Problems.DataAnalysis.Symbolic")]
172    [TestProperty("Time", "short")]
173    public void TestIntervalInterpreterExpRoot() {
174      EvaluateTest("exp(sqrt(x1*x2))", new Interval(Math.Exp(Math.Sqrt(6)), Math.Exp(Math.Sqrt(48))));
175      EvaluateTest("exp(sqrt(x1*x2))", new Interval(Math.Exp(Math.Sqrt(4)), Math.Exp(Math.Sqrt(60))), variableRanges);
176    }
177
178    [TestMethod]
179    [TestCategory("Problems.DataAnalysis.Symbolic")]
180    [TestProperty("Time", "short")]
181    public void TestIntervalInterpreterSqr() {
182      EvaluateTest("sqr(x1)", new Interval(Math.Pow(3, 2), Math.Pow(8, 2)));
183    }
184
185
186
187    [TestMethod]
188    [TestCategory("Problems.DataAnalysis")]
189    [TestProperty("Time", "short")]
190    public void TestDivisionAndSqr() {
191      Dictionary<string, Interval> dataIntervals = new Dictionary<string, Interval>() {
192        {"R", new Interval(0.2, 0.5) },
193        {"r", new Interval(0.5, 0.8) },
194      };
195
196      EvaluateTest("R*R", new Interval(0.2 * 0.2, 0.5 * 0.5), dataIntervals);
197      EvaluateTest("sqr(R)", new Interval(0.2 * 0.2, 0.5 * 0.5), dataIntervals);
198
199      EvaluateTest("r*r", new Interval(0.5 * 0.5, 0.8 * 0.8), dataIntervals);
200      EvaluateTest("sqr(r)", new Interval(0.5 * 0.5, 0.8 * 0.8), dataIntervals);
201
202      EvaluateTest("R/r", new Interval(0.2 / 0.8, 0.5 / 0.5), dataIntervals);
203
204      EvaluateTest("R/(r*r)", new Interval(0.2 / (0.8 * 0.8), 0.5 / (0.5 * 0.5)), dataIntervals);
205      EvaluateTest("R/sqr(r)", new Interval(0.2 / (0.8 * 0.8), 0.5 / (0.5 * 0.5)), dataIntervals);
206
207      EvaluateTest("R*R/sqr(r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals);
208      EvaluateTest("sqr(R)/sqr(r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals);
209      EvaluateTest("sqr(R/r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals);
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.