Changeset 328
- Timestamp:
- 06/20/08 18:12:20 (16 years ago)
- Location:
- branches/BottomUpTreeEvaluation
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/BottomUpTreeEvaluation/BakedTreeEvaluator.cs
r327 r328 33 33 private const int MAX_TREE_DEPTH = 20; 34 34 private class Instr { 35 public double result; 35 36 public double d_arg0; 36 37 public int i_arg0; … … 59 60 int length; 60 61 for(int i = 0; i < MAX_TREE_DEPTH; i++) nInstr[i] = 0; 61 TranslateToInstr(0, linearRepresentation, out length); 62 } 63 64 private static int TranslateToInstr(int pos, List<LightWeightFunction> linearRepresentation, out int branchLength) { 62 //TranslateToInstr(0, linearRepresentation, out length); 63 int[] heights = new int[linearRepresentation.Count]; 64 CalcHeights(linearRepresentation, heights, 0, out length); 65 TranslateToTable(0, linearRepresentation, heights); 66 } 67 68 private static int CalcHeights(List<LightWeightFunction> linearRepresentation, int[] heights, int p, out int branchLength) { 69 if(linearRepresentation[p].arity == 0) { 70 heights[p] = 1; 71 branchLength = 1; 72 return 1; 73 } 65 74 int height = 0; 66 75 int length = 1; 67 LightWeightFunction f = linearRepresentation[pos]; 68 for(int i = 0; i < f.arity; i++) { 76 for(int i = 0; i < linearRepresentation[p].arity; i++) { 69 77 int curBranchLength; 70 int curBranchHeight = TranslateToInstr(pos + length, linearRepresentation, out curBranchLength); 71 if(curBranchHeight > height) height = curBranchHeight; 78 int curHeight = CalcHeights(linearRepresentation, heights, p + length, out curBranchLength); 79 if(curHeight > height) { 80 height = curHeight; 81 } 72 82 length += curBranchLength; 73 83 } 74 Instr instr = evaluationTable[nInstr[height], height]; 75 instr.arity = f.arity; 76 instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType); 77 switch(instr.symbol) { 78 case EvaluatorSymbolTable.VARIABLE: { 79 instr.i_arg0 = (int)f.data[0]; // var 80 instr.d_arg0 = f.data[1]; // weight 81 instr.i_arg1 = (int)f.data[2]; // sample-offset 82 break; 84 heights[p] = height+1; 85 branchLength = length; 86 return height+1; 87 } 88 89 private static int TranslateToTable(int pos, List<LightWeightFunction> list, int[] heights) { 90 LightWeightFunction f = list[pos]; 91 if(f.arity == 0) { 92 Instr instr = evaluationTable[nInstr[0], 0]; 93 instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType); 94 switch(instr.symbol) { 95 case EvaluatorSymbolTable.VARIABLE: { 96 instr.i_arg0 = (int)f.data[0]; // var 97 instr.d_arg0 = f.data[1]; // weight 98 instr.i_arg1 = (int)f.data[2]; // sample-offset 99 break; 100 } 101 case EvaluatorSymbolTable.CONSTANT: { 102 instr.result = f.data[0]; // value 103 break; 104 } 105 } 106 nInstr[0]++; 107 return 1; 108 } else { 109 int length = 1; 110 int height = heights[pos]; 111 for(int i = 0; i < f.arity; i++) { 112 int curBranchHeight = heights[pos + length]; 113 if(curBranchHeight < height - 1) { 114 for(int j = curBranchHeight; j < height - 1; j++) { 115 evaluationTable[nInstr[j], j].symbol = EvaluatorSymbolTable.IDENTITY; 116 nInstr[j]++; 117 } 83 118 } 84 case EvaluatorSymbolTable.CONSTANT: { 85 instr.d_arg0 = f.data[0]; // value 86 break; 87 } 88 } 89 nInstr[height]++; 90 branchLength = length; 91 return height; 92 } 93 94 //private static Instr TranslateToInstr(LightWeightFunction f, Instr instr) { 119 int curBranchLength = TranslateToTable(pos + length, list, heights); 120 length += curBranchLength; 121 } 122 123 Instr cell = evaluationTable[nInstr[height-1], height-1]; 124 nInstr[height-1]++; 125 cell.arity = f.arity; 126 cell.symbol = EvaluatorSymbolTable.MapFunction(f.functionType); 127 return length; 128 } 129 } 130 131 132 //private static int TranslateToInstr(int pos, List<LightWeightFunction> linearRepresentation, out int branchLength) { 133 // int height = 0; 134 // int length = 1; 135 // LightWeightFunction f = linearRepresentation[pos]; 136 // for(int i = 0; i < f.arity; i++) { 137 // int curBranchLength; 138 // int curBranchHeight = TranslateToInstr(pos + length, linearRepresentation, out curBranchLength); 139 // if(curBranchHeight > height) height = curBranchHeight; 140 // length += curBranchLength; 141 // } 142 // Instr instr = evaluationTable[nInstr[height], height]; 95 143 // instr.arity = f.arity; 96 144 // instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType); … … 103 151 // } 104 152 // case EvaluatorSymbolTable.CONSTANT: { 105 // instr. d_arg0= f.data[0]; // value153 // instr.result = f.data[0]; // value 106 154 // break; 107 155 // } 108 156 // } 109 // return instr; 157 // nInstr[height]++; 158 // branchLength = length; 159 // return height + 1; 110 160 //} 111 161 … … 118 168 private static double EvaluateTable() { 119 169 int terminalP = 0; 120 for(; terminalP < nInstr[0]; terminalP += 2) { 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] - 4; terminalP += 4) { 121 181 Instr curInstr0 = evaluationTable[terminalP, 0]; 122 182 Instr curInstr1 = evaluationTable[terminalP + 1, 0]; 183 Instr curInstr2 = evaluationTable[terminalP + 2, 0]; 184 Instr curInstr3 = evaluationTable[terminalP + 3, 0]; 123 185 if(curInstr0.symbol == EvaluatorSymbolTable.VARIABLE) { 124 186 int row = sampleIndex + curInstr0.i_arg1; 125 if(row < 0 || row >= dataset.Rows) curInstr0. d_arg0= double.NaN;126 else curInstr0. d_arg0= curInstr0.d_arg0 * dataset.GetValue(row, curInstr0.i_arg0);187 if(row < 0 || row >= dataset.Rows) curInstr0.result = double.NaN; 188 else curInstr0.result = curInstr0.d_arg0 * dataset.GetValue(row, curInstr0.i_arg0); 127 189 } 128 190 if(curInstr1.symbol == EvaluatorSymbolTable.VARIABLE) { 129 191 int row = sampleIndex + curInstr1.i_arg1; 130 if(row < 0 || row >= dataset.Rows) curInstr1.d_arg0 = double.NaN; 131 else curInstr1.d_arg0 = curInstr1.d_arg0 * dataset.GetValue(row, curInstr1.i_arg0); 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); 132 204 } 133 205 } … … 140 212 switch(curInstr.symbol) { 141 213 case EvaluatorSymbolTable.MULTIPLICATION: { 142 curInstr. d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;214 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 143 215 for(int i = 1; i < curInstr.arity; i++) { 144 curInstr.d_arg0 *= evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0; 145 } 146 lastLayerInstrP += curInstr.arity; 216 curInstr.result *= evaluationTable[lastLayerInstrP++, curLevel - 1].result; 217 } 147 218 break; 148 219 } 149 220 case EvaluatorSymbolTable.ADDITION: { 150 curInstr. d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;221 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 151 222 for(int i = 1; i < curInstr.arity; i++) { 152 curInstr.d_arg0 += evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0; 153 } 154 lastLayerInstrP += curInstr.arity; 223 curInstr.result += evaluationTable[lastLayerInstrP++, curLevel - 1].result; 224 } 155 225 break; 156 226 } 157 227 case EvaluatorSymbolTable.SUBTRACTION: { 158 228 if(curInstr.arity == 1) { 159 curInstr. d_arg0 = -evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0;229 curInstr.result = -evaluationTable[lastLayerInstrP++, curLevel - 1].result; 160 230 } else { 161 curInstr. d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;231 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 162 232 for(int i = 1; i < curInstr.arity; i++) { 163 curInstr. d_arg0 -= evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0;233 curInstr.result -= evaluationTable[lastLayerInstrP++, curLevel - 1].result; 164 234 } 165 lastLayerInstrP += curInstr.arity;166 235 } 167 236 break; … … 169 238 case EvaluatorSymbolTable.DIVISION: { 170 239 if(curInstr.arity == 1) { 171 curInstr. d_arg0 = 1.0 / evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0;240 curInstr.result = 1.0 / evaluationTable[lastLayerInstrP++, curLevel - 1].result; 172 241 } else { 173 curInstr. d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;242 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 174 243 for(int i = 1; i < curInstr.arity; i++) { 175 curInstr. d_arg0 /= evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0;244 curInstr.result /= evaluationTable[lastLayerInstrP++, curLevel - 1].result; 176 245 } 177 lastLayerInstrP += curInstr.arity; 178 } 179 if(double.IsInfinity(curInstr.d_arg0)) curInstr.d_arg0 = 0.0; 246 } 247 if(double.IsInfinity(curInstr.result)) curInstr.result = 0.0; 180 248 break; 181 249 } 182 250 case EvaluatorSymbolTable.AVERAGE: { 183 curInstr. d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;251 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 184 252 for(int i = 1; i < curInstr.arity; i++) { 185 curInstr.d_arg0 += evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0; 186 } 187 lastLayerInstrP += curInstr.arity; 188 curInstr.d_arg0 /= curInstr.arity; 253 curInstr.result += evaluationTable[lastLayerInstrP++, curLevel - 1].result; 254 } 255 curInstr.result /= curInstr.arity; 189 256 break; 190 257 } 191 258 case EvaluatorSymbolTable.COSINUS: { 192 curInstr. d_arg0 = Math.Cos(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);259 curInstr.result = Math.Cos(evaluationTable[lastLayerInstrP++, curLevel - 1].result); 193 260 break; 194 261 } 195 262 case EvaluatorSymbolTable.SINUS: { 196 curInstr. d_arg0 = Math.Sin(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);263 curInstr.result = Math.Sin(evaluationTable[lastLayerInstrP++, curLevel - 1].result); 197 264 break; 198 265 } 199 266 case EvaluatorSymbolTable.EXP: { 200 curInstr. d_arg0 = Math.Exp(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);267 curInstr.result = Math.Exp(evaluationTable[lastLayerInstrP++, curLevel - 1].result); 201 268 break; 202 269 } 203 270 case EvaluatorSymbolTable.LOG: { 204 curInstr. d_arg0 = Math.Log(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);271 curInstr.result = Math.Log(evaluationTable[lastLayerInstrP++, curLevel - 1].result); 205 272 break; 206 273 } 207 274 case EvaluatorSymbolTable.POWER: { 208 double x = evaluationTable[lastLayerInstrP++, curLevel - 1]. d_arg0;209 double p = evaluationTable[lastLayerInstrP++, curLevel - 1]. d_arg0;210 curInstr. d_arg0= Math.Pow(x, p);275 double x = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 276 double p = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 277 curInstr.result = Math.Pow(x, p); 211 278 break; 212 279 } 213 280 case EvaluatorSymbolTable.SIGNUM: { 214 double value = evaluationTable[lastLayerInstrP++, curLevel - 1]. d_arg0;215 if(double.IsNaN(value)) curInstr. d_arg0= double.NaN;216 else curInstr. d_arg0= Math.Sign(value);281 double value = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 282 if(double.IsNaN(value)) curInstr.result = double.NaN; 283 else curInstr.result = Math.Sign(value); 217 284 break; 218 285 } 219 286 case EvaluatorSymbolTable.SQRT: { 220 curInstr. d_arg0 = Math.Sqrt(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);287 curInstr.result = Math.Sqrt(evaluationTable[lastLayerInstrP++, curLevel - 1].result); 221 288 break; 222 289 } 223 290 case EvaluatorSymbolTable.TANGENS: { 224 curInstr. d_arg0 = Math.Tan(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);291 curInstr.result = Math.Tan(evaluationTable[lastLayerInstrP++, curLevel - 1].result); 225 292 break; 226 293 } … … 285 352 // return double.NaN; 286 353 // } 354 case EvaluatorSymbolTable.IDENTITY: { 355 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result; 356 break; 357 } 287 358 default: { 288 359 throw new NotImplementedException(); … … 290 361 } 291 362 } 292 // copy remaining results from previous layer to current layer (identiy function)293 int r = 0;294 for(; lastLayerInstrP < nInstr[curLevel - 1]; lastLayerInstrP++) {295 evaluationTable[nInstr[curLevel] + r, curLevel].d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;296 r++;297 }298 363 curLevel++; 299 364 } 300 return evaluationTable[0, curLevel - 1]. d_arg0;365 return evaluationTable[0, curLevel - 1].result; 301 366 } 302 367 } -
branches/BottomUpTreeEvaluation/SymbolTable.cs
r319 r328 52 52 public const int VARIABLE = 22; 53 53 public const int XOR = 23; 54 public const int UNKNOWN = 24; 54 public const int IDENTITY = 24; 55 public const int UNKNOWN = 25; 55 56 56 57 private static Dictionary<Type, int> staticTypes = new Dictionary<Type,int>();
Note: See TracChangeset
for help on using the changeset viewer.