Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/Examples/multi-output-multiplier.txt @ 11023

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

#2026 copied examples from the GPDL problem instance provider plugin

File size: 3.6 KB
Line 
1PROBLEM MultiOutputMultiplier
2
3CODE <<
4  const int N = 4;
5  class State { public bool[] x1; public bool[] x2; public bool[] output; }
6  private void GenerateProblemData(int n, out bool[][] a, out bool[][] b, out bool[][] expectedOutput) {
7    var nConfigurations = (int)Math.Pow(2, n);
8    var e = from i in Enumerable.Range(0, nConfigurations)
9            from j in Enumerable.Range(0, i+1)
10            select new {a = ToBitArray(N, i), b = ToBitArray(N, j), res = ToBitArray(2*N, i*j) };
11   
12    a = e.Select(x=>x.a).ToArray();
13    b = e.Select(x=>x.b).ToArray();
14    expectedOutput = e.Select(x=>x.res).ToArray();
15  }
16 
17  private bool[] ToBitArray(int n, int x) {
18    var b = new bool[n];
19    for(int i=0; i<n; i++) {
20      b[n - i - 1] = (x % 2) == 0;
21      x = x / 2;
22    }
23    if(x>0) throw new ArgumentException();
24    return b;
25  }
26>>
27
28NONTERMINALS
29  Expr<<State state, out bool res>>.
30  Assign<<State state, out bool res>>.
31  AND<<State state, out bool res>>.
32  AND1<<State state, out bool res>>.
33  XOR<<State state, out bool res>>.
34  OR<<State state, out bool res>>.
35 
36
37TERMINALS
38  InputA<<out int id>>
39    CONSTRAINTS
40      id IN SET << Enumerable.Range(0, N) >>
41  .
42 
43  InputB<<out int id>>
44    CONSTRAINTS
45      id IN SET << Enumerable.Range(0, N) >>
46  .
47 
48  Output<<out int id>> 
49    CONSTRAINTS
50      id IN SET << Enumerable.Range(0, 2 * N) >>
51  .
52
53RULES
54  Expr<<State state, out bool res>> =                   LOCAL << int id; >>
55    Assign<<state, out res>>
56    | AND<<state, out res>>
57    | AND1<<state, out res>>
58    | XOR<<state, out res>>
59    | OR<<state, out res>>
60    | InputA<<out id>>                                                          SEM<< res = state.x1[id]; >>
61    | InputB<<out id>>                                                          SEM<< res = state.x2[id]; >>
62    | Output<<out id>>                                                          SEM<< res = state.output[id]; >>
63  .                                                                             
64                                                                               
65  Assign<<State state, out bool res>> =                 LOCAL << int outId; >>
66    Output<<out outId>>  Expr<<state, out res>>                        SEM<< state.output[outId] = res; >>
67  .
68 
69  AND<<State state, out bool res>> =                    LOCAL << bool resA, resB; >>
70    Expr<<state, out resA>> Expr<<state, out resB>>           SEM << res = resA & resB; >>
71  .
72  AND1<<State state, out bool res>> =                   LOCAL << bool resA, resB; >>
73    Expr<<state, out resA>> Expr<<state, out resB>>           SEM << res = resA & !resB; >>
74  .
75  XOR<<State state, out bool res>> =                    LOCAL << bool resA, resB; >>
76    Expr<<state, out resA>> Expr<<state, out resB>>           SEM << res = resA ^ !resB; >>
77  .
78  OR<<State state, out bool res>> =                     LOCAL << bool resA, resB; >>
79    Expr<<state, out resA>> Expr<<state, out resB>>           SEM << res = resA | resB; >>
80  .
81
82MINIMIZE
83  <<
84    bool[][] a, b, expectedOutput;
85    GenerateProblemData(N, out a, out b, out expectedOutput);
86   
87    int sumErr = 0;
88    for(int i=0; i<expectedOutput.Length; i++) {
89      bool tmp;
90      // evaluate tree for inputs a and b
91      var state = new State();
92      state.x1 = a[i]; state.x2 = b[i]; state.output = new bool[2*N];
93      Expr(state, out tmp);
94
95      // count incorrect bits
96      for(int j=0;j<state.output.Length;j++) {
97        if(state.output[j] != expectedOutput[i][j]) sumErr++;
98      }
99    }
100    return (double)sumErr;
101  >>
102END MultiOutputMultiplier.
Note: See TracBrowser for help on using the repository browser.