Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2966_interval_calculation/IntervalArithmeticTest.cs @ 16351

Last change on this file since 16351 was 16324, checked in by chaider, 6 years ago

#2966: Added test class to solution.

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Threading.Tasks;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28using HeuristicLab.Algorithms.DataAnalysis.IntervalArithmetic;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
32using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
33
34namespace Test {
35    [TestClass]
36    public class IntervalArithmeticTest {
37        private TestContext testContextInstance;
38        public TestContext TestContext {
39            get { return testContextInstance; }
40            set { testContextInstance = value; }
41        }
42
43        private Interval a = new Interval(-1, 1);
44        private Interval b = new Interval(-2, 2);
45        private Interval c = new Interval(0, 3);
46        private Interval d = new Interval(1, 3);
47        private Interval e = new Interval(4, 6);
48        private IRegressionProblemData problemData;
49        private Dictionary<string, Interval> customIntervals;
50
51        [TestInitialize]
52        public void InitTest() {
53            double[,] arr = new double[4, 3];
54            customIntervals = new Dictionary<string, Interval>();
55
56            arr[0, 0] = 3;
57            arr[0, 1] = 6;
58            arr[0, 2] = 2;
59            arr[1, 0] = 5;
60            arr[1, 1] = 2;
61            arr[1, 2] = 1;
62            arr[2, 0] = 8;
63            arr[2, 1] = 5;
64            arr[2, 2] = 0;
65            arr[3, 0] = 3;
66            arr[3, 1] = 4;
67            arr[3, 2] = 2;
68
69            customIntervals.Add("x1", new Interval(1, 10));
70            customIntervals.Add("x2", new Interval(4, 6));
71
72
73            var ds = new Dataset(new string[] { "x1", "x2", "y" }, arr);
74            problemData = (IRegressionProblemData)new RegressionProblemData(ds, new string[] { "x1", "x2" }, "y");
75        }
76
77        [TestMethod]
78        public void TestIntervalAddOperation() {
79            //add        [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
80            Assert.AreEqual<Interval>(Interval.Add(a, b), new Interval(-3, 3));
81            Assert.AreEqual<Interval>(Interval.Add(Interval.Add(a, b), c), new Interval(-3, 6));
82            Assert.AreEqual<Interval>(Interval.Add(Interval.Add(a, c), b), new Interval(-3, 6));
83        }
84
85        [TestMethod]
86        public void TestIntervalSubOperation() {
87            //subtract   [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
88            Assert.AreEqual<Interval>(Interval.Subtract(a, b), new Interval(-3, 3));
89            Assert.AreEqual<Interval>(Interval.Subtract(Interval.Subtract(a, b), c), new Interval(-6, 3));
90            Assert.AreEqual<Interval>(Interval.Subtract(Interval.Subtract(a, c), b), new Interval(-6, 3));
91        }
92
93        [TestMethod]
94        public void TestIntervalMutlipyOperation() {
95            //multiply   [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
96            Assert.AreEqual<Interval>(Interval.Multiply(a, b), new Interval(-2, 2));
97            Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, b), c), new Interval(-6, 6));
98            Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, c), b), new Interval(-6, 6));
99        }
100
101        [TestMethod]
102        public void TestIntervalDivideOperation() {
103            //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].
104            Assert.AreEqual<Interval>(Interval.Divide(e, d), new Interval(4.0 / 3.0, 6));
105            Assert.AreEqual<Interval>(Interval.Divide(Interval.Divide(e, d), d), new Interval(4.0 / 9.0, 6));
106            Assert.AreEqual<Interval>(Interval.Divide(e, c), new Interval(4.0 / 3.0, double.PositiveInfinity));
107            Assert.AreEqual<Interval>(Interval.Divide(a, c), new Interval(double.NegativeInfinity, double.PositiveInfinity));
108            Assert.IsTrue(Interval.Divide(e, c).IsInfiniteOrUndefined);
109            Assert.IsTrue(Interval.Divide(a, c).IsInfiniteOrUndefined);
110            Assert.AreEqual<Interval>(Interval.Divide(d, b), new Interval(double.NegativeInfinity, double.PositiveInfinity));
111        }
112
113        [TestMethod]
114        public void TestIntervalSineOperator() {
115            //sine depdends on interval
116            Assert.AreEqual<Interval>(Interval.Sine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
117            Assert.AreEqual<Interval>(Interval.Sine(new Interval(-1 * Math.PI / 2, Math.PI / 2)), new Interval(-1, 1));
118            Assert.AreEqual<Interval>(Interval.Sine(new Interval(0, Math.PI / 2)), new Interval(0, 1));
119            Assert.AreEqual<Interval>(Interval.Sine(new Interval(Math.PI, 3 * Math.PI / 2)), new Interval(-1, 0));
120            Assert.AreEqual<Interval>(Interval.Sine(new Interval(1, 2)), new Interval(Math.Min(Math.Sin(1), Math.Sin(2)), 1));
121            Assert.AreEqual<Interval>(Interval.Sine(new Interval(1, 3)), new Interval(Math.Min(Math.Sin(1), Math.Sin(3)), 1));
122            Assert.AreEqual<Interval>(Interval.Sine(new Interval(Math.PI, 5 * Math.PI / 2)), new Interval(-1, 1));
123        }
124
125        [TestMethod]
126        public void TestIntervalCosineOperator() {
127            Assert.AreEqual<Interval>(Interval.Cosine(new Interval(0, 2 * Math.PI)), new Interval(-1, 1));
128            Assert.AreEqual<Interval>(new Interval(-1, 1), Interval.Cosine(new Interval(Math.PI, 4 * Math.PI / 2)));
129        }
130
131        [TestMethod]
132        public void TestIntervalLogOperator() {
133            Assert.AreEqual<Interval>(new Interval(Math.Log(3), Math.Log(5)), Interval.Logarithm(new Interval(3, 5)));
134            Assert.AreEqual<Interval>(new Interval(Math.Log(0.5), 0), Interval.Logarithm(new Interval(0.5, 1)));
135        }
136
137        [TestMethod]
138        public void TestIntervalExpOperator() {
139            Assert.AreEqual<Interval>(new Interval(1, Math.Exp(1)), Interval.Exponential(new Interval(0, 1)));
140        }
141
142
143        [TestMethod]
144        public void TestIntervalCalculationAdd() {
145            var stringRepresentation = "x1 + x2";
146            var parser = new InfixExpressionParser();
147            var tree = parser.Parse(stringRepresentation);
148            var interpreter = new SymbolicDataAnalysisIntervalArithmeticInterpreter();
149            var result = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.AllIndices);
150
151            Assert.AreEqual(new Interval(5, 14), result);
152        }
153
154        [TestMethod]
155        public void TestIntervalCalculationLogAdd() {
156            var stringRepresentation = "log(x1 + x2)";
157            var parser = new InfixExpressionParser();
158            var tree = parser.Parse(stringRepresentation);
159            var interpreter = new SymbolicDataAnalysisIntervalArithmeticInterpreter();
160            var result = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.AllIndices);
161
162            Assert.AreEqual(new Interval(Math.Log(5), Math.Log(14)), result);
163        }
164
165        [TestMethod]
166        public void TestIntervalCalculationLogAddMul() {
167            var stringRepresentation = "log(3*x1 + x2)";
168            var parser = new InfixExpressionParser();
169            var tree = parser.Parse(stringRepresentation);
170            var interpreter = new SymbolicDataAnalysisIntervalArithmeticInterpreter();
171            var result = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.AllIndices);
172
173            Assert.AreEqual(new Interval(Math.Log(11), Math.Log(30)), result);
174        }
175
176        [TestMethod]
177        public void TestProblemDataIndices() {
178            var stringRepresentation = "x1 + x2";
179            var parser = new InfixExpressionParser();
180            var tree = parser.Parse(stringRepresentation);
181            var interpreter = new SymbolicDataAnalysisIntervalArithmeticInterpreter();
182            var x = problemData.TestIndices;
183            var result = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
184
185            Assert.AreEqual(new Interval(5, 11), result);
186        }
187
188        [TestMethod]
189        public void TestCustomIntervals() {
190            var stringRepresentation = "x1 + x2";
191            var parser = new InfixExpressionParser();
192            var tree = parser.Parse(stringRepresentation);
193            var interpreter = new SymbolicDataAnalysisIntervalArithmeticInterpreter();
194            var x = problemData.TestIndices;
195            var result = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices, customIntervals);
196
197            Assert.AreEqual(new Interval(5, 16), result);
198        }
199    }
200}
Note: See TracBrowser for help on using the repository browser.