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/RoyalSequenceProblem.cs

    r11832 r11865  
    66using System.Text.RegularExpressions;
    77using HeuristicLab.Common;
     8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    89
    910namespace HeuristicLab.Problems.GrammaticalOptimization {
     
    1516  //
    1617  // this problem should be hard for GP and easy for MCTS (TD should not have an advantage compared to MCTS)
    17   public class RoyalSequenceProblem : IProblem {
     18  public class RoyalSequenceProblem : ISymbolicExpressionTreeProblem {
    1819
    1920    private readonly IGrammar grammar;
     
    3132      this.correctReward = correctReward;
    3233      this.incorrectReward = incorrectReward;
     34
    3335      const char sentenceSymbol = 'S';
    3436      var terminalSymbols = Enumerable.Range(0, alphabetSize).Select(off => (char)((byte)'a' + off)).ToArray();
    3537      var nonTerminalSymbols = new char[] { sentenceSymbol };
    36       var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString()))
    37         .Concat(terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t + sentenceSymbol.ToString())));
    38       //var rules = terminalSymbols.Select(t => Tuple.Create('S', t + "S"))
    39       //  .Concat(terminalSymbols.Select(t => Tuple.Create('S', t.ToString())));
    40       this.grammar = new Grammar(sentenceSymbol, terminalSymbols, nonTerminalSymbols, rules);
     38
     39      {
     40        // create grammar for sequential search
     41        // S -> a..z | aS .. zS
     42        var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString()))
     43          .Concat(terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t + sentenceSymbol.ToString())));
     44        this.grammar = new Grammar(sentenceSymbol, terminalSymbols, nonTerminalSymbols, rules);
     45      }
     46      {
     47        // create grammar for sequential search
     48        // S -> a..z | SS
     49        var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString()))
     50          .Concat(terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t + sentenceSymbol.ToString())));
     51        this.grammar = new Grammar(sentenceSymbol, terminalSymbols, nonTerminalSymbols, rules);
     52      }
    4153
    4254      this.optimalSymbolsForPos = new SortedSet<char>[sequenceLen];
     
    89101    }
    90102
    91     public IEnumerable<Feature> GetFeatures(string phrase)
    92     {
     103    public IEnumerable<Feature> GetFeatures(string phrase) {
    93104      throw new NotImplementedException();
     105    }
     106
     107    public IGrammar TreeBasedGPGrammar { get; private set; }
     108    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
     109      var sb = new StringBuilder();
     110      foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
     111        if (s.Symbol.Name == "S") continue;
     112        sb.Append(s.Symbol.Name);
     113      }
     114      return sb.ToString();
    94115    }
    95116  }
Note: See TracChangeset for help on using the changeset viewer.