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

Last change on this file since 11727 was 11727, checked in by gkronber, 5 years ago

#2283: worked on grammatical optimization problem solvers (simple MCTS done)

File size: 1.2 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    }
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    public string Hash(string terminalPhrase) {
36      return terminalPhrase;
37    }
38
39  }
40}
Note: See TracBrowser for help on using the repository browser.