Changeset 15772
 Timestamp:
 02/13/18 17:56:49 (3 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15746 r15772 20 20 public NonterminalSymbol Term; 21 21 public NonterminalSymbol Factor; 22 public NonterminalSymbol LogFactor; 23 public NonterminalSymbol ExpFactor; 24 public NonterminalSymbol SinFactor; 25 26 public NonterminalSymbol SimpleExpr; 27 public NonterminalSymbol SimpleTerm; 22 28 23 29 public TerminalSymbol Addition; 24 30 public TerminalSymbol Multiplication; 31 public TerminalSymbol Log; 32 public TerminalSymbol Exp; 33 public TerminalSymbol Sin; 34 35 // For infix notation 36 public TerminalSymbol OpeningBracket; 37 public TerminalSymbol ClosingBracket; 25 38 26 39 #endregion … … 39 52 private ISymbol expSy; 40 53 private ISymbol divSy; 54 private ISymbol sinSy; 41 55 42 56 private ISymbol rootSy; … … 52 66 Term = new NonterminalSymbol("Term"); 53 67 Factor = new NonterminalSymbol("Factor"); 68 LogFactor = new NonterminalSymbol("LogFactor"); 69 ExpFactor = new NonterminalSymbol("ExpFactor"); 70 SinFactor = new NonterminalSymbol("SinFactor"); 71 72 SimpleExpr = new NonterminalSymbol("SimpleExpr"); 73 SimpleTerm = new NonterminalSymbol("SimpleTerm"); 54 74 55 75 Addition = new TerminalSymbol("+"); 56 76 Multiplication = new TerminalSymbol("*"); 77 Log = new TerminalSymbol("log"); 78 Exp = new TerminalSymbol("exp"); 79 Sin = new TerminalSymbol("sin"); 80 81 OpeningBracket = new TerminalSymbol("("); 82 ClosingBracket = new TerminalSymbol(")"); 57 83 #endregion 58 84 … … 70 96 71 97 Factor.AddProduction(Var); 98 Factor.AddProduction(LogFactor); 99 Factor.AddProduction(ExpFactor); 100 Factor.AddProduction(SinFactor); 101 102 LogFactor.AddProduction(SimpleExpr, Log); 103 ExpFactor.AddProduction(SimpleTerm, Exp); 104 SinFactor.AddProduction(SimpleExpr, Sin); 105 106 SimpleExpr.AddProduction(SimpleTerm, SimpleExpr, Addition); 107 SimpleExpr.AddProduction(SimpleTerm); 108 109 SimpleTerm.AddProduction(Var, SimpleTerm, Multiplication); 110 SimpleTerm.AddProduction(Var); 72 111 #endregion 73 112 … … 78 117 constSy = symbolicExpressionGrammar.Symbols.OfType<Constant>().First(); 79 118 varSy = symbolicExpressionGrammar.Symbols.OfType<Variable>().First(); 80 addSy = symbolicExpressionGrammar.AllowedSymbols.OfType<Addition>().First(); 81 mulSy = symbolicExpressionGrammar.AllowedSymbols.OfType<Multiplication>().First(); 82 logSy = symbolicExpressionGrammar.AllowedSymbols.OfType<Logarithm>().First(); 83 expSy = symbolicExpressionGrammar.AllowedSymbols.OfType<Exponential>().First(); 84 divSy = symbolicExpressionGrammar.AllowedSymbols.OfType<Division>().First(); 85 86 rootSy = symbolicExpressionGrammar.AllowedSymbols.OfType<ProgramRootSymbol>().First(); 87 startSy = symbolicExpressionGrammar.AllowedSymbols.OfType<StartSymbol>().First(); 119 addSy = symbolicExpressionGrammar.Symbols.OfType<Addition>().First(); 120 mulSy = symbolicExpressionGrammar.Symbols.OfType<Multiplication>().First(); 121 logSy = symbolicExpressionGrammar.Symbols.OfType<Logarithm>().First(); 122 expSy = symbolicExpressionGrammar.Symbols.OfType<Exponential>().First(); 123 divSy = symbolicExpressionGrammar.Symbols.OfType<Division>().First(); 124 sinSy = symbolicExpressionGrammar.Symbols.OfType<Sine>().First(); 125 126 rootSy = symbolicExpressionGrammar.Symbols.OfType<ProgramRootSymbol>().First(); 127 startSy = symbolicExpressionGrammar.Symbols.OfType<StartSymbol>().First(); 88 128 89 129 #endregion … … 104 144 private int[] GetSubtreeHashes(Stack<Symbol> parseStack) { 105 145 Symbol currentSymbol = parseStack.Pop(); 106 107 // VARIABLE108 // if (Var.VariableTerminalSymbols.Contains(currentSymbol)) {109 // return currentSymbol.StringRepresentation.GetHashCode().ToEnumerable().ToArray();110 // }111 146 112 147 // MULTIPLICATION … … 156 191 } 157 192 193 // LOG, EXP, SIN 194 if (ReferenceEquals(currentSymbol, Log)  ReferenceEquals(currentSymbol, Exp)  195 ReferenceEquals(currentSymbol, Sin)) { 196 return AggregateHashes(parseStack.Peek(), GetSubtreeHashes(parseStack)).ToEnumerable().ToArray(); 197 } 198 158 199 // var or nonterminal symbol 159 200 return currentSymbol.StringRepresentation.GetHashCode().ToEnumerable().ToArray(); … … 161 202 162 203 private int AggregateHashes(Symbol operatorSym, IEnumerable<int> hashes) { 163 // If multiple subtrees are "merged" (e.g. added, multiplied, etc.), consider the executed operation164 204 var hashesArray = hashes.ToArray(); 165 int start = hashesArray.Length > 1 ? operatorSym.StringRepresentation.GetHashCode() : 0; 205 206 int start; 207 if (ReferenceEquals(operatorSym, Addition) && hashesArray.Count() <= 1) { 208 start = 0; 209 } else { 210 start = operatorSym.StringRepresentation.GetHashCode(); 211 } 212 166 213 return hashesArray.Aggregate(start, (result, ti) => ((result << 5) + result) ^ ti.GetHashCode()); 167 214 } … … 199 246 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // left part 200 247 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); // right part 248 249 } else if (ReferenceEquals(currentSymbol, Log)) { 250 parsedSubTree = logSy.CreateTreeNode(); 251 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 252 253 } else if (ReferenceEquals(currentSymbol, Exp)) { 254 parsedSubTree = expSy.CreateTreeNode(); 255 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 256 257 } else if (ReferenceEquals(currentSymbol, Sin)) { 258 parsedSubTree = sinSy.CreateTreeNode(); 259 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 201 260 202 261 } else if (Var.VariableTerminalSymbols.Contains(currentSymbol)) { … … 233 292 result.Add(head); 234 293 result.AddRange(rightPart); 294 295 } else if (ReferenceEquals(head, Log)  ReferenceEquals(head, Exp)  ReferenceEquals(head, Sin)) { 296 result.Add(head); 297 result.Add(OpeningBracket); 298 result.AddRange(PostfixToInfixSubtreeParser(parseStack)); 299 result.Add(ClosingBracket); 300 235 301 } else { 236 302 result.Add(head);
Note: See TracChangeset
for help on using the changeset viewer.