Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/PalindromeProblem.cs @ 12394

Last change on this file since 12394 was 12099, checked in by gkronber, 10 years ago

#2283: name for all problems (for output), new unit test, and added testsettings file

File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6
7namespace HeuristicLab.Problems.GrammaticalOptimization {
8  public class PalindromeProblem : ISymbolicExpressionTreeProblem {
9    // length of the longest palindrome in the sentence
10    private const string grammarString = @"
11G(S):
12S -> T | TS
13T -> a .. z
14";
15
16
17    private const string hlGrammarString = @"
18G(S):
19S -> a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | SS
20";
21
22    private readonly IGrammar grammar;
23    public string Name { get { return "Palindrome"; } }
24    public PalindromeProblem() {
25      this.grammar = new Grammar(grammarString);
26      this.TreeBasedGPGrammar = new Grammar(hlGrammarString);
27    }
28
29    public double BestKnownQuality(int maxLen) {
30      // the whole sentence is a palindrome
31      return maxLen;
32    }
33
34    public IGrammar Grammar {
35      get { return grammar; }
36    }
37
38    public double Evaluate(string sentence) {
39      return LongestPalindromicSubstring(sentence).Length;
40    }
41
42
43    private static string Preprocess(string s) {
44      var t = new char[2 * s.Length + 3];
45      t[0] = '$';
46      t[t.Length - 2] = '#';
47      t[t.Length - 1] = '@';
48      for (int i = 0; i < s.Length; i++) {
49        t[2 * (i) + 1] = '#';
50        t[2 * i + 2] = s[i];
51      }
52
53      return new string(t);
54    }
55
56    public static string LongestPalindromicSubstring(string str) {
57      var tLen = 2 * str.Length + 3;
58      var p = new int[tLen];
59      var t = Preprocess(str);
60
61      var center = 0;
62      var right = 0;
63      for (int i = 1; i < t.Length - 1; i++) {
64        var mirror = 2 * center - i;
65        if (right > i) p[i] = Math.Min(right - i, p[mirror]);
66        while (t[i + (1 + p[i])] == t[i - (1 + p[i])]) {
67          p[i]++;
68        }
69        if (i + p[i] > right) {
70          center = i;
71          right = i + p[i];
72        }
73      }
74      var len = 0;
75      center = 0;
76      for (int i = 0; i < t.Length - 1; i++) {
77        if (p[i] > len) {
78          center = i;
79          len = p[i];
80        }
81      }
82
83      var start = (center - 1 - len) / 2;
84      var result = new StringBuilder();
85      for (int i = 0; i < len; i++) {
86        result.Append(str[start + i]);
87      }
88      return result.ToString();
89    }
90
91    public string CanonicalRepresentation(string phrase) {
92      return phrase;
93    }
94
95    public IEnumerable<Feature> GetFeatures(string phrase) {
96      throw new NotImplementedException();
97    }
98
99    public IGrammar TreeBasedGPGrammar { get; private set; }
100    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
101      var sb = new StringBuilder();
102      foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
103        if (s.Symbol.Name == "S") continue;
104        sb.Append(s.Symbol.Name);
105      }
106      return sb.ToString();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.