Changeset 6849 for trunk/sources
- Timestamp:
- 09/29/11 13:27:46 (13 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r6809 r6849 50 50 #region private classes 51 51 private class InterpreterState { 52 private double[] argumentStack;53 private int argumentStackPointer;54 52 private Instruction[] code; 55 53 private int pc; 54 56 55 public int ProgramCounter { 57 56 get { return pc; } 58 57 set { pc = value; } 59 58 } 60 internal InterpreterState(Instruction[] code, int argumentStackSize) { 59 60 private bool inLaggedContext; 61 public bool InLaggedContext { 62 get { return inLaggedContext; } 63 set { inLaggedContext = value; } 64 } 65 internal InterpreterState(Instruction[] code) { 66 this.inLaggedContext = false; 61 67 this.code = code; 62 68 this.pc = 0; 63 if (argumentStackSize > 0) {64 this.argumentStack = new double[argumentStackSize];65 }66 this.argumentStackPointer = 0;67 }68 69 internal void Reset() {70 this.pc = 0;71 this.argumentStackPointer = 0;72 69 } 73 70 74 71 internal Instruction NextInstruction() { 75 72 return code[pc++]; 76 }77 private void Push(double val) {78 argumentStack[argumentStackPointer++] = val;79 }80 private double Pop() {81 return argumentStack[--argumentStackPointer];82 }83 84 internal void CreateStackFrame(double[] argValues) {85 // push in reverse order to make indexing easier86 for (int i = argValues.Length - 1; i >= 0; i--) {87 argumentStack[argumentStackPointer++] = argValues[i];88 }89 Push(argValues.Length);90 }91 92 internal void RemoveStackFrame() {93 int size = (int)Pop();94 argumentStackPointer -= size;95 }96 97 internal double GetStackFrameValue(ushort index) {98 // layout of stack:99 // [0] <- argumentStackPointer100 // [StackFrameSize = N + 1]101 // [Arg0] <- argumentStackPointer - 2 - 0102 // [Arg1] <- argumentStackPointer - 2 - 1103 // [...]104 // [ArgN] <- argumentStackPointer - 2 - N105 // <Begin of stack frame>106 return argumentStack[argumentStackPointer - index - 2];107 73 } 108 74 } … … 241 207 } 242 208 } 243 var state = new InterpreterState(code , necessaryArgStackSize);209 var state = new InterpreterState(code); 244 210 245 211 Type[] methodArgs = { typeof(int), typeof(IList<double>[]) }; … … 465 431 il.Emit(System.Reflection.Emit.OpCodes.Add); 466 432 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 433 var prevLaggedContext = state.InLaggedContext; 434 state.InLaggedContext = true; 467 435 CompileInstructions(il, state, ds); 468 436 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag … … 470 438 il.Emit(System.Reflection.Emit.OpCodes.Sub); 471 439 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 440 state.InLaggedContext = prevLaggedContext; 472 441 return; 473 442 } … … 479 448 il.Emit(System.Reflection.Emit.OpCodes.Add); 480 449 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 450 var prevLaggedContext = state.InLaggedContext; 451 state.InLaggedContext = true; 481 452 CompileInstructions(il, state, ds); 482 453 for (int l = laggedTreeNode.Lag; l < 0; l++) { … … 489 460 il.Emit(System.Reflection.Emit.OpCodes.Add); 490 461 } 462 state.InLaggedContext = prevLaggedContext; 491 463 return; 492 464 } … … 504 476 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 505 477 state.ProgramCounter = savedPc; 478 var prevLaggedContext = state.InLaggedContext; 479 state.InLaggedContext = true; 506 480 CompileInstructions(il, state, ds); 507 481 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1 … … 533 507 il.Emit(System.Reflection.Emit.OpCodes.Add); 534 508 il.Emit(System.Reflection.Emit.OpCodes.Starg, 0); 509 state.InLaggedContext = prevLaggedContext; 535 510 return; 536 511 } … … 542 517 } 543 518 case OpCodes.Variable: { 519 VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode; 520 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array 521 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); 522 // load correct column of the current variable 523 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 524 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 525 if (!state.InLaggedContext) { 526 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 527 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight 528 il.Emit(System.Reflection.Emit.OpCodes.Mul); 529 } else { 530 var nanResult = il.DefineLabel(); 531 var normalResult = il.DefineLabel(); 532 il.Emit(System.Reflection.Emit.OpCodes.Dup); 533 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); 534 il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult); 535 il.Emit(System.Reflection.Emit.OpCodes.Dup); 536 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows); 537 il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult); 538 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 539 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight 540 il.Emit(System.Reflection.Emit.OpCodes.Mul); 541 il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult); 542 il.MarkLabel(nanResult); 543 il.Emit(System.Reflection.Emit.OpCodes.Pop); // rowIndex 544 il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference 545 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN); 546 il.MarkLabel(normalResult); 547 } 548 return; 549 } 550 case OpCodes.LagVariable: { 544 551 var nanResult = il.DefineLabel(); 545 552 var normalResult = il.DefineLabel(); 546 VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode;553 LaggedVariableTreeNode varNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 547 554 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array 548 555 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable 549 556 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 550 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // sampleIndex 557 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag 558 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 559 il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset 551 560 il.Emit(System.Reflection.Emit.OpCodes.Dup); 552 561 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); … … 566 575 return; 567 576 } 568 case OpCodes.LagVariable: {569 var nanResult = il.DefineLabel();570 var normalResult = il.DefineLabel();571 LaggedVariableTreeNode varNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;572 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array573 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable574 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);575 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag576 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // sampleIndex577 il.Emit(System.Reflection.Emit.OpCodes.Add); // row = sampleIndex + sampleOffset578 il.Emit(System.Reflection.Emit.OpCodes.Dup);579 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);580 il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult);581 il.Emit(System.Reflection.Emit.OpCodes.Dup);582 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows);583 il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult);584 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);585 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight586 il.Emit(System.Reflection.Emit.OpCodes.Mul);587 il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult);588 il.MarkLabel(nanResult);589 il.Emit(System.Reflection.Emit.OpCodes.Pop); // sample index590 il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference591 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN);592 il.MarkLabel(normalResult);593 return;594 }595 577 case OpCodes.Constant: { 596 578 ConstantTreeNode constNode = (ConstantTreeNode)currentInstr.dynamicNode; … … 614 596 throw new NotSupportedException("Symbol: " + treeNode.Symbol); 615 597 } 616 617 // skips a whole branch618 private void SkipInstructions(InterpreterState state) {619 int i = 1;620 while (i > 0) {621 i += state.NextInstruction().nArguments;622 i--;623 }624 }625 598 } 626 599 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r6769 r6849 132 132 133 133 public const byte VariableCondition = 27; 134 135 public const byte UncheckedVariable = 28; 136 public const byte UncheckedVariableCondition = 29; 134 137 } 135 138 #endregion 136 139 140 private Dictionary<Type, byte> symbolToUncheckedOpcode = new Dictionary<Type, byte>() { 141 { typeof(HeuristicLab.Problems.DataAnalysis.Symbolic.Variable), OpCodes.UncheckedVariable }, 142 { typeof(VariableCondition),OpCodes.UncheckedVariableCondition} 143 }; 137 144 private Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() { 138 145 { typeof(Addition), OpCodes.Add }, … … 206 213 for (int i = 0; i < code.Length; i++) { 207 214 Instruction instr = code[i]; 208 if (instr.opCode == OpCodes.Variable ) {215 if (instr.opCode == OpCodes.Variable || instr.opCode == OpCodes.UncheckedVariable) { 209 216 var variableTreeNode = instr.dynamicNode as VariableTreeNode; 210 217 instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName); … … 214 221 instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName); 215 222 code[i] = instr; 216 } else if (instr.opCode == OpCodes.VariableCondition ) {223 } else if (instr.opCode == OpCodes.VariableCondition || instr.opCode == OpCodes.UncheckedVariableCondition) { 217 224 var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode; 218 225 instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName); … … 410 417 return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight; 411 418 } 419 case OpCodes.UncheckedVariable: { 420 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 421 return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight; 422 } 412 423 case OpCodes.LagVariable: { 413 424 var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; … … 437 448 return trueBranch * p + falseBranch * (1 - p); 438 449 } 450 case OpCodes.UncheckedVariableCondition: { 451 var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode; 452 double variableValue = ((IList<double>)currentInstr.iArg0)[row]; 453 double x = variableValue - variableConditionTreeNode.Threshold; 454 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); 455 456 double trueBranch = Evaluate(dataset, ref row, state); 457 double falseBranch = Evaluate(dataset, ref row, state); 458 459 return trueBranch * p + falseBranch * (1 - p); 460 } 439 461 default: throw new NotSupportedException(); 440 462 } … … 442 464 443 465 private byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) { 444 if (symbolToOpcode.ContainsKey(treeNode.Symbol.GetType())) 445 return symbolToOpcode[treeNode.Symbol.GetType()]; 446 else 447 throw new NotSupportedException("Symbol: " + treeNode.Symbol); 466 if ((treeNode.Symbol.GetType() == typeof(Variable) || treeNode.Symbol.GetType() == typeof(VariableCondition)) && !InLaggedContext(treeNode)) { 467 return symbolToUncheckedOpcode[treeNode.Symbol.GetType()]; 468 } else { 469 if (symbolToOpcode.ContainsKey(treeNode.Symbol.GetType())) 470 return symbolToOpcode[treeNode.Symbol.GetType()]; 471 else 472 throw new NotSupportedException("Symbol: " + treeNode.Symbol); 473 } 474 } 475 476 private bool InLaggedContext(ISymbolicExpressionTreeNode treeNode) { 477 if (treeNode.Parent == null) return false; 478 if (treeNode.Parent is ILaggedTreeNode) return true; 479 if (treeNode.Parent.Symbol is Derivative) return true; 480 return InLaggedContext(treeNode.Parent); 448 481 } 449 482
Note: See TracChangeset
for help on using the changeset viewer.