1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.DataAnalysis;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using System.Xml;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Functions {
|
---|
31 | internal static class BakedTreeEvaluator {
|
---|
32 | private const int MAX_TREE_SIZE = 4096;
|
---|
33 |
|
---|
34 | private class Instr {
|
---|
35 | public double d_arg0;
|
---|
36 | public int i_arg0;
|
---|
37 | public int i_arg1;
|
---|
38 | public int arity;
|
---|
39 | public int symbol;
|
---|
40 | }
|
---|
41 |
|
---|
42 | private static Instr[] codeArr;
|
---|
43 | private static int PC;
|
---|
44 | private static Dataset dataset;
|
---|
45 | private static int sampleIndex;
|
---|
46 |
|
---|
47 |
|
---|
48 | static BakedTreeEvaluator() {
|
---|
49 | codeArr = new Instr[MAX_TREE_SIZE];
|
---|
50 | for(int i = 0; i < MAX_TREE_SIZE; i++) {
|
---|
51 | codeArr[i] = new Instr();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static void ResetEvaluator(Dataset dataset, List<LightWeightFunction> linearRepresentation) {
|
---|
56 | int i = 0;
|
---|
57 | BakedTreeEvaluator.dataset = dataset;
|
---|
58 | foreach(LightWeightFunction f in linearRepresentation) {
|
---|
59 | TranslateToInstr(f, codeArr[i++]);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static Instr TranslateToInstr(LightWeightFunction f, Instr instr) {
|
---|
64 | instr.arity = f.arity;
|
---|
65 | instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
|
---|
66 | switch(instr.symbol) {
|
---|
67 | case EvaluatorSymbolTable.VARIABLE: {
|
---|
68 | instr.i_arg0 = (int)f.data[0]; // var
|
---|
69 | instr.d_arg0 = f.data[1]; // weight
|
---|
70 | instr.i_arg1 = (int)f.data[2]; // sample-offset
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | case EvaluatorSymbolTable.CONSTANT: {
|
---|
74 | instr.d_arg0 = f.data[0]; // value
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return instr;
|
---|
79 | }
|
---|
80 |
|
---|
81 | internal static double Evaluate(int sampleIndex) {
|
---|
82 | PC = 0;
|
---|
83 | BakedTreeEvaluator.sampleIndex = sampleIndex;
|
---|
84 | return EvaluateBakedCode();
|
---|
85 | }
|
---|
86 |
|
---|
87 | private static double EvaluateBakedCode() {
|
---|
88 | Instr currInstr = codeArr[PC++];
|
---|
89 | switch(currInstr.symbol) {
|
---|
90 | case EvaluatorSymbolTable.VARIABLE: {
|
---|
91 | int row = sampleIndex + currInstr.i_arg1;
|
---|
92 | if(row < 0 || row >= dataset.Rows) return double.NaN;
|
---|
93 | else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0);
|
---|
94 | }
|
---|
95 | case EvaluatorSymbolTable.CONSTANT: {
|
---|
96 | return currInstr.d_arg0;
|
---|
97 | }
|
---|
98 | case EvaluatorSymbolTable.DIFFERENTIAL: {
|
---|
99 | int row = sampleIndex + currInstr.i_arg1;
|
---|
100 | if(row < 1 || row >= dataset.Rows) return double.NaN;
|
---|
101 | else return currInstr.d_arg0 * (dataset.GetValue(row, currInstr.i_arg0) - dataset.GetValue(row - 1, currInstr.i_arg0));
|
---|
102 | }
|
---|
103 | case EvaluatorSymbolTable.MULTIPLICATION: {
|
---|
104 | double result = EvaluateBakedCode();
|
---|
105 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
106 | result *= EvaluateBakedCode();
|
---|
107 | }
|
---|
108 | return result;
|
---|
109 | }
|
---|
110 | case EvaluatorSymbolTable.ADDITION: {
|
---|
111 | double sum = EvaluateBakedCode();
|
---|
112 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
113 | sum += EvaluateBakedCode();
|
---|
114 | }
|
---|
115 | return sum;
|
---|
116 | }
|
---|
117 | case EvaluatorSymbolTable.SUBTRACTION: {
|
---|
118 | if(currInstr.arity == 1) {
|
---|
119 | return -EvaluateBakedCode();
|
---|
120 | } else {
|
---|
121 | double result = EvaluateBakedCode();
|
---|
122 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
123 | result -= EvaluateBakedCode();
|
---|
124 | }
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | case EvaluatorSymbolTable.DIVISION: {
|
---|
129 | double result;
|
---|
130 | if(currInstr.arity == 1) {
|
---|
131 | result = 1.0 / EvaluateBakedCode();
|
---|
132 | } else {
|
---|
133 | result = EvaluateBakedCode();
|
---|
134 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
135 | result /= EvaluateBakedCode();
|
---|
136 | }
|
---|
137 | }
|
---|
138 | if(double.IsInfinity(result)) return 0.0;
|
---|
139 | else return result;
|
---|
140 | }
|
---|
141 | case EvaluatorSymbolTable.AVERAGE: {
|
---|
142 | double sum = EvaluateBakedCode();
|
---|
143 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
144 | sum += EvaluateBakedCode();
|
---|
145 | }
|
---|
146 | return sum / currInstr.arity;
|
---|
147 | }
|
---|
148 | case EvaluatorSymbolTable.COSINUS: {
|
---|
149 | return Math.Cos(EvaluateBakedCode());
|
---|
150 | }
|
---|
151 | case EvaluatorSymbolTable.SINUS: {
|
---|
152 | return Math.Sin(EvaluateBakedCode());
|
---|
153 | }
|
---|
154 | case EvaluatorSymbolTable.EXP: {
|
---|
155 | return Math.Exp(EvaluateBakedCode());
|
---|
156 | }
|
---|
157 | case EvaluatorSymbolTable.LOG: {
|
---|
158 | return Math.Log(EvaluateBakedCode());
|
---|
159 | }
|
---|
160 | case EvaluatorSymbolTable.POWER: {
|
---|
161 | double x = EvaluateBakedCode();
|
---|
162 | double p = EvaluateBakedCode();
|
---|
163 | return Math.Pow(x, p);
|
---|
164 | }
|
---|
165 | case EvaluatorSymbolTable.SIGNUM: {
|
---|
166 | double value = EvaluateBakedCode();
|
---|
167 | if(double.IsNaN(value)) return double.NaN;
|
---|
168 | else return Math.Sign(value);
|
---|
169 | }
|
---|
170 | case EvaluatorSymbolTable.SQRT: {
|
---|
171 | return Math.Sqrt(EvaluateBakedCode());
|
---|
172 | }
|
---|
173 | case EvaluatorSymbolTable.TANGENS: {
|
---|
174 | return Math.Tan(EvaluateBakedCode());
|
---|
175 | }
|
---|
176 | case EvaluatorSymbolTable.AND: {
|
---|
177 | double result = 1.0;
|
---|
178 | // have to evaluate all sub-trees, skipping would probably not lead to a big gain because
|
---|
179 | // we have to iterate over the linear structure anyway
|
---|
180 | for(int i = 0; i < currInstr.arity; i++) {
|
---|
181 | double x = Math.Round(EvaluateBakedCode());
|
---|
182 | if(x == 0 || x == 1.0) result *= x;
|
---|
183 | else result = double.NaN;
|
---|
184 | }
|
---|
185 | return result;
|
---|
186 | }
|
---|
187 | case EvaluatorSymbolTable.EQU: {
|
---|
188 | double x = EvaluateBakedCode();
|
---|
189 | double y = EvaluateBakedCode();
|
---|
190 | if(x == y) return 1.0; else return 0.0;
|
---|
191 | }
|
---|
192 | case EvaluatorSymbolTable.GT: {
|
---|
193 | double x = EvaluateBakedCode();
|
---|
194 | double y = EvaluateBakedCode();
|
---|
195 | if(x > y) return 1.0;
|
---|
196 | else return 0.0;
|
---|
197 | }
|
---|
198 | case EvaluatorSymbolTable.IFTE: {
|
---|
199 | double condition = Math.Round(EvaluateBakedCode());
|
---|
200 | double x = EvaluateBakedCode();
|
---|
201 | double y = EvaluateBakedCode();
|
---|
202 | if(condition < .5) return x;
|
---|
203 | else if(condition >= .5) return y;
|
---|
204 | else return double.NaN;
|
---|
205 | }
|
---|
206 | case EvaluatorSymbolTable.LT: {
|
---|
207 | double x = EvaluateBakedCode();
|
---|
208 | double y = EvaluateBakedCode();
|
---|
209 | if(x < y) return 1.0;
|
---|
210 | else return 0.0;
|
---|
211 | }
|
---|
212 | case EvaluatorSymbolTable.NOT: {
|
---|
213 | double result = Math.Round(EvaluateBakedCode());
|
---|
214 | if(result == 0.0) return 1.0;
|
---|
215 | else if(result == 1.0) return 0.0;
|
---|
216 | else return double.NaN;
|
---|
217 | }
|
---|
218 | case EvaluatorSymbolTable.OR: {
|
---|
219 | double result = 0.0; // default is false
|
---|
220 | for(int i = 0; i < currInstr.arity; i++) {
|
---|
221 | double x = Math.Round(EvaluateBakedCode());
|
---|
222 | if(x == 1.0 && result == 0.0) result = 1.0; // found first true (1.0) => set to true
|
---|
223 | else if(x != 0.0) result = double.NaN; // if it was not true it can only be false (0.0) all other cases are undefined => (NaN)
|
---|
224 | }
|
---|
225 | return result;
|
---|
226 | }
|
---|
227 | case EvaluatorSymbolTable.XOR: {
|
---|
228 | double x = Math.Round(EvaluateBakedCode());
|
---|
229 | double y = Math.Round(EvaluateBakedCode());
|
---|
230 | if(x == 0.0 && y == 0.0) return 0.0;
|
---|
231 | if(x == 1.0 && y == 0.0) return 1.0;
|
---|
232 | if(x == 0.0 && y == 1.0) return 1.0;
|
---|
233 | if(x == 1.0 && y == 1.0) return 0.0;
|
---|
234 | return double.NaN;
|
---|
235 | }
|
---|
236 | default: {
|
---|
237 | throw new NotImplementedException();
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|