Last change
on this file since 11659 was
11659,
checked in by gkronber, 10 years ago
|
#2283 initial import of implementation of grammatical optimization problem instances
|
File size:
1.1 KB
|
Line | |
---|
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);
|
---|
19 | }
|
---|
20 |
|
---|
21 | public double GetBestKnownQuality(int maxLen) {
|
---|
22 | return maxLen;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public IGrammar Grammar {
|
---|
26 | get { return grammar; }
|
---|
27 | }
|
---|
28 |
|
---|
29 | private Regex regex = new Regex("a"); // count the number of "a"s
|
---|
30 | public double Evaluate(string sentence) {
|
---|
31 | // sentence must contain only terminal symbols, we are not checking if the sentence is syntactically valid here because it would be too slow!
|
---|
32 | Debug.Assert(sentence.Any(c => grammar.IsTerminal(c)));
|
---|
33 | return regex.Matches(sentence).Count;
|
---|
34 | }
|
---|
35 | }
|
---|
36 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.