Changeset 16026 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
- Timestamp:
- 07/27/18 19:28:18 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15993 r16026 28 28 #region Symbols 29 29 30 public IReadOnlyDictionary<Symbol, IReadOnlyList< Production>> Productions { get; private set; }30 public IReadOnlyDictionary<Symbol, IReadOnlyList<SymbolList>> Productions { get; private set; } 31 31 32 32 public NonterminalSymbol Var; … … 85 85 infixExpressionFormatter = cloner.Clone(original.infixExpressionFormatter); 86 86 87 Productions = original.Productions.ToDictionary(x => cloner.Clone(x.Key), x => (IReadOnlyList< Production>)x.Value.Select(cloner.Clone).ToList());87 Productions = original.Productions.ToDictionary(x => cloner.Clone(x.Key), x => (IReadOnlyList<SymbolList>)x.Value.Select(cloner.Clone).ToList()); 88 88 VarTerminals = original.VarTerminals.Select(cloner.Clone).ToList(); 89 89 … … 144 144 StartSymbol = Expr; 145 145 146 Dictionary<Symbol, IReadOnlyList< Production>> productions = new Dictionary<Symbol, IReadOnlyList<Production>>();146 Dictionary<Symbol, IReadOnlyList<SymbolList>> productions = new Dictionary<Symbol, IReadOnlyList<SymbolList>>(); 147 147 148 148 // Map each variable to a separate production rule of the "Var" nonterminal symbol. 149 149 VarTerminals = variables.Select(v => new VariableTerminalSymbol(v)).ToArray(); 150 productions[Var] = VarTerminals.Select(v => new Production(v)).ToArray();150 productions[Var] = VarTerminals.Select(v => new SymbolList(v)).ToArray(); 151 151 152 152 // Expression Grammar Rules 153 var exprProductions = new List< Production>();153 var exprProductions = new List<SymbolList>(); 154 154 if (includedRules.Contains(GrammarRule.MultipleTerms)) 155 exprProductions.Add(new Production(Const, Term, Multiplication, Expr, Addition));156 157 exprProductions.Add(new Production(Const, Term, Multiplication, Const, Addition));155 exprProductions.Add(new SymbolList(Const, Term, Multiplication, Expr, Addition)); 156 157 exprProductions.Add(new SymbolList(Const, Term, Multiplication, Const, Addition)); 158 158 productions[Expr] = exprProductions.ToArray(); 159 159 160 160 // Term Grammar Rules 161 var termProductions = new List< Production>();161 var termProductions = new List<SymbolList>(); 162 162 if (includedRules.Contains(GrammarRule.MultipleFactors)) 163 termProductions.Add(new Production(Factor, Term, Multiplication));163 termProductions.Add(new SymbolList(Factor, Term, Multiplication)); 164 164 if (includedRules.Contains(GrammarRule.InverseTerm)) 165 termProductions.Add(new Production(InvExpr, Inv));166 termProductions.Add(new Production(Factor));165 termProductions.Add(new SymbolList(InvExpr, Inv)); 166 termProductions.Add(new SymbolList(Factor)); 167 167 productions[Term] = termProductions.ToArray(); 168 168 169 169 // Factor Grammar Rules 170 var factorProductions = new List< Production>();171 factorProductions.Add(new Production(Var));170 var factorProductions = new List<SymbolList>(); 171 factorProductions.Add(new SymbolList(Var)); 172 172 if (includedRules.Contains(GrammarRule.Logarithm)) 173 factorProductions.Add(new Production(LogFactor));173 factorProductions.Add(new SymbolList(LogFactor)); 174 174 if (includedRules.Contains(GrammarRule.Exponentiation)) 175 factorProductions.Add(new Production(ExpFactor));175 factorProductions.Add(new SymbolList(ExpFactor)); 176 176 if (includedRules.Contains(GrammarRule.Sine)) 177 factorProductions.Add(new Production(SinFactor));177 factorProductions.Add(new SymbolList(SinFactor)); 178 178 productions[Factor] = factorProductions.ToArray(); 179 179 180 productions[LogFactor] = new[] { new Production(SimpleExpr, Log) };181 productions[ExpFactor] = new[] { new Production(Const, SimpleTerm, Multiplication, Exp) };182 productions[SinFactor] = new[] { new Production(SimpleExpr, Sin) };180 productions[LogFactor] = new[] { new SymbolList(SimpleExpr, Log) }; 181 productions[ExpFactor] = new[] { new SymbolList(Const, SimpleTerm, Multiplication, Exp) }; 182 productions[SinFactor] = new[] { new SymbolList(SimpleExpr, Sin) }; 183 183 184 184 productions[SimpleExpr] = new[] { 185 new Production(Const, SimpleTerm, Multiplication, SimpleExpr, Addition),186 new Production(Const, SimpleTerm, Multiplication, Const, Addition)185 new SymbolList(Const, SimpleTerm, Multiplication, SimpleExpr, Addition), 186 new SymbolList(Const, SimpleTerm, Multiplication, Const, Addition) 187 187 }; 188 188 189 189 productions[SimpleTerm] = new[] { 190 new Production(Var, SimpleTerm, Multiplication),191 new Production(Var)190 new SymbolList(Var, SimpleTerm, Multiplication), 191 new SymbolList(Var) 192 192 }; 193 193 194 194 productions[InvExpr] = new[] { 195 new Production(Const, InvTerm, Multiplication, InvExpr, Addition),196 new Production(Const, InvTerm, Multiplication, Const, Addition)195 new SymbolList(Const, InvTerm, Multiplication, InvExpr, Addition), 196 new SymbolList(Const, InvTerm, Multiplication, Const, Addition) 197 197 }; 198 198 199 199 productions[InvTerm] = new[] { 200 new Production(Factor, InvTerm, Multiplication),201 new Production(Factor)200 new SymbolList(Factor, InvTerm, Multiplication), 201 new SymbolList(Factor) 202 202 }; 203 203 … … 234 234 } 235 235 236 public int GetComplexity(SymbolString s) {237 int c = 0;238 int length = s.Count();239 for (int i = 0; i < length; i++) {240 if (s[i] is NonterminalSymbol || s[i] is VariableTerminalSymbol) c++;241 }242 return c;243 }244 245 236 // returns the maximum achievable sentence length below the maximum complexity 246 237 public int GetMaxSentenceLength(int maxComplexity) { 247 Symbol String s = new SymbolString(StartSymbol);248 249 while (!s.IsSentence() && GetComplexity(s)<= maxComplexity) {238 SymbolList s = new SymbolList(StartSymbol); 239 240 while (!s.IsSentence() && s.Complexity <= maxComplexity) { 250 241 int expandedSymbolIndex = s.NextNonterminalIndex(); 251 242 NonterminalSymbol expandedSymbol = (NonterminalSymbol)s[expandedSymbolIndex]; 252 243 253 244 var productions = Productions[expandedSymbol]; 254 var longestProduction = productions // Find production with most terminal symbols to expand as much as possible...255 .OrderBy( CountTerminals)// but with lowest complexity/nonterminal count to keep complexity low.256 .ThenByDescending( CountNonTerminals)245 var longestProduction = productions // Find production with most terminal symbols to expand as much as possible... 246 .OrderBy(x => x.TerminalSymbolCount) // but with lowest complexity/nonterminal count to keep complexity low. 247 .ThenByDescending(x => x.NonTerminalSymbolCount) 257 248 .First(); 258 249 259 250 s = s.DerivePhrase(expandedSymbolIndex, longestProduction); 260 251 } 261 262 return s.Count(); 263 } 264 265 private int CountTerminals(Production p) { 266 return p.Count(s => s is TerminalSymbol); 267 } 268 269 private int CountNonTerminals(Production p) { 270 return p.Count(s => s is NonterminalSymbol); 271 } 272 273 public double EvaluatePhrase(SymbolString s, IRegressionProblemData problemData, bool optimizeConstants) { 252 return s.Count; 253 } 254 255 public double EvaluatePhrase(SymbolList s, IRegressionProblemData problemData, bool optimizeConstants, int iterations) { 274 256 SymbolicExpressionTree tree = ParseSymbolicExpressionTree(s); 275 257 276 return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants );258 return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants, iterations); 277 259 } 278 260 279 261 #region Parse to SymbolicExpressionTree 280 262 281 public string ToInfixString(Symbol Stringsentence) {263 public string ToInfixString(SymbolList sentence) { 282 264 Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!"); 283 265 Debug.Assert(sentence.All(s => s is TerminalSymbol), "Trying to evaluate symbol sequence with nonterminalsymbols!"); … … 286 268 } 287 269 288 public SymbolicExpressionTree ParseSymbolicExpressionTree(Symbol Stringsentence) {270 public SymbolicExpressionTree ParseSymbolicExpressionTree(SymbolList sentence) { 289 271 Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!"); 290 272
Note: See TracChangeset
for help on using the changeset viewer.