Changeset 16867
- Timestamp:
- 04/23/19 16:08:38 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicExpressionTreeBottomUpSimilarityCalculatorTest.cs
r16283 r16867 1 1 using System; 2 2 using System.Diagnostics; 3 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 3 4 using HeuristicLab.Random; 4 5 using Microsoft.VisualStudio.TestTools.UnitTesting; … … 7 8 [TestClass] 8 9 public class BottomUpSimilarityCalculatorTest { 9 private readonly SymbolicExpressionTreeBottomUpSimilarityCalculator similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator() { MatchConstantValues = false, MatchVariableWeights = false };10 10 private readonly SymbolicExpressionImporter importer = new SymbolicExpressionImporter(); 11 private readonly InfixExpressionParser parser = new InfixExpressionParser(); 11 12 12 private const int N = 1000;13 private const int N = 200; 13 14 private const int Rows = 1; 14 15 private const int Columns = 10; … … 22 23 [TestProperty("Time", "short")] 23 24 public void BottomUpTreeSimilarityCalculatorTestMapping() { 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);25 TestMatchedNodes("1 + 1", "2 + 2", 0, strict: true); 26 TestMatchedNodes("1 + 1", "2 + 2", 3, strict: false); 27 TestMatchedNodes("1 + 1", "1 + 2", 1, strict: true); 28 TestMatchedNodes("1 + 2", "2 + 1", 3, strict: true); 28 29 29 TestMatchedNodes(" (- 1 1)", "(- 2 2)", 0, strict: true);30 TestMatchedNodes(" (- 1 1)", "(- 2 2)", 3, strict: false);30 TestMatchedNodes("1 - 1", "2 - 2", 0, strict: true); 31 TestMatchedNodes("1 - 1", "2 - 2", 4, strict: false); // 4, because of the way strings are parsed into trees by the infix parser 31 32 32 TestMatchedNodes("(- 2 1)", "(- 1 2)", 2, strict: true); 33 TestMatchedNodes("(- 2 1)", "(- 1 2)", 3, strict: false); 33 TestMatchedNodes("2 - 1", "1 - 2", 2, strict: true); 34 TestMatchedNodes("2 - 1", "1 - 2", 4, strict: false); 35 36 TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) + (X3 * X4)", 7, strict: true); 37 TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) + (X3 * X4)", 7, strict: false); 38 39 TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) + (X5 * X6)", 3, strict: true); 40 TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) + (X5 * X6)", 3, strict: false); 41 42 TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) - (X5 * X6)", 3, strict: true); 43 TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) - (X5 * X6)", 3, strict: false); 44 45 TestMatchedNodes("SIN(SIN(SIN(X1)))", "SIN(SIN(SIN(X1)))", 4, strict: true); 46 TestMatchedNodes("SIN(SIN(SIN(X1)))", "COS(SIN(SIN(X1)))", 3, strict: true); 47 TestMatchedNodes("SIN(SIN(SIN(X1)))", "COS(COS(SIN(X1)))", 2, strict: true); 48 TestMatchedNodes("SIN(SIN(SIN(X1)))", "COS(COS(COS(X1)))", 1, strict: true); 49 50 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))"; 51 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))"; 52 53 TestMatchedNodes(lhs, lhs, 24, strict: true); 54 TestMatchedNodes(lhs, lhs, 24, strict: false); 55 56 TestMatchedNodes(lhs, rhs, 21, strict: true); 57 TestMatchedNodes(lhs, rhs, 21, strict: false); 34 58 } 35 59 36 60 private void TestMatchedNodes(string expr1, string expr2, int expected, bool strict) { 37 var t1 = importer.Import(expr1); 38 var t2 = importer.Import(expr2); 61 var t1 = parser.Parse(expr1); 62 var t2 = parser.Parse(expr2); 63 64 Console.WriteLine(SymbolicExpressionTreeHierarchicalFormatter.FormatTree(t1)); 65 Console.WriteLine(SymbolicExpressionTreeHierarchicalFormatter.FormatTree(t2)); 39 66 40 67 var map = SymbolicExpressionTreeBottomUpSimilarityCalculator.ComputeBottomUpMapping(t1, t2, strict); 68 Console.WriteLine($"Count: {map.Count}"); 41 69 42 70 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}) ");71 throw new Exception($"Match count {map.Count} is different than expected value {expected} for expressions:\n{expr1} and {expr2} (strict = {strict})\n"); 44 72 } 45 73 } … … 58 86 var sw = new Stopwatch(); 59 87 88 var similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { MatchVariableWeights = false, MatchConstantValues = false }; 89 60 90 sw.Start(); 61 91 for (int i = 0; i < trees.Length - 1; ++i) { … … 69 99 Console.WriteLine(N * (N + 1) / (2 * sw.ElapsedMilliseconds / 1000.0) + " similarity calculations per second."); 70 100 } 101 102 [TestMethod] 103 [TestCategory("Problems.DataAnalysis.Symbolic")] 104 [TestProperty("Time", "long")] 105 public void BottomUpTreeSimilarityCalculatorStrictMatchingConsistency() { 106 TestMatchingConsistency(strict: true); 107 } 108 109 [TestMethod] 110 [TestCategory("Problems.DataAnalysis.Symbolic")] 111 [TestProperty("Time", "long")] 112 public void BottomUpTreeSimilarityCalculatorRelaxedMatchingConsistency() { 113 TestMatchingConsistency(strict: false); 114 } 115 116 private static void TestMatchingConsistency(bool strict = false) { 117 var grammar = new TypeCoherentExpressionGrammar(); 118 grammar.ConfigureAsDefaultRegressionGrammar(); 119 var twister = new MersenneTwister(31415); 120 var ds = Util.CreateRandomDataset(twister, Rows, Columns); 121 var trees = Util.CreateRandomTrees(twister, ds, grammar, N, 1, 100, 0, 0); 122 123 var similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { MatchConstantValues = strict, MatchVariableWeights = strict }; 124 var bottomUpSimilarity = 0d; 125 for (int i = 0; i < trees.Length - 1; ++i) { 126 for (int j = i + 1; j < trees.Length; ++j) { 127 bottomUpSimilarity += similarityCalculator.CalculateSimilarity(trees[i], trees[j]); 128 } 129 } 130 bottomUpSimilarity /= N * (N - 1) / 2; 131 132 var hashBasedSimilarity = SymbolicExpressionTreeHash.ComputeAverageSimilarity(trees, false, strict); 133 134 Assert.AreEqual(bottomUpSimilarity, hashBasedSimilarity, 1e-6); 135 136 Console.WriteLine($"Bottom-up similarity: {bottomUpSimilarity}, hash-based similarity: {hashBasedSimilarity}"); 137 } 71 138 } 72 139 }
Note: See TracChangeset
for help on using the changeset viewer.