Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11793 was 11793, checked in by gkronber, 9 years ago

#2283 fixed compile errors and refactoring

File size: 1.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text.RegularExpressions;
6
7namespace 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 = @"
11G(S):
12S -> 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      //TODO: allow configuration of the number of symbols
20    }
21
22    public double BestKnownQuality(int maxLen) {
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)));
34      return regex.Matches(sentence.ToString()).Count;
35    }
36    public string CanonicalRepresentation(string terminalPhrase) {
37      throw new NotImplementedException();
38      return terminalPhrase;
39    }
40
41  }
42}
Note: See TracBrowser for help on using the repository browser.