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