Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/27/18 19:28:18 (6 years ago)
Author:
bburlacu
Message:

#2886:

  • replace functionally-overlapping classes Production and SymbolString with a single class SymbolList
  • refactor methods from Grammar class as methods and properties of SymbolList
  • add parameter for the number of constant optimization iterations
  • refactor code
File:
1 edited

Legend:

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

    r15993 r16026  
    2828    #region Symbols
    2929
    30     public IReadOnlyDictionary<Symbol, IReadOnlyList<Production>> Productions { get; private set; }
     30    public IReadOnlyDictionary<Symbol, IReadOnlyList<SymbolList>> Productions { get; private set; }
    3131
    3232    public NonterminalSymbol Var;
     
    8585      infixExpressionFormatter = cloner.Clone(original.infixExpressionFormatter);
    8686
    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());
    8888      VarTerminals = original.VarTerminals.Select(cloner.Clone).ToList();
    8989
     
    144144      StartSymbol = Expr;
    145145
    146       Dictionary<Symbol, IReadOnlyList<Production>> productions = new Dictionary<Symbol, IReadOnlyList<Production>>();
     146      Dictionary<Symbol, IReadOnlyList<SymbolList>> productions = new Dictionary<Symbol, IReadOnlyList<SymbolList>>();
    147147
    148148      // Map each variable to a separate production rule of the "Var" nonterminal symbol.
    149149      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();
    151151
    152152      // Expression Grammar Rules
    153       var exprProductions = new List<Production>();
     153      var exprProductions = new List<SymbolList>();
    154154      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));
    158158      productions[Expr] = exprProductions.ToArray();
    159159
    160160      // Term Grammar Rules
    161       var termProductions = new List<Production>();
     161      var termProductions = new List<SymbolList>();
    162162      if (includedRules.Contains(GrammarRule.MultipleFactors))
    163         termProductions.Add(new Production(Factor, Term, Multiplication));
     163        termProductions.Add(new SymbolList(Factor, Term, Multiplication));
    164164      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));
    167167      productions[Term] = termProductions.ToArray();
    168168
    169169      // 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));
    172172      if (includedRules.Contains(GrammarRule.Logarithm))
    173         factorProductions.Add(new Production(LogFactor));
     173        factorProductions.Add(new SymbolList(LogFactor));
    174174      if (includedRules.Contains(GrammarRule.Exponentiation))
    175         factorProductions.Add(new Production(ExpFactor));
     175        factorProductions.Add(new SymbolList(ExpFactor));
    176176      if (includedRules.Contains(GrammarRule.Sine))
    177         factorProductions.Add(new Production(SinFactor));
     177        factorProductions.Add(new SymbolList(SinFactor));
    178178      productions[Factor] = factorProductions.ToArray();
    179179
    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) };
    183183
    184184      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)
    187187      };
    188188
    189189      productions[SimpleTerm] = new[] {
    190         new Production(Var, SimpleTerm, Multiplication),
    191         new Production(Var)
     190        new SymbolList(Var, SimpleTerm, Multiplication),
     191        new SymbolList(Var)
    192192      };
    193193
    194194      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)
    197197      };
    198198
    199199      productions[InvTerm] = new[] {
    200         new Production(Factor, InvTerm, Multiplication),
    201         new Production(Factor)
     200        new SymbolList(Factor, InvTerm, Multiplication),
     201        new SymbolList(Factor)
    202202      };
    203203
     
    234234    }
    235235
    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 
    245236    // returns the maximum achievable sentence length below the maximum complexity
    246237    public int GetMaxSentenceLength(int maxComplexity) {
    247       SymbolString 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) {
    250241        int expandedSymbolIndex = s.NextNonterminalIndex();
    251242        NonterminalSymbol expandedSymbol = (NonterminalSymbol)s[expandedSymbolIndex];
    252243
    253244        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)
    257248          .First();
    258249
    259250        s = s.DerivePhrase(expandedSymbolIndex, longestProduction);
    260251      }
    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) {
    274256      SymbolicExpressionTree tree = ParseSymbolicExpressionTree(s);
    275257
    276       return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants);
     258      return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants, iterations);
    277259    }
    278260
    279261    #region Parse to SymbolicExpressionTree
    280262
    281     public string ToInfixString(SymbolString sentence) {
     263    public string ToInfixString(SymbolList sentence) {
    282264      Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!");
    283265      Debug.Assert(sentence.All(s => s is TerminalSymbol), "Trying to evaluate symbol sequence with nonterminalsymbols!");
     
    286268    }
    287269
    288     public SymbolicExpressionTree ParseSymbolicExpressionTree(SymbolString sentence) {
     270    public SymbolicExpressionTree ParseSymbolicExpressionTree(SymbolList sentence) {
    289271      Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!");
    290272
Note: See TracChangeset for help on using the changeset viewer.