using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text.RegularExpressions; namespace HeuristicLab.Problems.GrammaticalOptimization { // counts the number of times a symbol occurs in a sentence public class RoyalSymbolProblem : IProblem { private const string grammarString = @" G(S): S -> a | aS | b | bS "; // Obj(s \in L(G(S))) = Anzahl "a"-Symbole in s private readonly IGrammar grammar; public RoyalSymbolProblem() { this.grammar = new Grammar(grammarString); } public double BestKnownQuality(int maxLen) { return maxLen; } public IGrammar Grammar { get { return grammar; } } private Regex regex = new Regex("a"); // count the number of "a"s public double Evaluate(string sentence) { // sentence must contain only terminal symbols, we are not checking if the sentence is syntactically valid here because it would be too slow! Debug.Assert(sentence.Any(c => grammar.IsTerminal(c))); return regex.Matches(sentence).Count; } public string Hash(string terminalPhrase) { return terminalPhrase; } } }