[16407] | 1 | using System;
|
---|
| 2 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 4 |
|
---|
| 5 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
| 6 | [TestClass]
|
---|
| 7 | public class IntervalTest {
|
---|
| 8 | private readonly Interval a = new Interval(-1, 1);
|
---|
| 9 | private readonly Interval b = new Interval(-2, 2);
|
---|
| 10 | private readonly Interval c = new Interval(0, 3);
|
---|
| 11 | private readonly Interval d = new Interval(1, 3);
|
---|
| 12 | private readonly Interval e = new Interval(4, 6);
|
---|
| 13 |
|
---|
| 14 | [TestMethod]
|
---|
| 15 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 16 | [TestProperty("Time", "short")]
|
---|
| 17 | public void TestIntervalAddOperation() {
|
---|
| 18 | //add [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
|
---|
| 19 |
|
---|
| 20 | // [-1,1] + [-2,2] = [-3,3]
|
---|
| 21 | Assert.AreEqual(Interval.Add(a, b), new Interval(-3, 3));
|
---|
| 22 | //([-1, 1] + [-2, 2]) + [0, 3] = [-3, 6]
|
---|
| 23 | Assert.AreEqual(Interval.Add(Interval.Add(a, b), c), new Interval(-3, 6));
|
---|
| 24 | //([-1, 1] + [0, 3]) + [-2, 2] = [-3, 6]
|
---|
| 25 | Assert.AreEqual(Interval.Add(Interval.Add(a, c), b), new Interval(-3, 6));
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | [TestMethod]
|
---|
| 29 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 30 | [TestProperty("Time", "short")]
|
---|
| 31 | public void TestIntervalSubOperation() {
|
---|
| 32 | //subtract [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
|
---|
| 33 |
|
---|
| 34 | //[-1, 1] - [-2, 2] = [-3, 3]
|
---|
| 35 | Assert.AreEqual<Interval>(Interval.Subtract(a, b), new Interval(-3, 3));
|
---|
| 36 | //([-1, 1] - [-2, 2]) - [0, 3] = [-6, 3]
|
---|
| 37 | Assert.AreEqual<Interval>(Interval.Subtract(Interval.Subtract(a, b), c), new Interval(-6, 3));
|
---|
| 38 | //([-1, 1] - [0, 3]) - [-2, 2] = [-6, 3]
|
---|
| 39 | Assert.AreEqual<Interval>(Interval.Subtract(Interval.Subtract(a, c), b), new Interval(-6, 3));
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | [TestMethod]
|
---|
| 43 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 44 | [TestProperty("Time", "short")]
|
---|
| 45 | public void TestIntervalMutlipyOperation() {
|
---|
| 46 | //multiply [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
|
---|
| 47 |
|
---|
| 48 | //[-1, 1] * [-2, 2] = [-2, 2]
|
---|
| 49 | Assert.AreEqual<Interval>(Interval.Multiply(a, b), new Interval(-2, 2));
|
---|
| 50 | //([-1, 1] * [-2, 2]) * [0, 3] = [-6, 6]
|
---|
| 51 | Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, b), c), new Interval(-6, 6));
|
---|
| 52 | //([-1, 1] * [0, 3]) * [-2, 2] = [-6, 6]
|
---|
| 53 | Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, c), b), new Interval(-6, 6));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [TestMethod]
|
---|
| 57 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 58 | [TestProperty("Time", "short")]
|
---|
| 59 | public void TestIntervalDivideOperation() {
|
---|
| 60 | //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].
|
---|
| 61 |
|
---|
| 62 | //[4, 6] / [1, 3] = [4/3, 6]
|
---|
| 63 | Assert.AreEqual<Interval>(Interval.Divide(e, d), new Interval(4.0 / 3.0, 6));
|
---|
| 64 | //([4, 6] / [1, 3]) / [1, 3] = [4/9, 6]
|
---|
| 65 | Assert.AreEqual<Interval>(Interval.Divide(Interval.Divide(e, d), d), new Interval(4.0 / 9.0, 6));
|
---|
| 66 | //[4, 6] / [0, 3] = [4/3, +Inf]
|
---|
| 67 | Assert.AreEqual<Interval>(Interval.Divide(e, c), new Interval(4.0 / 3.0, double.PositiveInfinity));
|
---|
| 68 | //[-1, 1] / [0, 3] = [+Inf, -Inf]
|
---|
| 69 | Assert.AreEqual<Interval>(Interval.Divide(a, c), new Interval(double.NegativeInfinity, double.PositiveInfinity));
|
---|
| 70 | //Devision by 0 ==> IsInfiniteOrUndefined == true
|
---|
| 71 | Assert.IsTrue(Interval.Divide(e, c).IsInfiniteOrUndefined);
|
---|
| 72 | //Devision by 0 ==> IsInfiniteOrUndefined == true
|
---|
| 73 | Assert.IsTrue(Interval.Divide(a, c).IsInfiniteOrUndefined);
|
---|
| 74 | Assert.AreEqual<Interval>(Interval.Divide(d, b), new Interval(double.NegativeInfinity, double.PositiveInfinity));
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | [TestMethod]
|
---|
| 78 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 79 | [TestProperty("Time", "short")]
|
---|
| 80 | public void TestIntervalSineOperator() {
|
---|
| 81 | //sine depends on interval
|
---|
| 82 | //sin([0, 2*pi]) = [-1, 1]
|
---|
| 83 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
|
---|
| 84 | //sin([-pi/2, pi/2]) = [sin(-pi/2), sin(pi/2)]
|
---|
| 85 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(-1 * Math.PI / 2, Math.PI / 2)), new Interval(-1, 1));
|
---|
| 86 | //sin([0, pi/2]) = [sin(0), sin(pi/2)]
|
---|
| 87 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(0, Math.PI / 2)), new Interval(0, 1));
|
---|
| 88 | //sin([pi, 3*pi/2]) = [sin(pi), sin(3*pi/2)]
|
---|
| 89 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(Math.PI, 3 * Math.PI / 2)), new Interval(-1, 0));
|
---|
| 90 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(1, 2)), new Interval(Math.Min(Math.Sin(1), Math.Sin(2)), 1));
|
---|
| 91 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(1, 3)), new Interval(Math.Min(Math.Sin(1), Math.Sin(3)), 1));
|
---|
| 92 | Assert.AreEqual<Interval>(Interval.Sine(new Interval(Math.PI, 5 * Math.PI / 2)), new Interval(-1, 1));
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | [TestMethod]
|
---|
| 96 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 97 | [TestProperty("Time", "short")]
|
---|
| 98 | public void TestIntervalCosineOperator() {
|
---|
| 99 | //Cosine uses sine Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
|
---|
| 100 | Assert.AreEqual<Interval>(Interval.Cosine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
|
---|
| 101 | Assert.AreEqual<Interval>(new Interval(-1, 1), Interval.Cosine(new Interval(Math.PI, 4 * Math.PI / 2)));
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | [TestMethod]
|
---|
| 105 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 106 | [TestProperty("Time", "short")]
|
---|
| 107 | public void TestIntervalLogOperator() {
|
---|
| 108 | //Log([3, 5]) = [log(3), log(5)]
|
---|
| 109 | Assert.AreEqual<Interval>(new Interval(Math.Log(3), Math.Log(5)), Interval.Logarithm(new Interval(3, 5)));
|
---|
| 110 | //Log([0.5, 1]) = [log(0.5), log(1)]
|
---|
| 111 | Assert.AreEqual<Interval>(new Interval(Math.Log(0.5), 0), Interval.Logarithm(new Interval(0.5, 1)));
|
---|
| 112 | //Log([-1, 5]) = [NaN, log(5)]
|
---|
| 113 | var result = Interval.Logarithm(new Interval(-1, 5));
|
---|
| 114 | Assert.AreEqual<Interval>(new Interval(double.NaN, Math.Log(5)),result);
|
---|
| 115 | Assert.IsTrue(result.IsInfiniteOrUndefined);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | [TestMethod]
|
---|
| 120 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 121 | [TestProperty("Time", "short")]
|
---|
| 122 | public void TestIntervalExpOperator() {
|
---|
| 123 | //Exp([0, 1]) = [exp(0), exp(1)]
|
---|
| 124 | Assert.AreEqual<Interval>(new Interval(1, Math.Exp(1)), Interval.Exponential(new Interval(0, 1)));
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|