Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs @ 11803

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

#2283 moved problems into a separate folder

File size: 1.7 KB
RevLine 
[11659]1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6using System.Text.RegularExpressions;
7
8namespace HeuristicLab.Problems.GrammaticalOptimization {
9  // 4-bit even parity
10  public class EvenParityProblem : IProblem {
11    // + == OR
12    // * == AND
13    private const string grammarString = @"
14G(S):
[11770]15S -> a | b | c | d | a*S | b*S | c*S | d*S | a+S | b+S | c+S | d+S | !S | (S)
[11659]16";
17
18    private readonly IGrammar grammar;
19    private readonly ExpressionInterpreter interpreter = new ExpressionInterpreter();
20    public EvenParityProblem() {
[11770]21      this.grammar = new Grammar(grammarString);
[11659]22    }
23
[11732]24    public double BestKnownQuality(int maxLen) {
[11659]25      // for now only an upper bound is returned, ideally all fitness cases are predicted correctly
26      return Math.Pow(2, 4);
27    }
28
29    public IGrammar Grammar {
30      get { return grammar; }
31    }
32
33    public double Evaluate(string sentence) {
34      var vars = new bool[4];
35      var nCorrect = 0;
36      for (int b0 = 0; b0 <= 1; b0++)
37        for (int b1 = 0; b1 <= 1; b1++)
38          for (int b2 = 0; b2 <= 1; b2++)
39            for (int b3 = 0; b3 <= 1; b3++) {
40              vars[0] = b0 > 0;
41              vars[1] = b1 > 0;
42              vars[2] = b2 > 0;
43              vars[3] = b3 > 0;
44
45              var pred = interpreter.Interpret(sentence, vars);
46              var target = (b0 > 0) ^ (b1 > 0) ^ (b2 > 0) ^ (b3 > 0);
47              if (pred == target) nCorrect++;
48            }
49      return nCorrect;
50    }
[11727]51
[11742]52    public string CanonicalRepresentation(string terminalPhrase) {
[11770]53      throw new NotImplementedException();
[11727]54      return terminalPhrase;
55    }
[11659]56  }
57}
Note: See TracBrowser for help on using the repository browser.