1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HEAL.Attic;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
32 | [StorableType("FB94F333-B32A-44FB-A561-CBDE76693D20")]
|
---|
33 | [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
|
---|
34 | public class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem,
|
---|
35 | ISymbolicDataAnalysisExpressionTreeInterpreter {
|
---|
36 | private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
|
---|
37 | private const string CheckExpressionsWithIntervalArithmeticParameterDescription = "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.";
|
---|
38 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
39 |
|
---|
40 | public override bool CanChangeName {
|
---|
41 | get { return false; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public override bool CanChangeDescription {
|
---|
45 | get { return false; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | #region parameter properties
|
---|
49 | public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
|
---|
50 | get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
54 | get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region properties
|
---|
59 | public bool CheckExpressionsWithIntervalArithmetic {
|
---|
60 | get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
|
---|
61 | set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public int EvaluatedSolutions {
|
---|
65 | get { return EvaluatedSolutionsParameter.Value.Value; }
|
---|
66 | set { EvaluatedSolutionsParameter.Value.Value = value; }
|
---|
67 | }
|
---|
68 | #endregion
|
---|
69 |
|
---|
70 | [StorableConstructor]
|
---|
71 | protected SymbolicDataAnalysisExpressionTreeInterpreter(StorableConstructorFlag _) : base(_) { }
|
---|
72 |
|
---|
73 | protected SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original,
|
---|
74 | Cloner cloner)
|
---|
75 | : base(original, cloner) { }
|
---|
76 |
|
---|
77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
78 | return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public SymbolicDataAnalysisExpressionTreeInterpreter()
|
---|
82 | : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
|
---|
83 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
|
---|
84 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected SymbolicDataAnalysisExpressionTreeInterpreter(string name, string description)
|
---|
88 | : base(name, description) {
|
---|
89 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
|
---|
90 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
91 | }
|
---|
92 |
|
---|
93 | [StorableHook(HookType.AfterDeserialization)]
|
---|
94 | private void AfterDeserialization() {
|
---|
95 | var evaluatedSolutions = new IntValue(0);
|
---|
96 | var checkExpressionsWithIntervalArithmetic = new BoolValue(false);
|
---|
97 | if (Parameters.ContainsKey(EvaluatedSolutionsParameterName)) {
|
---|
98 | var evaluatedSolutionsParameter = (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName];
|
---|
99 | evaluatedSolutions = evaluatedSolutionsParameter.Value;
|
---|
100 | Parameters.Remove(EvaluatedSolutionsParameterName);
|
---|
101 | }
|
---|
102 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", evaluatedSolutions));
|
---|
103 | if (Parameters.ContainsKey(CheckExpressionsWithIntervalArithmeticParameterName)) {
|
---|
104 | var checkExpressionsWithIntervalArithmeticParameter = (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName];
|
---|
105 | Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
|
---|
106 | checkExpressionsWithIntervalArithmetic = checkExpressionsWithIntervalArithmeticParameter.Value;
|
---|
107 | }
|
---|
108 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, checkExpressionsWithIntervalArithmetic));
|
---|
109 | }
|
---|
110 |
|
---|
111 | #region IStatefulItem
|
---|
112 | public void InitializeState() {
|
---|
113 | EvaluatedSolutions = 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void ClearState() { }
|
---|
117 | #endregion
|
---|
118 |
|
---|
119 | private readonly object syncRoot = new object();
|
---|
120 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset,
|
---|
121 | IEnumerable<int> rows) {
|
---|
122 | if (CheckExpressionsWithIntervalArithmetic) {
|
---|
123 | throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
|
---|
124 | }
|
---|
125 |
|
---|
126 | lock (syncRoot) {
|
---|
127 | EvaluatedSolutions++; // increment the evaluated solutions counter
|
---|
128 | }
|
---|
129 | var state = PrepareInterpreterState(tree, dataset);
|
---|
130 |
|
---|
131 | foreach (var rowEnum in rows) {
|
---|
132 | int row = rowEnum;
|
---|
133 | yield return Evaluate(dataset, ref row, state);
|
---|
134 | state.Reset();
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, IDataset dataset) {
|
---|
139 | Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
140 | int necessaryArgStackSize = 0;
|
---|
141 | foreach (Instruction instr in code) {
|
---|
142 | if (instr.opCode == OpCodes.Variable) {
|
---|
143 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
144 | instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
145 | } else if (instr.opCode == OpCodes.FactorVariable) {
|
---|
146 | var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode;
|
---|
147 | instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
|
---|
148 | } else if (instr.opCode == OpCodes.BinaryFactorVariable) {
|
---|
149 | var factorTreeNode = instr.dynamicNode as BinaryFactorVariableTreeNode;
|
---|
150 | instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
|
---|
151 | } else if (instr.opCode == OpCodes.LagVariable) {
|
---|
152 | var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
|
---|
153 | instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
|
---|
154 | } else if (instr.opCode == OpCodes.VariableCondition) {
|
---|
155 | var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
|
---|
156 | instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
|
---|
157 | } else if (instr.opCode == OpCodes.Call) {
|
---|
158 | necessaryArgStackSize += instr.nArguments + 1;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return new InterpreterState(code, necessaryArgStackSize);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public virtual double Evaluate(IDataset dataset, ref int row, InterpreterState state) {
|
---|
165 | Instruction currentInstr = state.NextInstruction();
|
---|
166 | switch (currentInstr.opCode) {
|
---|
167 | case OpCodes.Add: {
|
---|
168 | double s = Evaluate(dataset, ref row, state);
|
---|
169 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
170 | s += Evaluate(dataset, ref row, state);
|
---|
171 | }
|
---|
172 | return s;
|
---|
173 | }
|
---|
174 | case OpCodes.Sub: {
|
---|
175 | double s = Evaluate(dataset, ref row, state);
|
---|
176 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
177 | s -= Evaluate(dataset, ref row, state);
|
---|
178 | }
|
---|
179 | if (currentInstr.nArguments == 1) { s = -s; }
|
---|
180 | return s;
|
---|
181 | }
|
---|
182 | case OpCodes.Mul: {
|
---|
183 | double p = Evaluate(dataset, ref row, state);
|
---|
184 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
185 | p *= Evaluate(dataset, ref row, state);
|
---|
186 | }
|
---|
187 | return p;
|
---|
188 | }
|
---|
189 | case OpCodes.Div: {
|
---|
190 | double p = Evaluate(dataset, ref row, state);
|
---|
191 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
192 | p /= Evaluate(dataset, ref row, state);
|
---|
193 | }
|
---|
194 | if (currentInstr.nArguments == 1) { p = 1.0 / p; }
|
---|
195 | return p;
|
---|
196 | }
|
---|
197 | case OpCodes.Average: {
|
---|
198 | double sum = Evaluate(dataset, ref row, state);
|
---|
199 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
200 | sum += Evaluate(dataset, ref row, state);
|
---|
201 | }
|
---|
202 | return sum / currentInstr.nArguments;
|
---|
203 | }
|
---|
204 | case OpCodes.Absolute: {
|
---|
205 | return Math.Abs(Evaluate(dataset, ref row, state));
|
---|
206 | }
|
---|
207 | case OpCodes.Tanh: {
|
---|
208 | return Math.Tanh(Evaluate(dataset, ref row, state));
|
---|
209 | }
|
---|
210 | case OpCodes.Cos: {
|
---|
211 | return Math.Cos(Evaluate(dataset, ref row, state));
|
---|
212 | }
|
---|
213 | case OpCodes.Sin: {
|
---|
214 | return Math.Sin(Evaluate(dataset, ref row, state));
|
---|
215 | }
|
---|
216 | case OpCodes.Tan: {
|
---|
217 | return Math.Tan(Evaluate(dataset, ref row, state));
|
---|
218 | }
|
---|
219 | case OpCodes.Square: {
|
---|
220 | return Math.Pow(Evaluate(dataset, ref row, state), 2);
|
---|
221 | }
|
---|
222 | case OpCodes.Cube: {
|
---|
223 | return Math.Pow(Evaluate(dataset, ref row, state), 3);
|
---|
224 | }
|
---|
225 | case OpCodes.Power: {
|
---|
226 | double x = Evaluate(dataset, ref row, state);
|
---|
227 | double y = Math.Round(Evaluate(dataset, ref row, state));
|
---|
228 | return Math.Pow(x, y);
|
---|
229 | }
|
---|
230 | case OpCodes.SquareRoot: {
|
---|
231 | return Math.Sqrt(Evaluate(dataset, ref row, state));
|
---|
232 | }
|
---|
233 | case OpCodes.CubeRoot: {
|
---|
234 | var arg = Evaluate(dataset, ref row, state);
|
---|
235 | return arg < 0 ? -Math.Pow(-arg, 1.0 / 3.0) : Math.Pow(arg, 1.0 / 3.0);
|
---|
236 | }
|
---|
237 | case OpCodes.Root: {
|
---|
238 | double x = Evaluate(dataset, ref row, state);
|
---|
239 | double y = Math.Round(Evaluate(dataset, ref row, state));
|
---|
240 | return Math.Pow(x, 1 / y);
|
---|
241 | }
|
---|
242 | case OpCodes.Exp: {
|
---|
243 | return Math.Exp(Evaluate(dataset, ref row, state));
|
---|
244 | }
|
---|
245 | case OpCodes.Log: {
|
---|
246 | return Math.Log(Evaluate(dataset, ref row, state));
|
---|
247 | }
|
---|
248 | case OpCodes.Gamma: {
|
---|
249 | var x = Evaluate(dataset, ref row, state);
|
---|
250 | if (double.IsNaN(x)) { return double.NaN; } else { return alglib.gammafunction(x); }
|
---|
251 | }
|
---|
252 | case OpCodes.Psi: {
|
---|
253 | var x = Evaluate(dataset, ref row, state);
|
---|
254 | if (double.IsNaN(x)) return double.NaN;
|
---|
255 | else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN;
|
---|
256 | return alglib.psi(x);
|
---|
257 | }
|
---|
258 | case OpCodes.Dawson: {
|
---|
259 | var x = Evaluate(dataset, ref row, state);
|
---|
260 | if (double.IsNaN(x)) { return double.NaN; }
|
---|
261 | return alglib.dawsonintegral(x);
|
---|
262 | }
|
---|
263 | case OpCodes.ExponentialIntegralEi: {
|
---|
264 | var x = Evaluate(dataset, ref row, state);
|
---|
265 | if (double.IsNaN(x)) { return double.NaN; }
|
---|
266 | return alglib.exponentialintegralei(x);
|
---|
267 | }
|
---|
268 | case OpCodes.SineIntegral: {
|
---|
269 | double si, ci;
|
---|
270 | var x = Evaluate(dataset, ref row, state);
|
---|
271 | if (double.IsNaN(x)) return double.NaN;
|
---|
272 | else {
|
---|
273 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
274 | return si;
|
---|
275 | }
|
---|
276 | }
|
---|
277 | case OpCodes.CosineIntegral: {
|
---|
278 | double si, ci;
|
---|
279 | var x = Evaluate(dataset, ref row, state);
|
---|
280 | if (double.IsNaN(x)) return double.NaN;
|
---|
281 | else {
|
---|
282 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
283 | return ci;
|
---|
284 | }
|
---|
285 | }
|
---|
286 | case OpCodes.HyperbolicSineIntegral: {
|
---|
287 | double shi, chi;
|
---|
288 | var x = Evaluate(dataset, ref row, state);
|
---|
289 | if (double.IsNaN(x)) return double.NaN;
|
---|
290 | else {
|
---|
291 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
292 | return shi;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | case OpCodes.HyperbolicCosineIntegral: {
|
---|
296 | double shi, chi;
|
---|
297 | var x = Evaluate(dataset, ref row, state);
|
---|
298 | if (double.IsNaN(x)) return double.NaN;
|
---|
299 | else {
|
---|
300 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
301 | return chi;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | case OpCodes.FresnelCosineIntegral: {
|
---|
305 | double c = 0, s = 0;
|
---|
306 | var x = Evaluate(dataset, ref row, state);
|
---|
307 | if (double.IsNaN(x)) return double.NaN;
|
---|
308 | else {
|
---|
309 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
310 | return c;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | case OpCodes.FresnelSineIntegral: {
|
---|
314 | double c = 0, s = 0;
|
---|
315 | var x = Evaluate(dataset, ref row, state);
|
---|
316 | if (double.IsNaN(x)) return double.NaN;
|
---|
317 | else {
|
---|
318 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
319 | return s;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | case OpCodes.AiryA: {
|
---|
323 | double ai, aip, bi, bip;
|
---|
324 | var x = Evaluate(dataset, ref row, state);
|
---|
325 | if (double.IsNaN(x)) return double.NaN;
|
---|
326 | else {
|
---|
327 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
328 | return ai;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | case OpCodes.AiryB: {
|
---|
332 | double ai, aip, bi, bip;
|
---|
333 | var x = Evaluate(dataset, ref row, state);
|
---|
334 | if (double.IsNaN(x)) return double.NaN;
|
---|
335 | else {
|
---|
336 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
337 | return bi;
|
---|
338 | }
|
---|
339 | }
|
---|
340 | case OpCodes.Norm: {
|
---|
341 | var x = Evaluate(dataset, ref row, state);
|
---|
342 | if (double.IsNaN(x)) return double.NaN;
|
---|
343 | else return alglib.normaldistribution(x);
|
---|
344 | }
|
---|
345 | case OpCodes.Erf: {
|
---|
346 | var x = Evaluate(dataset, ref row, state);
|
---|
347 | if (double.IsNaN(x)) return double.NaN;
|
---|
348 | else return alglib.errorfunction(x);
|
---|
349 | }
|
---|
350 | case OpCodes.Bessel: {
|
---|
351 | var x = Evaluate(dataset, ref row, state);
|
---|
352 | if (double.IsNaN(x)) return double.NaN;
|
---|
353 | else return alglib.besseli0(x);
|
---|
354 | }
|
---|
355 |
|
---|
356 | case OpCodes.AnalyticQuotient: {
|
---|
357 | var x1 = Evaluate(dataset, ref row, state);
|
---|
358 | var x2 = Evaluate(dataset, ref row, state);
|
---|
359 | return x1 / Math.Pow(1 + x2 * x2, 0.5);
|
---|
360 | }
|
---|
361 | case OpCodes.IfThenElse: {
|
---|
362 | double condition = Evaluate(dataset, ref row, state);
|
---|
363 | double result;
|
---|
364 | if (condition > 0.0) {
|
---|
365 | result = Evaluate(dataset, ref row, state); state.SkipInstructions();
|
---|
366 | } else {
|
---|
367 | state.SkipInstructions(); result = Evaluate(dataset, ref row, state);
|
---|
368 | }
|
---|
369 | return result;
|
---|
370 | }
|
---|
371 | case OpCodes.AND: {
|
---|
372 | double result = Evaluate(dataset, ref row, state);
|
---|
373 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
374 | if (result > 0.0) result = Evaluate(dataset, ref row, state);
|
---|
375 | else {
|
---|
376 | state.SkipInstructions();
|
---|
377 | }
|
---|
378 | }
|
---|
379 | return result > 0.0 ? 1.0 : -1.0;
|
---|
380 | }
|
---|
381 | case OpCodes.OR: {
|
---|
382 | double result = Evaluate(dataset, ref row, state);
|
---|
383 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
384 | if (result <= 0.0) result = Evaluate(dataset, ref row, state);
|
---|
385 | else {
|
---|
386 | state.SkipInstructions();
|
---|
387 | }
|
---|
388 | }
|
---|
389 | return result > 0.0 ? 1.0 : -1.0;
|
---|
390 | }
|
---|
391 | case OpCodes.NOT: {
|
---|
392 | return Evaluate(dataset, ref row, state) > 0.0 ? -1.0 : 1.0;
|
---|
393 | }
|
---|
394 | case OpCodes.XOR: {
|
---|
395 | //mkommend: XOR on multiple inputs is defined as true if the number of positive signals is odd
|
---|
396 | // this is equal to a consecutive execution of binary XOR operations.
|
---|
397 | int positiveSignals = 0;
|
---|
398 | for (int i = 0; i < currentInstr.nArguments; i++) {
|
---|
399 | if (Evaluate(dataset, ref row, state) > 0.0) { positiveSignals++; }
|
---|
400 | }
|
---|
401 | return positiveSignals % 2 != 0 ? 1.0 : -1.0;
|
---|
402 | }
|
---|
403 | case OpCodes.GT: {
|
---|
404 | double x = Evaluate(dataset, ref row, state);
|
---|
405 | double y = Evaluate(dataset, ref row, state);
|
---|
406 | if (x > y) { return 1.0; } else { return -1.0; }
|
---|
407 | }
|
---|
408 | case OpCodes.LT: {
|
---|
409 | double x = Evaluate(dataset, ref row, state);
|
---|
410 | double y = Evaluate(dataset, ref row, state);
|
---|
411 | if (x < y) { return 1.0; } else { return -1.0; }
|
---|
412 | }
|
---|
413 | case OpCodes.TimeLag: {
|
---|
414 | var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
|
---|
415 | row += timeLagTreeNode.Lag;
|
---|
416 | double result = Evaluate(dataset, ref row, state);
|
---|
417 | row -= timeLagTreeNode.Lag;
|
---|
418 | return result;
|
---|
419 | }
|
---|
420 | case OpCodes.Integral: {
|
---|
421 | int savedPc = state.ProgramCounter;
|
---|
422 | var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
|
---|
423 | double sum = 0.0;
|
---|
424 | for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); i++) {
|
---|
425 | row += Math.Sign(timeLagTreeNode.Lag);
|
---|
426 | sum += Evaluate(dataset, ref row, state);
|
---|
427 | state.ProgramCounter = savedPc;
|
---|
428 | }
|
---|
429 | row -= timeLagTreeNode.Lag;
|
---|
430 | sum += Evaluate(dataset, ref row, state);
|
---|
431 | return sum;
|
---|
432 | }
|
---|
433 |
|
---|
434 | //mkommend: derivate calculation taken from:
|
---|
435 | //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
|
---|
436 | //one sided smooth differentiatior, N = 4
|
---|
437 | // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
|
---|
438 | case OpCodes.Derivative: {
|
---|
439 | int savedPc = state.ProgramCounter;
|
---|
440 | double f_0 = Evaluate(dataset, ref row, state); row--;
|
---|
441 | state.ProgramCounter = savedPc;
|
---|
442 | double f_1 = Evaluate(dataset, ref row, state); row -= 2;
|
---|
443 | state.ProgramCounter = savedPc;
|
---|
444 | double f_3 = Evaluate(dataset, ref row, state); row--;
|
---|
445 | state.ProgramCounter = savedPc;
|
---|
446 | double f_4 = Evaluate(dataset, ref row, state);
|
---|
447 | row += 4;
|
---|
448 |
|
---|
449 | return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
|
---|
450 | }
|
---|
451 | case OpCodes.Call: {
|
---|
452 | // evaluate sub-trees
|
---|
453 | double[] argValues = new double[currentInstr.nArguments];
|
---|
454 | for (int i = 0; i < currentInstr.nArguments; i++) {
|
---|
455 | argValues[i] = Evaluate(dataset, ref row, state);
|
---|
456 | }
|
---|
457 | // push on argument values on stack
|
---|
458 | state.CreateStackFrame(argValues);
|
---|
459 |
|
---|
460 | // save the pc
|
---|
461 | int savedPc = state.ProgramCounter;
|
---|
462 | // set pc to start of function
|
---|
463 | state.ProgramCounter = (ushort)currentInstr.data;
|
---|
464 | // evaluate the function
|
---|
465 | double v = Evaluate(dataset, ref row, state);
|
---|
466 |
|
---|
467 | // delete the stack frame
|
---|
468 | state.RemoveStackFrame();
|
---|
469 |
|
---|
470 | // restore the pc => evaluation will continue at point after my subtrees
|
---|
471 | state.ProgramCounter = savedPc;
|
---|
472 | return v;
|
---|
473 | }
|
---|
474 | case OpCodes.Arg: {
|
---|
475 | return state.GetStackFrameValue((ushort)currentInstr.data);
|
---|
476 | }
|
---|
477 | case OpCodes.Variable: {
|
---|
478 | if (row < 0 || row >= dataset.Rows) return double.NaN;
|
---|
479 | var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
|
---|
480 | return ((IList<double>)currentInstr.data)[row] * variableTreeNode.Weight;
|
---|
481 | }
|
---|
482 | case OpCodes.BinaryFactorVariable: {
|
---|
483 | if (row < 0 || row >= dataset.Rows) return double.NaN;
|
---|
484 | var factorVarTreeNode = currentInstr.dynamicNode as BinaryFactorVariableTreeNode;
|
---|
485 | return ((IList<string>)currentInstr.data)[row] == factorVarTreeNode.VariableValue ? factorVarTreeNode.Weight : 0;
|
---|
486 | }
|
---|
487 | case OpCodes.FactorVariable: {
|
---|
488 | if (row < 0 || row >= dataset.Rows) return double.NaN;
|
---|
489 | var factorVarTreeNode = currentInstr.dynamicNode as FactorVariableTreeNode;
|
---|
490 | return factorVarTreeNode.GetValue(((IList<string>)currentInstr.data)[row]);
|
---|
491 | }
|
---|
492 | case OpCodes.LagVariable: {
|
---|
493 | var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
|
---|
494 | int actualRow = row + laggedVariableTreeNode.Lag;
|
---|
495 | if (actualRow < 0 || actualRow >= dataset.Rows) { return double.NaN; }
|
---|
496 | return ((IList<double>)currentInstr.data)[actualRow] * laggedVariableTreeNode.Weight;
|
---|
497 | }
|
---|
498 | case OpCodes.Constant: {
|
---|
499 | var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
|
---|
500 | return constTreeNode.Value;
|
---|
501 | }
|
---|
502 |
|
---|
503 | //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
|
---|
504 | //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
|
---|
505 | case OpCodes.VariableCondition: {
|
---|
506 | if (row < 0 || row >= dataset.Rows) return double.NaN;
|
---|
507 | var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
|
---|
508 | if (!variableConditionTreeNode.Symbol.IgnoreSlope) {
|
---|
509 | double variableValue = ((IList<double>)currentInstr.data)[row];
|
---|
510 | double x = variableValue - variableConditionTreeNode.Threshold;
|
---|
511 | double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
|
---|
512 |
|
---|
513 | double trueBranch = Evaluate(dataset, ref row, state);
|
---|
514 | double falseBranch = Evaluate(dataset, ref row, state);
|
---|
515 |
|
---|
516 | return trueBranch * p + falseBranch * (1 - p);
|
---|
517 | } else {
|
---|
518 | // strict threshold
|
---|
519 | double variableValue = ((IList<double>)currentInstr.data)[row];
|
---|
520 | if (variableValue <= variableConditionTreeNode.Threshold) {
|
---|
521 | var left = Evaluate(dataset, ref row, state);
|
---|
522 | state.SkipInstructions();
|
---|
523 | return left;
|
---|
524 | } else {
|
---|
525 | state.SkipInstructions();
|
---|
526 | return Evaluate(dataset, ref row, state);
|
---|
527 | }
|
---|
528 | }
|
---|
529 | }
|
---|
530 |
|
---|
531 | default:
|
---|
532 | throw new NotSupportedException();
|
---|
533 | }
|
---|
534 | }
|
---|
535 | }
|
---|
536 | } |
---|