Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs @ 7624

Last change on this file since 7624 was 7615, checked in by gkronber, 13 years ago

#1081 merged r7462:7609 from trunk into time series branch

File size: 23.3 KB
RevLine 
[5571]1#region License Information
2/* HeuristicLab
[7268]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5571]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
22using System;
23using System.Collections.Generic;
[7120]24using System.Linq;
[5571]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[6740]27using HeuristicLab.Data;
[5571]28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[6740]29using HeuristicLab.Parameters;
[5571]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [StorableClass]
34  [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
[7120]35  public sealed class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem,
36    ISymbolicDataAnalysisExpressionTreeInterpreter, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter {
[5749]37    private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
[7615]38    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
[5749]39    #region private classes
[5571]40    private class InterpreterState {
41      private double[] argumentStack;
42      private int argumentStackPointer;
43      private Instruction[] code;
44      private int pc;
45      public int ProgramCounter {
46        get { return pc; }
47        set { pc = value; }
48      }
[5987]49      internal InterpreterState(Instruction[] code, int argumentStackSize) {
[5571]50        this.code = code;
51        this.pc = 0;
[5987]52        if (argumentStackSize > 0) {
53          this.argumentStack = new double[argumentStackSize];
54        }
[5571]55        this.argumentStackPointer = 0;
56      }
57
58      internal void Reset() {
59        this.pc = 0;
60        this.argumentStackPointer = 0;
61      }
62
63      internal Instruction NextInstruction() {
64        return code[pc++];
65      }
66      private void Push(double val) {
67        argumentStack[argumentStackPointer++] = val;
68      }
69      private double Pop() {
70        return argumentStack[--argumentStackPointer];
71      }
72
73      internal void CreateStackFrame(double[] argValues) {
74        // push in reverse order to make indexing easier
75        for (int i = argValues.Length - 1; i >= 0; i--) {
76          argumentStack[argumentStackPointer++] = argValues[i];
77        }
78        Push(argValues.Length);
79      }
80
81      internal void RemoveStackFrame() {
82        int size = (int)Pop();
83        argumentStackPointer -= size;
84      }
85
86      internal double GetStackFrameValue(ushort index) {
87        // layout of stack:
88        // [0]   <- argumentStackPointer
89        // [StackFrameSize = N + 1]
90        // [Arg0] <- argumentStackPointer - 2 - 0
91        // [Arg1] <- argumentStackPointer - 2 - 1
92        // [...]
93        // [ArgN] <- argumentStackPointer - 2 - N
94        // <Begin of stack frame>
95        return argumentStack[argumentStackPointer - index - 2];
96      }
97    }
98    private class OpCodes {
99      public const byte Add = 1;
100      public const byte Sub = 2;
101      public const byte Mul = 3;
102      public const byte Div = 4;
103
104      public const byte Sin = 5;
105      public const byte Cos = 6;
106      public const byte Tan = 7;
107
108      public const byte Log = 8;
109      public const byte Exp = 9;
110
111      public const byte IfThenElse = 10;
112
113      public const byte GT = 11;
114      public const byte LT = 12;
115
116      public const byte AND = 13;
117      public const byte OR = 14;
118      public const byte NOT = 15;
119
120
121      public const byte Average = 16;
122
123      public const byte Call = 17;
124
125      public const byte Variable = 18;
126      public const byte LagVariable = 19;
127      public const byte Constant = 20;
128      public const byte Arg = 21;
129
130      public const byte Power = 22;
131      public const byte Root = 23;
132      public const byte TimeLag = 24;
133      public const byte Integral = 25;
134      public const byte Derivative = 26;
135
136      public const byte VariableCondition = 27;
137    }
[5749]138    #endregion
[5571]139
140    private Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
141      { typeof(Addition), OpCodes.Add },
142      { typeof(Subtraction), OpCodes.Sub },
143      { typeof(Multiplication), OpCodes.Mul },
144      { typeof(Division), OpCodes.Div },
145      { typeof(Sine), OpCodes.Sin },
146      { typeof(Cosine), OpCodes.Cos },
147      { typeof(Tangent), OpCodes.Tan },
148      { typeof(Logarithm), OpCodes.Log },
149      { typeof(Exponential), OpCodes.Exp },
150      { typeof(IfThenElse), OpCodes.IfThenElse },
151      { typeof(GreaterThan), OpCodes.GT },
152      { typeof(LessThan), OpCodes.LT },
153      { typeof(And), OpCodes.AND },
154      { typeof(Or), OpCodes.OR },
155      { typeof(Not), OpCodes.NOT},
156      { typeof(Average), OpCodes.Average},
157      { typeof(InvokeFunction), OpCodes.Call },
158      { typeof(HeuristicLab.Problems.DataAnalysis.Symbolic.Variable), OpCodes.Variable },
159      { typeof(LaggedVariable), OpCodes.LagVariable },
160      { typeof(Constant), OpCodes.Constant },
161      { typeof(Argument), OpCodes.Arg },
162      { typeof(Power),OpCodes.Power},
163      { typeof(Root),OpCodes.Root},
164      { typeof(TimeLag), OpCodes.TimeLag},
165      { typeof(Integral), OpCodes.Integral},
166      { typeof(Derivative), OpCodes.Derivative},
167      { typeof(VariableCondition),OpCodes.VariableCondition}
168    };
169
170    public override bool CanChangeName {
171      get { return false; }
172    }
173    public override bool CanChangeDescription {
174      get { return false; }
175    }
176
[5749]177    #region parameter properties
178    public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
179      get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
180    }
[7615]181
182    public IValueParameter<IntValue> EvaluatedSolutionsParameter {
183      get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
184    }
[5749]185    #endregion
186
187    #region properties
188    public BoolValue CheckExpressionsWithIntervalArithmetic {
189      get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
190      set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
191    }
[7615]192
193    public IntValue EvaluatedSolutions {
194      get { return EvaluatedSolutionsParameter.Value; }
195      set { EvaluatedSolutionsParameter.Value = value; }
196    }
[5749]197    #endregion
198
[5571]199    [StorableConstructor]
200    private SymbolicDataAnalysisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { }
201    private SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
202    public override IDeepCloneable Clone(Cloner cloner) {
203      return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner);
204    }
205
206    public SymbolicDataAnalysisExpressionTreeInterpreter()
[5749]207      : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
208      Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
[7615]209      Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
[5571]210    }
211
[7615]212    [StorableHook(HookType.AfterDeserialization)]
213    private void AfterDeserialization() {
214      if (!Parameters.ContainsKey(EvaluatedSolutionsParameterName))
215        Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
216    }
217
218    #region IStatefulItem
219    public void InitializeState() {
220      EvaluatedSolutions.Value = 0;
221    }
222
223    public void ClearState() {
224    }
225    #endregion
226
[5571]227    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) {
[7154]228      return GetSymbolicExpressionTreeValues(tree, dataset, new string[] { "#NOTHING#" }, rows);
[7120]229    }
230
[7154]231    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows) {
232      return GetSymbolicExpressionTreeValues(tree, dataset, targetVariables, rows, 1);
233    }
234
235
236    // for each row for each horizon for each target variable one value
237    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows, int horizon) {
[5749]238      if (CheckExpressionsWithIntervalArithmetic.Value)
239        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
[7615]240      EvaluatedSolutions.Value++; // increment the evaluated solutions counter
[5571]241      var compiler = new SymbolicExpressionTreeCompiler();
242      Instruction[] code = compiler.Compile(tree, MapSymbolToOpCode);
[5987]243      int necessaryArgStackSize = 0;
[5571]244      for (int i = 0; i < code.Length; i++) {
245        Instruction instr = code[i];
[6860]246        if (instr.opCode == OpCodes.Variable) {
[5571]247          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
[6740]248          instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
[5897]249          code[i] = instr;
[5571]250        } else if (instr.opCode == OpCodes.LagVariable) {
[6740]251          var laggedVariableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;
252          instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
[5897]253          code[i] = instr;
[6860]254        } else if (instr.opCode == OpCodes.VariableCondition) {
[5571]255          var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
[6740]256          instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
[5987]257        } else if (instr.opCode == OpCodes.Call) {
258          necessaryArgStackSize += instr.nArguments + 1;
[5571]259        }
260      }
[5987]261      var state = new InterpreterState(code, necessaryArgStackSize);
[5571]262
[7120]263      int nComponents = tree.Root.GetSubtree(0).SubtreeCount;
264      // produce a n-step forecast for each target variable for all rows
265      var cachedPrognosedValues = new Dictionary<string, double[]>();
266      foreach (var targetVariable in targetVariables)
267        cachedPrognosedValues[targetVariable] = new double[horizon];
[5571]268      foreach (var rowEnum in rows) {
269        int row = rowEnum;
[7120]270        foreach (var horizonRow in Enumerable.Range(row, horizon)) {
271          int localRow = horizonRow; // create a local variable for the ref parameter
[7154]272          for (int c = 0; c < nComponents; c++) {
273            var prog = Evaluate(dataset, ref localRow, row - 1, state, cachedPrognosedValues);
274            yield return prog;
275            cachedPrognosedValues[targetVariables[c]][horizonRow - row] = prog;
276          }
[7120]277
278          state.Reset();
279        }
[5571]280      }
281    }
282
[7120]283    private double Evaluate(Dataset dataset, ref int row, int lastObservedRow, InterpreterState state, Dictionary<string, double[]> cachedPrognosedValues) {
[5571]284      Instruction currentInstr = state.NextInstruction();
285      switch (currentInstr.opCode) {
286        case OpCodes.Add: {
[7120]287            double s = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]288            for (int i = 1; i < currentInstr.nArguments; i++) {
[7120]289              s += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]290            }
291            return s;
292          }
293        case OpCodes.Sub: {
[7120]294            double s = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]295            for (int i = 1; i < currentInstr.nArguments; i++) {
[7120]296              s -= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]297            }
298            if (currentInstr.nArguments == 1) s = -s;
299            return s;
300          }
301        case OpCodes.Mul: {
[7120]302            double p = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]303            for (int i = 1; i < currentInstr.nArguments; i++) {
[7120]304              p *= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]305            }
306            return p;
307          }
308        case OpCodes.Div: {
[7120]309            double p = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]310            for (int i = 1; i < currentInstr.nArguments; i++) {
[7120]311              p /= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]312            }
313            if (currentInstr.nArguments == 1) p = 1.0 / p;
314            return p;
315          }
316        case OpCodes.Average: {
[7120]317            double sum = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]318            for (int i = 1; i < currentInstr.nArguments; i++) {
[7120]319              sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]320            }
321            return sum / currentInstr.nArguments;
322          }
323        case OpCodes.Cos: {
[7120]324            return Math.Cos(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
[5571]325          }
326        case OpCodes.Sin: {
[7120]327            return Math.Sin(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
[5571]328          }
329        case OpCodes.Tan: {
[7120]330            return Math.Tan(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
[5571]331          }
332        case OpCodes.Power: {
[7120]333            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
334            double y = Math.Round(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
[5571]335            return Math.Pow(x, y);
336          }
337        case OpCodes.Root: {
[7120]338            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
339            double y = Math.Round(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
[5571]340            return Math.Pow(x, 1 / y);
341          }
342        case OpCodes.Exp: {
[7120]343            return Math.Exp(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
[5571]344          }
345        case OpCodes.Log: {
[7120]346            return Math.Log(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
[5571]347          }
348        case OpCodes.IfThenElse: {
[7120]349            double condition = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]350            double result;
351            if (condition > 0.0) {
[7120]352              result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); SkipInstructions(state);
[5571]353            } else {
[7120]354              SkipInstructions(state); result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]355            }
356            return result;
357          }
358        case OpCodes.AND: {
[7120]359            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]360            for (int i = 1; i < currentInstr.nArguments; i++) {
[7120]361              if (result > 0.0) result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]362              else {
[6732]363                SkipInstructions(state);
[5571]364              }
365            }
[6732]366            return result > 0.0 ? 1.0 : -1.0;
[5571]367          }
368        case OpCodes.OR: {
[7120]369            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]370            for (int i = 1; i < currentInstr.nArguments; i++) {
[7120]371              if (result <= 0.0) result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]372              else {
[6732]373                SkipInstructions(state);
[5571]374              }
375            }
376            return result > 0.0 ? 1.0 : -1.0;
377          }
378        case OpCodes.NOT: {
[7120]379            return Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues) > 0.0 ? -1.0 : 1.0;
[5571]380          }
381        case OpCodes.GT: {
[7120]382            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
383            double y = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]384            if (x > y) return 1.0;
385            else return -1.0;
386          }
387        case OpCodes.LT: {
[7120]388            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
389            double y = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]390            if (x < y) return 1.0;
391            else return -1.0;
392          }
393        case OpCodes.TimeLag: {
394            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
395            row += timeLagTreeNode.Lag;
[7120]396            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]397            row -= timeLagTreeNode.Lag;
398            return result;
399          }
400        case OpCodes.Integral: {
401            int savedPc = state.ProgramCounter;
402            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
403            double sum = 0.0;
404            for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); i++) {
405              row += Math.Sign(timeLagTreeNode.Lag);
[7120]406              sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]407              state.ProgramCounter = savedPc;
408            }
409            row -= timeLagTreeNode.Lag;
[7120]410            sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]411            return sum;
412          }
413
414        //mkommend: derivate calculation taken from:
415        //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
416        //one sided smooth differentiatior, N = 4
417        // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
418        case OpCodes.Derivative: {
419            int savedPc = state.ProgramCounter;
[7120]420            double f_0 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row--;
[5571]421            state.ProgramCounter = savedPc;
[7120]422            double f_1 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row -= 2;
[5571]423            state.ProgramCounter = savedPc;
[7120]424            double f_3 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row--;
[5571]425            state.ProgramCounter = savedPc;
[7120]426            double f_4 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]427            row += 4;
428
429            return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
430          }
431        case OpCodes.Call: {
432            // evaluate sub-trees
433            double[] argValues = new double[currentInstr.nArguments];
434            for (int i = 0; i < currentInstr.nArguments; i++) {
[7120]435              argValues[i] = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]436            }
437            // push on argument values on stack
438            state.CreateStackFrame(argValues);
439
440            // save the pc
441            int savedPc = state.ProgramCounter;
442            // set pc to start of function 
[6740]443            state.ProgramCounter = (ushort)currentInstr.iArg0;
[5571]444            // evaluate the function
[7120]445            double v = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]446
447            // delete the stack frame
448            state.RemoveStackFrame();
449
450            // restore the pc => evaluation will continue at point after my subtrees 
451            state.ProgramCounter = savedPc;
452            return v;
453          }
454        case OpCodes.Arg: {
[6740]455            return state.GetStackFrameValue((ushort)currentInstr.iArg0);
[5571]456          }
457        case OpCodes.Variable: {
[5923]458            if (row < 0 || row >= dataset.Rows)
459              return double.NaN;
[6740]460            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
[7120]461            if (row <= lastObservedRow || !cachedPrognosedValues.ContainsKey(variableTreeNode.VariableName)) return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
462            else return cachedPrognosedValues[variableTreeNode.VariableName][row - lastObservedRow - 1] * variableTreeNode.Weight;
[5571]463          }
464        case OpCodes.LagVariable: {
[6740]465            var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
[5571]466            int actualRow = row + laggedVariableTreeNode.Lag;
[5923]467            if (actualRow < 0 || actualRow >= dataset.Rows)
468              return double.NaN;
[7120]469            if (actualRow <= lastObservedRow || !cachedPrognosedValues.ContainsKey(laggedVariableTreeNode.VariableName)) return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
470            else return cachedPrognosedValues[laggedVariableTreeNode.VariableName][actualRow - lastObservedRow - 1] * laggedVariableTreeNode.Weight;
[5571]471          }
472        case OpCodes.Constant: {
[5897]473            var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
474            return constTreeNode.Value;
[5571]475          }
476
477        //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
478        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
479        case OpCodes.VariableCondition: {
[5923]480            if (row < 0 || row >= dataset.Rows)
481              return double.NaN;
[5571]482            var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
[7120]483            double variableValue;
484            if (row <= lastObservedRow || !cachedPrognosedValues.ContainsKey(variableConditionTreeNode.VariableName))
485              variableValue = ((IList<double>)currentInstr.iArg0)[row];
486            else
487              variableValue = cachedPrognosedValues[variableConditionTreeNode.VariableName][row - lastObservedRow - 1];
488
[5897]489            double x = variableValue - variableConditionTreeNode.Threshold;
[5571]490            double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
491
[7120]492            double trueBranch = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
493            double falseBranch = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
[5571]494
495            return trueBranch * p + falseBranch * (1 - p);
496          }
497        default: throw new NotSupportedException();
498      }
499    }
500
501    private byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
[6860]502      if (symbolToOpcode.ContainsKey(treeNode.Symbol.GetType()))
503        return symbolToOpcode[treeNode.Symbol.GetType()];
504      else
505        throw new NotSupportedException("Symbol: " + treeNode.Symbol);
[5571]506    }
507
508    // skips a whole branch
509    private void SkipInstructions(InterpreterState state) {
510      int i = 1;
511      while (i > 0) {
512        i += state.NextInstruction().nArguments;
513        i--;
514      }
515    }
516  }
517}
Note: See TracBrowser for help on using the repository browser.