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