Free cookie consent management tool by TermsFeed Policy Generator

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

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

linear value function approximation and good results for poly-10 benchmark

File size: 1.8 KB
Line 
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):
15S -> a | b | c | d | a*S | b*S | c*S | d*S | a+S | b+S | c+S | d+S | !S | (S)
16";
17
18    private readonly IGrammar grammar;
19    private readonly ExpressionInterpreter interpreter = new ExpressionInterpreter();
20    public EvenParityProblem() {
21      this.grammar = new Grammar(grammarString);
22    }
23
24    public double BestKnownQuality(int maxLen) {
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    }
51
52    public string CanonicalRepresentation(string phrase) {
53      throw new NotImplementedException();
54      return phrase;
55    }
56
57    public IEnumerable<Feature> GetFeatures(string phrase)
58    {
59      throw new NotImplementedException();
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.