Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/02/15 20:41:11 (9 years ago)
Author:
gkronber
Message:

#2283: implemented royal tree problem and grid test for tree-based gp variants

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPhraseSequenceProblem.cs

    r11832 r11865  
    66using System.Text.RegularExpressions;
    77using HeuristicLab.Common;
     8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    89
    910namespace HeuristicLab.Problems.GrammaticalOptimization {
     
    2122  // for phraseLen > 1 this should be harder than RoyalSymbolProblem
    2223  // when phrases are symbol sets instead of sequences then value-estimation routines should be better (TD)
    23   public class RoyalPhraseSequenceProblem : IProblem {
     24  public class RoyalPhraseSequenceProblem : ISymbolicExpressionTreeProblem {
    2425
    2526    private readonly IGrammar grammar;
     
    4344      this.incorrectReward = incorrectReward;
    4445      this.phrasesAsSets = phrasesAsSets;
     46
    4547      var sentenceSymbol = 'S';
    4648      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 };
    5050
    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      }
    5267
    5368      this.optimalPhrasesForPos = new SortedSet<string>[sequenceLen];
     
    133148    }
    134149
    135     public IEnumerable<Feature> GetFeatures(string phrase)
    136     {
     150    public IEnumerable<Feature> GetFeatures(string phrase) {
    137151      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();
    138162    }
    139163  }
Note: See TracChangeset for help on using the changeset viewer.