Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/examples/symbreg HEAL.txt @ 9430

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

initial import of GPDL parser plugin

File size: 3.9 KB
Line 
1PROBLEM SymbReg
2
3CODE <<
4  double[,] inputValues;
5  double[] targetValues;
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 data[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
28  void LoadData(string fileName, out double[,] inputValues, out string[] variableNames, out double[] target) {
29    var prov = new HeuristicLab.Problems.Instances.DataAnalysis.RegressionRealWorldInstanceProvider();
30    var dd = prov.GetDataDescriptors().OfType<HeuristicLab.Problems.Instances.DataAnalysis.Housing>().Single();
31    var problemData = prov.LoadData(dd);   
32   
33    inputValues = new double[problemData.TrainingIndices.Count(), problemData.AllowedInputVariables.Count()];
34    foreach(var r in problemData.TrainingIndices) {
35       int i=0;
36       foreach(var v in problemData.AllowedInputVariables) {
37          inputValues[r, i++] = problemData.Dataset.GetDoubleValue(v, r);
38       }
39    }
40    variableNames = problemData.AllowedInputVariables.ToArray();
41    target = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray();
42  }
43>>
44
45INIT <<
46  LoadData("filename.csv", out inputValues, out variableNames, out targetValues);
47>>
48
49NONTERMINALS
50  Model<<int row, out double val>>.
51  RPB<<int row, out double val>>.
52  Addition<<int row, out double val>>.
53  Subtraction<<int row, out double val>>.
54  Multiplication<<int row, out double val>>.
55  Division<<int row, out double val>>.
56
57TERMINALS
58  Const<<out double val>>
59    CONSTRAINTS
60      val IN RANGE <<-100>> .. <<100>>
61  .
62  Var<<out string varName, out double weight>> 
63    CONSTRAINTS
64      varName IN SET <<variableNames>>
65      weight IN RANGE <<-100>> .. <<100>>
66  .
67
68RULES
69  Model<<int row, out double val>> =                       
70    RPB<<row, out val>> .
71   
72  RPB<<int row, out double val>> =                         LOCAL << string varName; double w; >>
73    Addition<<row, out val>>
74    | Subtraction<<row, out val>>
75    | Division<<row, out val>>
76    | Multiplication<<row, out val>>                       
77    | Var<<out varName, out w>>                            SEM << val = w * GetValue(inputValues, varName, row); >>
78    | Const<<out val>>
79  .
80
81  Addition<<int row, out double val>> =                    LOCAL << double x1, x2; >>
82    RPB<<row, out x1>> RPB<<row, out x2>>                  SEM << val = x1 + x2; >>
83  .
84  Subtraction<<int row, out double val>> =                 LOCAL << double x1, x2; >>
85    RPB<<row, out x1>> RPB<<row, out x2>>                  SEM << val = x1 - x2; >>
86  .
87  Division<<int row, out double val>> =                    LOCAL << double x1, x2; >>
88    RPB<<row, out x1>> RPB<<row, out x2>>                  SEM << val = x1 / x2; >>
89  .
90  Multiplication<<int row, out double val>> =              LOCAL << double x1, x2; >>
91    RPB<<row, out x1>> RPB<<row, out x2>>                  SEM << val = x1 * x2; >>
92  .
93
94MAXIMIZE                                                   /* could also use the keyword MINIMIZE here */
95  <<
96    var rows = System.Linq.Enumerable.Range(0, inputValues.GetLength(0));
97    var predicted = rows.Select(r => {
98      double result;
99      Model(r, out result);                                /* we can call the root symbol directly */
100      return result;
101    });
102    return RSquared(predicted, targetValues);
103  >>
104END SymbReg.
Note: See TracBrowser for help on using the repository browser.