Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/28/18 22:21:11 (5 years ago)
Author:
gkronber
Message:

#2915: merged r16241:16309 (current HEAD) from HL trunk to branch

Location:
branches/2915-AbsoluteSymbol
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/2915-AbsoluteSymbol

  • branches/2915-AbsoluteSymbol/HeuristicLab.Tests

  • branches/2915-AbsoluteSymbol/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/SubtreeCrossoverTest.cs

    r15583 r16332  
    4646          SubroutineCreater.CreateSubroutine(random, trees[i], 100, 10, 3, 3);
    4747      }
     48      var children = new List<ISymbolicExpressionTree>(trees);
     49
    4850      Stopwatch stopwatch = new Stopwatch();
    4951      stopwatch.Start();
     
    5254          var par0 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
    5355          var par1 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
    54           SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10);
     56          children[i] = SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10);
    5557        }
     58        trees = children;
    5659      }
    5760      stopwatch.Stop();
  • branches/2915-AbsoluteSymbol/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/DeriveTest.cs

    r16240 r16332  
    4545      Assert.AreEqual("6", Derive("2*3*x", "x"));
    4646      Assert.AreEqual("(10*'y')", Derive("10*x*y+20*y", "x"));
    47       Assert.AreEqual("(1 / (SQR('x') * (-1)))",  Derive("1/x", "x"));
     47      Assert.AreEqual("(1 / (SQR('x') * (-1)))", Derive("1/x", "x"));
    4848      Assert.AreEqual("('y' / (SQR('x') * (-1)))", Derive("y/x", "x"));
    4949      Assert.AreEqual("((((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + ('x' * 'x'))))",
     
    5858      Assert.AreEqual("(COS((3*'x')) * 3)", Derive("sin(3*x)", "x"));
    5959      Assert.AreEqual("(SIN((3*'x')) * (-3))", Derive("cos(3*x)", "x"));
     60      Assert.AreEqual("(1 / (SQR(COS((3*'x'))) * 0.333333333333333))", Derive("tan(3*x)", "x")); // diff(tan(f(x)), x) = 1.0 / cos²(f(x)), simplifier puts constant factor into the denominator
    6061
     62      {
     63        // special case: Inv(x) using only one argument to the division symbol
     64        // f(x) = 1/x
     65        var root = new ProgramRootSymbol().CreateTreeNode();
     66        var start = new StartSymbol().CreateTreeNode();
     67        var div = new Division().CreateTreeNode();
     68        var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
     69        varNode.Weight = 1.0;
     70        varNode.VariableName = "x";
     71        div.AddSubtree(varNode);
     72        start.AddSubtree(div);
     73        root.AddSubtree(start);
     74        var t = new SymbolicExpressionTree(root);
     75        Assert.AreEqual("(1 / (SQR('x') * (-1)))",
     76          formatter.Format(DerivativeCalculator.Derive(t, "x")));
     77      }
    6178
    62       // special case: Inv(x) using only one argument to the division symbol
    63       // f(x) = 1/x
    64       var root = new ProgramRootSymbol().CreateTreeNode();
    65       var start = new StartSymbol().CreateTreeNode();
    66       var div = new Division().CreateTreeNode();
    67       var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
    68       varNode.Weight = 1.0;
    69       varNode.VariableName = "x";
    70       div.AddSubtree(varNode);
    71       start.AddSubtree(div);
    72       root.AddSubtree(start);
    73       var t = new SymbolicExpressionTree(root);
    74       Assert.AreEqual("(1 / (SQR('x') * (-1)))",
    75         formatter.Format(DerivativeCalculator.Derive(t, "x")));
     79      {
     80        // special case: multiplication with only one argument
     81        var root = new ProgramRootSymbol().CreateTreeNode();
     82        var start = new StartSymbol().CreateTreeNode();
     83        var mul = new Multiplication().CreateTreeNode();
     84        var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
     85        varNode.Weight = 3.0;
     86        varNode.VariableName = "x";
     87        mul.AddSubtree(varNode);
     88        start.AddSubtree(mul);
     89        root.AddSubtree(start);
     90        var t = new SymbolicExpressionTree(root);
     91        Assert.AreEqual("3",
     92          formatter.Format(DerivativeCalculator.Derive(t, "x")));
     93      }
     94
     95      {
     96        // division with multiple arguments
     97        // div(x, y, z) is interpreted as (x / y) / z
     98        var root = new ProgramRootSymbol().CreateTreeNode();
     99        var start = new StartSymbol().CreateTreeNode();
     100        var div = new Division().CreateTreeNode();
     101        var varNode1 = (VariableTreeNode)(new Variable().CreateTreeNode());
     102        varNode1.Weight = 3.0;
     103        varNode1.VariableName = "x";
     104        var varNode2 = (VariableTreeNode)(new Variable().CreateTreeNode());
     105        varNode2.Weight = 4.0;
     106        varNode2.VariableName = "y";
     107        var varNode3 = (VariableTreeNode)(new Variable().CreateTreeNode());
     108        varNode3.Weight = 5.0;
     109        varNode3.VariableName = "z";
     110        div.AddSubtree(varNode1); div.AddSubtree(varNode2); div.AddSubtree(varNode3);
     111        start.AddSubtree(div);
     112        root.AddSubtree(start);
     113        var t = new SymbolicExpressionTree(root);
     114
     115        Assert.AreEqual("(('y' * 'z' * 60) / SQR(('y' * 'z' * 20)))", // actually 3 / (4y  5z) but simplifier is not smart enough to cancel numerator and denominator
     116                                                                      // 60 y z / y² z² 20² == 6 / y z 40 == 3 / y z 20
     117          formatter.Format(DerivativeCalculator.Derive(t, "x")));
     118        Assert.AreEqual("(('x' * 'z' * (-60)) / SQR(('y' * 'z' * 20)))", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z)
     119                                                                         // -3 4 5 x z / 4² y² 5² z² = -60 x z / 20² z² y² ==    -60 x z / y² z² 20²
     120          formatter.Format(DerivativeCalculator.Derive(t, "y")));
     121        Assert.AreEqual("(('x' * 'y' * (-60)) / SQR(('y' * 'z' * 20)))",
     122          formatter.Format(DerivativeCalculator.Derive(t, "z")));
     123      }
    76124    }
    77125
  • branches/2915-AbsoluteSymbol/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeInterpreterTest.cs

    r15583 r16332  
    119119    public void LinearInterpreterTestArithmeticGrammarPerformance() {
    120120      TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), 12.5e6);
     121    }
     122
     123    [TestMethod]
     124    [TestCategory("Problems.DataAnalysis.Symbolic")]
     125    [TestProperty("Time", "long")]
     126    public void BatchInterpreterTestTypeCoherentGrammarPerformance() {
     127      TestTypeCoherentGrammarPerformance(new SymbolicDataAnalysisExpressionTreeBatchInterpreter(), 12.5e6);
     128    }
     129    [TestMethod]
     130    [TestCategory("Problems.DataAnalysis.Symbolic")]
     131    [TestProperty("Time", "long")]
     132    public void BatchInterpreterTestArithmeticGrammarPerformance() {
     133      TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionTreeBatchInterpreter(), 12.5e6);
    121134    }
    122135
  • branches/2915-AbsoluteSymbol/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicExpressionTreeBottomUpSimilarityCalculatorTest.cs

    r14354 r16332  
    77  [TestClass]
    88  public class BottomUpSimilarityCalculatorTest {
    9     private readonly SymbolicExpressionTreeBottomUpSimilarityCalculator busCalculator;
    10     private readonly SymbolicExpressionImporter importer;
     9    private readonly SymbolicExpressionTreeBottomUpSimilarityCalculator similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator() { MatchConstantValues = false, MatchVariableWeights = false };
     10    private readonly SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
    1111
    12     private const int N = 150;
     12    private const int N = 1000;
    1313    private const int Rows = 1;
    1414    private const int Columns = 10;
    1515
    1616    public BottomUpSimilarityCalculatorTest() {
    17       busCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator();
    18       importer = new SymbolicExpressionImporter();
     17      var parser = new InfixExpressionParser();
    1918    }
    2019
     
    2322    [TestProperty("Time", "short")]
    2423    public void BottomUpTreeSimilarityCalculatorTestMapping() {
    25       TestMatchedNodes("(+ 1 2)", "(+ 2 1)", 5);
    26       TestMatchedNodes("(- 2 1)", "(- 1 2)", 2);
    27       TestMatchedNodes("(* (variable 1 X1) (variable 1 X2))", "(* (+ (variable 1 X1) 1) (+ (variable 1 X2) 1))", 2);
     24      TestMatchedNodes("(+ 1 1)", "(+ 2 2)", 0, strict: true);
     25      TestMatchedNodes("(+ 1 1)", "(+ 2 2)", 3, strict: false);
     26      TestMatchedNodes("(+ 1 1)", "(+ 1 2)", 1, strict: true);
     27      TestMatchedNodes("(+ 2 1)", "(+ 1 2)", 3, strict: true);
    2828
    29       TestMatchedNodes("(* (variable 1 X1) (variable 1 X2))", "(* (+ (variable 1 X1) 1) (variable 1 X2))", 2);
     29      TestMatchedNodes("(- 1 1)", "(- 2 2)", 0, strict: true);
     30      TestMatchedNodes("(- 1 1)", "(- 2 2)", 3, strict: false);
    3031
    31       TestMatchedNodes("(+ (variable 1 a) (variable 1 b))", "(+ (variable 1 a) (variable 1 a))", 1);
    32       TestMatchedNodes("(+ (+ (variable 1 a) (variable 1 b)) (variable 1 b))", "(+ (* (+ (variable 1 a) (variable 1 b)) (variable 1 b)) (+ (+ (variable 1 a) (variable 1 b)) (variable 1 b)))", 5);
    33 
    34       TestMatchedNodes(
    35         "(* (+ 2.84 (exp (+ (log (/ (variable 2.0539 X5) (variable -9.2452e-1 X6))) (/ (variable 2.0539 X5) (variable -9.2452e-1 X6))))) 2.9081)",
    36         "(* (- (variable 9.581e-1 X6) (+ (- (variable 5.1491e-1 X5) 1.614e+1) (+ (/ (variable 2.0539 X5) (variable -9.2452e-1 X6)) (log (/ (variable 2.0539 X5) (variable -9.2452e-1 X6)))))) 2.9081)",
    37         9);
    38 
    39       TestMatchedNodes("(* (* (* (variable 1.68 x) (* (variable 1.68 x) (variable 2.55 x))) (variable 1.68 x)) (* (* (variable 1.68 x) (* (variable 1.68 x) (* (variable 1.68 x) (variable 2.55 x)))) (variable 2.55 x)))", "(* (variable 2.55 x) (* (variable 1.68 x) (* (variable 1.68 x) (* (variable 1.68 x) (variable 2.55 x)))))", 9);
    40 
    41       TestMatchedNodes("(+ (exp 2.1033) (/ -4.3072 (variable 2.4691 X7)))", "(/ 1 (+ (/ -4.3072 (variable 2.4691 X7)) (exp 2.1033)))", 6);
    42       TestMatchedNodes("(+ (exp 2.1033) (/ -4.3072 (variable 2.4691 X7)))", "(/ 1 (+ (/ (variable 2.4691 X7) -4.3072) (exp 2.1033)))", 4);
    43 
    44       const string expr1 = "(* (- 1.2175e+1 (+ (/ (exp -1.4134e+1) (exp 9.2013)) (exp (log (exp (/ (exp (- (* -4.2461 (variable 2.2634 X5)) (- -9.6267e-1 3.3243))) (- (/ (/ (variable 1.0883 X1) (variable 6.9620e-1 X2)) (log 1.3011e+1)) (variable -4.3098e-1 X7)))))))) (log 1.3011e+1))";
    45       const string expr2 = "(* (- 1.2175e+1 (+ (/ (/ (+ (variable 3.0140 X9) (variable 1.3430 X8)) -1.0864e+1) (exp 9.2013)) (exp (log (exp (/ (exp (- (* -4.2461 (variable 2.2634 X5)) (- -9.6267e-1 3.3243))) (- (/ (/ (variable 1.0883 X1) (variable 6.9620e-1 X2)) (log 1.3011e+1)) (variable -4.3098e-1 X7)))))))) (exp (variable 4.0899e-1 X7)))";
    46 
    47       TestMatchedNodes(expr1, expr2, 23);
    48 
     32      TestMatchedNodes("(- 2 1)", "(- 1 2)", 2, strict: true);
     33      TestMatchedNodes("(- 2 1)", "(- 1 2)", 3, strict: false);
    4934    }
    5035
    51     private void TestMatchedNodes(string expr1, string expr2, int expected) {
     36    private void TestMatchedNodes(string expr1, string expr2, int expected, bool strict) {
    5237      var t1 = importer.Import(expr1);
    5338      var t2 = importer.Import(expr2);
    5439
    55       var mapping = busCalculator.ComputeBottomUpMapping(t1.Root, t2.Root);
    56       var c = mapping.Count;
     40      var map = SymbolicExpressionTreeBottomUpSimilarityCalculator.ComputeBottomUpMapping(t1, t2, strict);
    5741
    58       if (c != expected) {
    59         throw new Exception("Match count " + c + " is different than expected value " + expected);
     42      if (map.Count != expected) {
     43        throw new Exception($"Match count {map.Count} is different than expected value {expected} for expressions:\n{expr1} and {expr2} (strict = {strict})");
    6044      }
    6145    }
     
    7761      for (int i = 0; i < trees.Length - 1; ++i) {
    7862        for (int j = i + 1; j < trees.Length; ++j) {
    79           s += busCalculator.CalculateSimilarity(trees[i], trees[j]);
     63          s += similarityCalculator.CalculateSimilarity(trees[i], trees[j]);
    8064        }
    8165      }
Note: See TracChangeset for help on using the changeset viewer.