Changeset 18175 for branches/3140_NumberSymbol/HeuristicLab.Tests
- Timestamp:
- 01/03/22 11:49:24 (2 years ago)
- Location:
- branches/3140_NumberSymbol
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3140_NumberSymbol
- Property svn:mergeinfo changed
/trunk merged: 18136-18138,18153,18165-18174
- Property svn:mergeinfo changed
-
branches/3140_NumberSymbol/HeuristicLab.Tests
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Tests (added) merged: 18167-18173
- Property svn:mergeinfo changed
-
branches/3140_NumberSymbol/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/DeriveTest.cs
r18120 r18175 34 34 public void DeriveExpressions() { 35 35 var formatter = new InfixExpressionFormatter(); 36 var parser = new InfixExpressionParser();37 36 Assert.AreEqual("0", Derive("<num=3>", "x")); 38 37 Assert.AreEqual("1", Derive("x", "x")); 39 38 Assert.AreEqual("10", Derive("<num=10>*x", "x")); 40 39 Assert.AreEqual("10", Derive("x*<num=10>", "x")); 41 Assert.AreEqual(" (2*'x')", Derive("x*x", "x"));42 Assert.AreEqual(" ((('x' * 'x') * 2) + ('x' * 'x'))", Derive("x*x*x", "x")); // simplifier does not merge (x*x)*2 + x*x to 3*x*x40 Assert.AreEqual("2*'x'", Derive("x*x", "x")); 41 Assert.AreEqual("'x' * 'x' * 2 + 'x' * 'x'", Derive("x*x*x", "x")); // simplifier does not merge (x*x)*2 + x*x to 3*x*x 43 42 Assert.AreEqual("0", Derive("<num=10>*x", "y")); 44 43 Assert.AreEqual("20", Derive("<num=10>*x+<num=20>*y", "y")); 45 44 Assert.AreEqual("6", Derive("<num=2>*<num=3>*x", "x")); 46 Assert.AreEqual(" (10*'y')", Derive("<num=10>*x*y+<num=20>*y", "x"));47 Assert.AreEqual(" (1 / (SQR('x') * (-1)))", Derive("1/x", "x"));48 Assert.AreEqual(" ('y' / (SQR('x') * (-1)))", Derive("y/x", "x"));49 Assert.AreEqual("( (((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + ('x' * 'x'))))",45 Assert.AreEqual("10*'y'", Derive("<num=10>*x*y+<num=20>*y", "x")); 46 Assert.AreEqual("1 * -1 / SQR('x')", Derive("1/x", "x")); 47 Assert.AreEqual("-1*'y' / SQR('x')", Derive("y/x", "x")); 48 Assert.AreEqual("('a' + 'b') * (-2*'x' + -1) / SQR('x' + 'x' * 'x')", 50 49 Derive("(a+b)/(x+x*x)", "x")); 51 Assert.AreEqual("( (((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + SQR('x'))))", Derive("(a+b)/(x+SQR(x))", "x"));50 Assert.AreEqual("('a' + 'b') * (-2*'x' + -1) / SQR('x' + SQR('x'))", Derive("(a+b)/(x+SQR(x))", "x")); 52 51 Assert.AreEqual("EXP('x')", Derive("exp(x)", "x")); 53 Assert.AreEqual(" (EXP((3*'x')) * 3)", Derive("exp(<num=3>*x)", "x"));54 Assert.AreEqual(" (1 / 'x')", Derive("log(x)", "x"));55 Assert.AreEqual(" (1 / 'x')", Derive("log(<num=3>*x)", "x")); // 3 * 1/(3*x)56 Assert.AreEqual(" (1 / ('x' + (0.333333333333333*'y')))", Derive("log(<num=3>*x+y)", "x")); // simplifier does not try to keep fractions57 Assert.AreEqual(" (1 / (SQRT(((3*'x') + 'y')) * 0.666666666666667))", Derive("sqrt(<num=3>*x+y)", "x")); // 3 / (2 * sqrt(3*x+y)) = 1 / ((2/3) * sqrt(3*x+y))58 Assert.AreEqual(" (COS((3*'x')) * 3)", Derive("sin(<num=3>*x)", "x"));59 Assert.AreEqual(" (SIN((3*'x')) * (-3))", Derive("cos(<num=3>*x)", "x"));60 Assert.AreEqual(" (1 / (SQR(COS((3*'x'))) * 0.333333333333333))", Derive("tan(<num=3>*x)", "x")); // diff(tan(f(x)), x) = 1.0 / cos²(f(x)), simplifier puts constant factor into the denominator52 Assert.AreEqual("EXP(3*'x') * 3", Derive("exp(<num=3>*x)", "x")); 53 Assert.AreEqual("1 / 'x'", Derive("log(x)", "x")); 54 Assert.AreEqual("1 / 'x'", Derive("log(<num=3>*x)", "x")); // 3 * 1/(3*x) 55 Assert.AreEqual("1 / ('x' + 0.333333333333333*'y')", Derive("log(<num=3>*x+y)", "x")); // simplifier does not try to keep fractions 56 Assert.AreEqual("1 / (SQRT(3*'x' + 'y') * 0.666666666666667)", Derive("sqrt(<num=3>*x+y)", "x")); // 3 / (2 * sqrt(3*x+y)) = 1 / ((2/3) * sqrt(3*x+y)) 57 Assert.AreEqual("COS(3*'x') * 3", Derive("sin(<num=3>*x)", "x")); 58 Assert.AreEqual("SIN(3*'x') * -3", Derive("cos(<num=3>*x)", "x")); 59 Assert.AreEqual("1 / (SQR(COS(3*'x')) * 0.333333333333333)", Derive("tan(<num=3>*x)", "x")); // diff(tan(f(x)), x) = 1.0 / cos²(f(x)), simplifier puts constant factor into the denominator 61 60 62 Assert.AreEqual(" ((9*'x') / ABS((3*'x')))", Derive("abs(<num=3>*x)", "x"));63 Assert.AreEqual(" (SQR('x') * 3)", Derive("cube(x)", "x"));64 Assert.AreEqual(" (1 / (SQR(CUBEROOT('x')) * 3))", Derive("cuberoot(x)", "x"));61 Assert.AreEqual("9*'x' / ABS(3*'x')", Derive("abs(<num=3>*x)", "x")); 62 Assert.AreEqual("SQR('x') * 3", Derive("cube(x)", "x")); 63 Assert.AreEqual("1 / (SQR(CUBEROOT('x')) * 3)", Derive("cuberoot(x)", "x")); 65 64 66 65 Assert.AreEqual("0", Derive("(a+b)/(x+SQR(x))", "y")); // df(a,b,x) / dy = 0 67 66 68 67 69 Assert.AreEqual(" ('a' * 'b' * 'c')", Derive("a*b*c*d", "d"));70 Assert.AreEqual(" ('a' / ('b' * 'c' * SQR('d') * (-1)))", Derive("a/b/c/d", "d"));68 Assert.AreEqual("'a' * 'b' * 'c'", Derive("a*b*c*d", "d")); 69 Assert.AreEqual("'b' * 'c' * -1*'a' / (SQR('b') * SQR('c') * SQR('d'))", Derive("a/b/c/d", "d")); // TODO simplifier should be able to simplify this 71 70 72 Assert.AreEqual(" ('x' * ((SQR(TANH(SQR('x'))) * (-1)) + 1) * 2)", Derive("tanh(sqr(x))", "x")); // (2*'x'*(1 - SQR(TANH(SQR('x'))))71 Assert.AreEqual("'x' * (SQR(TANH(SQR('x'))) * -1 + 1) * 2", Derive("tanh(sqr(x))", "x")); // (2*'x'*(1 - SQR(TANH(SQR('x')))) 73 72 74 73 { … … 85 84 root.AddSubtree(start); 86 85 var t = new SymbolicExpressionTree(root); 87 Assert.AreEqual(" (1 / (SQR('x') * (-1)))",86 Assert.AreEqual("1 / (SQR('x') * -1)", 88 87 formatter.Format(DerivativeCalculator.Derive(t, "x"))); 89 88 } … … 125 124 var t = new SymbolicExpressionTree(root); 126 125 127 Assert.AreEqual(" (('y' * 'z' * 60) / (SQR('y') * SQR('z') * 400))", // actually 3 / (4y 5z) but simplifier is not smart enough to cancel numerator and denominator126 Assert.AreEqual("'y' * 'z' * 60 / (SQR('y') * SQR('z') * 400)", // actually 3 / (4y 5z) but simplifier is not smart enough to cancel numerator and denominator 128 127 // 60 y z / y² z² 20² == 6 / y z 40 == 3 / y z 20 129 128 formatter.Format(DerivativeCalculator.Derive(t, "x"))); 130 Assert.AreEqual(" (('x' * 'z' * (-60)) / (SQR('y') * SQR('z') * 400))", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z)129 Assert.AreEqual("'x' * 'z' * -60 / (SQR('y') * SQR('z') * 400)", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z) 131 130 // -3 4 5 x z / 4² y² 5² z² = -60 x z / 20² z² y² == -60 x z / y² z² 20² 132 131 formatter.Format(DerivativeCalculator.Derive(t, "y"))); 133 Assert.AreEqual(" (('x' * 'y' * (-60)) / (SQR('y') * SQR('z') * 400))",132 Assert.AreEqual("'x' * 'y' * -60 / (SQR('y') * SQR('z') * 400)", 134 133 formatter.Format(DerivativeCalculator.Derive(t, "z"))); 135 134 } -
branches/3140_NumberSymbol/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs
r17180 r18175 34 34 var formatter = new InfixExpressionFormatter(); 35 35 var parser = new InfixExpressionParser(); 36 Console.WriteLine(formatter.Format(parser.Parse("3")));37 Console.WriteLine(formatter.Format(parser.Parse("3*3")));38 Console.WriteLine(formatter.Format(parser.Parse("3 * 4")));39 Console.WriteLine(formatter.Format(parser.Parse("123E-03")));40 Console.WriteLine(formatter.Format(parser.Parse("123e-03")));41 Console.WriteLine(formatter.Format(parser.Parse("123e+03")));42 Console.WriteLine(formatter.Format(parser.Parse("123E+03")));43 Console.WriteLine(formatter.Format(parser.Parse("123.0E-03")));44 Console.WriteLine(formatter.Format(parser.Parse("123.0e-03")));45 Console.WriteLine(formatter.Format(parser.Parse("123.0e+03")));46 Console.WriteLine(formatter.Format(parser.Parse("123.0E+03")));47 Console.WriteLine(formatter.Format(parser.Parse("123.0E-3")));48 Console.WriteLine(formatter.Format(parser.Parse("123.0e-3")));49 Console.WriteLine(formatter.Format(parser.Parse("123.0e+3")));50 Console.WriteLine(formatter.Format(parser.Parse("123.0E+3")));36 Assert.AreEqual("3", formatter.Format(parser.Parse("3"))); 37 Assert.AreEqual("3 * 3", formatter.Format(parser.Parse("3*3"))); 38 Assert.AreEqual("3 * 4",formatter.Format(parser.Parse("3 * 4"))); 39 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123E-03"))); 40 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123e-03"))); 41 Assert.AreEqual("123000",formatter.Format(parser.Parse("123e+03"))); 42 Assert.AreEqual("123000",formatter.Format(parser.Parse("123E+03"))); 43 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0E-03"))); 44 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0e-03"))); 45 Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0e+03"))); 46 Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0E+03"))); 47 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0E-3"))); 48 Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0e-3"))); 49 Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0e+3"))); 50 Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0E+3"))); 51 51 52 Console.WriteLine(formatter.Format(parser.Parse("3.1415+2.0")));53 Console.WriteLine(formatter.Format(parser.Parse("3.1415/2.0")));54 Console.WriteLine(formatter.Format(parser.Parse("3.1415*2.0")));55 Console.WriteLine(formatter.Format(parser.Parse("3.1415-2.0")));52 Assert.AreEqual("3.1415 + 2",formatter.Format(parser.Parse("3.1415+2.0"))); 53 Assert.AreEqual("3.1415 / 2",formatter.Format(parser.Parse("3.1415/2.0"))); 54 Assert.AreEqual("3.1415 * 2",formatter.Format(parser.Parse("3.1415*2.0"))); 55 Assert.AreEqual("3.1415 - 2", formatter.Format(parser.Parse("3.1415-2.0"))); 56 56 // round-trip 57 Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0")))));58 Console.WriteLine(formatter.Format(parser.Parse("3.1415+(2.0)")));59 Console.WriteLine(formatter.Format(parser.Parse("(3.1415+(2.0))")));57 Assert.AreEqual("3.1415 - 2",formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0"))))); 58 Assert.AreEqual("3.1415 + 2",formatter.Format(parser.Parse("3.1415+(2.0)"))); 59 Assert.AreEqual("3.1415 + 2", formatter.Format(parser.Parse("(3.1415+(2.0))"))); 60 60 61 61 62 Console.WriteLine(formatter.Format(parser.Parse("log(3)")));63 Console.WriteLine(formatter.Format(parser.Parse("log(-3)"))); // should produce log(-3) as output64 Console.WriteLine(formatter.Format(parser.Parse("exp(3)")));65 Console.WriteLine(formatter.Format(parser.Parse("exp(-3)"))); // should produce exp(-3) as output66 Console.WriteLine(formatter.Format(parser.Parse("sqrt(3)")));62 Assert.AreEqual("LOG(3)",formatter.Format(parser.Parse("log(3)"))); 63 Assert.AreEqual("LOG(-3)",formatter.Format(parser.Parse("log(-3)"))); 64 Assert.AreEqual("EXP(3)",formatter.Format(parser.Parse("exp(3)"))); 65 Assert.AreEqual("EXP(-3)",formatter.Format(parser.Parse("exp(-3)"))); 66 Assert.AreEqual("SQRT(3)", formatter.Format(parser.Parse("sqrt(3)"))); 67 67 68 Console.WriteLine(formatter.Format(parser.Parse("sqr((-3))"))); // should produce sqr((-3)) as output68 Assert.AreEqual("SQR(-3)", formatter.Format(parser.Parse("sqr((-3))"))); 69 69 70 Console.WriteLine(formatter.Format(parser.Parse("3/3+2/2+1/1"))); // ((3 * 1 / 3) + (2 * 1 / 2) + (1 * 1 / 1)) 71 Console.WriteLine(formatter.Format(parser.Parse("-3+30-2+20-1+10"))); // not (30 + 20 + 10 + ((-1) * (3 + 2 + 1))) 72 // round trip 73 Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("-3+30-2+20-1+10"))))); // not (30 + 20 + 10 + ((-(1)) * (3 + 2 + 1))) 70 Assert.AreEqual("3 / 3 + 2 / 2 + 1 / 1",formatter.Format(parser.Parse("3/3+2/2+1/1"))); 71 Assert.AreEqual("-3 + 30 - 2 + 20 - 1 + 10", formatter.Format(parser.Parse("-3+30-2+20-1+10"))); 74 72 75 Console.WriteLine(formatter.Format(parser.Parse("\"x1\""))); 76 Console.WriteLine(formatter.Format(parser.Parse("\'var name\'"))); 77 Console.WriteLine(formatter.Format(parser.Parse("\"var name\""))); 78 Console.WriteLine(formatter.Format(parser.Parse("\"1\""))); 73 // 'flattening' of nested addition, subtraction, multiplication, or division 74 Assert.AreEqual("1 + 2 + 3 + 4", formatter.Format(parser.Parse("1 + 2 + 3 + 4"))); 75 Assert.AreEqual("1 - 2 - 3 - 4", formatter.Format(parser.Parse("1 - 2 - 3 - 4"))); 76 Assert.AreEqual("1 * 2 * 3 * 4", formatter.Format(parser.Parse("1 * 2 * 3 * 4"))); 77 Assert.AreEqual("1 / 2 / 3 / 4", formatter.Format(parser.Parse("1 / 2 / 3 / 4"))); 79 78 80 Console.WriteLine(formatter.Format(parser.Parse("'var \" name\'"))); 81 Console.WriteLine(formatter.Format(parser.Parse("\"var \' name\""))); 79 // signed variables / constants 80 Assert.AreEqual("-1*'x1'", formatter.Format(parser.Parse("-x1"))); 81 Assert.AreEqual("1", formatter.Format(parser.Parse("--1.0"))); 82 Assert.AreEqual("1", formatter.Format(parser.Parse("----1.0"))); 83 Assert.AreEqual("1", formatter.Format(parser.Parse("-+-1.0"))); 84 Assert.AreEqual("1", formatter.Format(parser.Parse("+-+-1.0"))); 85 Assert.AreEqual("-3 + -1", formatter.Format(parser.Parse("-3 + -1.0"))); 82 86 83 87 84 Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\""))); 85 Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\""))); 86 Console.WriteLine(formatter.Format(parser.Parse("x1*x2+x3*x4"))); 88 Assert.AreEqual("'x1'",formatter.Format(parser.Parse("\"x1\""))); 89 Assert.AreEqual("'var name'",formatter.Format(parser.Parse("\'var name\'"))); 90 Assert.AreEqual("'var name'",formatter.Format(parser.Parse("\"var name\""))); 91 Assert.AreEqual("'1'",formatter.Format(parser.Parse("\"1\""))); 92 93 Assert.AreEqual("'var \" name'",formatter.Format(parser.Parse("'var \" name\'"))); 94 Assert.AreEqual("\"var ' name\"", formatter.Format(parser.Parse("\"var \' name\""))); 87 95 88 96 89 Console.WriteLine(formatter.Format(parser.Parse("POW(3, 2)"))); 90 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1, 2.1)"))); 91 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1 , 2.1)"))); 92 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1 ,2.1)"))); 93 Console.WriteLine(formatter.Format(parser.Parse("POW(-3.1 , - 2.1)"))); 94 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3, 2)"))); 95 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1, 2.1)"))); 96 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1 , 2.1)"))); 97 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1 ,2.1)"))); 98 Console.WriteLine(formatter.Format(parser.Parse("ROOT(-3.1 , - 2.1)"))); 97 Assert.AreEqual("'x1' * 'x2'",formatter.Format(parser.Parse("\"x1\"*\"x2\""))); 98 Assert.AreEqual("'x1' * 'x2' + 'x3' * 'x4'",formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\""))); 99 Assert.AreEqual("'x1' * 'x2' + 'x3' * 'x4'", formatter.Format(parser.Parse("x1*x2+x3*x4"))); 99 100 100 Console.WriteLine(formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)")));101 Console.WriteLine(formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)")));102 101 103 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, 1)"))); 104 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, -1)"))); 105 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, +1)"))); 106 Console.WriteLine(formatter.Format(parser.Parse("x * LAG('x', +1)"))); 102 Assert.AreEqual("3 ^ 2",formatter.Format(parser.Parse("POW(3, 2)"))); 103 Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1, 2.1)"))); 104 Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1 , 2.1)"))); 105 Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1 ,2.1)"))); 106 Assert.AreEqual("-3.1 ^ -2.1",formatter.Format(parser.Parse("POW(-3.1 , - 2.1)"))); 107 Assert.AreEqual("ROOT(3, 2)",formatter.Format(parser.Parse("ROOT(3, 2)"))); 108 Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1, 2.1)"))); 109 Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 , 2.1)"))); 110 Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 ,2.1)"))); 111 Assert.AreEqual("ROOT(-3.1, -2.1)", formatter.Format(parser.Parse("ROOT(-3.1 , -2.1)"))); 107 112 108 Console.WriteLine(formatter.Format(parser.Parse("x [1.0] * y"))); 109 Console.WriteLine(formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]"))); 110 Console.WriteLine(formatter.Format(parser.Parse("x[1] * y"))); 111 Console.WriteLine(formatter.Format(parser.Parse("x[1, 2] * y [1, 2]"))); 113 Assert.AreEqual("IF(GT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)"))); 114 Assert.AreEqual("IF(LT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)"))); 115 Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, 1)"))); 116 Assert.AreEqual("LAG('x', -1)",formatter.Format(parser.Parse("LAG(x, -1)"))); 117 Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, +1)"))); 118 Assert.AreEqual("'x' * LAG('x', 1)", formatter.Format(parser.Parse("x * LAG('x', +1)"))); 112 119 113 Console.WriteLine(formatter.Format(parser.Parse("x [+1.0] * y"))); 114 Console.WriteLine(formatter.Format(parser.Parse("x [-1.0] * y"))); 115 Console.WriteLine(formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]"))); 120 // factor variables 121 Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x [1.0] * y"))); 122 Assert.AreEqual("'x'[1, 2] * 'y'[1, 2]",formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]"))); 123 Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x[1] * y"))); 124 Assert.AreEqual("'x'[1, 2] * 'y'[1, 2]",formatter.Format(parser.Parse("x[1, 2] * y [1, 2]"))); 125 Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x [+1.0] * y"))); 126 Assert.AreEqual("'x'[-1] * 'y'",formatter.Format(parser.Parse("x [-1.0] * y"))); 127 Assert.AreEqual("'x'[-1, -2] * 'y'[1, 2]", formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]"))); 116 128 117 Console.WriteLine(formatter.Format(parser.Parse("x='bla' * y"))); 118 Console.WriteLine(formatter.Format(parser.Parse("x = 'bla'"))); 119 Console.WriteLine(formatter.Format(parser.Parse("x = \"bla\""))); 120 Console.WriteLine(formatter.Format(parser.Parse("1.0 * x = bla"))); 121 Console.WriteLine(formatter.Format(parser.Parse("-1.0 * x = bla"))); 122 Console.WriteLine(formatter.Format(parser.Parse("+1.0 * \"x\" = bla + y = \"bla2\""))); 129 // one-hot for factor 130 Assert.AreEqual("'x' = 'val' * 'y'",formatter.Format(parser.Parse("x='val' * y"))); 131 Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = 'val'"))); 132 Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = \"val\""))); 133 Assert.AreEqual("1 * 'x' = 'val'",formatter.Format(parser.Parse("1.0 * x = val"))); 134 Assert.AreEqual("-1 * 'x' = 'val'",formatter.Format(parser.Parse("-1.0 * x = val"))); 135 Assert.AreEqual("1 * 'x' = 'val1' + 'y' = 'val2'", formatter.Format(parser.Parse("+1.0 * \"x\" = val1 + y = \"val2\""))); 136 137 // numeric parameters 138 Assert.AreEqual("0", formatter.Format(parser.Parse("<num>"))); // default initial value is zero 139 Assert.AreEqual("0", formatter.Format(parser.Parse("< num >"))); 140 Assert.AreEqual("1", formatter.Format(parser.Parse("< num=1.0>"))); 141 Assert.AreEqual("1", formatter.Format(parser.Parse("< num = 1.0>"))); 142 Assert.AreEqual("-1", formatter.Format(parser.Parse("< num =-1.0>"))); 143 Assert.AreEqual("-1", formatter.Format(parser.Parse("< num = - 1.0>"))); 144 145 // numeric parameter with sign 146 Assert.AreEqual("1", formatter.Format(parser.Parse("-<num=-1.0>"))); 147 148 // nested functions 149 Assert.AreEqual("SIN(SIN(SIN('X1')))", formatter.Format(parser.Parse("SIN(SIN(SIN(X1)))"))); 123 150 } 124 151 } -
branches/3140_NumberSymbol/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicExpressionTreeBottomUpSimilarityCalculatorTest.cs
r18145 r18175 7 7 [TestClass] 8 8 public class BottomUpSimilarityCalculatorTest { 9 private readonly SymbolicExpressionImporter importer = new SymbolicExpressionImporter();10 9 private readonly InfixExpressionParser parser = new InfixExpressionParser(); 11 10 … … 13 12 private const int Rows = 1; 14 13 private const int Columns = 10; 15 16 public BottomUpSimilarityCalculatorTest() {17 var parser = new InfixExpressionParser();18 }19 14 20 15 [TestMethod] … … 28 23 29 24 TestMatchedNodes("1 - 1", "2 - 2", 0, strict: true); 30 TestMatchedNodes("1 - 1", "2 - 2", 4, strict: false); // 4, because of the way strings are parsed into trees by the infix parser25 TestMatchedNodes("1 - 1", "2 - 2", 3, strict: false); 31 26 32 27 TestMatchedNodes("2 - 1", "1 - 2", 2, strict: true); 33 TestMatchedNodes("2 - 1", "1 - 2", 4, strict: false);28 TestMatchedNodes("2 - 1", "1 - 2", 3, strict: false); 34 29 35 TestMatchedNodes(" (X1 * X2) + (X3 * X4)", "(X1 * X2) + (X3 * X4)", 7, strict: true);36 TestMatchedNodes(" (X1 * X2) + (X3 * X4)", "(X1 * X2) + (X3 * X4)", 7, strict: false);30 TestMatchedNodes("X1 * X2 + X3 * X4", "X1 * X2 + X3 * X4", 7, strict: true); 31 TestMatchedNodes("X1 * X2 + X3 * X4", "X1 * X2 + X3 * X4", 7, strict: false); 37 32 38 TestMatchedNodes(" (X1 * X2) + (X3 * X4)", "(X1 * X2) + (X5 * X6)", 3, strict: true);39 TestMatchedNodes(" (X1 * X2) + (X3 * X4)", "(X1 * X2) + (X5 * X6)", 3, strict: false);33 TestMatchedNodes("X1 * X2 + X3 * X4", "X1 * X2 + X5 * X6", 3, strict: true); 34 TestMatchedNodes("X1 * X2 + X3 * X4", "X1 * X2 + X5 * X6", 3, strict: false); 40 35 41 TestMatchedNodes(" (X1 * X2) + (X3 * X4)", "(X1 * X2) - (X5 * X6)", 3, strict: true);42 TestMatchedNodes(" (X1 * X2) + (X3 * X4)", "(X1 * X2) - (X5 * X6)", 3, strict: false);36 TestMatchedNodes("X1 * X2 + X3 * X4", "X1 * X2 - X5 * X6", 3, strict: true); 37 TestMatchedNodes("X1 * X2 + X3 * X4", "X1 * X2 - X5 * X6", 3, strict: false); 43 38 44 39 TestMatchedNodes("SIN(SIN(SIN(X1)))", "SIN(SIN(SIN(X1)))", 4, strict: true); … … 47 42 TestMatchedNodes("SIN(SIN(SIN(X1)))", "COS(COS(COS(X1)))", 1, strict: true); 48 43 49 const string lhs = " (0.006153 + (X9 * X7 * X2 * 0.229506) + (X6 * X10 * X3 * 0.924598) + (X2 * X1 * 0.951272) + (X4 * X3 * 0.992570) + (X6 * X5 * 1.027299))";50 const string rhs = " (0.006153 + (X10 * X7 * X2 * 0.229506) + (X6 * X10 * X3 * 0.924598) + (X2 * X1 * 0.951272) + (X4 * X3 * 0.992570) + (X6 * X5 * 1.027299))";44 const string lhs = "0.006153 + X9 * X7 * X2 * 0.229506 + X6 * X10 * X3 * 0.924598 + X2 * X1 * 0.951272 + X4 * X3 * 0.992570 + X6 * X5 * 1.027299"; 45 const string rhs = "0.006153 + X10 * X7 * X2 * 0.229506 + X6 * X10 * X3 * 0.924598 + X2 * X1 * 0.951272 + X4 * X3 * 0.992570 + X6 * X5 * 1.027299"; 51 46 52 47 TestMatchedNodes(lhs, lhs, 24, strict: true);
Note: See TracChangeset
for help on using the changeset viewer.