Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15794


Ignore:
Timestamp:
02/20/18 15:54:21 (6 years ago)
Author:
lkammere
Message:

#2886: Add "full" inverse production, cancellation of factors by their inverse and resolution of compound division.

Location:
branches/2886_SymRegGrammarEnumeration
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15791 r15794  
    2424    public NonterminalSymbol SinFactor;
    2525
    26     public NonterminalSymbol InvExpr;
    27     public NonterminalSymbol InvTerm;
    28     public NonterminalSymbol InvFactor;
    29 
    3026    public NonterminalSymbol SimpleExpr;
    3127    public NonterminalSymbol SimpleTerm;
     
    7571      SinFactor = new NonterminalSymbol("SinFactor");
    7672
    77       InvExpr = new NonterminalSymbol("InvExpr");
    78       InvTerm = new NonterminalSymbol("InvTerm");
    79       InvFactor = new NonterminalSymbol("InvFactor");
    80 
    8173      SimpleExpr = new NonterminalSymbol("SimpleExpr");
    8274      SimpleTerm = new NonterminalSymbol("SimpleTerm");
     
    9587
    9688      #region Production rules
    97       // order of production is important, since they are accessed via index
    98       // in memoization.
    9989      StartSymbol = Expr;
    10090
     
    10393
    10494      Term.AddProduction(Factor, Term, Multiplication);
     95      Term.AddProduction(Expr, Inv, Term, Multiplication);
    10596      Term.AddProduction(Factor);
    106       Term.AddProduction(InvExpr, Inv);
    10797
    10898      Factor.AddProduction(Var);
     
    120110      SimpleTerm.AddProduction(Var, SimpleTerm, Multiplication);
    121111      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);
    132112      #endregion
    133113
     
    157137
    158138      Stack<Symbol> parseStack = new Stack<Symbol>(sentence);
     139      CancelOutCompoundInverse(parseStack);
    159140
    160141      Symbol peek = parseStack.Peek();
     
    164145
    165146    private string[] GetSubtreeHashes(Stack<Symbol> parseStack) {
     147      CancelOutCompoundInverse(parseStack);
    166148      Symbol currentSymbol = parseStack.Pop();
    167149
     
    211193
    212194        // 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])
    219213          .ToArray();
    220214      }
     
    241235
    242236      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.
    243252    }
    244253    #endregion
  • branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs

    r15791 r15794  
    179179    [TestProperty("Goal", "structure search")]
    180180    public void NoConstants_Inverse() {
    181       // sin(x) / (log(x)*x + x)
     181      // x / (log(x)*x + x)
    182182      alg.MaxTreeSize = 12;
    183183
Note: See TracChangeset for help on using the changeset viewer.