Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/Examples/symbreg Koza.txt @ 10217

Last change on this file since 10217 was 10099, checked in by gkronber, 11 years ago

#2026 minor corrections in GPDL example files

File size: 4.1 KB
Line 
1PROBLEM SymbRegKoza
2CODE <<
3  double[,] x;
4  double[] y;
5  int[] rows;
6  string[] variableNames;
7  double[] randomConsts;
8 
9  Dictionary<string,int> nameToCol;
10 
11  double GetValue(double[,] data, string varName, int row) {
12    if(nameToCol == null) {
13      /* init mapping */
14      nameToCol = new Dictionary<string, int>();
15      for(int i=0; i<variableNames.Length; i++) {
16        nameToCol[variableNames[i]] = i;
17      }
18    }
19    return x[row, nameToCol[varName]];
20  }
21
22  double RSquared(IEnumerable<double> xs, IEnumerable<double> ys) {
23    // calculate Pearson's correlation in one pass over xs and ys
24    double sumx = 0.0;
25    double sumy = 0.0;
26    double sumxSq = 0.0;
27    double sumySq = 0.0;
28    double sumxy = 0.0;
29    int n = 0;
30    var xEnum = xs.GetEnumerator();
31    var yEnum = ys.GetEnumerator();
32    while(xEnum.MoveNext() & yEnum.MoveNext()) {
33      sumx += xEnum.Current;
34      sumy += yEnum.Current;
35      sumxSq += xEnum.Current * xEnum.Current;
36      sumySq += yEnum.Current * yEnum.Current;
37      sumxy += xEnum.Current * yEnum.Current;
38      n++;
39    }
40    System.Diagnostics.Debug.Assert(!(xEnum.MoveNext() | yEnum.MoveNext()));
41
42    double num;
43    double den;
44    double r = 0.0;
45    num = sumxy - ( ( sumx * sumy ) / n );
46    den = Math.Sqrt( ( sumxSq - ( sumx*sumx ) / n ) *
47                     ( sumySq - ( sumy*sumy ) / n ) );
48    if(den > 0){
49      r = num / den;
50    }
51    return r*r;
52  }
53>>
54
55INIT <<
56  // generate 500 cases of poly-10 benchmark function
57  int n = 500;
58  variableNames = new string[] {"x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10" };
59  var rand = new System.Random();
60  x = new double[n, 10];
61  y = new double[n];
62  for(int row = 0; row < n; row++) {
63    for(int col = 0; col < 10; col++) {
64      x[row, col] = rand.NextDouble() * 2.0 - 1.0;
65    }
66    y[row] = x[row, 0] * x[row, 1] +
67             x[row, 2] * x[row, 3] +
68             x[row, 4] * x[row, 5] +
69             x[row, 0] * x[row, 6] + x[row, 8] +
70             x[row, 2] * x[row, 5] + x[row, 9];
71  }
72 
73  rows = System.Linq.Enumerable.Range(0, n).ToArray();
74 
75  // generate 100 random constants in [-10.0 .. 10.0[
76  randomConsts = Enumerable.Range(0, 100).Select(i => rand.NextDouble()*20.0 - 10.0).ToArray();
77>>
78
79NONTERMINALS
80  Model<<int row, out double val>>.
81  RPB<<int row, out double val>>.
82  Addition<<int row, out double val>>.
83  Subtraction<<int row, out double val>>.
84  Multiplication<<int row, out double val>>.
85  Division<<int row, out double val>>.
86
87TERMINALS
88  ERC<<out double val>>
89    CONSTRAINTS
90      val IN SET << randomConsts >>
91  .
92
93  Var<<out string varName>>
94    CONSTRAINTS
95      varName IN SET << variableNames >>
96  .
97
98RULES
99  Model<<int row, out double val>> =
100    RPB<<row, out val>> .
101
102  RPB<<int row, out double val>> =                         LOCAL << string varName; >>
103    Addition<<row, out val>>
104    | Subtraction<<row, out val>>
105    | Division<<row, out val>>
106    | Multiplication<<row, out val>>
107    | Var<<out varName>>                                   SEM << val = GetValue(x, varName, row); >>
108    /* | ERC<<out val>> */
109  .
110
111  Addition<<int row, out double val>> =                    LOCAL << double x1, x2; >>
112      RPB<<row, out x1>> RPB<<row, out x2>>                SEM<< val = x1 + x2; >>
113  .
114  Subtraction<<int row, out double val>> =                 LOCAL << double x1, x2; >>
115      RPB<<row, out x1>> RPB<<row, out x2>>                SEM<< val = x1 - x2; >>
116  .
117  Division<<int row, out double val>> =                    LOCAL << double x1, x2; >>
118      RPB<<row, out x1>> RPB<<row, out x2>>                SEM<< val = x1 / x2; >>
119  .
120  Multiplication<<int row, out double val>> =              LOCAL << double x1, x2; >>
121      RPB<<row, out x1>> RPB<<row, out x2>>                SEM<< val = x1 * x2; >>
122  .
123
124MAXIMIZE
125  <<
126    var predicted = rows.Select(r => {
127      double result;
128      Model(r, out result);                                /* we can call the root symbol directly */
129      return result;
130    });
131    return RSquared(predicted, y);
132  >>
133END SymbRegKoza.
Note: See TracBrowser for help on using the repository browser.