Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/IntervalInterpreterTest.cs @ 17511

Last change on this file since 17511 was 17100, checked in by mkommend, 5 years ago

#2966: Merged 16629, 16631, 16646, 16740, 16743, 16757, 16758, 16769, 16822 to stable.

File size: 7.9 KB
RevLine 
[16407]1using System;
2using System.Collections.Generic;
3using Microsoft.VisualStudio.TestTools.UnitTesting;
4
5namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
6  [TestClass]
7  public class IntervalInterpreterTest {
8    private IRegressionProblemData problemData;
9    private Dictionary<string, Interval> variableRanges;
10
11    [TestInitialize]
12    public void InitTest() {
13      double[,] arr = new double[4, 3];
14
15      arr[0, 0] = 3;
16      arr[0, 1] = 6;
17      arr[0, 2] = 2;
18      arr[1, 0] = 5;
19      arr[1, 1] = 2;
20      arr[1, 2] = 1;
21      arr[2, 0] = 8;
22      arr[2, 1] = 5;
23      arr[2, 2] = 0;
24      arr[3, 0] = 3;
25      arr[3, 1] = 4;
26      arr[3, 2] = 2;
27
28      var ds = new Dataset(new string[] { "x1", "x2", "y" }, arr);
29      problemData = (IRegressionProblemData)new RegressionProblemData(ds, new string[] { "x1", "x2" }, "y");
30
31      variableRanges = new Dictionary<string, Interval>();
32      variableRanges.Add("x1", new Interval(1, 10));
33      variableRanges.Add("x2", new Interval(4, 6));
34    }
35
[17100]36    private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null, double lowerDelta =0, double upperDelta = 0) {
[16407]37      var parser = new InfixExpressionParser();
38      var tree = parser.Parse(expression);
39      var interpreter = new IntervalInterpreter();
40      Interval result;
41      if (variableRanges == null)
[17100]42        result = interpreter.GetSymbolicExpressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices);
[16407]43      else
[17100]44        result = interpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
[16407]45
[17100]46      Assert.AreEqual(expectedResult.LowerBound, result.LowerBound, lowerDelta);
47      Assert.AreEqual(expectedResult.UpperBound, result.UpperBound, upperDelta);
[16407]48    }
49
50
51    [TestMethod]
52    [TestCategory("Problems.DataAnalysis.Symbolic")]
53    [TestProperty("Time", "short")]
54    public void TestIntervalInterpreterAdd() {
55      EvaluateTest("x1 + x2", new Interval(5, 14));
56      EvaluateTest("x1 + x2", new Interval(5, 16), variableRanges);
57    }
58
59    [TestMethod]
60    [TestCategory("Problems.DataAnalysis.Symbolic")]
61    [TestProperty("Time", "short")]
62    public void TestIntervalInterpreterLogAdd() {
63      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(14)));
64      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(16)), variableRanges);
65    }
66
67    [TestMethod]
68    [TestCategory("Problems.DataAnalysis.Symbolic")]
69    [TestProperty("Time", "short")]
70    public void TestIntervalInterpreterLogAddMul() {
71      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(11), Math.Log(30)));
72      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(7), Math.Log(36)), variableRanges);
73    }
74
75    [TestMethod]
76    [TestCategory("Problems.DataAnalysis.Symbolic")]
77    [TestProperty("Time", "short")]
78    public void TestIntervalInterpreterSin() {
79      EvaluateTest("sin(x1+x2)", new Interval(-1, 1));
80      EvaluateTest("sin(x1+x2)", new Interval(-1, 1), variableRanges);
81      EvaluateTest("sin(1+2)", new Interval(Math.Sin(3), Math.Sin(3)));
[17100]82
83      var localVarRanges = new Dictionary<string, Interval>();
84      localVarRanges.Add("x1", new Interval(-1, 1));
85      localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
86      localVarRanges.Add("x3", new Interval(0, Math.PI / 2));
87      localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI));
88      localVarRanges.Add("x5", new Interval(Math.PI/4, Math.PI*3.0/4));
89
90      EvaluateTest("sin(x1)", new Interval(Math.Sin(-1), Math.Sin(1)), localVarRanges, 1E-8, 1E-8);
91      EvaluateTest("sin(x2)", new Interval(-1, 0), localVarRanges, 1E-8, 1E-8);
92      EvaluateTest("sin(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
93      EvaluateTest("sin(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8);
94      EvaluateTest("sin(x5)", new Interval(Math.Sin(Math.PI/4), 1), localVarRanges, 1E-8, 1E-8);
[16407]95    }
96
97    [TestMethod]
98    [TestCategory("Problems.DataAnalysis.Symbolic")]
99    [TestProperty("Time", "short")]
100    public void TestIntervalInterpreterCos() {
101      EvaluateTest("cos(x1+x2)", new Interval(-1, 1));
102      EvaluateTest("cos(x1+x2)", new Interval(-1, 1), variableRanges);
[17100]103      EvaluateTest("cos(1+2)", new Interval(Math.Sin(3 + Math.PI / 2), Math.Sin(3 + Math.PI / 2)));
104
105      var localVarRanges = new Dictionary<string, Interval>();
106      localVarRanges.Add("x1", new Interval(-1, 1));
107      localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
108      localVarRanges.Add("x3", new Interval(0, Math.PI / 2));
109      localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI));
110      localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4));
111
112      EvaluateTest("cos(x1)", new Interval(Math.Cos(-1), 1), localVarRanges, 1E-8, 1E-8);
113      EvaluateTest("cos(x2)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
114      EvaluateTest("cos(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
115      EvaluateTest("cos(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8);
116      EvaluateTest("cos(x5)", new Interval(Math.Cos(Math.PI *3.0/ 4), Math.Cos(Math.PI/ 4)), localVarRanges, 1E-8, 1E-8);
117
[16407]118    }
119
120    [TestMethod]
121    [TestCategory("Problems.DataAnalysis.Symbolic")]
122    [TestProperty("Time", "short")]
[17100]123    public void TestIntervalInterpreterTan() {
124      // critical values:
125      // lim tan(x) = -inf for x => -pi/2
126      // lim tan(x) = +inf for x =>  pi/2
127      var variableRanges = new Dictionary<string, Interval>();
128      variableRanges.Add("x1", new Interval(-1, 1));
129      variableRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
130      variableRanges.Add("x3", new Interval(0, Math.PI / 2));
131      variableRanges.Add("x4", new Interval(-Math.PI, Math.PI));
132
133      EvaluateTest("tan(x1)", new Interval(Math.Tan(-1), Math.Tan(1)), variableRanges, 1E-8, 1E-8);
134      EvaluateTest("tan(x2)", new Interval(double.NegativeInfinity, 0), variableRanges, 0, 1E-8);
135      EvaluateTest("tan(x3)", new Interval(0, 8.16588936419192E+15), variableRanges, 0, 1E6); // actually upper bound should be infinity.
136      EvaluateTest("tan(x4)", new Interval(double.NegativeInfinity, double.PositiveInfinity), variableRanges);
137    }
138
139    [TestMethod]
140    [TestCategory("Problems.DataAnalysis.Symbolic")]
141    [TestProperty("Time", "short")]
142    public void TestIntervalInterpreterTanh() {
143      // critical values:
144      // lim tanh(x) = -1 for x => -inf
145      // lim tanh(x) =  1 for x =>  inf
146      var variableRanges = new Dictionary<string, Interval>();
147      variableRanges.Add("x1", new Interval(-1, 1));
148      variableRanges.Add("x2", new Interval(double.NegativeInfinity, 0));
149      variableRanges.Add("x3", new Interval(0, double.PositiveInfinity));
150
151      EvaluateTest("tanh(x1)", new Interval(Math.Tanh(-1), Math.Tanh(1)), variableRanges);
152      EvaluateTest("tanh(x2)", new Interval(-1, 0), variableRanges);
153      EvaluateTest("tanh(x3)", new Interval(0, 1), variableRanges);
154    }
155
156
157    [TestMethod]
158    [TestCategory("Problems.DataAnalysis.Symbolic")]
159    [TestProperty("Time", "short")]
[16407]160    public void TestIntervalInterpreterExp() {
161      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-3), Math.Exp(6)));
162      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-5), Math.Exp(6)), variableRanges);
163    }
164
165    [TestMethod]
166    [TestCategory("Problems.DataAnalysis.Symbolic")]
167    [TestProperty("Time", "short")]
168    public void TestIntervalInterpreterExpRoot() {
169      EvaluateTest("exp(root(x1*x2, 2))", new Interval(Math.Exp(Math.Sqrt(6)), Math.Exp(Math.Sqrt(48))));
170      EvaluateTest("exp(root(x1*x2, 2))", new Interval(Math.Exp(Math.Sqrt(4)), Math.Exp(Math.Sqrt(60))), variableRanges);
171    }
172
173    [TestMethod]
174    [TestCategory("Problems.DataAnalysis.Symbolic")]
175    [TestProperty("Time", "short")]
176    public void TestIntervalInterpreterPower() {
177      EvaluateTest("pow(x1, 2)", new Interval(Math.Pow(3, 1), Math.Pow(8, 3)));
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.