Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL.Backend/HeuristicLab.Problems.GPDL.Views/3.4/Resources/symbreg Koza.txt @ 13806

Last change on this file since 13806 was 9696, checked in by gkronber, 11 years ago

#2026 simplified symb-reg example, cleaned ATG file (whitespace), removed obsolete files

File size: 3.4 KB
Line 
1PROBLEM SymbRegKoza
2CODE <<
3  double[,] x;
4  double[] y;
5  string[] variableNames;
6  Dictionary<string,int> nameToCol;
7 
8  double GetValue(double[,] data, string varName, int row) {
9    if(nameToCol == null) {
10      /* init mapping */
11      nameToCol = new Dictionary<string, int>();
12      for(int i=0; i<variableNames.Length; i++) {
13        nameToCol[variableNames[i]] = i;
14      }
15    }
16    return x[row, nameToCol[varName]];
17  }
18
19  double RSquared(IEnumerable<double> xs, IEnumerable<double> ys) {
20     HeuristicLab.Problems.DataAnalysis.OnlineCalculatorError error;
21     var r2 = HeuristicLab.Problems.DataAnalysis.OnlinePearsonsRSquaredCalculator.Calculate(xs, ys, out error);
22     if(error == HeuristicLab.Problems.DataAnalysis.OnlineCalculatorError.None) return r2;
23     else return 0.0;
24  }
25>>
26
27INIT <<
28  // generate 500 case of poly-10 benchmark function
29  int n = 500;
30  variableNames = new string[] {"x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10" };
31  var rand = new System.Random();
32  x = new double[n, 10];
33  y = new double[n];
34  for(int row = 0; row < 500; row++) {
35    for(int col = 0; col < 10; col++) {
36      x[row, col] = rand.NextDouble() * 2.0 - 1.0;
37    }
38    y[row] = x[row, 0] * x[row, 1] +
39             x[row, 2] * x[row, 3] +
40             x[row, 4] * x[row, 5] +
41             x[row, 0] * x[row, 6] + x[row, 8] +
42             x[row, 2] * x[row, 5] + x[row, 9];
43  }
44>>
45
46NONTERMINALS
47  Model<<int row, out double val>>.
48  RPB<<int row, out double val>>.
49  Addition<<int row, out double val>>.
50  Subtraction<<int row, out double val>>.
51  Multiplication<<int row, out double val>>.
52  Division<<int row, out double val>>.
53
54TERMINALS
55  ERC<<out double val>>
56    CONSTRAINTS
57    val IN RANGE <<-100>> .. <<100>>
58  .
59
60  Var<<out string varName>>
61    CONSTRAINTS
62    varName IN SET <<variableNames>>
63  .
64
65RULES
66  Model<<int row, out double val>> =
67    RPB<<row, out val>> .
68
69  RPB<<int row, out double val>> =                         LOCAL << string varName; >>
70    Addition<<row, out val>>
71    | Subtraction<<row, out val>>
72    | Division<<row, out val>>
73    | Multiplication<<row, out val>>
74    | Var<<out varName>>                                   SEM << val = GetValue(x, varName, row); >>
75    | ERC<<out val>>
76  .
77
78  Addition<<int row, out double val>> =                    LOCAL << double x1, x2; >>
79      RPB<<row, out x1>> RPB<<row, out x2>>                SEM<< val = x1 + x2; >>
80  .
81  Subtraction<<int row, out double val>> =                 LOCAL << double x1, x2; >>
82      RPB<<row, out x1>> RPB<<row, out x2>>                SEM<< val = x1 - x2; >>
83  .
84  Division<<int row, out double val>> =                    LOCAL << double x1, x2; >>
85      RPB<<row, out x1>> RPB<<row, out x2>>                SEM<< val = x1 / x2; >>
86  .
87  Multiplication<<int row, out double val>> =              LOCAL << double x1, x2; >>
88      RPB<<row, out x1>> RPB<<row, out x2>>                SEM<< val = x1 * x2; >>
89  .
90
91MAXIMIZE                                                   /* could also use the keyword MINIMIZE here */
92  <<
93    var rows = System.Linq.Enumerable.Range(0, x.GetLength(0));
94    var predicted = rows.Select(r => {
95      double result;
96      Model(r, out result);                                /* we can call the root symbol directly */
97      return result;
98    });
99    return RSquared(predicted, y);
100  >>
101END SymbRegKoza.
Note: See TracBrowser for help on using the repository browser.