[11659] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Diagnostics;
|
---|
| 4 | using System.Linq;
|
---|
[11847] | 5 | using System.Text;
|
---|
[11659] | 6 | using System.Text.RegularExpressions;
|
---|
[11847] | 7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[11659] | 8 |
|
---|
| 9 | namespace HeuristicLab.Problems.GrammaticalOptimization {
|
---|
| 10 | // counts the number of times a symbol occurs in a sentence
|
---|
[11847] | 11 | public class RoyalSymbolProblem : ISymbolicExpressionTreeProblem {
|
---|
[11659] | 12 | private const string grammarString = @"
|
---|
| 13 | G(S):
|
---|
| 14 | S -> a | aS | b | bS
|
---|
| 15 | ";
|
---|
[11847] | 16 |
|
---|
| 17 | private const string hlGrammarString = @"
|
---|
| 18 | G(S):
|
---|
| 19 | S -> a | b | SS
|
---|
| 20 | ";
|
---|
| 21 |
|
---|
[11659] | 22 | // Obj(s \in L(G(S))) = Anzahl "a"-Symbole in s
|
---|
| 23 |
|
---|
| 24 | private readonly IGrammar grammar;
|
---|
[12099] | 25 | public string Name { get { return "RoyalSymbol"; } }
|
---|
[11659] | 26 | public RoyalSymbolProblem() {
|
---|
| 27 | this.grammar = new Grammar(grammarString);
|
---|
[11857] | 28 | this.TreeBasedGPGrammar = new Grammar(hlGrammarString);
|
---|
[11793] | 29 | //TODO: allow configuration of the number of symbols
|
---|
[11659] | 30 | }
|
---|
| 31 |
|
---|
[11732] | 32 | public double BestKnownQuality(int maxLen) {
|
---|
[11659] | 33 | return maxLen;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public IGrammar Grammar {
|
---|
| 37 | get { return grammar; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | private Regex regex = new Regex("a"); // count the number of "a"s
|
---|
| 41 | public double Evaluate(string sentence) {
|
---|
| 42 | // sentence must contain only terminal symbols, we are not checking if the sentence is syntactically valid here because it would be too slow!
|
---|
| 43 | Debug.Assert(sentence.Any(c => grammar.IsTerminal(c)));
|
---|
[11806] | 44 | return regex.Matches(sentence).Count;
|
---|
[11659] | 45 | }
|
---|
[11832] | 46 | public string CanonicalRepresentation(string phrase) {
|
---|
| 47 | return phrase;
|
---|
[11727] | 48 | }
|
---|
| 49 |
|
---|
[11847] | 50 | public IEnumerable<Feature> GetFeatures(string phrase) {
|
---|
[11832] | 51 | throw new NotImplementedException();
|
---|
| 52 | }
|
---|
[12893] | 53 | public bool IsOptimalPhrase(string phrase) {
|
---|
| 54 | throw new NotImplementedException();
|
---|
| 55 | }
|
---|
[11847] | 56 |
|
---|
[11857] | 57 | public IGrammar TreeBasedGPGrammar { get; private set; }
|
---|
[11847] | 58 | public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
|
---|
| 59 | var sb = new StringBuilder();
|
---|
| 60 | foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
|
---|
| 61 | if (s.Symbol.Name == "S") continue;
|
---|
| 62 | sb.Append(s.Symbol.Name);
|
---|
| 63 | }
|
---|
| 64 | return sb.ToString();
|
---|
| 65 | }
|
---|
[11659] | 66 | }
|
---|
| 67 | }
|
---|