Changeset 15930
- Timestamp:
- 05/08/18 16:40:07 (7 years ago)
- 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; 1 using System; 2 using System.Collections.Generic; 2 3 using System.Diagnostics; 3 4 using System.Linq; … … 8 9 9 10 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration { 11 public enum GrammarRule { 12 MultipleTerms, 13 MultipleFactors, 14 InverseTerm, 15 Logarithm, 16 Exponentiation, 17 Sine 18 } 19 10 20 public class Grammar { 11 21 … … 63 73 #endregion 64 74 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) { 66 78 #region Define Symbols 67 79 Var = new NonterminalSymbol("Var"); … … 99 111 productions[Var] = VarTerminals.Select(v => new Production(v)).ToArray(); 100 112 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(); 118 140 119 141 productions[LogFactor] = new[] { new Production(SimpleExpr, Log) }; -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r15915 r15930 35 35 private readonly string MaxComplexityParameterName = "Max. Complexity"; 36 36 private readonly string GuiUpdateIntervalParameterName = "GUI Update Interval"; 37 private readonly string GrammarSymbolsParameterName = "Grammar Symbols"; 37 38 38 39 public override bool SupportsPause { get { return false; } } … … 85 86 public ICheckedItemCollection<IGrammarEnumerationAnalyzer> Analyzers { 86 87 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; } 87 96 } 88 97 … … 136 145 Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is RSquaredEvaluator), true); 137 146 //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 } 138 159 } 139 160 140 161 public GrammarEnumerationAlgorithm(GrammarEnumerationAlgorithm original, Cloner cloner) : base(original, cloner) { 141 142 143 162 } 144 163 #endregion … … 165 184 PhraseExpansionCount = 0; 166 185 167 Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray() );186 Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray(), GrammarSymbols.CheckedItems.Select(v => v.Value)); 168 187 169 188 OpenPhrases = new SearchDataStore(SearchDataStructure); // Select search strategy … … 261 280 SymbolString s = new SymbolString(Grammar.StartSymbol); 262 281 263 while ( Grammar.GetComplexity(s) <= MaxComplexity) {282 while (!s.IsSentence() && Grammar.GetComplexity(s) <= MaxComplexity) { 264 283 int expandedSymbolIndex = s.NextNonterminalIndex(); 265 284 NonterminalSymbol expandedSymbol = (NonterminalSymbol)s[expandedSymbolIndex];
Note: See TracChangeset
for help on using the changeset viewer.