Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSymbolProblem.cs @ 12815

Last change on this file since 12815 was 12815, checked in by aballeit, 9 years ago

#2283 added SelectionIndicator for MCTS and problems SantaFeAnt, SymbolicRegression10, RoyalSymbol; updated SantaFeAnt grammar

File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Security.Authentication.ExtendedProtection.Configuration;
6using System.Text;
7using System.Text.RegularExpressions;
8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
9
10namespace HeuristicLab.Problems.GrammaticalOptimization
11{
12    // counts the number of times a symbol occurs in a sentence
13    public class RoyalSymbolProblem : ISymbolicExpressionTreeProblem
14    {
15        private const string grammarString = @"
16G(S):
17S -> a | aS | b | bS
18";
19
20        private const string hlGrammarString = @"
21G(S):
22S -> a | b | SS
23";
24
25        // Obj(s \in L(G(S))) = Anzahl "a"-Symbole in s
26
27        private readonly IGrammar grammar;
28        public string Name { get { return "RoyalSymbol"; } }
29        public RoyalSymbolProblem()
30        {
31            this.grammar = new Grammar(grammarString);
32            this.TreeBasedGPGrammar = new Grammar(hlGrammarString);
33            //TODO: allow configuration of the number of symbols
34        }
35
36        public double BestKnownQuality(int maxLen)
37        {
38            return maxLen;
39        }
40
41        public IGrammar Grammar
42        {
43            get { return grammar; }
44        }
45
46        private Regex regex = new Regex("a"); // count the number of "a"s
47        public double Evaluate(string sentence)
48        {
49            // sentence must contain only terminal symbols, we are not checking if the sentence is syntactically valid here because it would be too slow!
50            Debug.Assert(sentence.Any(c => grammar.IsTerminal(c)));
51            return regex.Matches(sentence).Count;
52        }
53        public string CanonicalRepresentation(string phrase)
54        {
55            return phrase;
56        }
57
58        public IEnumerable<Feature> GetFeatures(string phrase)
59        {
60            throw new NotImplementedException();
61        }
62
63        public IGrammar TreeBasedGPGrammar { get; private set; }
64        public string ConvertTreeToSentence(ISymbolicExpressionTree tree)
65        {
66            var sb = new StringBuilder();
67            foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix())
68            {
69                if (s.Symbol.Name == "S") continue;
70                sb.Append(s.Symbol.Name);
71            }
72            return sb.ToString();
73        }
74
75        private string solution = string.Empty;
76
77        public void GenerateProblemSolutions(int maxLen)
78        {
79            StringBuilder sb = new StringBuilder();
80            for (int i = 0; i < maxLen; i++)
81            {
82                sb.Append('a');
83            }
84            solution = sb.ToString();
85        }
86
87        private readonly Regex isParentSolution = new Regex("a+S");
88        public bool IsParentOfProblemSolution(string sentence, int maxLen)
89        {
90            if (sentence.Length < maxLen)
91            {
92                return isParentSolution.IsMatch(sentence);
93            }
94            else
95            {
96                return solution == sentence;
97            }
98        }
99    }
100}
Note: See TracBrowser for help on using the repository browser.