Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/Examples/EvenParity.txt @ 17506

Last change on this file since 17506 was 10061, checked in by gkronber, 11 years ago

#2026 copied examples from the GPDL problem instance provider plugin

File size: 1.9 KB
Line 
1PROBLEM EvenParity
2CODE <<
3  const int N = 5;
4>>
5
6NONTERMINALS
7  Expr<<bool[] d, out bool p>>.
8  AND<<bool[] d, out bool p>>.
9  OR<<bool[] d, out bool p>>.
10  NAND<<bool[] d, out bool p>>.
11  NOR<<bool[] d, out bool p>>.
12
13TERMINALS
14  D<<out int val>>
15    CONSTRAINTS
16      val IN SET <<Enumerable.Range(0, N);>>
17  .
18
19RULES
20  Expr<<bool[] d, out bool p>> =                           LOCAL<< int val = 0; >>
21    AND<<d, out p>>
22    | OR<<d, out p>>
23    | NAND<<d, out p>>
24    | NOR<<d, out p>>
25    | D<<out val>>                                         SEM<< p = d[val]; >>
26  .
27
28  AND<<bool[] d, out bool p>> =                            LOCAL << bool p1, p2; >>
29    Expr<<d, out p1>> Expr<<d, out p2>>                    SEM << p = p1 & p2; >>
30  .
31
32  OR<<bool[] d, out bool p>> =                             LOCAL << bool p1, p2; >>
33    Expr<<d, out p1>> Expr<<d, out p2>>                    SEM << p = p1 | p2; >>
34  .
35
36  NAND<<bool[] d, out bool p>> =                           LOCAL << bool p1, p2; >>
37    Expr<<d, out p1>> Expr<<d, out p2>>                    SEM << p = !(p1 & p2); >>
38  .
39
40  NOR<<bool[] d, out bool p>> =                            LOCAL << bool p1, p2; >>
41    Expr<<d, out p1>> Expr<<d, out p2>>                    SEM << p = !(p1 | p2); >>
42  .
43
44MINIMIZE
45  <<
46    bool[] d = new bool[N];
47    int mistakes = 0;
48    for(int i = 0; i < Math.Pow(2, N); i++) {
49      // generate bit-vector and count number of 1-bits
50      int x = i;
51      int nBitsOne = 0;
52      for(int j = 0; j < N; j++) {
53        if((x % 2) == 1) {
54          d[N - j - 1] = true;
55          nBitsOne++;
56        } else {
57          d[N - j - 1] = false;
58        }
59        x = (x / 2);
60      }
61      bool target = (nBitsOne % 2) == 0;
62      // apply model
63      bool p;
64      Expr(d, out p);
65
66      if(p != target) mistakes++;
67    }
68    return mistakes;
69  >>
70END EvenParity.
Note: See TracBrowser for help on using the repository browser.