Changeset 15930


Ignore:
Timestamp:
05/08/18 16:40:07 (16 months ago)
Author:
lkammere
Message:

#2886: Make grammar more configurable in grammar enumeration.

Location:
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration
Files:
2 edited

Legend:

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

    r15907 r15930  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23using System.Diagnostics;
    34using System.Linq;
     
    89
    910namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
     11  public enum GrammarRule {
     12    MultipleTerms,
     13    MultipleFactors,
     14    InverseTerm,
     15    Logarithm,
     16    Exponentiation,
     17    Sine
     18  }
     19
    1020  public class Grammar {
    1121
     
    6373    #endregion
    6474
    65     public Grammar(string[] variables) {
     75    public Grammar(string[] variables) : this(variables, Enum.GetValues(typeof(GrammarRule)).Cast<GrammarRule>()) { }
     76
     77    public Grammar(string[] variables, IEnumerable<GrammarRule> includedRules) {
    6678      #region Define Symbols
    6779      Var = new NonterminalSymbol("Var");
     
    99111      productions[Var] = VarTerminals.Select(v => new Production(v)).ToArray();
    100112
    101       productions[Expr] = new[] {
    102         new Production(Const, Term, Multiplication, Expr, Addition),
    103         new Production(Const, Term, Multiplication, Const, Addition)
    104       };
    105 
    106       productions[Term] = new[] {
    107         new Production(Factor, Term, Multiplication),
    108         new Production(Factor),
    109         new Production(InvExpr, Inv)
    110       };
    111 
    112       productions[Factor] = new[] {
    113         new Production(Var),
    114         new Production(LogFactor),
    115         new Production(ExpFactor),
    116         new Production(SinFactor),
    117       };
     113      // Expression Grammar Rules
     114      var exprProductions = new List<Production>();
     115      if (includedRules.Contains(GrammarRule.MultipleTerms))
     116        exprProductions.Add(new Production(Const, Term, Multiplication, Expr, Addition));
     117
     118      exprProductions.Add(new Production(Const, Term, Multiplication, Const, Addition));
     119      productions[Expr] = exprProductions.ToArray();
     120
     121      // Term Grammar Rules
     122      var termProductions = new List<Production>();
     123      if (includedRules.Contains(GrammarRule.MultipleFactors))
     124        termProductions.Add(new Production(Factor, Term, Multiplication));
     125      if (includedRules.Contains(GrammarRule.InverseTerm))
     126        termProductions.Add(new Production(InvExpr, Inv));
     127      termProductions.Add(new Production(Factor));
     128      productions[Term] = termProductions.ToArray();
     129
     130      // Factor Grammar Rules
     131      var factorProductions = new List<Production>();
     132      factorProductions.Add(new Production(Var));
     133      if (includedRules.Contains(GrammarRule.Logarithm))
     134        factorProductions.Add(new Production(LogFactor));
     135      if (includedRules.Contains(GrammarRule.Exponentiation))
     136        factorProductions.Add(new Production(ExpFactor));
     137      if (includedRules.Contains(GrammarRule.Sine))
     138        factorProductions.Add(new Production(SinFactor));
     139      productions[Factor] = factorProductions.ToArray();
    118140
    119141      productions[LogFactor] = new[] { new Production(SimpleExpr, Log) };
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r15915 r15930  
    3535    private readonly string MaxComplexityParameterName = "Max. Complexity";
    3636    private readonly string GuiUpdateIntervalParameterName = "GUI Update Interval";
     37    private readonly string GrammarSymbolsParameterName = "Grammar Symbols";
    3738
    3839    public override bool SupportsPause { get { return false; } }
     
    8586    public ICheckedItemCollection<IGrammarEnumerationAnalyzer> Analyzers {
    8687      get { return AnalyzersParameter.Value; }
     88    }
     89
     90    public IFixedValueParameter<ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>>> GrammarSymbolsParameter {
     91      get { return (IFixedValueParameter<ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>>>)Parameters[GrammarSymbolsParameterName]; }
     92    }
     93
     94    public ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>> GrammarSymbols {
     95      get { return GrammarSymbolsParameter.Value; }
    8796    }
    8897
     
    136145      Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is RSquaredEvaluator), true);
    137146      //Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is SentenceLogger), true);
     147
     148      var grammarSymbols = Enum.GetValues(typeof(GrammarRule))
     149        .Cast<GrammarRule>()
     150        .Select(v => new EnumValue<GrammarRule>(v));
     151
     152      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>>>(
     153        GrammarSymbolsParameterName,
     154        new ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>>(new CheckedItemCollection<EnumValue<GrammarRule>>(grammarSymbols))
     155      ));
     156      foreach (EnumValue<GrammarRule> grammarSymbol in GrammarSymbols) {
     157        GrammarSymbols.SetItemCheckedState(grammarSymbol, true);
     158      }
    138159    }
    139160
    140161    public GrammarEnumerationAlgorithm(GrammarEnumerationAlgorithm original, Cloner cloner) : base(original, cloner) {
    141 
    142 
    143162    }
    144163    #endregion
     
    165184      PhraseExpansionCount = 0;
    166185
    167       Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray());
     186      Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray(), GrammarSymbols.CheckedItems.Select(v => v.Value));
    168187
    169188      OpenPhrases = new SearchDataStore(SearchDataStructure); // Select search strategy
     
    261280      SymbolString s = new SymbolString(Grammar.StartSymbol);
    262281
    263       while (Grammar.GetComplexity(s) <= MaxComplexity) {
     282      while (!s.IsSentence() && Grammar.GetComplexity(s) <= MaxComplexity) {
    264283        int expandedSymbolIndex = s.NextNonterminalIndex();
    265284        NonterminalSymbol expandedSymbol = (NonterminalSymbol)s[expandedSymbolIndex];
Note: See TracChangeset for help on using the changeset viewer.