Changeset 15794
- Timestamp:
- 02/20/18 15:54:21 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15791 r15794 24 24 public NonterminalSymbol SinFactor; 25 25 26 public NonterminalSymbol InvExpr;27 public NonterminalSymbol InvTerm;28 public NonterminalSymbol InvFactor;29 30 26 public NonterminalSymbol SimpleExpr; 31 27 public NonterminalSymbol SimpleTerm; … … 75 71 SinFactor = new NonterminalSymbol("SinFactor"); 76 72 77 InvExpr = new NonterminalSymbol("InvExpr");78 InvTerm = new NonterminalSymbol("InvTerm");79 InvFactor = new NonterminalSymbol("InvFactor");80 81 73 SimpleExpr = new NonterminalSymbol("SimpleExpr"); 82 74 SimpleTerm = new NonterminalSymbol("SimpleTerm"); … … 95 87 96 88 #region Production rules 97 // order of production is important, since they are accessed via index98 // in memoization.99 89 StartSymbol = Expr; 100 90 … … 103 93 104 94 Term.AddProduction(Factor, Term, Multiplication); 95 Term.AddProduction(Expr, Inv, Term, Multiplication); 105 96 Term.AddProduction(Factor); 106 Term.AddProduction(InvExpr, Inv);107 97 108 98 Factor.AddProduction(Var); … … 120 110 SimpleTerm.AddProduction(Var, SimpleTerm, Multiplication); 121 111 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);132 112 #endregion 133 113 … … 157 137 158 138 Stack<Symbol> parseStack = new Stack<Symbol>(sentence); 139 CancelOutCompoundInverse(parseStack); 159 140 160 141 Symbol peek = parseStack.Peek(); … … 164 145 165 146 private string[] GetSubtreeHashes(Stack<Symbol> parseStack) { 147 CancelOutCompoundInverse(parseStack); 166 148 Symbol currentSymbol = parseStack.Pop(); 167 149 … … 211 193 212 194 // 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)) 195 bool[] isFactorRemaining = Enumerable.Repeat(true, childHashes.Count).ToArray(); 196 197 for (int i = 0; i < isFactorRemaining.Length; i++) { 198 if (!isFactorRemaining[i]) continue; 199 if (isFactorRemaining.Count(b => b) <= 2) break; // Until we have constants, we can't cancel out all terms. 200 201 var currFactor = childHashes[i]; 202 var invFactor = AggregateHashes(Inv, currFactor.ToEnumerable()); 203 204 int indexOfInv = childHashes.IndexOf(invFactor); 205 if (indexOfInv >= 0 && isFactorRemaining[indexOfInv]) { 206 isFactorRemaining[i] = isFactorRemaining[indexOfInv] = false; 207 } 208 } 209 return Enumerable 210 .Range(0, isFactorRemaining.Length) 211 .Where(i => isFactorRemaining[i]) 212 .Select(i => childHashes[i]) 219 213 .ToArray(); 220 214 } … … 241 235 242 236 return "[" + hashesArray.Aggregate(operatorSym.StringRepresentation, (result, ti) => result + " ° " + ti) + "]"; 237 } 238 239 private void CancelOutCompoundInverse(Stack<Symbol> parseStack) { 240 // Resolve compound divisions 241 int compoundFractionsCount = 0; 242 243 while (ReferenceEquals(parseStack.Peek(), Inv)) { 244 compoundFractionsCount++; 245 parseStack.Pop(); 246 } 247 248 if (compoundFractionsCount % 2 != 0) { 249 // Compound division are reduced to a single division. 250 parseStack.Push(Inv); 251 } // else: compound divisions fully cancel out each other. 243 252 } 244 253 #endregion -
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15791 r15794 179 179 [TestProperty("Goal", "structure search")] 180 180 public void NoConstants_Inverse() { 181 // sin(x)/ (log(x)*x + x)181 // x / (log(x)*x + x) 182 182 alg.MaxTreeSize = 12; 183 183
Note: See TracChangeset
for help on using the changeset viewer.