Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/Examples/symbreg HEAL.txt @ 18221

Last change on this file since 18221 was 10399, checked in by gkronber, 11 years ago

#2026 performance improvement for the second symbolic regression example

File size: 3.6 KB
Line 
1PROBLEM SymbReg
2
3CODE <<
4  double[,] x;
5  double[] y;
6  int[] variables;
7  int[] rows;
8
9  double RSquared(IEnumerable<double> xs, IEnumerable<double> ys) {
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  }
40>>
41
42INIT <<
43  // generate 500 cases of poly-10 benchmark function
44  int n = 500;
45  variables = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
46  var rand = new System.Random();
47  x = new double[n, 10];
48  y = new double[n];
49  for(int row = 0; row < n; row++) {
50    for(int col = 0; col < 10; col++) {
51      x[row, col] = rand.NextDouble() * 2.0 - 1.0;
52    }
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];
58  }
59
60  rows = System.Linq.Enumerable.Range(0, n).ToArray();
61>>
62
63NONTERMINALS
64  Model<<int row, out double val>>.
65  RPB<<int row, out double val>>.
66  Addition<<int row, out double val>>.
67  Subtraction<<int row, out double val>>.
68  Multiplication<<int row, out double val>>.
69  Division<<int row, out double val>>.
70
71TERMINALS
72  Const<<out double val>>
73    CONSTRAINTS
74      val IN RANGE <<-10.0>> .. <<10.0>>
75  .
76  Var<<out int variable, out double weight>> 
77    CONSTRAINTS
78      variable IN SET <<variables>>
79      weight IN RANGE <<-10.0>> .. <<10.0>>
80  .
81
82RULES
83  Model<<int row, out double val>> =                       
84    RPB<<row, out val>> .
85   
86  RPB<<int row, out double val>> =                         LOCAL << int variable; double w; >>
87    Addition<<row, out val>>
88    | Subtraction<<row, out val>>
89    | Division<<row, out val>>
90    | Multiplication<<row, out val>>                       
91    | Var<<out variable, out w>>                            SEM << val = w * x[row, variable]; >>
92    | Const<<out val>>
93  .
94
95  Addition<<int row, out double val>> =                    LOCAL << double x1, x2; >>
96    RPB<<row, out x1>> RPB<<row, out x2>>                  SEM << val = x1 + x2; >>
97  .
98  Subtraction<<int row, out double val>> =                 LOCAL << double x1, x2; >>
99    RPB<<row, out x1>> RPB<<row, out x2>>                  SEM << val = x1 - x2; >>
100  .
101  Division<<int row, out double val>> =                    LOCAL << double x1, x2; >>
102    RPB<<row, out x1>> RPB<<row, out x2>>                  SEM << val = x1 / x2; >>
103  .
104  Multiplication<<int row, out double val>> =              LOCAL << double x1, x2; >>
105    RPB<<row, out x1>> RPB<<row, out x2>>                  SEM << val = x1 * x2; >>
106  .
107
108MAXIMIZE
109  <<
110    var predicted = rows.Select(r => {
111      double result;
112      Model(r, out result);                                /* we can call the root symbol directly */
113      return result;
114    });
115    return RSquared(predicted, y);
116  >>
117END SymbReg.
Note: See TracBrowser for help on using the repository browser.