Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12394 was 12290, checked in by gkronber, 10 years ago

#2283 created a new branch to separate development from aballeit

File size: 4.0 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 string CanonicalRepresentation(string phrase) {
66      throw new NotImplementedException();
67      return phrase;
68    }
69
70    public IEnumerable<Feature> GetFeatures(string phrase)
71    {
72      return new[] {new Feature(phrase, 1.0)};
73    }
74
75    public IGrammar TreeBasedGPGrammar { get; private set; }
76    public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
77      var sb = new StringBuilder();
78
79      TreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb);
80
81      return sb.ToString();
82    }
83
84    private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) {
85      if (treeNode.SubtreeCount == 0) {
86        // terminal
87        sb.Append(treeNode.Symbol.Name);
88      } else {
89        switch (treeNode.Symbol.Name) {
90          case "O": {
91              sb.Append("(");
92              TreeToSentence(treeNode.Subtrees.First(), sb);
93              foreach (var subTree in treeNode.Subtrees.Skip(1)) {
94                sb.Append("+");
95                TreeToSentence(subTree, sb);
96              }
97              sb.Append(")");
98              break;
99            }
100          case "A": {
101              TreeToSentence(treeNode.Subtrees.First(), sb);
102              foreach (var subTree in treeNode.Subtrees.Skip(1)) {
103                sb.Append("*");
104                TreeToSentence(subTree, sb);
105              }
106              break;
107            }
108          case "N": {
109              Debug.Assert(treeNode.SubtreeCount == 1);
110              sb.Append("!(");
111              TreeToSentence(treeNode.Subtrees.Single(), sb);
112              sb.Append(")");
113              break;
114            }
115          case "C": {
116              Debug.Assert(treeNode.SubtreeCount == 1);
117              sb.Append("(");
118              TreeToSentence(treeNode.Subtrees.Single(), sb);
119              sb.Append(")");
120              break;
121            }
122          default: {
123              Debug.Assert(treeNode.SubtreeCount == 1);
124              TreeToSentence(treeNode.Subtrees.Single(), sb);
125              break;
126            }
127        }
128      }
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.