Ignore:
Timestamp:
05/04/19 08:22:42 (4 months ago)
Author:
gkronber
Message:

#2925 merged r16661:16890 from trunk to branch

Location:
branches/2925_AutoDiffForDynamicalModels
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Tests

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/IntervalInterpreterTest.cs

    r16662 r16892  
    3434    }
    3535
    36     private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null) {
     36    private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null, double lowerDelta =0, double upperDelta = 0) {
    3737      var parser = new InfixExpressionParser();
    3838      var tree = parser.Parse(expression);
     
    4040      Interval result;
    4141      if (variableRanges == null)
    42         result = interpreter.GetSymbolicExressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices);
     42        result = interpreter.GetSymbolicExpressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices);
    4343      else
    44         result = interpreter.GetSymbolicExressionTreeInterval(tree, variableRanges);
     44        result = interpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
    4545
    46       Assert.AreEqual(expectedResult, result);
     46      Assert.AreEqual(expectedResult.LowerBound, result.LowerBound, lowerDelta);
     47      Assert.AreEqual(expectedResult.UpperBound, result.UpperBound, upperDelta);
    4748    }
    4849
     
    7980      EvaluateTest("sin(x1+x2)", new Interval(-1, 1), variableRanges);
    8081      EvaluateTest("sin(1+2)", new Interval(Math.Sin(3), Math.Sin(3)));
     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);
    8195    }
    8296
     
    87101      EvaluateTest("cos(x1+x2)", new Interval(-1, 1));
    88102      EvaluateTest("cos(x1+x2)", new Interval(-1, 1), variableRanges);
    89       EvaluateTest("cos(1+2)", new Interval(Math.Sin(3 - Math.PI / 2), Math.Sin(3 - Math.PI / 2)));
     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
    90118    }
     119
     120    [TestMethod]
     121    [TestCategory("Problems.DataAnalysis.Symbolic")]
     122    [TestProperty("Time", "short")]
     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
    91156
    92157    [TestMethod]
Note: See TracChangeset for help on using the changeset viewer.