Changeset 15834


Ignore:
Timestamp:
03/08/18 11:42:18 (3 years ago)
Author:
lkammere
Message:

#2886: Store production rules in grammar instead of nonterminal symbols.

Location:
branches/2886_SymRegGrammarEnumeration
Files:
6 edited

Legend:

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

    r15832 r15834  
    1414
    1515    #region Symbols
    16     public VariableSymbol Var;
     16
     17    public IReadOnlyDictionary<Symbol, IReadOnlyList<Production>> Productions { get; }
     18
     19    public NonterminalSymbol Var;
     20    public IReadOnlyList<VariableTerminalSymbol> VarTerminals;
    1721
    1822    public NonterminalSymbol Expr;
     
    6569    public Grammar(string[] variables) {
    6670      #region Define Symbols
    67       Var = new VariableSymbol("var", variables);
     71      Var = new NonterminalSymbol("Var");
    6872
    6973      Expr = new NonterminalSymbol("Expr");
     
    96100      StartSymbol = Expr;
    97101
    98       Expr.AddProduction(Term, Expr, Addition);
    99       Expr.AddProduction(Term);
    100 
    101       Term.AddProduction(Factor, Term, Multiplication);
    102       Term.AddProduction(Factor);
    103       Term.AddProduction(InvExpr, Inv);
    104 
    105       Factor.AddProduction(Var);
    106       Factor.AddProduction(LogFactor);
    107       Factor.AddProduction(ExpFactor);
    108       Factor.AddProduction(SinFactor);
    109       Factor.AddProduction(CosFactor);
    110 
    111       LogFactor.AddProduction(SimpleExpr, Log);
    112       ExpFactor.AddProduction(SimpleTerm, Exp);
    113       SinFactor.AddProduction(SimpleExpr, Sin);
    114       CosFactor.AddProduction(SimpleExpr, Cos);
    115 
    116       SimpleExpr.AddProduction(SimpleTerm, SimpleExpr, Addition);
    117       SimpleExpr.AddProduction(SimpleTerm);
    118 
    119       SimpleTerm.AddProduction(Var, SimpleTerm, Multiplication);
    120       SimpleTerm.AddProduction(Var);
    121 
    122       InvExpr.AddProduction(InvTerm, InvExpr, Addition);
    123       InvExpr.AddProduction(InvTerm);
    124 
    125       InvTerm.AddProduction(Factor, InvTerm, Multiplication);
    126       InvTerm.AddProduction(Factor);
     102      Dictionary<Symbol, IReadOnlyList<Production>> productions = new Dictionary<Symbol, IReadOnlyList<Production>>();
     103
     104      // Map each variable to a separate production rule of the "Var" nonterminal symbol.
     105      VarTerminals = variables.Select(v => new VariableTerminalSymbol(v)).ToArray();
     106      productions[Var] = VarTerminals.Select(v => new Production(v)).ToArray();
     107
     108      productions[Expr] = new[] {
     109        new Production(Term, Expr, Addition),
     110        new Production(Term)
     111      };
     112
     113      productions[Term] = new[] {
     114        new Production(Factor, Term, Multiplication),
     115        new Production(Factor),
     116        new Production(InvExpr, Inv)
     117      };
     118
     119      productions[Factor] = new[] {
     120        new Production(Var),
     121        new Production(LogFactor),
     122        new Production(ExpFactor),
     123        new Production(SinFactor),
     124        new Production(CosFactor),
     125      };
     126
     127      productions[LogFactor] = new[] { new Production(SimpleExpr, Log) };
     128      productions[ExpFactor] = new[] { new Production(SimpleTerm, Exp) };
     129      productions[SinFactor] = new[] { new Production(SimpleExpr, Sin) };
     130      productions[CosFactor] = new[] { new Production(SimpleExpr, Cos) };
     131
     132      productions[SimpleExpr] = new[] {
     133        new Production(SimpleTerm, SimpleExpr, Addition),
     134        new Production(SimpleTerm)
     135      };
     136
     137      productions[SimpleTerm] = new[] {
     138        new Production(Var, SimpleTerm, Multiplication),
     139        new Production(Var)
     140      };
     141
     142      productions[InvExpr] = new[] {
     143        new Production(InvTerm, InvExpr, Addition),
     144        new Production(InvTerm)
     145      };
     146
     147      productions[InvTerm] = new[] {
     148        new Production(Factor, InvTerm, Multiplication),
     149        new Production(Factor)
     150      };
     151
     152      Productions = productions;
    127153      #endregion
    128154
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r15832 r15834  
    152152        int nonterminalSymbolIndex = currPhrase.NextNonterminalIndex();
    153153        NonterminalSymbol expandedSymbol = (NonterminalSymbol)currPhrase[nonterminalSymbolIndex];
    154         var appliedProductions = expandedSymbol.Alternatives;
     154        var appliedProductions = Grammar.Productions[expandedSymbol];
    155155
    156156        for (int i = 0; i < appliedProductions.Count; i++) {
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs

    r15832 r15834  
    6060
    6161  public class NonterminalSymbol : Symbol {
    62     private readonly List<Production> alternatives;
    63 
    64     public IReadOnlyList<Production> Alternatives {
    65       get { return alternatives.AsReadOnly(); }
    66     }
    67 
    68     public NonterminalSymbol(string representation) : base(representation) {
    69       alternatives = new List<Production>();
    70     }
    71 
    72     public void AddProduction(params Symbol[] production) {
    73       alternatives.Add(new Production(production));
    74     }
    75   }
    76 
    77   public class VariableSymbol : NonterminalSymbol { // Convenience class
    78     public IEnumerable<VariableTerminalSymbol> VariableTerminalSymbols { get; }
    79 
    80     public VariableSymbol(string representation, IEnumerable<string> variableNames) : base(representation) {
    81       List<VariableTerminalSymbol> createdSymbols = new List<VariableTerminalSymbol>();
    82       VariableTerminalSymbols = createdSymbols;
    83 
    84       foreach (string variableName in variableNames) {
    85         VariableTerminalSymbol s = new VariableTerminalSymbol(variableName);
    86         createdSymbols.Add(s);
    87         AddProduction(s);
    88       }
    89     }
     62    public NonterminalSymbol(string representation) : base(representation) { }
    9063  }
    9164
  • branches/2886_SymRegGrammarEnumeration/SymRegGrammarEnumeration.sln

    r15823 r15834  
    99EndProject
    1010Global
    11   GlobalSection(Performance) = preSolution
    12     HasPerformanceSessions = true
    13   EndGlobalSection
    1411  GlobalSection(SolutionConfigurationPlatforms) = preSolution
    1512    Debug|Any CPU = Debug|Any CPU
  • branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs

    r15832 r15834  
    6262      alg.Start();
    6363
    64       TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First();
     64      TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
    6565      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
    6666      TerminalSymbol addSymbol = alg.Grammar.Addition;
     
    117117      alg.Start();
    118118
    119       TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First();
     119      TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
    120120      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
    121121      TerminalSymbol addSymbol = alg.Grammar.Addition;
     
    145145      alg.Start();
    146146
    147       TerminalSymbol xSymbol = alg.Grammar.Var.VariableTerminalSymbols.First(v => v.StringRepresentation == "X");
    148       TerminalSymbol ySymbol = alg.Grammar.Var.VariableTerminalSymbols.First(v => v.StringRepresentation == "Y");
     147      TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First(v => v.StringRepresentation == "X");
     148      TerminalSymbol ySymbol = alg.Grammar.VarTerminals.First(v => v.StringRepresentation == "Y");
    149149      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
    150150      TerminalSymbol addSymbol = alg.Grammar.Addition;
  • branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs

    r15832 r15834  
    1717      grammar = new Grammar(new[] { "a", "b", "c" });
    1818
    19       varA = grammar.Var.VariableTerminalSymbols.First(s => s.StringRepresentation == "a");
    20       varB = grammar.Var.VariableTerminalSymbols.First(s => s.StringRepresentation == "b");
    21       varC = grammar.Var.VariableTerminalSymbols.First(s => s.StringRepresentation == "c");
     19      varA = grammar.VarTerminals.First(s => s.StringRepresentation == "a");
     20      varB = grammar.VarTerminals.First(s => s.StringRepresentation == "b");
     21      varC = grammar.VarTerminals.First(s => s.StringRepresentation == "c");
    2222    }
    2323
Note: See TracChangeset for help on using the changeset viewer.