Changeset 15784 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
- Timestamp:
- 02/16/18 18:36:17 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15772 r15784 24 24 public NonterminalSymbol SinFactor; 25 25 26 public NonterminalSymbol InvExpr; 27 public NonterminalSymbol InvTerm; 28 public NonterminalSymbol InvFactor; 29 26 30 public NonterminalSymbol SimpleExpr; 27 31 public NonterminalSymbol SimpleTerm; … … 32 36 public TerminalSymbol Exp; 33 37 public TerminalSymbol Sin; 38 public TerminalSymbol Inv; 34 39 35 40 // For infix notation … … 70 75 SinFactor = new NonterminalSymbol("SinFactor"); 71 76 77 InvExpr = new NonterminalSymbol("InvExpr"); 78 InvTerm = new NonterminalSymbol("InvTerm"); 79 InvFactor = new NonterminalSymbol("InvFactor"); 80 72 81 SimpleExpr = new NonterminalSymbol("SimpleExpr"); 73 82 SimpleTerm = new NonterminalSymbol("SimpleTerm"); … … 78 87 Exp = new TerminalSymbol("exp"); 79 88 Sin = new TerminalSymbol("sin"); 89 Inv = new TerminalSymbol("inv"); 80 90 81 91 OpeningBracket = new TerminalSymbol("("); … … 94 104 Term.AddProduction(Factor, Term, Multiplication); 95 105 Term.AddProduction(Factor); 106 Term.AddProduction(InvExpr, Inv); 96 107 97 108 Factor.AddProduction(Var); … … 109 120 SimpleTerm.AddProduction(Var, SimpleTerm, Multiplication); 110 121 SimpleTerm.AddProduction(Var); 122 123 InvExpr.AddProduction(InvTerm, InvExpr, Addition); 124 InvExpr.AddProduction(InvTerm); 125 126 InvTerm.AddProduction(InvFactor, InvTerm, Multiplication); 127 InvTerm.AddProduction(InvFactor); 128 129 InvFactor.AddProduction(Var); 130 InvFactor.AddProduction(LogFactor); 131 InvFactor.AddProduction(SinFactor); 111 132 #endregion 112 133 … … 144 165 private int[] GetSubtreeHashes(Stack<Symbol> parseStack) { 145 166 Symbol currentSymbol = parseStack.Pop(); 146 147 // MULTIPLICATION148 if (ReferenceEquals(currentSymbol, Multiplication)) {149 List<int> childHashes = new List<int>();150 151 // First subtree152 if (ReferenceEquals(parseStack.Peek(), Multiplication)) {153 childHashes.AddRange(GetSubtreeHashes(parseStack));154 } else {155 childHashes.Add(AggregateHashes(parseStack.Peek(), GetSubtreeHashes(parseStack)));156 }157 // Second subtree158 if (ReferenceEquals(parseStack.Peek(), Multiplication)) {159 childHashes.AddRange(GetSubtreeHashes(parseStack));160 } else {161 childHashes.Add(AggregateHashes(parseStack.Peek(), GetSubtreeHashes(parseStack)));162 }163 164 // Sort due to commutativity165 childHashes.Sort();166 return childHashes.ToArray();167 }168 167 169 168 // ADDITION … … 191 190 } 192 191 193 // LOG, EXP, SIN 192 // MULTIPLICATION 193 if (ReferenceEquals(currentSymbol, Multiplication)) { 194 List<int> childHashes = new List<int>(); 195 196 // First subtree 197 if (ReferenceEquals(parseStack.Peek(), Multiplication)) { 198 childHashes.AddRange(GetSubtreeHashes(parseStack)); 199 } else { 200 childHashes.Add(AggregateHashes(parseStack.Peek(), GetSubtreeHashes(parseStack))); 201 } 202 // Second subtree 203 if (ReferenceEquals(parseStack.Peek(), Multiplication)) { 204 childHashes.AddRange(GetSubtreeHashes(parseStack)); 205 } else { 206 childHashes.Add(AggregateHashes(parseStack.Peek(), GetSubtreeHashes(parseStack))); 207 } 208 209 // Sort due to commutativity 210 childHashes.Sort(); 211 212 // Cancel out inverse factors. 213 var inversChildHashes = childHashes 214 .Select(ch => AggregateHashes(Inv, ch.ToEnumerable())) 215 .ToList(); 216 217 return childHashes // If this factor cancels out another one, then remove BOTH. Otherwise, keep the factor. 218 .Where(ch => !inversChildHashes.Remove(ch)) 219 .ToArray(); 220 } 221 222 // LOG, EXP, SIN, INV 194 223 if (ReferenceEquals(currentSymbol, Log) || ReferenceEquals(currentSymbol, Exp) || 195 ReferenceEquals(currentSymbol, Sin) ) {224 ReferenceEquals(currentSymbol, Sin) || ReferenceEquals(currentSymbol, Inv)) { 196 225 return AggregateHashes(parseStack.Peek(), GetSubtreeHashes(parseStack)).ToEnumerable().ToArray(); 197 226 } … … 205 234 206 235 int start; 207 if ( ReferenceEquals(operatorSym, Addition) && hashesArray.Count() <= 1) {236 if ((ReferenceEquals(operatorSym, Addition) || ReferenceEquals(operatorSym, Multiplication)) && hashesArray.Count() <= 1) { 208 237 start = 0; 209 238 } else { … … 257 286 } else if (ReferenceEquals(currentSymbol, Sin)) { 258 287 parsedSubTree = sinSy.CreateTreeNode(); 288 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 289 290 } else if (ReferenceEquals(currentSymbol, Inv)) { 291 parsedSubTree = divSy.CreateTreeNode(); 292 ConstantTreeNode dividend = (ConstantTreeNode)constSy.CreateTreeNode(); 293 dividend.Value = 1.0; 294 parsedSubTree.AddSubtree(dividend); 259 295 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 260 296 … … 293 329 result.AddRange(rightPart); 294 330 295 } else if (ReferenceEquals(head, Log) || ReferenceEquals(head, Exp) || ReferenceEquals(head, Sin)) { 331 } else if (ReferenceEquals(head, Log) || ReferenceEquals(head, Exp) 332 || ReferenceEquals(head, Sin) || ReferenceEquals(head, Inv)) { 296 333 result.Add(head); 297 334 result.Add(OpeningBracket);
Note: See TracChangeset
for help on using the changeset viewer.