Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15071 was 10409, checked in by gkronber, 11 years ago

#2026 prepare for inclusion of terminals into the search tree

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