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