Changeset 15791
- Timestamp:
- 02/20/18 13:43:36 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15784 r15791 120 120 SimpleTerm.AddProduction(Var, SimpleTerm, Multiplication); 121 121 SimpleTerm.AddProduction(Var); 122 122 123 123 InvExpr.AddProduction(InvTerm, InvExpr, Addition); 124 124 InvExpr.AddProduction(InvTerm); … … 159 159 160 160 Symbol peek = parseStack.Peek(); 161 int[] subtreeHashes = GetSubtreeHashes(parseStack);162 return AggregateHashes(peek, subtreeHashes) ;163 } 164 165 private int[] GetSubtreeHashes(Stack<Symbol> parseStack) {161 string[] subtreeHashes = GetSubtreeHashes(parseStack); 162 return AggregateHashes(peek, subtreeHashes).GetHashCode(); 163 } 164 165 private string[] GetSubtreeHashes(Stack<Symbol> parseStack) { 166 166 Symbol currentSymbol = parseStack.Pop(); 167 167 168 168 // ADDITION 169 169 if (ReferenceEquals(currentSymbol, Addition)) { 170 HashSet<int> uniqueChildHashes = new HashSet<int>();170 var uniqueChildHashes = new HashSet<string>(); 171 171 172 172 // First subtree … … 192 192 // MULTIPLICATION 193 193 if (ReferenceEquals(currentSymbol, Multiplication)) { 194 List<int> childHashes = new List<int>();194 var childHashes = new List<string>(); 195 195 196 196 // First subtree … … 227 227 228 228 // var or nonterminal symbol 229 return currentSymbol.StringRepresentation. GetHashCode().ToEnumerable().ToArray();230 } 231 232 private int AggregateHashes(Symbol operatorSym, IEnumerable<int> hashes) {229 return currentSymbol.StringRepresentation.ToEnumerable().ToArray(); 230 } 231 232 private string AggregateHashes(Symbol operatorSym, IEnumerable<string> hashes) { 233 233 var hashesArray = hashes.ToArray(); 234 234 235 int start;236 235 if ((ReferenceEquals(operatorSym, Addition) || ReferenceEquals(operatorSym, Multiplication)) && hashesArray.Count() <= 1) { 237 start = 0; 238 } else { 239 start = operatorSym.StringRepresentation.GetHashCode(); 240 } 241 242 return hashesArray.Aggregate(start, (result, ti) => ((result << 5) + result) ^ ti.GetHashCode()); 236 return hashesArray[0]; 237 } 238 if (Var.VariableTerminalSymbols.Contains(operatorSym)) { 239 return operatorSym.StringRepresentation; 240 } 241 242 return "[" + hashesArray.Aggregate(operatorSym.StringRepresentation, (result, ti) => result + " ° " + ti) + "]"; 243 243 } 244 244 #endregion -
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15784 r15791 75 75 int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence); 76 76 77 Assert.IsTrue(alg.DistinctSentences.ContainsKey( actualSolutionHash), "Actual solution was not generated!");77 Assert.IsTrue(alg.DistinctSentences.ContainsKey(targetSolutionHash), "Actual solution was not generated!"); 78 78 79 79 Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one."); … … 112 112 public void NoConstants_Nguyen6() { 113 113 // sin(x) + sin(x + x²) 114 alg.MaxTreeSize = 1 3;114 alg.MaxTreeSize = 10; 115 115 alg.Problem.ProblemData = new NguyenFunctionSix(Seed).GenerateRegressionData(); 116 116 … … 130 130 int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence); 131 131 132 Assert.IsTrue(alg.DistinctSentences.ContainsKey(actualSolutionHash), "Actual solution was not generated!"); 133 132 Assert.IsTrue(alg.DistinctSentences.ContainsKey(targetSolutionHash), "Actual solution was not generated!"); 134 133 Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one."); 135 134 … … 141 140 public void NoConstants_Nguyen9() { 142 141 // sin(x) + sin(y²) 143 alg.MaxTreeSize = 1 0;142 alg.MaxTreeSize = 11; 144 143 alg.Problem.ProblemData = new NguyenFunctionNine(Seed).GenerateRegressionData(); 145 144 146 145 alg.Start(); 146 147 TerminalSymbol xSymbol = alg.Grammar.Var.VariableTerminalSymbols.First(v => v.StringRepresentation == "X"); 148 TerminalSymbol ySymbol = alg.Grammar.Var.VariableTerminalSymbols.First(v => v.StringRepresentation == "Y"); 149 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; 150 TerminalSymbol addSymbol = alg.Grammar.Addition; 151 TerminalSymbol sinSymbol = alg.Grammar.Sin; 152 153 SymbolString targetSolution = new SymbolString(new[] { 154 xSymbol, sinSymbol, 155 ySymbol, ySymbol, mulSymbol, sinSymbol, addSymbol 156 }); 157 158 int targetSolutionHash = alg.Grammar.CalcHashCode(targetSolution); 159 int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence); 160 161 Assert.IsTrue(alg.DistinctSentences.ContainsKey(targetSolutionHash), "Actual solution was not generated!"); 162 Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one."); 163 147 164 EvaluateGrammarEnumeration(); 148 165 } … … 162 179 [TestProperty("Goal", "structure search")] 163 180 public void NoConstants_Inverse() { 164 // 1/ (log(x)*x + x)181 // sin(x) / (log(x)*x + x) 165 182 alg.MaxTreeSize = 12; 166 183 167 184 var x = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() + 1.1).ToList(); 168 var y = x.Select(xi => 1/ (Math.Log(xi) * xi + xi)).ToList();185 var y = x.Select(xi => xi / (Math.Log(xi) * xi + xi)).ToList(); 169 186 alg.Problem.ProblemData = new RegressionProblemData(new Dataset(new List<string>() { "x", "y" }, new List<IList>() { x, y }), "x".ToEnumerable(), "y"); 170 187 -
branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs
r15784 r15791 109 109 [TestCategory("TreeHashing")] 110 110 public void InverseFactorCancelationSimple() { 111 // 1/a * b * a * a 111 112 SymbolString s1 = new SymbolString(new Symbol[] { varA, grammar.Inv, varB, grammar.Multiplication, varA, grammar.Multiplication, varA, grammar.Multiplication }); 113 // a * b 112 114 SymbolString s2 = new SymbolString(new Symbol[] { varA, varB, grammar.Multiplication }); 113 115 … … 130 132 } 131 133 134 [TestMethod] 135 [TestCategory("TreeHashing")] 136 public void CompoundInverseCancellationToSingleInverse() { 137 SymbolString s1 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv, grammar.Inv }); 138 SymbolString s2 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv }); 139 140 int hash1 = grammar.CalcHashCode(s1); 141 int hash2 = grammar.CalcHashCode(s2); 142 143 Assert.AreEqual(hash1, hash2); 144 } 145 146 [TestMethod] 147 [TestCategory("TreeHashing")] 148 public void CompoundInverseCancellationToDivisor() { 149 SymbolString s1 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv }); 150 SymbolString s2 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition }); 151 152 int hash1 = grammar.CalcHashCode(s1); 153 int hash2 = grammar.CalcHashCode(s2); 154 155 Assert.AreEqual(hash1, hash2); 156 } 157 158 [TestMethod] 159 [TestCategory("TreeHashing")] 160 public void a_UncancelableCompoundInverse() { 161 // 1 / ( 1/b + sin(a*c) ) 162 SymbolString s1 = new SymbolString(new Symbol[] { varB, grammar.Inv, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition, grammar.Inv }); 163 // b + sin(a*c) 164 SymbolString s2 = new SymbolString(new Symbol[] { varB, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition }); 165 166 int hash1 = grammar.CalcHashCode(s1); 167 int hash2 = grammar.CalcHashCode(s2); 168 169 Assert.AreNotEqual(hash1, hash2); 170 } 171 132 172 #region parser 133 173 … … 144 184 [TestCategory("Parser")] 145 185 public void InfixParserComplexTest() { 146 SymbolString postfix = new SymbolString(new[] { varB, varA, varB, varC, grammar.Multiplication, grammar.Addition, grammar.Addition });186 SymbolString postfix = new SymbolString(new[] { varB, varA, varB, varC, grammar.Multiplication, grammar.Addition, grammar.Addition }); 147 187 SymbolString infix = new SymbolString(new[] { varB, grammar.Addition, varA, grammar.Addition, varB, grammar.Multiplication, varC }); 148 188
Note: See TracChangeset
for help on using the changeset viewer.