Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/PermutationProblem.cs @ 11972

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

#2283 new experiments

File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6using System.Text.RegularExpressions;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8
9namespace HeuristicLab.Problems.GrammaticalOptimization {
10  public class PermutationProblem : ISymbolicExpressionTreeProblem {
11    private const string grammarString = @"
12G(S):
13S -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c | d | e | f | 0S | 1S | 2S | 3S | 4S | 5S | 6S | 7S | 8S | 9S | aS | bS | cS | dS | eS | fS
14";
15
16    private const string hlGrammarString = @"
17G(S):
18S -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c | d | e | f | SS
19";
20
21    private readonly IGrammar grammar;
22    public PermutationProblem() {
23      this.grammar = new Grammar(grammarString);
24      this.TreeBasedGPGrammar = new Grammar(hlGrammarString);
25    }
26
27    public double BestKnownQuality(int maxLen) {
28      return Math.Min(16.0, maxLen);
29    }
30
31    public IGrammar Grammar {
32      get { return grammar; }
33    }
34
35    public double Evaluate(string sentence) {
36      // sentence must contain only terminal symbols, we are not checking if the sentence is syntactically valid here because it would be too slow!
37      Debug.Assert(sentence.Any(c => grammar.IsTerminal(c)));
38      return sentence.Distinct().Count();
39    }
40
41    public string CanonicalRepresentation(string phrase) {
42      return phrase;
43    }
44
45    public IEnumerable<Feature> GetFeatures(string phrase) {
46      throw new NotImplementedException();
47    }
48    public IGrammar TreeBasedGPGrammar { get; private set; }
49    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
50      var sb = new StringBuilder();
51      foreach (var s in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
52        if (s.Symbol.Name == "S") continue;
53        sb.Append(s.Symbol.Name);
54      }
55      return sb.ToString();
56    }
57  }
58}
Note: See TracBrowser for help on using the repository browser.