[11659] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
[11847] | 5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[11659] | 6 |
|
---|
| 7 | namespace HeuristicLab.Problems.GrammaticalOptimization {
|
---|
[11847] | 8 | public class HardPalindromeProblem : ISymbolicExpressionTreeProblem {
|
---|
[11659] | 9 | // length of the longest palindrome in the sentence + number of different symbols occurring in the palindrome
|
---|
| 10 | private const string grammarString = @"
|
---|
| 11 | G(S):
|
---|
| 12 | S -> T | TS
|
---|
| 13 | T -> a .. z
|
---|
| 14 | ";
|
---|
[11847] | 15 | private const string hlGrammarString = @"
|
---|
| 16 | G(S):
|
---|
| 17 | S -> 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
|
---|
| 18 | ";
|
---|
[11659] | 19 |
|
---|
| 20 | private readonly IGrammar grammar;
|
---|
[12099] | 21 | public string Name { get { return "HardPalindrome"; } }
|
---|
| 22 |
|
---|
[11659] | 23 | public HardPalindromeProblem() {
|
---|
| 24 | this.grammar = new Grammar(grammarString);
|
---|
[11857] | 25 | this.TreeBasedGPGrammar = new Grammar(hlGrammarString);
|
---|
[11659] | 26 | }
|
---|
| 27 |
|
---|
[11732] | 28 | public double BestKnownQuality(int maxLen) {
|
---|
[11659] | 29 | // the whole sentence is a palindrome + each symbol occurs only once or twice
|
---|
| 30 | // for odd total length the number of different characters can be larger than len/2 (aba)
|
---|
| 31 | // the second part is limited to 26 different characters
|
---|
| 32 | return maxLen + Math.Min(26, (maxLen + 1) / 2);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public IGrammar Grammar {
|
---|
| 36 | get { return grammar; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public double Evaluate(string sentence) {
|
---|
| 40 | var palindrome = PalindromeProblem.LongestPalindromicSubstring(sentence);
|
---|
| 41 | var palindromeBytes = ASCIIEncoding.ASCII.GetBytes(palindrome);
|
---|
| 42 | var chrCount = new int[256];
|
---|
| 43 | foreach (var b in palindromeBytes) {
|
---|
| 44 | chrCount[b]++;
|
---|
| 45 | }
|
---|
| 46 | return palindrome.Length + chrCount.Count(c => c > 0);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[11832] | 49 | public string CanonicalRepresentation(string phrase) {
|
---|
| 50 | return phrase;
|
---|
[11727] | 51 | }
|
---|
[11832] | 52 |
|
---|
| 53 | public IEnumerable<Feature> GetFeatures(string phrase)
|
---|
| 54 | {
|
---|
| 55 | throw new NotImplementedException();
|
---|
| 56 | }
|
---|
[12893] | 57 | public bool IsOptimalPhrase(string phrase) {
|
---|
| 58 | throw new NotImplementedException();
|
---|
| 59 | }
|
---|
[11847] | 60 |
|
---|
[11857] | 61 | public IGrammar TreeBasedGPGrammar { get; private set; }
|
---|
[11847] | 62 | public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
|
---|
| 63 | var sb = new StringBuilder();
|
---|
| 64 | foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
|
---|
| 65 | if (s.Symbol.Name == "S") continue;
|
---|
| 66 | sb.Append(s.Symbol.Name);
|
---|
| 67 | }
|
---|
| 68 | return sb.ToString();
|
---|
| 69 | }
|
---|
[11659] | 70 | }
|
---|
| 71 | }
|
---|