Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/IntervalEvaluatorTest.cs

Last change on this file was 18163, checked in by gkronber, 3 years ago

#2994: push of old local changes

File size: 16.4 KB
RevLine 
[17318]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6using HeuristicLab.Random;
7using Microsoft.VisualStudio.TestTools.UnitTesting;
8
9namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
10  [TestClass]
11  public class IntervalEvaluatorTest {
12    private IRegressionProblemData problemData;
13    private IDictionary<string, Interval> variableRanges;
14
15    [TestInitialize]
16    public void InitTest() {
17      double[,] arr = new double[4, 3];
18
19      arr[0, 0] = 3;
20      arr[0, 1] = 6;
21      arr[0, 2] = 2;
22      arr[1, 0] = 5;
23      arr[1, 1] = 2;
24      arr[1, 2] = 1;
25      arr[2, 0] = 8;
26      arr[2, 1] = 5;
27      arr[2, 2] = 0;
28      arr[3, 0] = 3;
29      arr[3, 1] = 4;
30      arr[3, 2] = 2;
31
32      // intervals for dataset
33      // x1: 3 .. 8
34      // x2: 2 .. 6
35
36      var ds = new Dataset(new string[] { "x1", "x2", "y" }, arr);
37      problemData = (IRegressionProblemData)new RegressionProblemData(ds, new string[] { "x1", "x2" }, "y");
38
39
40      variableRanges = new Dictionary<string, Interval>();
41      variableRanges.Add("x1", new Interval(1, 10));
42      variableRanges.Add("x2", new Interval(4, 6));
43    }
44
45    private void EvaluateTest(string expression, Interval expectedResult, IDictionary<string, Interval> variableRanges = null, double lowerDelta = 1e-6, double upperDelta = 1e-6) {
46      var parser = new InfixExpressionParser();
47      var tree = parser.Parse(expression);
48      var interpreter = new IntervalEvaluator();
49      Interval result;
50      if (variableRanges == null) variableRanges = problemData.VariableRanges.GetIntervals();
51      result = interpreter.Evaluate(tree, variableRanges, new ISymbolicExpressionTreeNode[0], out double[] _, out double[] __);
52
53      Assert.AreEqual(expectedResult.LowerBound, result.LowerBound, lowerDelta);
54      Assert.AreEqual(expectedResult.UpperBound, result.UpperBound, upperDelta);
55    }
56
57
58    [TestMethod]
59    [TestCategory("Problems.DataAnalysis.Symbolic")]
60    [TestProperty("Time", "short")]
61    public void IntervalEvaluatorAdd() {
62      EvaluateTest("x1 + x2", new Interval(5, 14));
63      EvaluateTest("x1 + x2", new Interval(5, 16), variableRanges);
64    }
65
66    [TestMethod]
67    [TestCategory("Problems.DataAnalysis.Symbolic")]
68    [TestProperty("Time", "short")]
69    public void IntervalEvaluatorLogAdd() {
70      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(14)));
71      EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(16)), variableRanges);
72    }
73
74    [TestMethod]
75    [TestCategory("Problems.DataAnalysis.Symbolic")]
76    [TestProperty("Time", "short")]
77    public void IntervalEvaluatorLogAddMul() {
78      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(11), Math.Log(30)));
79      EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(7), Math.Log(36)), variableRanges);
80    }
81
82    [TestMethod]
83    [TestCategory("Problems.DataAnalysis.Symbolic")]
84    [TestProperty("Time", "short")]
85    public void IntervalEvaluatorSin() {
86      EvaluateTest("sin(x1+x2)", new Interval(-1, 1));
87      EvaluateTest("sin(x1+x2)", new Interval(-1, 1), variableRanges);
88      EvaluateTest("sin(1+2)", new Interval(Math.Sin(3), Math.Sin(3)));
89
90      var localVarRanges = new Dictionary<string, Interval>();
91      localVarRanges.Add("x1", new Interval(-1, 1));
92      localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
93      localVarRanges.Add("x3", new Interval(0, Math.PI / 2));
94      localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI));
95      localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4));
96
97      EvaluateTest("sin(x1)", new Interval(Math.Sin(-1), Math.Sin(1)), localVarRanges, 1E-8, 1E-8);
98      EvaluateTest("sin(x2)", new Interval(-1, 0), localVarRanges, 1E-8, 1E-8);
99      EvaluateTest("sin(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
100      EvaluateTest("sin(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8);
101      EvaluateTest("sin(x5)", new Interval(Math.Sin(Math.PI / 4), 1), localVarRanges, 1E-8, 1E-8);
102    }
103
104    [TestMethod]
105    [TestCategory("Problems.DataAnalysis.Symbolic")]
106    [TestProperty("Time", "short")]
107    public void IntervalEvaluatorCos() {
108      EvaluateTest("cos(x1+x2)", new Interval(-1, 1));
109      EvaluateTest("cos(x1+x2)", new Interval(-1, 1), variableRanges);
110      EvaluateTest("cos(1+2)", new Interval(Math.Cos(3), Math.Cos(3)));
111
112      var localVarRanges = new Dictionary<string, Interval>();
113      localVarRanges.Add("x1", new Interval(-1, 1));
114      localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
115      localVarRanges.Add("x3", new Interval(0, Math.PI / 2));
116      localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI));
117      localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4));
118
119      EvaluateTest("cos(x1)", new Interval(Math.Cos(-1), 1), localVarRanges, 1E-8, 1E-8);
120      EvaluateTest("cos(x2)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
121      EvaluateTest("cos(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8);
122      EvaluateTest("cos(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8);
123      EvaluateTest("cos(x5)", new Interval(Math.Cos(Math.PI * 3.0 / 4), Math.Cos(Math.PI / 4)), localVarRanges, 1E-8, 1E-8);
124
125    }
126
127    [TestMethod]
128    [TestCategory("Problems.DataAnalysis.Symbolic")]
129    [TestProperty("Time", "short")]
130    public void IntervalEvaluatorTan() {
131      // critical values:
132      // lim tan(x) = -inf for x => -pi/2
133      // lim tan(x) = +inf for x =>  pi/2
134      var variableRanges = new Dictionary<string, Interval>();
135      variableRanges.Add("x1", new Interval(-1, 1));
136      variableRanges.Add("x2", new Interval(-(Math.PI / 2), 0));
137      variableRanges.Add("x3", new Interval(0, Math.PI / 2));
138      variableRanges.Add("x4", new Interval(-Math.PI, Math.PI));
139
140      EvaluateTest("tan(x1)", new Interval(Math.Tan(-1), Math.Tan(1)), variableRanges, 1E-8, 1E-8);
141      EvaluateTest("tan(x2)", new Interval(double.NegativeInfinity, 0), variableRanges, 0, 1E-8);
142      EvaluateTest("tan(x3)", new Interval(0, 8.16588936419192E+15), variableRanges, 0, 1E6); // actually upper bound should be infinity.
143      EvaluateTest("tan(x4)", new Interval(double.NegativeInfinity, double.PositiveInfinity), variableRanges);
144    }
145
146    [TestMethod]
147    [TestCategory("Problems.DataAnalysis.Symbolic")]
148    [TestProperty("Time", "short")]
149    public void IntervalEvaluatorTanh() {
150      // critical values:
151      // lim tanh(x) = -1 for x => -inf
152      // lim tanh(x) =  1 for x =>  inf
153      var variableRanges = new Dictionary<string, Interval>();
154      variableRanges.Add("x1", new Interval(-1, 1));
155      variableRanges.Add("x2", new Interval(double.NegativeInfinity, 0));
156      variableRanges.Add("x3", new Interval(0, double.PositiveInfinity));
157
158      EvaluateTest("tanh(x1)", new Interval(Math.Tanh(-1), Math.Tanh(1)), variableRanges);
159      EvaluateTest("tanh(x2)", new Interval(-1, 0), variableRanges);
160      EvaluateTest("tanh(x3)", new Interval(0, 1), variableRanges);
161    }
162
163
164    [TestMethod]
165    [TestCategory("Problems.DataAnalysis.Symbolic")]
166    [TestProperty("Time", "short")]
167    public void IntervalEvaluatorExp() {
168      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-3), Math.Exp(6)));
169      EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-5), Math.Exp(6)), variableRanges);
170    }
171
172    [TestMethod]
173    [TestCategory("Problems.DataAnalysis.Symbolic")]
174    [TestProperty("Time", "short")]
175    public void IntervalEvaluatorExpRoot() {
176      EvaluateTest("exp(sqrt(x1*x2))", new Interval(Math.Exp(Math.Sqrt(6)), Math.Exp(Math.Sqrt(48))));
177      EvaluateTest("exp(sqrt(x1*x2))", new Interval(Math.Exp(Math.Sqrt(4)), Math.Exp(Math.Sqrt(60))), variableRanges);
178    }
179
180    [TestMethod]
181    [TestCategory("Problems.DataAnalysis.Symbolic")]
182    [TestProperty("Time", "short")]
183    public void IntervalEvaluatorSqr() {
184      EvaluateTest("sqr(x1)", new Interval(Math.Pow(3, 2), Math.Pow(8, 2)));
185    }
186
187    [TestMethod]
188    [TestCategory("Problems.DataAnalysis")]
189    [TestProperty("Time", "short")]
[18163]190    public void IntervalEvaluatorSqrAndDiv() {
[17318]191      Dictionary<string, Interval> dataIntervals = new Dictionary<string, Interval>() {
192        {"R", new Interval(0.2, 0.5) },
193        {"r", new Interval(0.5, 0.8) },
194      };
195
196      EvaluateTest("R*R", new Interval(0.2 * 0.2, 0.5 * 0.5), dataIntervals);
197      EvaluateTest("sqr(R)", new Interval(0.2 * 0.2, 0.5 * 0.5), dataIntervals);
198
199      EvaluateTest("r*r", new Interval(0.5 * 0.5, 0.8 * 0.8), dataIntervals);
200      EvaluateTest("sqr(r)", new Interval(0.5 * 0.5, 0.8 * 0.8), dataIntervals);
201
202      EvaluateTest("R/r", new Interval(0.2 / 0.8, 0.5 / 0.5), dataIntervals);
203
204      EvaluateTest("R/(r*r)", new Interval(0.2 / (0.8 * 0.8), 0.5 / (0.5 * 0.5)), dataIntervals);
205      EvaluateTest("R/sqr(r)", new Interval(0.2 / (0.8 * 0.8), 0.5 / (0.5 * 0.5)), dataIntervals);
206
207      EvaluateTest("R*R/sqr(r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals);
208      EvaluateTest("sqr(R)/sqr(r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals);
209      EvaluateTest("sqr(R/r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals);
210    }
211
[17319]212    [TestMethod]
213    [TestCategory("Problems.DataAnalysis.Symbolic")]
214    [TestProperty("Time", "short")]
215    public void IntervalEvaluatorSqrt() {
216      EvaluateTest("sqrt(x1)", new Interval(0, 1), new Dictionary<string, Interval>() { {"x1", new Interval(0, 1) }});
217    }
[17318]218
219
220    [TestMethod]
221    [TestCategory("Problems.DataAnalysis")]
222    [TestProperty("Time", "short")]
223    public void IntervalEvaluatorExamples() {
224      var parser = new InfixExpressionParser();
225      var evaluator = new IntervalEvaluator();
226      var intervals = new Dictionary<string, Interval>() {
[17319]227        {"x", new Interval(1, 2) },
228        {"unit", new Interval(0, 1) },
[17318]229      };
230
231      var t = parser.Parse("SQR(EXP(CUBE((2.10981074965936*'x'))))");
[17319]232      AssertInterval(143638040.396283, 1.81198989971641E+65, evaluator.Evaluate(t, intervals));
233
234
235      t = parser.Parse("sqrt(unit)");
236      AssertInterval(0, 1, evaluator.Evaluate(t, intervals));
237
[17318]238    }
239
240    private void AssertInterval(double expectedLow, double expectedHigh, Interval r) {
241      Assert.AreEqual(expectedLow, r.LowerBound, Math.Abs(expectedLow * 1e-5));
242      Assert.AreEqual(expectedHigh, r.UpperBound, Math.Abs(expectedHigh * 1e-5));
243    }
244
245
246    [TestMethod]
247    [TestCategory("Problems.DataAnalysis")]
248    [TestProperty("Time", "long")]
249    public void IntervalEvaluatorConsistencyForRandomExpressions() {
250      var grammar = new TypeCoherentExpressionGrammar();
251      grammar.ConfigureAsDefaultRegressionGrammar();
252      // activate supported symbols
253      grammar.Symbols.First(s => s is Square).Enabled = true;
254      grammar.Symbols.First(s => s is SquareRoot).Enabled = true;
255      grammar.Symbols.First(s => s is Cube).Enabled = true;
256      grammar.Symbols.First(s => s is CubeRoot).Enabled = true;
257      grammar.Symbols.First(s => s is Sine).Enabled = true;
258      grammar.Symbols.First(s => s is Cosine).Enabled = true;
259      grammar.Symbols.First(s => s is Exponential).Enabled = true;
260      grammar.Symbols.First(s => s is Logarithm).Enabled = true;
261      grammar.Symbols.First(s => s is Absolute).Enabled = false; // XXX not yet supported by old interval calculator
262      grammar.Symbols.First(s => s is AnalyticQuotient).Enabled = false; // not yet supported by old interval calculator
263
264      var varSy = (Variable)grammar.Symbols.First(s => s is Variable);
265      varSy.AllVariableNames = new string[] { "x", "y" };
266      varSy.VariableNames = varSy.AllVariableNames;
267      varSy.WeightMu = 1.0;
268      varSy.WeightSigma = 1.0;
269      var rand = new FastRandom(1234);
270      var eval1 = new IntervalEvaluator();
271      var eval2 = new IntervalInterpreter();
272
273      IDictionary<string, Interval> posIntervals = new Dictionary<string, Interval>() {
274        { "x", new Interval(1, 2) },
275        { "y", new Interval(0, 1) }
276      };
277      IDictionary<string, Interval> negIntervals = new Dictionary<string, Interval>() {
278        { "x", new Interval(-2, -1) },
279        { "y", new Interval(-1, 0) }
280      };
281      IDictionary<string, Interval> fullIntervals = new Dictionary<string, Interval>() {
282        { "x", new Interval(-2, 2) },
283        { "y", new Interval(-1, 1) }
284      };
285      IDictionary<string, Interval> specialIntervals = new Dictionary<string, Interval>() {
286        { "x", new Interval(1, double.PositiveInfinity) },
287        { "y", new Interval(double.NegativeInfinity, double.PositiveInfinity) }
288      };
289
290      var formatter = new InfixExpressionFormatter();
291      var sb = new StringBuilder();
292      foreach (var interval in new[] { posIntervals, negIntervals, fullIntervals, specialIntervals }) {
293        int N = 10000;
294        int i = 0;
295        while (i < N) {
296          var t = ProbabilisticTreeCreator.Create(rand, grammar, maxTreeLength: 5, maxTreeDepth: 5);
297          var r1 = eval1.Evaluate(t, interval);
298          var r2 = eval2.GetSymbolicExpressionTreeInterval(t, interval);
299          // Console.WriteLine(formatter.Format(t));
300
301          // all NaN is ok (but don't count NaN expressions)
302          if (double.IsNaN(r1.LowerBound) && double.IsNaN(r2.LowerBound) && double.IsNaN(r1.UpperBound) && double.IsNaN(r2.UpperBound)) continue;
303          if (r1.LowerBound == r2.LowerBound && r1.UpperBound == r2.UpperBound) {
304            /* exactly the same value (incl. Inf / -Inf) => ok */
305          } else if ((Math.Abs(r1.LowerBound - r2.LowerBound) <= Math.Max(1e-10, Math.Abs(r1.LowerBound * 1e-4))) &&
306                     (Math.Abs(r1.UpperBound - r2.UpperBound) <= Math.Max(1e-10, Math.Abs(r1.UpperBound * 1e-4)))) {
307            /* approximately the same value => OK */
308          } else {
309            sb.AppendLine($"{r1} <> {r2} for {formatter.Format(t)} x={interval["x"]} y={interval["y"]}");
310          }
311          i++;
312        }
313      }
314      if (sb.Length > 0) {
315        Console.WriteLine(sb.ToString());
316        Assert.Fail("There were different interval calculation results");
317      }
318    }
319
320    [TestMethod]
321    [TestCategory("Problems.DataAnalysis")]
322    [TestProperty("Time", "short")]
323    public void IntervalEvaluatorConsistencyForExamples() {
324      var parser = new InfixExpressionParser();
325      var eval1 = new IntervalEvaluator();
326      var eval2 = new IntervalInterpreter();
327      IDictionary<string, Interval> interval = new Dictionary<string, Interval>() {
328        { "x", new Interval(1, 2) },
329        { "y", new Interval(0, 1) },
330        { "z", new Interval(double.NegativeInfinity, double.PositiveInfinity) },
331      };
332
333      var exprs = new string[] {
334        "CUBE((0.642971622547268*'x')) * (-16.5400720573962)",
335        "sqr(y / y)", // one interpreter produces [NaN, inf], the other [NaN, 0]
336        "cuberoot(-x)", // the old interpreter calculates cuberoot incorrectly
337        "sqr(log(-x))", // Interval: [NaN, NaN] <> Interval (old): [NaN, 0]
338        "log(1.8*'y' - 1.4*'y')", // Interval: [NaN, 0,587786664902119] <> Interval (old): [0,587786664902119, NaN]
339        "log(z)", // Interval: [NaN, ∞] <> Interval (old): [∞, NaN]
340        "sqr(sqrt(-1))" // Interval: [NaN, NaN] <> Interval (old): [NaN, 0]
341      };
342
343      var formatter = new InfixExpressionFormatter();
344      var sb = new StringBuilder();
345      foreach (var expr in exprs) {
346        var t = parser.Parse(expr);
347
348        var r1 = eval1.Evaluate(t, interval);
349        var r2 = eval2.GetSymbolicExpressionTreeInterval(t, interval);
350        // Console.WriteLine(formatter.Format(t));
351
352        // all NaN is ok
353        if (double.IsNaN(r1.LowerBound) && double.IsNaN(r2.LowerBound) && double.IsNaN(r1.UpperBound) && double.IsNaN(r2.UpperBound)) continue;
354        if (r1.LowerBound == r2.LowerBound && r1.UpperBound == r2.UpperBound) continue;  // Inf, -Inf and exactly the same value are ok
355
356        if ((Math.Abs(r1.LowerBound - r2.LowerBound) <= Math.Abs(r1.LowerBound * 1e-4)) &&
357            (Math.Abs(r1.UpperBound - r2.UpperBound) <= Math.Abs(r1.UpperBound * 1e-4))) { /* OK */ } else {
358          sb.AppendLine($"{r1} <> {r2} for {formatter.Format(t)} x={interval["x"]} y={interval["y"]}");
359        }
360      }
361      if (sb.Length > 0) {
362        Console.WriteLine(sb.ToString());
363        Assert.Fail("There were different interval calculation results");
364      }
365    }
366  }
367}
Note: See TracBrowser for help on using the repository browser.