1 | PROBLEM SymbRegKoza
|
---|
2 | CODE <<
|
---|
3 | double[,] x;
|
---|
4 | double[] y;
|
---|
5 | int[] rows;
|
---|
6 | int[] variables;
|
---|
7 | double[] randomConsts;
|
---|
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 |
|
---|
42 | INIT <<
|
---|
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(31415);
|
---|
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 | // generate 100 random constants in [-10.0 .. 10.0[
|
---|
63 | randomConsts = Enumerable.Range(0, 100).Select(i => rand.NextDouble()*20.0 - 10.0).ToArray();
|
---|
64 | >>
|
---|
65 |
|
---|
66 | NONTERMINALS
|
---|
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 |
|
---|
74 | TERMINALS
|
---|
75 | ERC<<out double val>>
|
---|
76 | CONSTRAINTS
|
---|
77 | val IN SET << randomConsts >>
|
---|
78 | .
|
---|
79 |
|
---|
80 | Var<<out int variable>>
|
---|
81 | CONSTRAINTS
|
---|
82 | variable IN SET << variables >>
|
---|
83 | .
|
---|
84 |
|
---|
85 | RULES
|
---|
86 | Model<<int row, out double val>> =
|
---|
87 | RPB<<row, out val>> .
|
---|
88 |
|
---|
89 | RPB<<int row, out double val>> = LOCAL << int variable; >>
|
---|
90 | Addition<<row, out val>>
|
---|
91 | | Subtraction<<row, out val>>
|
---|
92 | | Division<<row, out val>>
|
---|
93 | | Multiplication<<row, out val>>
|
---|
94 | | Var<<out variable>> SEM << val = x[row, variable]; >>
|
---|
95 | | ERC<<out val>>
|
---|
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 |
|
---|
111 | MAXIMIZE
|
---|
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 | });
|
---|
118 | return RSquared(predicted, y);
|
---|
119 | >>
|
---|
120 | END SymbRegKoza.
|
---|