1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Text.RegularExpressions;
|
---|
7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Problems.GrammaticalOptimization {
|
---|
10 | // 4-bit even parity
|
---|
11 | public class EvenParityProblem : ISymbolicExpressionTreeProblem {
|
---|
12 | // + == OR
|
---|
13 | // * == AND
|
---|
14 | private const string grammarString = @"
|
---|
15 | G(S):
|
---|
16 | S -> 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 = @"
|
---|
20 | G(E):
|
---|
21 | E -> A | O | N | C | a | b | c | d
|
---|
22 | A -> EE | EEE
|
---|
23 | O -> EE | EEE
|
---|
24 | N -> E
|
---|
25 | C -> 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 | throw new NotImplementedException();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public IGrammar TreeBasedGPGrammar { get; private set; }
|
---|
75 | public string ConvertTreeToSentence(ISymbolicExpressionTree tree) {
|
---|
76 | var sb = new StringBuilder();
|
---|
77 |
|
---|
78 | TreeToSentence(tree.Root.GetSubtree(0).GetSubtree(0), sb);
|
---|
79 |
|
---|
80 | return sb.ToString();
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) {
|
---|
84 | if (treeNode.SubtreeCount == 0) {
|
---|
85 | // terminal
|
---|
86 | sb.Append(treeNode.Symbol.Name);
|
---|
87 | } else {
|
---|
88 | switch (treeNode.Symbol.Name) {
|
---|
89 | case "O": {
|
---|
90 | sb.Append("(");
|
---|
91 | TreeToSentence(treeNode.Subtrees.First(), sb);
|
---|
92 | foreach (var subTree in treeNode.Subtrees.Skip(1)) {
|
---|
93 | sb.Append("+");
|
---|
94 | TreeToSentence(subTree, sb);
|
---|
95 | }
|
---|
96 | sb.Append(")");
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | case "A": {
|
---|
100 | TreeToSentence(treeNode.Subtrees.First(), sb);
|
---|
101 | foreach (var subTree in treeNode.Subtrees.Skip(1)) {
|
---|
102 | sb.Append("*");
|
---|
103 | TreeToSentence(subTree, sb);
|
---|
104 | }
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | case "N": {
|
---|
108 | Debug.Assert(treeNode.SubtreeCount == 1);
|
---|
109 | sb.Append("!(");
|
---|
110 | TreeToSentence(treeNode.Subtrees.Single(), sb);
|
---|
111 | sb.Append(")");
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | case "C": {
|
---|
115 | Debug.Assert(treeNode.SubtreeCount == 1);
|
---|
116 | sb.Append("(");
|
---|
117 | TreeToSentence(treeNode.Subtrees.Single(), sb);
|
---|
118 | sb.Append(")");
|
---|
119 | break;
|
---|
120 | }
|
---|
121 | default: {
|
---|
122 | Debug.Assert(treeNode.SubtreeCount == 1);
|
---|
123 | TreeToSentence(treeNode.Subtrees.Single(), sb);
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|