Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17584


Ignore:
Timestamp:
06/04/20 14:20:19 (4 years ago)
Author:
chaider
Message:

#2971 Updated unit tests for interval

Location:
trunk/HeuristicLab.Tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/IntervalTest.cs

    r17350 r17584  
    11using System;
     2using System.Collections.Generic;
    23using Microsoft.VisualStudio.TestTools.UnitTesting;
    34
     
    141142    [TestProperty("Time", "short")]
    142143    public void SquarerootIntervalTest() {
    143       Assert.AreEqual<Interval>(new Interval(1, 2), Interval.SquareRoot(new Interval(1, 4)));
     144      Assert.AreEqual<Interval>(new Interval(-2, 2), Interval.SquareRoot(new Interval(1, 4)));
    144145      Assert.AreEqual<Interval>(new Interval(double.NaN, double.NaN), Interval.SquareRoot(new Interval(-4, -1)));
    145146    }
     
    192193
    193194      //Second interval goes over zero
    194       Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound), Interval.AnalyticalQuotient(aPos, bZero));
    195       Assert.AreEqual(new Interval(aZero.LowerBound, aZero.UpperBound), Interval.AnalyticalQuotient(aZero, bZero));
    196       Assert.AreEqual(new Interval(aNeg.LowerBound, aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bZero));
    197       //Second interval is positive
    198       Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aPos, bPos));
    199       Assert.AreEqual(new Interval(aZero.LowerBound/Math.Sqrt(5), aZero.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aZero, bPos));
    200       Assert.AreEqual(new Interval(aNeg.LowerBound/Math.Sqrt(5), aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bPos));
    201       //Second interval is negative
    202       Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aPos, bNeg));
    203       Assert.AreEqual(new Interval(aZero.LowerBound/Math.Sqrt(5), aZero.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aZero, bNeg));
    204       Assert.AreEqual(new Interval(aNeg.LowerBound/Math.Sqrt(5), aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bNeg));
     195      //Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound), Interval.AnalyticalQuotient(aPos, bZero));
     196      //Assert.AreEqual(new Interval(aZero.LowerBound, aZero.UpperBound), Interval.AnalyticalQuotient(aZero, bZero));
     197      //Assert.AreEqual(new Interval(aNeg.LowerBound, aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bZero));
     198      ////Second interval is positive
     199      //Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aPos, bPos));
     200      //Assert.AreEqual(new Interval(aZero.LowerBound/Math.Sqrt(5), aZero.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aZero, bPos));
     201      //Assert.AreEqual(new Interval(aNeg.LowerBound/Math.Sqrt(5), aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bPos));
     202      ////Second interval is negative
     203      //Assert.AreEqual(new Interval(aPos.LowerBound/Math.Sqrt(17), aPos.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aPos, bNeg));
     204      //Assert.AreEqual(new Interval(aZero.LowerBound/Math.Sqrt(5), aZero.UpperBound/Math.Sqrt(5)), Interval.AnalyticalQuotient(aZero, bNeg));
     205      //Assert.AreEqual(new Interval(aNeg.LowerBound/Math.Sqrt(5), aNeg.UpperBound/Math.Sqrt(17)), Interval.AnalyticalQuotient(aNeg, bNeg));
     206      Assert.AreEqual(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.AnalyticalQuotient(aPos, bZero));
     207      Assert.AreEqual(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.AnalyticalQuotient(aPos, bPos));
     208      Assert.AreEqual(new Interval(double.NegativeInfinity, double.PositiveInfinity), Interval.AnalyticalQuotient(aZero, bNeg));
    205209    }
    206210
     
    247251      Assert.AreEqual(0, zeroInterval.LowerBound);
    248252      Assert.AreEqual(0, zeroInterval.UpperBound);
    249 
    250     }
     253    }
     254
     255    [TestMethod]
     256    [TestCategory("Problems.DataAnalysis")]
     257    [TestProperty("Time", "short")]
     258    public void ContaintsTest() {
     259      var negativeInterval = new Interval(-10, -5);
     260      var positiveInterval = new Interval(5, 10);
     261      var overZeroInterval = new Interval(-5, 5);
     262
     263      //Tests for negative intervals
     264      Assert.AreEqual(true, negativeInterval.Contains(new Interval(-9, -7)));
     265      Assert.AreEqual(false, negativeInterval.Contains(new Interval(-11, -3)));
     266      Assert.AreEqual(false, negativeInterval.Contains(positiveInterval));
     267      Assert.AreEqual(false, negativeInterval.Contains(overZeroInterval));
     268      Assert.AreEqual(true, negativeInterval.Contains(-8));
     269      Assert.AreEqual(false, negativeInterval.Contains(-12));
     270      Assert.AreEqual(false, negativeInterval.Contains(0));
     271      //Testes for positive intervals
     272      Assert.AreEqual(true, positiveInterval.Contains(new Interval(6, 10)));
     273      Assert.AreEqual(false, positiveInterval.Contains(new Interval(6, 12)));
     274      Assert.AreEqual(false, positiveInterval.Contains(negativeInterval));
     275      Assert.AreEqual(false, positiveInterval.Contains(overZeroInterval));
     276      Assert.AreEqual(true, positiveInterval.Contains(7));
     277      Assert.AreEqual(false, positiveInterval.Contains(11));
     278      Assert.AreEqual(false, positiveInterval.Contains(0));
     279      //Tests for over zero intervals
     280      Assert.AreEqual(true, overZeroInterval.Contains(new Interval(-3, 3)));
     281      Assert.AreEqual(true, overZeroInterval.Contains(new Interval(-4, -1)));
     282      Assert.AreEqual(true, overZeroInterval.Contains(new Interval(1, 5)));
     283      Assert.AreEqual(false, overZeroInterval.Contains(new Interval(-6, 0)));
     284      Assert.AreEqual(false, overZeroInterval.Contains(new Interval(0, 6)));
     285      Assert.AreEqual(false, overZeroInterval.Contains(new Interval(-7, 7)));
     286      Assert.AreEqual(true, overZeroInterval.Contains(-3));
     287      Assert.AreEqual(true, overZeroInterval.Contains(0));
     288      Assert.AreEqual(true, overZeroInterval.Contains(3));
     289      Assert.AreEqual(false, overZeroInterval.Contains(12));
     290      Assert.AreEqual(false, overZeroInterval.Contains(-7));
     291    }
     292
     293    [TestMethod]
     294    [TestCategory("Problems.DataAnalysis")]
     295    [TestProperty("Time", "short")]
     296    public void GetIntervalTest() {
     297      var values = new List<double>() { -2.5, -9, 2, 7, 0 ,12, 12.4, 12.4, 9.29, 1002, -29.9};
     298      var valuesNan = new List<double>() { double.NaN, 2, 4, 19, -2, -12.2};
     299      var valuesInf = new List<double>() {double.NegativeInfinity, double.PositiveInfinity, 12, 2, -2, -12.2};
     300
     301      var valuesInterval = new Interval(-29.9, 1002);
     302      var valuesNanInterval = new Interval(double.NaN, double.NaN);
     303      var valuesInfInterval = new Interval(double.NegativeInfinity, double.PositiveInfinity);
     304
     305
     306      Assert.AreEqual(valuesInterval, Interval.GetInterval(values));
     307      Assert.AreEqual(valuesNanInterval, Interval.GetInterval(valuesNan));
     308      Assert.AreEqual(valuesInfInterval, Interval.GetInterval(valuesInf));
     309    }
     310
     311    [TestMethod]
     312    [TestCategory("Problems.DataAnalysis")]
     313    [TestProperty("Time", "short")]
     314    public void EqualsTest() {
     315      var interval1 = new Interval(0, 12);
     316      var interval2 = new Interval(-12, 8);
     317      var interval3 = new Interval(double.NegativeInfinity, 0);
     318
     319      Assert.AreEqual(true, interval1.Equals(new Interval(0, 12)));
     320      Assert.AreEqual(false, interval1.Equals(interval2));
     321      Assert.AreEqual(true, interval3 == new Interval(double.NegativeInfinity, 0));
     322      Assert.AreEqual(true, interval1 != interval2);
     323      Assert.AreEqual(false, interval2 == interval3);
     324      Assert.AreEqual(false, interval1 != new Interval(0, 12));
     325    }
     326    //[TestMethod]
     327    //[TestCategory("Problems.DataAnalysis")]
     328    //[TestProperty("Time", "short")]
     329    //public void RootTest() {
     330    //  var posInterval = new Interval(3, 5);
     331    //  var negInterval = new Interval(-6, -2);
     332    //  var posIntervalConst = new Interval(5, 5);
     333    //  var negIntervalConst = new Interval(-3, -3);
     334    //  var zeroIntervalConst = new Interval(0, 0);
     335    //  var zeroPosInterval = new Interval(0, 2);
     336    //  var zeroNegInterval = new Interval(-2, 0);
     337
     338    //  var posRoot = new Interval(4, 4);
     339    //  var negRoot = new Interval(-4, -4);
     340    //  var zeroRoot = new Interval(0, 0);
     341    //  var oneRoot = new Interval(1, 1);
     342
     343    //  Assert.AreEqual(Interval.Root(posInterval, posRoot), new Interval(Math.Pow(3, (1.0/4)), Math.Pow(5, (1.0/4))));
     344    //  Assert.AreEqual(Interval.Root(posInterval, negRoot), new Interval(Math.Pow(5, -(1.0/4)), Math.Pow(3, -(1.0/4))));
     345    //  Assert.AreEqual(Interval.Root(posInterval, zeroRoot), new Interval(double.NaN, double.NaN));
     346    //  Assert.AreEqual(Interval.Root(posInterval, oneRoot), new Interval(3, 5));
     347
     348    //  Assert.AreEqual(Interval.Root(negInterval, posRoot), new Interval(Math.Pow(-6, (1.0 / 4)), Math.Pow(-2, (1.0 / 4))));
     349    //  Assert.AreEqual(Interval.Root(negInterval, negRoot), new Interval(Math.Pow(-2, -(1.0 / 4)), Math.Pow(-6, -(1.0 / 4))));
     350    //  Assert.AreEqual(Interval.Root(negInterval, zeroRoot), new Interval(double.NaN, double.NaN));
     351    //  Assert.AreEqual(Interval.Root(negInterval, oneRoot), new Interval(-6, -2));
     352
     353    //  Assert.AreEqual(Interval.Root(posIntervalConst, posRoot), new Interval(Math.Pow(5, (1.0 / 4)), Math.Pow(5, (1.0 / 4))));
     354    //  Assert.AreEqual(Interval.Root(posIntervalConst, negRoot), new Interval(Math.Pow(5, -(1.0 / 4)), Math.Pow(5, -(1.0 / 4))));
     355    //  Assert.AreEqual(Interval.Root(posIntervalConst, zeroRoot), new Interval(double.NaN, double.NaN));
     356    //  Assert.AreEqual(Interval.Root(posIntervalConst, oneRoot), new Interval(5, 5));
     357
     358    //  Assert.AreEqual(Interval.Root(negIntervalConst, posRoot), new Interval(Math.Pow(-3, (1.0 / 4)), Math.Pow(-3, (1.0 / 4))));
     359    //  Assert.AreEqual(Interval.Root(negIntervalConst, negRoot), new Interval(Math.Pow(-3, -(1.0 / 4)), Math.Pow(-3, -(1.0 / 4))));
     360    //  Assert.AreEqual(Interval.Root(negIntervalConst, zeroRoot), new Interval(double.NaN, double.NaN));
     361    //  Assert.AreEqual(Interval.Root(negIntervalConst, oneRoot), new Interval(-3, -3));
     362
     363    //  Assert.AreEqual(Interval.Root(zeroIntervalConst, posRoot), new Interval(0, 0));
     364    //  //Compley Infinity https://www.wolframalpha.com/input/?i=root%28-4%2C+0%29
     365    //  Assert.AreEqual(Interval.Root(zeroIntervalConst, negRoot), new Interval(double.PositiveInfinity, double.PositiveInfinity));
     366    //  Assert.AreEqual(Interval.Root(zeroIntervalConst, zeroRoot), new Interval(0, 0));
     367    //  Assert.AreEqual(Interval.Root(zeroIntervalConst, oneRoot), new Interval(0, 0));
     368
     369    //  Assert.AreEqual(Interval.Root(zeroPosInterval, posRoot), new Interval(0, Math.Pow(2, (1.0 / 4))));
     370    //  //Check for complex infinity
     371    //  Assert.AreEqual(Interval.Root(zeroPosInterval, negRoot), new Interval(Math.Pow(2, -(1.0 / 4)), double.PositiveInfinity));
     372    //  Assert.AreEqual(Interval.Root(zeroPosInterval, zeroRoot), new Interval(0, 0));
     373    //  Assert.AreEqual(Interval.Root(zeroPosInterval, oneRoot), new Interval(0, 2));
     374
     375    //  Assert.AreEqual(Interval.Root(zeroNegInterval, posRoot), new Interval(Math.Pow(-2, (1.0 / 4)),0));
     376    //  //Check for complex infinity
     377    //  Assert.AreEqual(Interval.Root(zeroNegInterval, negRoot), new Interval(Math.Pow(-2, -(1.0 / 4)), double.PositiveInfinity));
     378    //  Assert.AreEqual(Interval.Root(zeroNegInterval, zeroRoot), new Interval(double.NaN, double.NaN));
     379    //  Assert.AreEqual(Interval.Root(zeroNegInterval, oneRoot), new Interval(-2, 0));
     380    //}
    251381  }
    252382}
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/IntervalInterpreterTest.cs

    r16769 r17584  
    4040      Interval result;
    4141      if (variableRanges == null)
    42         result = interpreter.GetSymbolicExpressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices);
     42        result = interpreter.GetSymbolicExpressionTreeInterval(tree, problemData.Dataset, rows:problemData.AllIndices);
    4343      else
    4444        result = interpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
     
    163163    }
    164164
    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     }
    179165  }
    180166}
Note: See TracChangeset for help on using the changeset viewer.