Rev | Line | |
---|
[11659] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Diagnostics;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Text.RegularExpressions;
|
---|
| 6 |
|
---|
| 7 | namespace HeuristicLab.Problems.GrammaticalOptimization {
|
---|
| 8 | // counts the number of times a symbol occurs in a sentence
|
---|
| 9 | public class RoyalSymbolProblem : IProblem {
|
---|
| 10 | private const string grammarString = @"
|
---|
| 11 | G(S):
|
---|
| 12 | S -> a | aS | b | bS
|
---|
| 13 | ";
|
---|
| 14 | // Obj(s \in L(G(S))) = Anzahl "a"-Symbole in s
|
---|
| 15 |
|
---|
| 16 | private readonly IGrammar grammar;
|
---|
| 17 | public RoyalSymbolProblem() {
|
---|
| 18 | this.grammar = new Grammar(grammarString);
|
---|
[11793] | 19 | //TODO: allow configuration of the number of symbols
|
---|
[11659] | 20 | }
|
---|
| 21 |
|
---|
[11732] | 22 | public double BestKnownQuality(int maxLen) {
|
---|
[11659] | 23 | return maxLen;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public IGrammar Grammar {
|
---|
| 27 | get { return grammar; }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | private Regex regex = new Regex("a"); // count the number of "a"s
|
---|
| 31 | public double Evaluate(string sentence) {
|
---|
| 32 | // sentence must contain only terminal symbols, we are not checking if the sentence is syntactically valid here because it would be too slow!
|
---|
| 33 | Debug.Assert(sentence.Any(c => grammar.IsTerminal(c)));
|
---|
[11793] | 34 | return regex.Matches(sentence.ToString()).Count;
|
---|
[11659] | 35 | }
|
---|
[11742] | 36 | public string CanonicalRepresentation(string terminalPhrase) {
|
---|
[11793] | 37 | throw new NotImplementedException();
|
---|
[11727] | 38 | return terminalPhrase;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[11659] | 41 | }
|
---|
| 42 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.