Changeset 15834 for branches/2886_SymRegGrammarEnumeration
- Timestamp:
- 03/08/18 11:42:18 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15832 r15834 14 14 15 15 #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; 17 21 18 22 public NonterminalSymbol Expr; … … 65 69 public Grammar(string[] variables) { 66 70 #region Define Symbols 67 Var = new VariableSymbol("var", variables);71 Var = new NonterminalSymbol("Var"); 68 72 69 73 Expr = new NonterminalSymbol("Expr"); … … 96 100 StartSymbol = Expr; 97 101 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; 127 153 #endregion 128 154 -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r15832 r15834 152 152 int nonterminalSymbolIndex = currPhrase.NextNonterminalIndex(); 153 153 NonterminalSymbol expandedSymbol = (NonterminalSymbol)currPhrase[nonterminalSymbolIndex]; 154 var appliedProductions = expandedSymbol.Alternatives;154 var appliedProductions = Grammar.Productions[expandedSymbol]; 155 155 156 156 for (int i = 0; i < appliedProductions.Count; i++) { -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs
r15832 r15834 60 60 61 61 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) { } 90 63 } 91 64 -
branches/2886_SymRegGrammarEnumeration/SymRegGrammarEnumeration.sln
r15823 r15834 9 9 EndProject 10 10 Global 11 GlobalSection(Performance) = preSolution12 HasPerformanceSessions = true13 EndGlobalSection14 11 GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 12 Debug|Any CPU = Debug|Any CPU -
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15832 r15834 62 62 alg.Start(); 63 63 64 TerminalSymbol varSymbol = alg.Grammar.Var .VariableTerminalSymbols.First();64 TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First(); 65 65 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; 66 66 TerminalSymbol addSymbol = alg.Grammar.Addition; … … 117 117 alg.Start(); 118 118 119 TerminalSymbol varSymbol = alg.Grammar.Var .VariableTerminalSymbols.First();119 TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First(); 120 120 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; 121 121 TerminalSymbol addSymbol = alg.Grammar.Addition; … … 145 145 alg.Start(); 146 146 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"); 149 149 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; 150 150 TerminalSymbol addSymbol = alg.Grammar.Addition; -
branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs
r15832 r15834 17 17 grammar = new Grammar(new[] { "a", "b", "c" }); 18 18 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"); 22 22 } 23 23
Note: See TracChangeset
for help on using the changeset viewer.