source: branches/2994-AutoDiffForIntervals/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/AutoDiffIntervalTest.cs @ 17299

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

#2994 add a unit test for Abs()

File size: 10.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Problems.DataAnalysis.Symbolic;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace HeuristicLab.Problems.DataAnalysis.Tests {
7  [TestClass]
8  public class AutoDiffIntervalTest {
9    private readonly AlgebraicInterval a = new AlgebraicInterval(-1, 1);
10    private readonly AlgebraicInterval b = new AlgebraicInterval(-2, 2);
11    private readonly AlgebraicInterval c = new AlgebraicInterval(0, 3);
12    private readonly AlgebraicInterval d = new AlgebraicInterval(1, 3);
13    private readonly AlgebraicInterval e = new AlgebraicInterval(4, 6);
14    private readonly IntervalEvaluator eval = new IntervalEvaluator();
15
16    [TestMethod]
17    [TestCategory("Problems.DataAnalysis")]
18    [TestProperty("Time", "short")]
19    public void TestIntervalAddOperation() {
20      //add        [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
21
22      // [-1,1] + [-2,2] = [-3,3]
23      AssertAreEqualInterval(Add(a, b), new AlgebraicInterval(-3, 3));
24      //([-1, 1] + [-2, 2]) + [0, 3] = [-3, 6]
25      AssertAreEqualInterval(Add(Add(a, b), c), new AlgebraicInterval(-3, 6));
26      //([-1, 1] + [0, 3]) + [-2, 2] = [-3, 6]
27      AssertAreEqualInterval(Add(Add(a, c), b), new AlgebraicInterval(-3, 6));
28    }
29
30    private void AssertAreEqualInterval(AlgebraicInterval a, AlgebraicInterval b) {
31      if (double.IsNaN(a.LowerBound.Value)) {
32        Assert.IsTrue(double.IsNaN(b.LowerBound.Value));
33      } else {
34        Assert.AreEqual(a.LowerBound.Value.Value, b.LowerBound.Value.Value, Math.Abs(a.LowerBound.Value.Value)*1e-4); // relative error < 0.1%
35      }
36
37      if (double.IsNaN(a.UpperBound.Value)) {
38        Assert.IsTrue(double.IsNaN(b.UpperBound.Value));
39      } else {
40        Assert.AreEqual(a.UpperBound.Value.Value, b.UpperBound.Value.Value, Math.Abs(a.UpperBound.Value.Value) * 1e-4);
41      }
42    }
43
44    private static AlgebraicInterval Add(AlgebraicInterval a, AlgebraicInterval b) {
45      return a.Clone().Add(b);
46    }
47
48    [TestMethod]
49    [TestCategory("Problems.DataAnalysis")]
50    [TestProperty("Time", "short")]
51    public void TestIntervalSubOperation() {
52      //subtract   [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
53
54      //[-1, 1] - [-2, 2] = [-3, 3]
55      AssertAreEqualInterval(Subtract(a, b), new AlgebraicInterval(-3, 3));
56      //([-1, 1] - [-2, 2]) - [0, 3] = [-6, 3]
57      AssertAreEqualInterval(Subtract(Subtract(a, b), c), new AlgebraicInterval(-6, 3));
58      //([-1, 1] - [0, 3]) - [-2, 2] = [-6, 3]
59      AssertAreEqualInterval(Subtract(Subtract(a, c), b), new AlgebraicInterval(-6, 3));
60    }
61
62    private AlgebraicInterval Subtract(AlgebraicInterval a, AlgebraicInterval b) {
63      return a.Clone().Sub(b);
64    }
65
66    [TestMethod]
67    [TestCategory("Problems.DataAnalysis")]
68    [TestProperty("Time", "short")]
69    public void TestIntervalMutlipyOperation() {
70      //multiply   [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
71
72      //[-1, 1] * [-2, 2] = [-2, 2]
73      AssertAreEqualInterval(Multiply(a, b), new AlgebraicInterval(-2, 2));
74      //([-1, 1] * [-2, 2]) * [0, 3] = [-6, 6]
75      AssertAreEqualInterval(Multiply(Multiply(a, b), c), new AlgebraicInterval(-6, 6));
76      //([-1, 1] * [0, 3]) * [-2, 2] = [-6, 6]
77      AssertAreEqualInterval(Multiply(Multiply(a, c), b), new AlgebraicInterval(-6, 6));
78
79      // [-2, 0] * [-2, 0]  = [0, 4]
80      AssertAreEqualInterval(new AlgebraicInterval(0, 4), Multiply(new AlgebraicInterval(-2, 0), new AlgebraicInterval(-2, 0)));
81    }
82
83    private AlgebraicInterval Multiply(AlgebraicInterval a, AlgebraicInterval b) {
84      return a.Clone().Mul(b);
85    }
86
87    [TestMethod]
88    [TestCategory("Problems.DataAnalysis")]
89    [TestProperty("Time", "short")]
90    public void TestIntervalDivideOperation() {
91      //divide  [x1, x2] / [y1, y2] = [x1, x2] * (1/[y1, y2]), where 1 / [y1,y2] = [1 / y2,1 / y1] if 0 not in [y_1, y_2].
92
93      //[4, 6] / [1, 3] = [4/3, 6]
94      AssertAreEqualInterval(Divide(e, d), new AlgebraicInterval(4.0 / 3.0, 6));
95      //([4, 6] / [1, 3]) / [1, 3] = [4/9, 6]
96      AssertAreEqualInterval(Divide(Divide(e, d), d), new AlgebraicInterval(4.0 / 9.0, 6));
97      //[4, 6] / [0, 3] = [4/3, +Inf]
98      AssertAreEqualInterval(Divide(e, c), new AlgebraicInterval(4.0 / 3.0, double.PositiveInfinity));
99      //[-1, 1] / [0, 3] = [+Inf, -Inf]
100      AssertAreEqualInterval(Divide(a, c), new AlgebraicInterval(double.NegativeInfinity, double.PositiveInfinity));
101
102      //Division by 0 ==> IsInfiniteOrUndefined == true
103      AssertAreEqualInterval(Divide(d, b), new AlgebraicInterval(double.NegativeInfinity, double.PositiveInfinity));
104    }
105
106    private AlgebraicInterval Divide(AlgebraicInterval e, AlgebraicInterval d) {
107      return e.Clone().Div(d);
108    }
109
110    [TestMethod]
111    [TestCategory("Problems.DataAnalysis")]
112    [TestProperty("Time", "short")]
113    public void TestIntervalSineOperator() {
114      //sine depends on interval
115      //sin([0, 2*pi]) = [-1, 1]
116      AssertAreEqualInterval(Sine(new AlgebraicInterval(0, 2 * Math.PI)), new AlgebraicInterval(-1, 1));
117      //sin([-pi/2, pi/2]) = [sin(-pi/2), sin(pi/2)]
118      AssertAreEqualInterval(Sine(new AlgebraicInterval(-1 * Math.PI / 2, Math.PI / 2)), new AlgebraicInterval(-1, 1));
119      //sin([0, pi/2]) = [sin(0), sin(pi/2)]
120      AssertAreEqualInterval(Sine(new AlgebraicInterval(0, Math.PI / 2)), new AlgebraicInterval(0, 1));
121      //sin([pi, 3*pi/2]) = [sin(pi), sin(3*pi/2)]
122      AssertAreEqualInterval(Sine(new AlgebraicInterval(Math.PI, 3 * Math.PI / 2)), new AlgebraicInterval(-1, 0));
123      AssertAreEqualInterval(Sine(new AlgebraicInterval(1, 2)), new AlgebraicInterval(Math.Min(Math.Sin(1), Math.Sin(2)), 1));
124      AssertAreEqualInterval(Sine(new AlgebraicInterval(1, 3)), new AlgebraicInterval(Math.Min(Math.Sin(1), Math.Sin(3)), 1));
125      AssertAreEqualInterval(Sine(new AlgebraicInterval(Math.PI, 5 * Math.PI / 2)), new AlgebraicInterval(-1, 1));
126    }
127
128    private AlgebraicInterval Sine(AlgebraicInterval algebraicInterval) {
129      return algebraicInterval.Sin();
130    }
131
132    [TestMethod]
133    [TestCategory("Problems.DataAnalysis")]
134    [TestProperty("Time", "short")]
135    public void TestIntervalCosineOperator() {
136      //Cosine uses sine Sine(Subtract(a, new AlgebraicInterval(Math.PI / 2, Math.PI / 2)));
137      AssertAreEqualInterval(Cosine(new AlgebraicInterval(0, 2 * Math.PI)), new AlgebraicInterval(-1, 1));
138      AssertAreEqualInterval(new AlgebraicInterval(-1, 1), Cosine(new AlgebraicInterval(Math.PI, 4 * Math.PI / 2)));
139    }
140
141    private AlgebraicInterval Cosine(AlgebraicInterval algebraicInterval) {
142      return algebraicInterval.Cos();
143    }
144
145    [TestMethod]
146    [TestCategory("Problems.DataAnalysis")]
147    [TestProperty("Time", "short")]
148    public void TestIntervalLogOperator() {
149      //Log([3, 5]) = [log(3), log(5)]
150      AssertAreEqualInterval(new AlgebraicInterval(Math.Log(3), Math.Log(5)), Logarithm(new AlgebraicInterval(3, 5)));
151      //Log([0.5, 1]) = [log(0.5), log(1)]
152      AssertAreEqualInterval(new AlgebraicInterval(Math.Log(0.5), 0), Logarithm(new AlgebraicInterval(0.5, 1)));
153      //Log([-1, 5]) = [NaN, log(5)]
154      var result = Logarithm(new AlgebraicInterval(-1, 5));
155      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, Math.Log(5)), result);
156
157    }
158
159    private AlgebraicInterval Logarithm(AlgebraicInterval algebraicInterval) {
160      return algebraicInterval.Log();
161    }
162
163    [TestMethod]
164    [TestCategory("Problems.DataAnalysis")]
165    [TestProperty("Time", "short")]
166    public void TestIntervalExpOperator() {
167      //Exp([0, 1]) = [exp(0), exp(1)]
168      AssertAreEqualInterval(new AlgebraicInterval(1, Math.Exp(1)), Exponential(new AlgebraicInterval(0, 1)));
169    }
170
171    private AlgebraicInterval Exponential(AlgebraicInterval algebraicInterval) {
172      return algebraicInterval.Exp();
173    }
174
175    [TestMethod]
176    [TestCategory("Problems.DataAnalysis")]
177    [TestProperty("Time", "short")]
178    public void TestIntervalSqrOperator() {
179      AssertAreEqualInterval(new AlgebraicInterval(1, 4), Square(new AlgebraicInterval(1, 2)));
180      AssertAreEqualInterval(new AlgebraicInterval(1, 4), Square(new AlgebraicInterval(-2, -1)));
181      AssertAreEqualInterval(new AlgebraicInterval(0, 4), Square(new AlgebraicInterval(-2, 2)));
182    }
183
184    private AlgebraicInterval Square(AlgebraicInterval algebraicInterval) {
185      return algebraicInterval.IntPower(2);
186    }
187
188    [TestMethod]
189    [TestCategory("Problems.DataAnalysis")]
190    [TestProperty("Time", "short")]
191    public void TestIntervalSqrtOperator() {
192      AssertAreEqualInterval(new AlgebraicInterval(1, 2), SquareRoot(new AlgebraicInterval(1, 4)));
193      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, double.NaN), SquareRoot(new AlgebraicInterval(-4, -1)));
194    }
195
196    private AlgebraicInterval SquareRoot(AlgebraicInterval algebraicInterval) {
197      return algebraicInterval.IntRoot(2);
198    }
199
200    [TestMethod]
201    [TestCategory("Problems.DataAnalysis")]
202    [TestProperty("Time", "short")]
203    public void TestIntervalCubeOperator() {
204      AssertAreEqualInterval(new AlgebraicInterval(1, 8), Cube(new AlgebraicInterval(1, 2)));
205      AssertAreEqualInterval(new AlgebraicInterval(-8, -1), Cube(new AlgebraicInterval(-2, -1)));
206      AssertAreEqualInterval(new AlgebraicInterval(-8, 8), Cube(new AlgebraicInterval(-2, 2)));
207    }
208
209    private AlgebraicInterval Cube(AlgebraicInterval algebraicInterval) {
210      return algebraicInterval.IntPower(3);
211    }
212
213    [TestMethod]
214    [TestCategory("Problems.DataAnalysis")]
215    [TestProperty("Time", "short")]
216    public void TestIntervalAbsOperator() {
217      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(1, 2).Abs());
218      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(-2, -1).Abs());
219      AssertAreEqualInterval(new AlgebraicInterval(0, 2), new AlgebraicInterval(-2, 2).Abs());
220      AssertAreEqualInterval(new AlgebraicInterval(0, double.PositiveInfinity), new AlgebraicInterval(double.NegativeInfinity, double.PositiveInfinity).Abs());
221      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, 1), new AlgebraicInterval(-1, double.NaN).Abs()); // XXX unclear how we should handle NaN
222      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, 1), new AlgebraicInterval(double.NaN, -1).Abs());
223    }
224
225
226    [TestMethod]
227    [TestCategory("Problems.DataAnalysis")]
228    [TestProperty("Time", "short")]
229    public void TestIntervalCbrtOperator() {
230      AssertAreEqualInterval(new AlgebraicInterval(1, 2), CubicRoot(new AlgebraicInterval(1, 8)));
231      AssertAreEqualInterval(new AlgebraicInterval(-2, -1), CubicRoot(new AlgebraicInterval(-8, -1)));
232    }
233
234    private AlgebraicInterval CubicRoot(AlgebraicInterval algebraicInterval) {
235      return algebraicInterval.IntRoot(3);
236    }
237  }
238}
Note: See TracBrowser for help on using the repository browser.