using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Random; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests { [TestClass] public class IntervalEvaluatorTest { private IRegressionProblemData problemData; private IDictionary variableRanges; [TestInitialize] public void InitTest() { double[,] arr = new double[4, 3]; arr[0, 0] = 3; arr[0, 1] = 6; arr[0, 2] = 2; arr[1, 0] = 5; arr[1, 1] = 2; arr[1, 2] = 1; arr[2, 0] = 8; arr[2, 1] = 5; arr[2, 2] = 0; arr[3, 0] = 3; arr[3, 1] = 4; arr[3, 2] = 2; // intervals for dataset // x1: 3 .. 8 // x2: 2 .. 6 var ds = new Dataset(new string[] { "x1", "x2", "y" }, arr); problemData = (IRegressionProblemData)new RegressionProblemData(ds, new string[] { "x1", "x2" }, "y"); variableRanges = new Dictionary(); variableRanges.Add("x1", new Interval(1, 10)); variableRanges.Add("x2", new Interval(4, 6)); } private void EvaluateTest(string expression, Interval expectedResult, IDictionary variableRanges = null, double lowerDelta = 1e-6, double upperDelta = 1e-6) { var parser = new InfixExpressionParser(); var tree = parser.Parse(expression); var interpreter = new IntervalEvaluator(); Interval result; if (variableRanges == null) variableRanges = problemData.VariableRanges.GetIntervals(); result = interpreter.Evaluate(tree, variableRanges, new ISymbolicExpressionTreeNode[0], out double[] _, out double[] __); Assert.AreEqual(expectedResult.LowerBound, result.LowerBound, lowerDelta); Assert.AreEqual(expectedResult.UpperBound, result.UpperBound, upperDelta); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorAdd() { EvaluateTest("x1 + x2", new Interval(5, 14)); EvaluateTest("x1 + x2", new Interval(5, 16), variableRanges); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorLogAdd() { EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(14))); EvaluateTest("log(x1 + x2)", new Interval(Math.Log(5), Math.Log(16)), variableRanges); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorLogAddMul() { EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(11), Math.Log(30))); EvaluateTest("log(3*x1 + x2)", new Interval(Math.Log(7), Math.Log(36)), variableRanges); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorSin() { EvaluateTest("sin(x1+x2)", new Interval(-1, 1)); EvaluateTest("sin(x1+x2)", new Interval(-1, 1), variableRanges); EvaluateTest("sin(1+2)", new Interval(Math.Sin(3), Math.Sin(3))); var localVarRanges = new Dictionary(); localVarRanges.Add("x1", new Interval(-1, 1)); localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0)); localVarRanges.Add("x3", new Interval(0, Math.PI / 2)); localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI)); localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4)); EvaluateTest("sin(x1)", new Interval(Math.Sin(-1), Math.Sin(1)), localVarRanges, 1E-8, 1E-8); EvaluateTest("sin(x2)", new Interval(-1, 0), localVarRanges, 1E-8, 1E-8); EvaluateTest("sin(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8); EvaluateTest("sin(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8); EvaluateTest("sin(x5)", new Interval(Math.Sin(Math.PI / 4), 1), localVarRanges, 1E-8, 1E-8); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorCos() { EvaluateTest("cos(x1+x2)", new Interval(-1, 1)); EvaluateTest("cos(x1+x2)", new Interval(-1, 1), variableRanges); EvaluateTest("cos(1+2)", new Interval(Math.Cos(3), Math.Cos(3))); var localVarRanges = new Dictionary(); localVarRanges.Add("x1", new Interval(-1, 1)); localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0)); localVarRanges.Add("x3", new Interval(0, Math.PI / 2)); localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI)); localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4)); EvaluateTest("cos(x1)", new Interval(Math.Cos(-1), 1), localVarRanges, 1E-8, 1E-8); EvaluateTest("cos(x2)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8); EvaluateTest("cos(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8); EvaluateTest("cos(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8); EvaluateTest("cos(x5)", new Interval(Math.Cos(Math.PI * 3.0 / 4), Math.Cos(Math.PI / 4)), localVarRanges, 1E-8, 1E-8); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorTan() { // critical values: // lim tan(x) = -inf for x => -pi/2 // lim tan(x) = +inf for x => pi/2 var variableRanges = new Dictionary(); variableRanges.Add("x1", new Interval(-1, 1)); variableRanges.Add("x2", new Interval(-(Math.PI / 2), 0)); variableRanges.Add("x3", new Interval(0, Math.PI / 2)); variableRanges.Add("x4", new Interval(-Math.PI, Math.PI)); EvaluateTest("tan(x1)", new Interval(Math.Tan(-1), Math.Tan(1)), variableRanges, 1E-8, 1E-8); EvaluateTest("tan(x2)", new Interval(double.NegativeInfinity, 0), variableRanges, 0, 1E-8); EvaluateTest("tan(x3)", new Interval(0, 8.16588936419192E+15), variableRanges, 0, 1E6); // actually upper bound should be infinity. EvaluateTest("tan(x4)", new Interval(double.NegativeInfinity, double.PositiveInfinity), variableRanges); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorTanh() { // critical values: // lim tanh(x) = -1 for x => -inf // lim tanh(x) = 1 for x => inf var variableRanges = new Dictionary(); variableRanges.Add("x1", new Interval(-1, 1)); variableRanges.Add("x2", new Interval(double.NegativeInfinity, 0)); variableRanges.Add("x3", new Interval(0, double.PositiveInfinity)); EvaluateTest("tanh(x1)", new Interval(Math.Tanh(-1), Math.Tanh(1)), variableRanges); EvaluateTest("tanh(x2)", new Interval(-1, 0), variableRanges); EvaluateTest("tanh(x3)", new Interval(0, 1), variableRanges); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorExp() { EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-3), Math.Exp(6))); EvaluateTest("exp(x1-x2)", new Interval(Math.Exp(-5), Math.Exp(6)), variableRanges); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorExpRoot() { EvaluateTest("exp(sqrt(x1*x2))", new Interval(Math.Exp(Math.Sqrt(6)), Math.Exp(Math.Sqrt(48)))); EvaluateTest("exp(sqrt(x1*x2))", new Interval(Math.Exp(Math.Sqrt(4)), Math.Exp(Math.Sqrt(60))), variableRanges); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorSqr() { EvaluateTest("sqr(x1)", new Interval(Math.Pow(3, 2), Math.Pow(8, 2))); } [TestMethod] [TestCategory("Problems.DataAnalysis")] [TestProperty("Time", "short")] public void IntervalEvaluatorSqrAndDiv() { Dictionary dataIntervals = new Dictionary() { {"R", new Interval(0.2, 0.5) }, {"r", new Interval(0.5, 0.8) }, }; EvaluateTest("R*R", new Interval(0.2 * 0.2, 0.5 * 0.5), dataIntervals); EvaluateTest("sqr(R)", new Interval(0.2 * 0.2, 0.5 * 0.5), dataIntervals); EvaluateTest("r*r", new Interval(0.5 * 0.5, 0.8 * 0.8), dataIntervals); EvaluateTest("sqr(r)", new Interval(0.5 * 0.5, 0.8 * 0.8), dataIntervals); EvaluateTest("R/r", new Interval(0.2 / 0.8, 0.5 / 0.5), dataIntervals); EvaluateTest("R/(r*r)", new Interval(0.2 / (0.8 * 0.8), 0.5 / (0.5 * 0.5)), dataIntervals); EvaluateTest("R/sqr(r)", new Interval(0.2 / (0.8 * 0.8), 0.5 / (0.5 * 0.5)), dataIntervals); EvaluateTest("R*R/sqr(r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals); EvaluateTest("sqr(R)/sqr(r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals); EvaluateTest("sqr(R/r)", new Interval(0.2 * 0.2 / (0.8 * 0.8), 0.5 * 0.5 / (0.5 * 0.5)), dataIntervals); } [TestMethod] [TestCategory("Problems.DataAnalysis.Symbolic")] [TestProperty("Time", "short")] public void IntervalEvaluatorSqrt() { EvaluateTest("sqrt(x1)", new Interval(0, 1), new Dictionary() { {"x1", new Interval(0, 1) }}); } [TestMethod] [TestCategory("Problems.DataAnalysis")] [TestProperty("Time", "short")] public void IntervalEvaluatorExamples() { var parser = new InfixExpressionParser(); var evaluator = new IntervalEvaluator(); var intervals = new Dictionary() { {"x", new Interval(1, 2) }, {"unit", new Interval(0, 1) }, }; var t = parser.Parse("SQR(EXP(CUBE((2.10981074965936*'x'))))"); AssertInterval(143638040.396283, 1.81198989971641E+65, evaluator.Evaluate(t, intervals)); t = parser.Parse("sqrt(unit)"); AssertInterval(0, 1, evaluator.Evaluate(t, intervals)); } private void AssertInterval(double expectedLow, double expectedHigh, Interval r) { Assert.AreEqual(expectedLow, r.LowerBound, Math.Abs(expectedLow * 1e-5)); Assert.AreEqual(expectedHigh, r.UpperBound, Math.Abs(expectedHigh * 1e-5)); } [TestMethod] [TestCategory("Problems.DataAnalysis")] [TestProperty("Time", "long")] public void IntervalEvaluatorConsistencyForRandomExpressions() { var grammar = new TypeCoherentExpressionGrammar(); grammar.ConfigureAsDefaultRegressionGrammar(); // activate supported symbols grammar.Symbols.First(s => s is Square).Enabled = true; grammar.Symbols.First(s => s is SquareRoot).Enabled = true; grammar.Symbols.First(s => s is Cube).Enabled = true; grammar.Symbols.First(s => s is CubeRoot).Enabled = true; grammar.Symbols.First(s => s is Sine).Enabled = true; grammar.Symbols.First(s => s is Cosine).Enabled = true; grammar.Symbols.First(s => s is Exponential).Enabled = true; grammar.Symbols.First(s => s is Logarithm).Enabled = true; grammar.Symbols.First(s => s is Absolute).Enabled = false; // XXX not yet supported by old interval calculator grammar.Symbols.First(s => s is AnalyticQuotient).Enabled = false; // not yet supported by old interval calculator var varSy = (Variable)grammar.Symbols.First(s => s is Variable); varSy.AllVariableNames = new string[] { "x", "y" }; varSy.VariableNames = varSy.AllVariableNames; varSy.WeightMu = 1.0; varSy.WeightSigma = 1.0; var rand = new FastRandom(1234); var eval1 = new IntervalEvaluator(); var eval2 = new IntervalInterpreter(); IDictionary posIntervals = new Dictionary() { { "x", new Interval(1, 2) }, { "y", new Interval(0, 1) } }; IDictionary negIntervals = new Dictionary() { { "x", new Interval(-2, -1) }, { "y", new Interval(-1, 0) } }; IDictionary fullIntervals = new Dictionary() { { "x", new Interval(-2, 2) }, { "y", new Interval(-1, 1) } }; IDictionary specialIntervals = new Dictionary() { { "x", new Interval(1, double.PositiveInfinity) }, { "y", new Interval(double.NegativeInfinity, double.PositiveInfinity) } }; var formatter = new InfixExpressionFormatter(); var sb = new StringBuilder(); foreach (var interval in new[] { posIntervals, negIntervals, fullIntervals, specialIntervals }) { int N = 10000; int i = 0; while (i < N) { var t = ProbabilisticTreeCreator.Create(rand, grammar, maxTreeLength: 5, maxTreeDepth: 5); var r1 = eval1.Evaluate(t, interval); var r2 = eval2.GetSymbolicExpressionTreeInterval(t, interval); // Console.WriteLine(formatter.Format(t)); // all NaN is ok (but don't count NaN expressions) if (double.IsNaN(r1.LowerBound) && double.IsNaN(r2.LowerBound) && double.IsNaN(r1.UpperBound) && double.IsNaN(r2.UpperBound)) continue; if (r1.LowerBound == r2.LowerBound && r1.UpperBound == r2.UpperBound) { /* exactly the same value (incl. Inf / -Inf) => ok */ } else if ((Math.Abs(r1.LowerBound - r2.LowerBound) <= Math.Max(1e-10, Math.Abs(r1.LowerBound * 1e-4))) && (Math.Abs(r1.UpperBound - r2.UpperBound) <= Math.Max(1e-10, Math.Abs(r1.UpperBound * 1e-4)))) { /* approximately the same value => OK */ } else { sb.AppendLine($"{r1} <> {r2} for {formatter.Format(t)} x={interval["x"]} y={interval["y"]}"); } i++; } } if (sb.Length > 0) { Console.WriteLine(sb.ToString()); Assert.Fail("There were different interval calculation results"); } } [TestMethod] [TestCategory("Problems.DataAnalysis")] [TestProperty("Time", "short")] public void IntervalEvaluatorConsistencyForExamples() { var parser = new InfixExpressionParser(); var eval1 = new IntervalEvaluator(); var eval2 = new IntervalInterpreter(); IDictionary interval = new Dictionary() { { "x", new Interval(1, 2) }, { "y", new Interval(0, 1) }, { "z", new Interval(double.NegativeInfinity, double.PositiveInfinity) }, }; var exprs = new string[] { "CUBE((0.642971622547268*'x')) * (-16.5400720573962)", "sqr(y / y)", // one interpreter produces [NaN, inf], the other [NaN, 0] "cuberoot(-x)", // the old interpreter calculates cuberoot incorrectly "sqr(log(-x))", // Interval: [NaN, NaN] <> Interval (old): [NaN, 0] "log(1.8*'y' - 1.4*'y')", // Interval: [NaN, 0,587786664902119] <> Interval (old): [0,587786664902119, NaN] "log(z)", // Interval: [NaN, ∞] <> Interval (old): [∞, NaN] "sqr(sqrt(-1))" // Interval: [NaN, NaN] <> Interval (old): [NaN, 0] }; var formatter = new InfixExpressionFormatter(); var sb = new StringBuilder(); foreach (var expr in exprs) { var t = parser.Parse(expr); var r1 = eval1.Evaluate(t, interval); var r2 = eval2.GetSymbolicExpressionTreeInterval(t, interval); // Console.WriteLine(formatter.Format(t)); // all NaN is ok if (double.IsNaN(r1.LowerBound) && double.IsNaN(r2.LowerBound) && double.IsNaN(r1.UpperBound) && double.IsNaN(r2.UpperBound)) continue; if (r1.LowerBound == r2.LowerBound && r1.UpperBound == r2.UpperBound) continue; // Inf, -Inf and exactly the same value are ok if ((Math.Abs(r1.LowerBound - r2.LowerBound) <= Math.Abs(r1.LowerBound * 1e-4)) && (Math.Abs(r1.UpperBound - r2.UpperBound) <= Math.Abs(r1.UpperBound * 1e-4))) { /* OK */ } else { sb.AppendLine($"{r1} <> {r2} for {formatter.Format(t)} x={interval["x"]} y={interval["y"]}"); } } if (sb.Length > 0) { Console.WriteLine(sb.ToString()); Assert.Fail("There were different interval calculation results"); } } } }