Changeset 11865 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPhraseSequenceProblem.cs
- Timestamp:
- 02/02/15 20:41:11 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPhraseSequenceProblem.cs
r11832 r11865 6 6 using System.Text.RegularExpressions; 7 7 using HeuristicLab.Common; 8 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 8 9 9 10 namespace HeuristicLab.Problems.GrammaticalOptimization { … … 21 22 // for phraseLen > 1 this should be harder than RoyalSymbolProblem 22 23 // when phrases are symbol sets instead of sequences then value-estimation routines should be better (TD) 23 public class RoyalPhraseSequenceProblem : I Problem {24 public class RoyalPhraseSequenceProblem : ISymbolicExpressionTreeProblem { 24 25 25 26 private readonly IGrammar grammar; … … 43 44 this.incorrectReward = incorrectReward; 44 45 this.phrasesAsSets = phrasesAsSets; 46 45 47 var sentenceSymbol = 'S'; 46 48 var terminalSymbols = Enumerable.Range(0, alphabetSize).Select(off => (char)((byte)'a' + off)).ToArray(); 47 var nonTerminalSymbols = new char[] { 'S' }; 48 var rules = terminalSymbols.Select(t => Tuple.Create('S', t.ToString())) 49 .Concat(terminalSymbols.Select(t => Tuple.Create('S', t + "S"))); 49 var nonTerminalSymbols = new char[] { sentenceSymbol }; 50 50 51 this.grammar = new Grammar(sentenceSymbol, terminalSymbols, nonTerminalSymbols, rules); 51 { 52 // create grammar 53 // S -> a..z | aS .. zS 54 var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString())) 55 .Concat(terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t + sentenceSymbol.ToString()))); 56 57 this.grammar = new Grammar(sentenceSymbol, terminalSymbols, nonTerminalSymbols, rules); 58 } 59 { 60 // create grammar for tree-based GP 61 // S -> a..z | SS 62 var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString())) 63 .Concat(new Tuple<char, string>[] { Tuple.Create(sentenceSymbol, sentenceSymbol.ToString() + sentenceSymbol) }); 64 65 this.TreeBasedGPGrammar = new Grammar(sentenceSymbol, terminalSymbols, nonTerminalSymbols, rules); 66 } 52 67 53 68 this.optimalPhrasesForPos = new SortedSet<string>[sequenceLen]; … … 133 148 } 134 149 135 public IEnumerable<Feature> GetFeatures(string phrase) 136 { 150 public IEnumerable<Feature> GetFeatures(string phrase) { 137 151 throw new NotImplementedException(); 152 } 153 154 public IGrammar TreeBasedGPGrammar { get; private set; } 155 public string ConvertTreeToSentence(ISymbolicExpressionTree tree) { 156 var sb = new StringBuilder(); 157 foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) { 158 if (s.Symbol.Name == "S") continue; 159 sb.Append(s.Symbol.Name); 160 } 161 return sb.ToString(); 138 162 } 139 163 }
Note: See TracChangeset
for help on using the changeset viewer.