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