Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17303 was 17303, checked in by gkronber, 5 years ago

#2994 continued refactoring and extended unit tests. Interval calculation still fails for some edge cases (mainly for undefined behaviour). VectorEvaluator and VectorAutoDiffEvaluator produce the same results as the LinearInterpreter. TODO: check gradient calculation

File size: 10.6 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      // [0, 1] / [0, 1]
106      AssertAreEqualInterval(new AlgebraicInterval(0, 1).Div(new AlgebraicInterval(0, 1)), new AlgebraicInterval(1, double.PositiveInfinity)); // returns [NaN, PositiveInfinity]
107
108    }
109
110    private AlgebraicInterval Divide(AlgebraicInterval e, AlgebraicInterval d) {
111      return e.Clone().Div(d);
112    }
113
114    [TestMethod]
115    [TestCategory("Problems.DataAnalysis")]
116    [TestProperty("Time", "short")]
117    public void TestIntervalSineOperator() {
118      //sine depends on interval
119      //sin([0, 2*pi]) = [-1, 1]
120      AssertAreEqualInterval(Sine(new AlgebraicInterval(0, 2 * Math.PI)), new AlgebraicInterval(-1, 1));
121      //sin([-pi/2, pi/2]) = [sin(-pi/2), sin(pi/2)]
122      AssertAreEqualInterval(Sine(new AlgebraicInterval(-1 * Math.PI / 2, Math.PI / 2)), new AlgebraicInterval(-1, 1));
123      //sin([0, pi/2]) = [sin(0), sin(pi/2)]
124      AssertAreEqualInterval(Sine(new AlgebraicInterval(0, Math.PI / 2)), new AlgebraicInterval(0, 1));
125      //sin([pi, 3*pi/2]) = [sin(pi), sin(3*pi/2)]
126      AssertAreEqualInterval(Sine(new AlgebraicInterval(Math.PI, 3 * Math.PI / 2)), new AlgebraicInterval(-1, 0));
127      AssertAreEqualInterval(Sine(new AlgebraicInterval(1, 2)), new AlgebraicInterval(Math.Min(Math.Sin(1), Math.Sin(2)), 1));
128      AssertAreEqualInterval(Sine(new AlgebraicInterval(1, 3)), new AlgebraicInterval(Math.Min(Math.Sin(1), Math.Sin(3)), 1));
129      AssertAreEqualInterval(Sine(new AlgebraicInterval(Math.PI, 5 * Math.PI / 2)), new AlgebraicInterval(-1, 1));
130    }
131
132    private AlgebraicInterval Sine(AlgebraicInterval algebraicInterval) {
133      return algebraicInterval.Sin();
134    }
135
136    [TestMethod]
137    [TestCategory("Problems.DataAnalysis")]
138    [TestProperty("Time", "short")]
139    public void TestIntervalCosineOperator() {
140      //Cosine uses sine Sine(Subtract(a, new AlgebraicInterval(Math.PI / 2, Math.PI / 2)));
141      AssertAreEqualInterval(Cosine(new AlgebraicInterval(0, 2 * Math.PI)), new AlgebraicInterval(-1, 1));
142      AssertAreEqualInterval(new AlgebraicInterval(-1, 1), Cosine(new AlgebraicInterval(Math.PI, 4 * Math.PI / 2)));
143    }
144
145    private AlgebraicInterval Cosine(AlgebraicInterval algebraicInterval) {
146      return algebraicInterval.Cos();
147    }
148
149    [TestMethod]
150    [TestCategory("Problems.DataAnalysis")]
151    [TestProperty("Time", "short")]
152    public void TestIntervalLogOperator() {
153      //Log([3, 5]) = [log(3), log(5)]
154      AssertAreEqualInterval(new AlgebraicInterval(Math.Log(3), Math.Log(5)), Logarithm(new AlgebraicInterval(3, 5)));
155      //Log([0.5, 1]) = [log(0.5), log(1)]
156      AssertAreEqualInterval(new AlgebraicInterval(Math.Log(0.5), 0), Logarithm(new AlgebraicInterval(0.5, 1)));
157      //Log([-1, 5]) = [NaN, log(5)]
158      var result = Logarithm(new AlgebraicInterval(-1, 5));
159      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, Math.Log(5)), result);
160
161    }
162
163    private AlgebraicInterval Logarithm(AlgebraicInterval algebraicInterval) {
164      return algebraicInterval.Log();
165    }
166
167    [TestMethod]
168    [TestCategory("Problems.DataAnalysis")]
169    [TestProperty("Time", "short")]
170    public void TestIntervalExpOperator() {
171      //Exp([0, 1]) = [exp(0), exp(1)]
172      AssertAreEqualInterval(new AlgebraicInterval(1, Math.Exp(1)), Exponential(new AlgebraicInterval(0, 1)));
173    }
174
175    private AlgebraicInterval Exponential(AlgebraicInterval algebraicInterval) {
176      return algebraicInterval.Exp();
177    }
178
179    [TestMethod]
180    [TestCategory("Problems.DataAnalysis")]
181    [TestProperty("Time", "short")]
182    public void TestIntervalSqrOperator() {
183      AssertAreEqualInterval(new AlgebraicInterval(1, 4), new AlgebraicInterval(1, 2).IntPower(2));
184      AssertAreEqualInterval(new AlgebraicInterval(1, 4), new AlgebraicInterval(-2, -1).IntPower(2));
185      AssertAreEqualInterval(new AlgebraicInterval(0, 4), new AlgebraicInterval(-2, 2).IntPower(2));
186
187      AssertAreEqualInterval(new AlgebraicInterval(0, 16), new AlgebraicInterval(-2, 2).Mul(new AlgebraicInterval(2, 2)).IntPower(2));
188    }
189
190    [TestMethod]
191    [TestCategory("Problems.DataAnalysis")]
192    [TestProperty("Time", "short")]
193    public void TestIntervalSqrtOperator() {
194      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(1, 4).IntRoot(2));
195      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, double.NaN), new AlgebraicInterval(-4, -1).IntRoot(2));
196    }
197
198    [TestMethod]
199    [TestCategory("Problems.DataAnalysis")]
200    [TestProperty("Time", "short")]
201    public void TestIntervalCubeOperator() {
202      AssertAreEqualInterval(new AlgebraicInterval(1, 8), new AlgebraicInterval(1, 2).IntPower(3));
203      AssertAreEqualInterval(new AlgebraicInterval(-8, -1), new AlgebraicInterval(-2, -1).IntPower(3));
204      AssertAreEqualInterval(new AlgebraicInterval(-8, 8), new AlgebraicInterval(-2, 2).IntPower(3));
205    }
206
207
208    [TestMethod]
209    [TestCategory("Problems.DataAnalysis")]
210    [TestProperty("Time", "short")]
211    public void TestIntervalAbsOperator() {
212      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(1, 2).Abs());
213      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(-2, -1).Abs());
214      AssertAreEqualInterval(new AlgebraicInterval(0, 2), new AlgebraicInterval(-2, 2).Abs());
215      AssertAreEqualInterval(new AlgebraicInterval(0, double.PositiveInfinity), new AlgebraicInterval(double.NegativeInfinity, double.PositiveInfinity).Abs());
216      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, 1), new AlgebraicInterval(-1, double.NaN).Abs()); // XXX unclear how we should handle NaN
217      AssertAreEqualInterval(new AlgebraicInterval(double.NaN, 1), new AlgebraicInterval(double.NaN, -1).Abs());
218    }
219
220
221    [TestMethod]
222    [TestCategory("Problems.DataAnalysis")]
223    [TestProperty("Time", "short")]
224    public void TestIntervalCbrtOperator() {
225      AssertAreEqualInterval(new AlgebraicInterval(1, 2), new AlgebraicInterval(1, 8).IntRoot(3));
226      AssertAreEqualInterval(new AlgebraicInterval(-2, -1), new AlgebraicInterval(-8, -1).IntRoot(3));
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.