[11659] | 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;
|
---|
[11847] | 7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[11659] | 8 |
|
---|
| 9 | namespace HeuristicLab.Problems.GrammaticalOptimization {
|
---|
| 10 | // 4-bit even parity
|
---|
[11847] | 11 | public class EvenParityProblem : ISymbolicExpressionTreeProblem {
|
---|
[11659] | 12 | // + == OR
|
---|
| 13 | // * == AND
|
---|
| 14 | private const string grammarString = @"
|
---|
| 15 | G(S):
|
---|
[11770] | 16 | S -> a | b | c | d | a*S | b*S | c*S | d*S | a+S | b+S | c+S | d+S | !S | (S)
|
---|
[11659] | 17 | ";
|
---|
[11847] | 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 | ";
|
---|
[11659] | 27 |
|
---|
| 28 | private readonly IGrammar grammar;
|
---|
[12099] | 29 | public string Name { get { return "EvenParity"; } }
|
---|
| 30 |
|
---|
[11659] | 31 | public EvenParityProblem() {
|
---|
[11770] | 32 | this.grammar = new Grammar(grammarString);
|
---|
[11857] | 33 | this.TreeBasedGPGrammar = new Grammar(hlGrammarString);
|
---|
[11659] | 34 | }
|
---|
| 35 |
|
---|
[11732] | 36 | public double BestKnownQuality(int maxLen) {
|
---|
[11659] | 37 | // for now only an upper bound is returned, ideally all fitness cases are predicted correctly
|
---|
[11847] | 38 | return 16;
|
---|
[11659] | 39 | }
|
---|
| 40 |
|
---|
| 41 | public IGrammar Grammar {
|
---|
| 42 | get { return grammar; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public double Evaluate(string sentence) {
|
---|
[11847] | 46 | var interpreter = new ExpressionInterpreter(); // for concurrent evaluation
|
---|
[11659] | 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 | }
|
---|
[11727] | 64 |
|
---|
[11832] | 65 | public string CanonicalRepresentation(string phrase) {
|
---|
[11770] | 66 | throw new NotImplementedException();
|
---|
[11832] | 67 | return phrase;
|
---|
[11727] | 68 | }
|
---|
[11832] | 69 |
|
---|
[12290] | 70 | public IEnumerable<Feature> GetFeatures(string phrase)
|
---|
| 71 | {
|
---|
| 72 | return new[] {new Feature(phrase, 1.0)};
|
---|
[11832] | 73 | }
|
---|
[11847] | 74 |
|
---|
[11857] | 75 | public IGrammar TreeBasedGPGrammar { get; private set; }
|
---|
[11847] | 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 | }
|
---|
[11659] | 130 | }
|
---|
| 131 | }
|
---|