source: trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/IntervalInterpreterTest.cs @ 16757

Last change on this file since 16757 was 16757, checked in by gkronber, 2 years ago

#2966: fixed bug in interval calculation for cos(x) and added/fixed unit tests for sin and cos

File size: 5.8 KB
Line 
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
36    private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null) {
37      var parser = new InfixExpressionParser();
38      var tree = parser.Parse(expression);
39      var interpreter = new IntervalInterpreter();
40      Interval result;
41      if (variableRanges == null)
42        result = interpreter.GetSymbolicExpressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices);
43      else
44        result = interpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
45
46      Assert.AreEqual(expectedResult, result);
47    }
48
49
50    [TestMethod]
51    [TestCategory("Problems.DataAnalysis.Symbolic")]
52    [TestProperty("Time", "short")]
53    public void TestIntervalInterpreterAdd() {
54      EvaluateTest("x1 + x2", new Interval(5, 14));
55      EvaluateTest("x1 + x2", new Interval(5, 16), variableRanges);
56    }
57
58    [TestMethod]
59    [TestCategory("Problems.DataAnalysis.Symbolic")]
60    [TestProperty("Time", "short")]
61    public void TestIntervalInterpreterLogAdd() {
62      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(14)));
63      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(16)), variableRanges);
64    }
65
66    [TestMethod]
67    [TestCategory("Problems.DataAnalysis.Symbolic")]
68    [TestProperty("Time", "short")]
69    public void TestIntervalInterpreterLogAddMul() {
70      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(11), Math.Log(30)));
71      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(7), Math.Log(36)), variableRanges);
72    }
73
74    [TestMethod]
75    [TestCategory("Problems.DataAnalysis.Symbolic")]
76    [TestProperty("Time", "short")]
77    public void TestIntervalInterpreterSin() {
78      EvaluateTest("sin(x1+x2)", new Interval(-1, 1));
79      EvaluateTest("sin(x1+x2)", new Interval(-1, 1), variableRanges);
80      EvaluateTest("sin(1+2)", new Interval(Math.Sin(3), Math.Sin(3)));
81
82      var localVarRanges = new Dictionary<string, Interval>();
83      localVarRanges.Add("x1", new Interval(-1, 1));
84      localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
85      localVarRanges.Add("x3", new Interval(0, Math.PI / 2));
86      localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI));
87      localVarRanges.Add("x5", new Interval(Math.PI/4, Math.PI*3.0/4));
88
89      EvaluateTest("sin(x1)", new Interval(Math.Sin(-1), Math.Sin(1)), localVarRanges);
90      EvaluateTest("sin(x2)", new Interval(-1, 0), localVarRanges);
91      EvaluateTest("sin(x3)", new Interval(0, 1), localVarRanges);
92      EvaluateTest("sin(x4)", new Interval(-1, 1), localVarRanges);
93      EvaluateTest("sin(x5)", new Interval(Math.Sin(Math.PI/4), 1), localVarRanges);
94    }
95
96    [TestMethod]
97    [TestCategory("Problems.DataAnalysis.Symbolic")]
98    [TestProperty("Time", "short")]
99    public void TestIntervalInterpreterCos() {
100      EvaluateTest("cos(x1+x2)", new Interval(-1, 1));
101      EvaluateTest("cos(x1+x2)", new Interval(-1, 1), variableRanges);
102      EvaluateTest("cos(1+2)", new Interval(Math.Sin(3 + Math.PI / 2), Math.Sin(3 + Math.PI / 2)));
103
104      var localVarRanges = new Dictionary<string, Interval>();
105      localVarRanges.Add("x1", new Interval(-1, 1));
106      localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
107      localVarRanges.Add("x3", new Interval(0, Math.PI / 2));
108      localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI));
109      localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4));
110
111      EvaluateTest("cos(x1)", new Interval(Math.Cos(-1), 1), localVarRanges);
112      EvaluateTest("cos(x2)", new Interval(0, 1), localVarRanges);
113      EvaluateTest("cos(x3)", new Interval(0, 1), localVarRanges);
114      EvaluateTest("cos(x4)", new Interval(-1, 1), localVarRanges);
115      EvaluateTest("cos(x5)", new Interval(Math.Cos(Math.PI *3.0/ 4), Math.Cos(Math.PI/ 4)), localVarRanges);
116
117    }
118
119    [TestMethod]
120    [TestCategory("Problems.DataAnalysis.Symbolic")]
121    [TestProperty("Time", "short")]
122    public void TestIntervalInterpreterExp() {
123      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-3), Math.Exp(6)));
124      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-5), Math.Exp(6)), variableRanges);
125    }
126
127    [TestMethod]
128    [TestCategory("Problems.DataAnalysis.Symbolic")]
129    [TestProperty("Time", "short")]
130    public void TestIntervalInterpreterExpRoot() {
131      EvaluateTest("exp(root(x1*x2, 2))", new Interval(Math.Exp(Math.Sqrt(6)), Math.Exp(Math.Sqrt(48))));
132      EvaluateTest("exp(root(x1*x2, 2))", new Interval(Math.Exp(Math.Sqrt(4)), Math.Exp(Math.Sqrt(60))), variableRanges);
133    }
134
135    [TestMethod]
136    [TestCategory("Problems.DataAnalysis.Symbolic")]
137    [TestProperty("Time", "short")]
138    public void TestIntervalInterpreterPower() {
139      EvaluateTest("pow(x1, 2)", new Interval(Math.Pow(3, 1), Math.Pow(8, 3)));
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.