Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs @ 13042

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

#2283: experiments on grammatical optimization algorithms (maxreward instead of avg reward, ...)

File size: 4.1 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  // 4-bit even parity
11  public class EvenParityProblem : ISymbolicExpressionTreeProblem {
12    // + == OR
13    // * == AND
14    private const string grammarString = @"
15G(S):
16S -> a | b | c | d | a*S | b*S | c*S | d*S | a+S | b+S | c+S | d+S | !S | (S)
17";
18    // A = AND, O = OR, N = NOT, C = Clause
19    private const string hlGrammarString = @"
20G(E):
21E -> A | O | N | C | a | b | c | d
22A -> EE | EEE
23O -> EE | EEE
24N -> E
25C -> E
26    ";
27
28    private readonly IGrammar grammar;
29    public string Name { get { return "EvenParity"; } }
30
31    public EvenParityProblem() {
32      this.grammar = new Grammar(grammarString);
33      this.TreeBasedGPGrammar = new Grammar(hlGrammarString);
34    }
35
36    public double BestKnownQuality(int maxLen) {
37      // for now only an upper bound is returned, ideally all fitness cases are predicted correctly
38      return 16;
39    }
40
41    public IGrammar Grammar {
42      get { return grammar; }
43    }
44
45    public double Evaluate(string sentence) {
46      var interpreter = new ExpressionInterpreter(); // for concurrent evaluation
47      var vars = new bool[4];
48      var nCorrect = 0;
49      for (int b0 = 0; b0 <= 1; b0++)
50        for (int b1 = 0; b1 <= 1; b1++)
51          for (int b2 = 0; b2 <= 1; b2++)
52            for (int b3 = 0; b3 <= 1; b3++) {
53              vars[0] = b0 > 0;
54              vars[1] = b1 > 0;
55              vars[2] = b2 > 0;
56              vars[3] = b3 > 0;
57
58              var pred = interpreter.Interpret(sentence, vars);
59              var target = (b0 > 0) ^ (b1 > 0) ^ (b2 > 0) ^ (b3 > 0);
60              if (pred == target) nCorrect++;
61            }
62      return nCorrect;
63    }
64
65    public bool IsOptimalPhrase(string phrase) {
66      throw new NotImplementedException();
67    }
68
69    public string CanonicalRepresentation(string phrase) {
70      throw new NotImplementedException();
71      return phrase;
72    }
73
74    public IEnumerable<Feature> GetFeatures(string phrase) {
75      return new[] { new Feature(phrase, 1.0) };
76    }
77
78    public IGrammar TreeBasedGPGrammar { get; private set; }
79    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
80      var sb = new StringBuilder();
81
82      TreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb);
83
84      return sb.ToString();
85    }
86
87    private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) {
88      if (treeNode.SubtreeCount == 0) {
89        // terminal
90        sb.Append(treeNode.Symbol.Name);
91      } else {
92        switch (treeNode.Symbol.Name) {
93          case "O": {
94              sb.Append("(");
95              TreeToSentence(treeNode.Subtrees.First(), sb);
96              foreach (var subTree in treeNode.Subtrees.Skip(1)) {
97                sb.Append("+");
98                TreeToSentence(subTree, sb);
99              }
100              sb.Append(")");
101              break;
102            }
103          case "A": {
104              TreeToSentence(treeNode.Subtrees.First(), sb);
105              foreach (var subTree in treeNode.Subtrees.Skip(1)) {
106                sb.Append("*");
107                TreeToSentence(subTree, sb);
108              }
109              break;
110            }
111          case "N": {
112              Debug.Assert(treeNode.SubtreeCount == 1);
113              sb.Append("!(");
114              TreeToSentence(treeNode.Subtrees.Single(), sb);
115              sb.Append(")");
116              break;
117            }
118          case "C": {
119              Debug.Assert(treeNode.SubtreeCount == 1);
120              sb.Append("(");
121              TreeToSentence(treeNode.Subtrees.Single(), sb);
122              sb.Append(")");
123              break;
124            }
125          default: {
126              Debug.Assert(treeNode.SubtreeCount == 1);
127              TreeToSentence(treeNode.Subtrees.Single(), sb);
128              break;
129            }
130        }
131      }
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.