Free cookie consent management tool by TermsFeed Policy Generator

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, 5 years ago

#2994 add a unit test for Abs()

File size: 10.8 KB
RevLine 
[17205]1using System;
[17293]2using System.Collections.Generic;
[17205]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);
[17212]14    private readonly IntervalEvaluator eval = new IntervalEvaluator();
[17205]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]
[17212]23      AssertAreEqualInterval(Add(a, b), new AlgebraicInterval(-3, 3));
[17205]24      //([-1, 1] + [-2, 2]) + [0, 3] = [-3, 6]
[17212]25      AssertAreEqualInterval(Add(Add(a, b), c), new AlgebraicInterval(-3, 6));
[17205]26      //([-1, 1] + [0, 3]) + [-2, 2] = [-3, 6]
[17212]27      AssertAreEqualInterval(Add(Add(a, c), b), new AlgebraicInterval(-3, 6));
[17205]28    }
29
[17212]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 {
[17293]34        Assert.AreEqual(a.LowerBound.Value.Value, b.LowerBound.Value.Value, Math.Abs(a.LowerBound.Value.Value)*1e-4); // relative error < 0.1%
[17212]35      }
36
37      if (double.IsNaN(a.UpperBound.Value)) {
38        Assert.IsTrue(double.IsNaN(b.UpperBound.Value));
39      } else {
[17293]40        Assert.AreEqual(a.UpperBound.Value.Value, b.UpperBound.Value.Value, Math.Abs(a.UpperBound.Value.Value) * 1e-4);
[17212]41      }
42    }
43
[17205]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]
[17212]55      AssertAreEqualInterval(Subtract(a, b), new AlgebraicInterval(-3, 3));
[17205]56      //([-1, 1] - [-2, 2]) - [0, 3] = [-6, 3]
[17212]57      AssertAreEqualInterval(Subtract(Subtract(a, b), c), new AlgebraicInterval(-6, 3));
[17205]58      //([-1, 1] - [0, 3]) - [-2, 2] = [-6, 3]
[17212]59      AssertAreEqualInterval(Subtract(Subtract(a, c), b), new AlgebraicInterval(-6, 3));
[17205]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]
[17212]73      AssertAreEqualInterval(Multiply(a, b), new AlgebraicInterval(-2, 2));
[17205]74      //([-1, 1] * [-2, 2]) * [0, 3] = [-6, 6]
[17212]75      AssertAreEqualInterval(Multiply(Multiply(a, b), c), new AlgebraicInterval(-6, 6));
[17205]76      //([-1, 1] * [0, 3]) * [-2, 2] = [-6, 6]
[17212]77      AssertAreEqualInterval(Multiply(Multiply(a, c), b), new AlgebraicInterval(-6, 6));
[17205]78
79      // [-2, 0] * [-2, 0]  = [0, 4]
[17212]80      AssertAreEqualInterval(new AlgebraicInterval(0, 4), Multiply(new AlgebraicInterval(-2, 0), new AlgebraicInterval(-2, 0)));
[17205]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]
[17212]94      AssertAreEqualInterval(Divide(e, d), new AlgebraicInterval(4.0 / 3.0, 6));
[17205]95      //([4, 6] / [1, 3]) / [1, 3] = [4/9, 6]
[17212]96      AssertAreEqualInterval(Divide(Divide(e, d), d), new AlgebraicInterval(4.0 / 9.0, 6));
[17205]97      //[4, 6] / [0, 3] = [4/3, +Inf]
[17212]98      AssertAreEqualInterval(Divide(e, c), new AlgebraicInterval(4.0 / 3.0, double.PositiveInfinity));
[17205]99      //[-1, 1] / [0, 3] = [+Inf, -Inf]
[17212]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));
[17205]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]
[17212]116      AssertAreEqualInterval(Sine(new AlgebraicInterval(0, 2 * Math.PI)), new AlgebraicInterval(-1, 1));
[17205]117      //sin([-pi/2, pi/2]) = [sin(-pi/2), sin(pi/2)]
[17212]118      AssertAreEqualInterval(Sine(new AlgebraicInterval(-1 * Math.PI / 2, Math.PI / 2)), new AlgebraicInterval(-1, 1));
[17205]119      //sin([0, pi/2]) = [sin(0), sin(pi/2)]
[17212]120      AssertAreEqualInterval(Sine(new AlgebraicInterval(0, Math.PI / 2)), new AlgebraicInterval(0, 1));
[17205]121      //sin([pi, 3*pi/2]) = [sin(pi), sin(3*pi/2)]
[17212]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));
[17205]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)));
[17212]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)));
[17205]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)]
[17212]150      AssertAreEqualInterval(new AlgebraicInterval(Math.Log(3), Math.Log(5)), Logarithm(new AlgebraicInterval(3, 5)));
[17205]151      //Log([0.5, 1]) = [log(0.5), log(1)]
[17212]152      AssertAreEqualInterval(new AlgebraicInterval(Math.Log(0.5), 0), Logarithm(new AlgebraicInterval(0.5, 1)));
[17205]153      //Log([-1, 5]) = [NaN, log(5)]
154      var result = Logarithm(new AlgebraicInterval(-1, 5));
[17212]155      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, Math.Log(5)), result);
156
[17205]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)]
[17212]168      AssertAreEqualInterval(new AlgebraicInterval(1, Math.Exp(1)), Exponential(new AlgebraicInterval(0, 1)));
[17205]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() {
[17212]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)));
[17205]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() {
[17212]192      AssertAreEqualInterval(new AlgebraicInterval(1, 2), SquareRoot(new AlgebraicInterval(1, 4)));
193      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, double.NaN), SquareRoot(new AlgebraicInterval(-4, -1)));
[17205]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() {
[17212]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)));
[17205]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")]
[17299]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")]
[17205]229    public void TestIntervalCbrtOperator() {
[17212]230      AssertAreEqualInterval(new AlgebraicInterval(1, 2), CubicRoot(new AlgebraicInterval(1, 8)));
[17290]231      AssertAreEqualInterval(new AlgebraicInterval(-2, -1), CubicRoot(new AlgebraicInterval(-8, -1)));
[17205]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.