Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL.Backend/HeuristicLab.Problems.GPDL.Views/3.4/Resources/symbreg HEAL.txt @ 15737

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

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

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