[9519] | 1 | PROBLEM SymbReg
|
---|
| 2 |
|
---|
| 3 | CODE <<
|
---|
[9696] | 4 | double[,] x;
|
---|
| 5 | double[] y;
|
---|
[10399] | 6 | int[] variables;
|
---|
[10086] | 7 | int[] rows;
|
---|
[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 );
|
---|
[10099] | 33 | den = Math.Sqrt( ( sumxSq - ( sumx * sumx ) / n ) *
|
---|
| 34 | ( sumySq - ( sumy * sumy ) / n ) );
|
---|
[10086] | 35 | if(den > 0){
|
---|
| 36 | r = num / den;
|
---|
| 37 | }
|
---|
| 38 | return r*r;
|
---|
| 39 | }
|
---|
[9696] | 40 | >>
|
---|
[9519] | 41 |
|
---|
[9696] | 42 | INIT <<
|
---|
[10099] | 43 | // generate 500 cases of poly-10 benchmark function
|
---|
[9696] | 44 | int n = 500;
|
---|
[10399] | 45 | variables = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
---|
[9696] | 46 | var rand = new System.Random();
|
---|
| 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();
|
---|
[9519] | 61 | >>
|
---|
| 62 |
|
---|
| 63 | NONTERMINALS
|
---|
| 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 |
|
---|
| 71 | TERMINALS
|
---|
| 72 | Const<<out double val>>
|
---|
| 73 | CONSTRAINTS
|
---|
[10099] | 74 | val IN RANGE <<-10.0>> .. <<10.0>>
|
---|
[9519] | 75 | .
|
---|
[10399] | 76 | Var<<out int variable, out double weight>>
|
---|
[9519] | 77 | CONSTRAINTS
|
---|
[10399] | 78 | variable IN SET <<variables>>
|
---|
[10099] | 79 | weight IN RANGE <<-10.0>> .. <<10.0>>
|
---|
[9519] | 80 | .
|
---|
| 81 |
|
---|
| 82 | RULES
|
---|
| 83 | Model<<int row, out double val>> =
|
---|
| 84 | RPB<<row, out val>> .
|
---|
| 85 |
|
---|
[10399] | 86 | RPB<<int row, out double val>> = LOCAL << int variable; double w; >>
|
---|
[9519] | 87 | Addition<<row, out val>>
|
---|
| 88 | | Subtraction<<row, out val>>
|
---|
| 89 | | Division<<row, out val>>
|
---|
| 90 | | Multiplication<<row, out val>>
|
---|
[10399] | 91 | | Var<<out variable, out w>> SEM << val = w * x[row, variable]; >>
|
---|
[9519] | 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 |
|
---|
[10086] | 108 | MAXIMIZE
|
---|
[9519] | 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 | });
|
---|
[9696] | 115 | return RSquared(predicted, y);
|
---|
[9519] | 116 | >>
|
---|
| 117 | END SymbReg.
|
---|