- Timestamp:
- 06/21/08 12:26:58 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/BottomUpTreeEvaluation/BakedTreeEvaluator.cs
r329 r330 27 27 using HeuristicLab.Core; 28 28 using System.Xml; 29 using System.Runtime.InteropServices; 29 30 30 31 namespace HeuristicLab.Functions { 31 32 internal static class BakedTreeEvaluator { 32 private const int MAX_TREE_SIZE = 4096;33 private const int MAX_TREE_SIZE = 128; 33 34 private const int MAX_TREE_DEPTH = 20; 34 35 private class Instr { … … 42 43 43 44 private static int[] nInstr; 44 private static Instr[,] evaluationTable; 45 private static Instr[] variables; 46 private static int variablesCount; 47 private static Instr[] evaluationTable; 45 48 private static Dataset dataset; 46 49 private static int sampleIndex; … … 48 51 49 52 static BakedTreeEvaluator() { 50 evaluationTable = new Instr[MAX_TREE_SIZE, MAX_TREE_DEPTH]; 53 variables = new Instr[100]; 54 evaluationTable = new Instr[MAX_TREE_SIZE* MAX_TREE_DEPTH]; 51 55 nInstr = new int[MAX_TREE_DEPTH]; 52 56 for(int j = 0; j < MAX_TREE_DEPTH; j++) { 53 57 for(int i = 0; i < MAX_TREE_SIZE; i++) { 54 evaluationTable[ i, j] = new Instr();58 evaluationTable[j*MAX_TREE_SIZE+i] = new Instr(); 55 59 } 56 60 } … … 59 63 public static void ResetEvaluator(List<LightWeightFunction> linearRepresentation) { 60 64 int length; 65 variablesCount = 0; 61 66 for(int i = 0; i < MAX_TREE_DEPTH; i++) nInstr[i] = 0; 62 67 //TranslateToInstr(0, linearRepresentation, out length); … … 82 87 length += curBranchLength; 83 88 } 84 heights[p] = height +1;89 heights[p] = height + 1; 85 90 branchLength = length; 86 return height +1;91 return height + 1; 87 92 } 88 93 … … 90 95 LightWeightFunction f = list[pos]; 91 96 if(f.arity == 0) { 92 Instr instr = evaluationTable[nInstr[0] , 0];97 Instr instr = evaluationTable[nInstr[0]]; 93 98 instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType); 94 99 switch(instr.symbol) { … … 97 102 instr.d_arg0 = f.data[1]; // weight 98 103 instr.i_arg1 = (int)f.data[2]; // sample-offset 104 variables[variablesCount++] = instr; 99 105 break; 100 106 } … … 113 119 if(curBranchHeight < height - 1) { 114 120 for(int j = curBranchHeight; j < height - 1; j++) { 115 evaluationTable[nInstr[j] , j].symbol = EvaluatorSymbolTable.IDENTITY;121 evaluationTable[nInstr[j]+ j*MAX_TREE_SIZE].symbol = EvaluatorSymbolTable.IDENTITY; 116 122 nInstr[j]++; 117 123 } … … 121 127 } 122 128 123 Instr cell = evaluationTable[nInstr[height -1], height-1];124 nInstr[height -1]++;129 Instr cell = evaluationTable[nInstr[height - 1]+MAX_TREE_SIZE*( height - 1)]; 130 nInstr[height - 1]++; 125 131 cell.arity = f.arity; 126 132 cell.symbol = EvaluatorSymbolTable.MapFunction(f.functionType); … … 167 173 168 174 private static double EvaluateTable() { 169 int terminalP = 0; 170 // process remaining instr first 171 for(int i = 0; i < nInstr[0] % 4; i++) { 172 Instr curInstr = evaluationTable[terminalP++, 0]; 173 if(curInstr.symbol == EvaluatorSymbolTable.VARIABLE) { 174 int row = sampleIndex + curInstr.i_arg1; 175 if(row < 0 || row >= dataset.Rows) curInstr.result = double.NaN; 176 else curInstr.result = curInstr.d_arg0 * dataset.GetValue(row, curInstr.i_arg0); 177 } 178 } 179 // unrolled loop 180 for(; terminalP < nInstr[0] - 3; terminalP += 4) { 181 Instr curInstr0 = evaluationTable[terminalP, 0]; 182 Instr curInstr1 = evaluationTable[terminalP + 1, 0]; 183 Instr curInstr2 = evaluationTable[terminalP + 2, 0]; 184 Instr curInstr3 = evaluationTable[terminalP + 3, 0]; 185 if(curInstr0.symbol == EvaluatorSymbolTable.VARIABLE) { 186 int row = sampleIndex + curInstr0.i_arg1; 187 if(row < 0 || row >= dataset.Rows) curInstr0.result = double.NaN; 188 else curInstr0.result = curInstr0.d_arg0 * dataset.GetValue(row, curInstr0.i_arg0); 189 } 190 if(curInstr1.symbol == EvaluatorSymbolTable.VARIABLE) { 191 int row = sampleIndex + curInstr1.i_arg1; 192 if(row < 0 || row >= dataset.Rows) curInstr1.result = double.NaN; 193 else curInstr1.result = curInstr1.d_arg0 * dataset.GetValue(row, curInstr1.i_arg0); 194 } 195 if(curInstr2.symbol == EvaluatorSymbolTable.VARIABLE) { 196 int row = sampleIndex + curInstr2.i_arg1; 197 if(row < 0 || row >= dataset.Rows) curInstr2.result = double.NaN; 198 else curInstr2.result = curInstr2.d_arg0 * dataset.GetValue(row, curInstr2.i_arg0); 199 } 200 if(curInstr3.symbol == EvaluatorSymbolTable.VARIABLE) { 201 int row = sampleIndex + curInstr3.i_arg1; 202 if(row < 0 || row >= dataset.Rows) curInstr3.result = double.NaN; 203 else curInstr3.result = curInstr3.d_arg0 * dataset.GetValue(row, curInstr3.i_arg0); 204 } 205 } 206 175 for(int i = 0; i < variablesCount; i++) { 176 Instr curInstr = variables[i]; 177 int row = sampleIndex + curInstr.i_arg1; 178 if(row < 0 || row >= dataset.Rows) curInstr.result = double.NaN; 179 else curInstr.result = curInstr.d_arg0 * dataset.GetValue(row, curInstr.i_arg0); 180 } 207 181 int curLevel = 1; 182 int lastLayerInstrP = 0; 183 int curLayerOffset = MAX_TREE_SIZE; 208 184 while(nInstr[curLevel] > 0) { 209 int lastLayerInstrP = 0;210 185 for(int curLayerInstrP = 0; curLayerInstrP < nInstr[curLevel]; curLayerInstrP++) { 211 Instr curInstr = evaluationTable[curLayerInstrP , curLevel];186 Instr curInstr = evaluationTable[curLayerInstrP+curLayerOffset]; 212 187 switch(curInstr.symbol) { 213 188 case EvaluatorSymbolTable.MULTIPLICATION: { 214 curInstr.result = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;189 curInstr.result = evaluationTable[lastLayerInstrP++].result; 215 190 for(int i = 1; i < curInstr.arity; i++) { 216 curInstr.result *= evaluationTable[lastLayerInstrP++ , curLevel - 1].result;191 curInstr.result *= evaluationTable[lastLayerInstrP++].result; 217 192 } 218 193 break; 219 194 } 220 195 case EvaluatorSymbolTable.ADDITION: { 221 curInstr.result = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;196 curInstr.result = evaluationTable[lastLayerInstrP++].result; 222 197 for(int i = 1; i < curInstr.arity; i++) { 223 curInstr.result += evaluationTable[lastLayerInstrP++ , curLevel - 1].result;198 curInstr.result += evaluationTable[lastLayerInstrP++].result; 224 199 } 225 200 break; … … 227 202 case EvaluatorSymbolTable.SUBTRACTION: { 228 203 if(curInstr.arity == 1) { 229 curInstr.result = -evaluationTable[lastLayerInstrP++ , curLevel - 1].result;204 curInstr.result = -evaluationTable[lastLayerInstrP++].result; 230 205 } else { 231 curInstr.result = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;206 curInstr.result = evaluationTable[lastLayerInstrP++].result; 232 207 for(int i = 1; i < curInstr.arity; i++) { 233 curInstr.result -= evaluationTable[lastLayerInstrP++ , curLevel - 1].result;208 curInstr.result -= evaluationTable[lastLayerInstrP++].result; 234 209 } 235 210 } … … 238 213 case EvaluatorSymbolTable.DIVISION: { 239 214 if(curInstr.arity == 1) { 240 curInstr.result = 1.0 / evaluationTable[lastLayerInstrP++ , curLevel - 1].result;215 curInstr.result = 1.0 / evaluationTable[lastLayerInstrP++].result; 241 216 } else { 242 curInstr.result = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;217 curInstr.result = evaluationTable[lastLayerInstrP++].result; 243 218 for(int i = 1; i < curInstr.arity; i++) { 244 curInstr.result /= evaluationTable[lastLayerInstrP++ , curLevel - 1].result;219 curInstr.result /= evaluationTable[lastLayerInstrP++].result; 245 220 } 246 221 } … … 249 224 } 250 225 case EvaluatorSymbolTable.AVERAGE: { 251 curInstr.result = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;226 curInstr.result = evaluationTable[lastLayerInstrP++].result; 252 227 for(int i = 1; i < curInstr.arity; i++) { 253 curInstr.result += evaluationTable[lastLayerInstrP++ , curLevel - 1].result;228 curInstr.result += evaluationTable[lastLayerInstrP++].result; 254 229 } 255 230 curInstr.result /= curInstr.arity; … … 257 232 } 258 233 case EvaluatorSymbolTable.COSINUS: { 259 curInstr.result = Math.Cos(evaluationTable[lastLayerInstrP++ , curLevel - 1].result);234 curInstr.result = Math.Cos(evaluationTable[lastLayerInstrP++].result); 260 235 break; 261 236 } 262 237 case EvaluatorSymbolTable.SINUS: { 263 curInstr.result = Math.Sin(evaluationTable[lastLayerInstrP++ , curLevel - 1].result);238 curInstr.result = Math.Sin(evaluationTable[lastLayerInstrP++].result); 264 239 break; 265 240 } 266 241 case EvaluatorSymbolTable.EXP: { 267 curInstr.result = Math.Exp(evaluationTable[lastLayerInstrP++ , curLevel - 1].result);242 curInstr.result = Math.Exp(evaluationTable[lastLayerInstrP++].result); 268 243 break; 269 244 } 270 245 case EvaluatorSymbolTable.LOG: { 271 curInstr.result = Math.Log(evaluationTable[lastLayerInstrP++ , curLevel - 1].result);246 curInstr.result = Math.Log(evaluationTable[lastLayerInstrP++].result); 272 247 break; 273 248 } 274 249 case EvaluatorSymbolTable.POWER: { 275 double x = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;276 double p = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;250 double x = evaluationTable[lastLayerInstrP++].result; 251 double p = evaluationTable[lastLayerInstrP++].result; 277 252 curInstr.result = Math.Pow(x, p); 278 253 break; 279 254 } 280 255 case EvaluatorSymbolTable.SIGNUM: { 281 double value = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;256 double value = evaluationTable[lastLayerInstrP++].result; 282 257 if(double.IsNaN(value)) curInstr.result = double.NaN; 283 258 else curInstr.result = Math.Sign(value); … … 285 260 } 286 261 case EvaluatorSymbolTable.SQRT: { 287 curInstr.result = Math.Sqrt(evaluationTable[lastLayerInstrP++ , curLevel - 1].result);262 curInstr.result = Math.Sqrt(evaluationTable[lastLayerInstrP++].result); 288 263 break; 289 264 } 290 265 case EvaluatorSymbolTable.TANGENS: { 291 curInstr.result = Math.Tan(evaluationTable[lastLayerInstrP++ , curLevel - 1].result);266 curInstr.result = Math.Tan(evaluationTable[lastLayerInstrP++].result); 292 267 break; 293 268 } … … 353 328 // } 354 329 case EvaluatorSymbolTable.IDENTITY: { 355 curInstr.result = evaluationTable[lastLayerInstrP++ , curLevel - 1].result;330 curInstr.result = evaluationTable[lastLayerInstrP++].result; 356 331 break; 357 332 } … … 362 337 } 363 338 curLevel++; 364 } 365 return evaluationTable[0, curLevel - 1].result; 339 lastLayerInstrP = curLayerOffset; 340 curLayerOffset += MAX_TREE_SIZE; 341 } 342 return evaluationTable[lastLayerInstrP].result; 366 343 } 367 344 }
Note: See TracChangeset
for help on using the changeset viewer.