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 HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
32 | [StorableClass]
|
---|
33 | [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
|
---|
34 | public sealed class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
|
---|
35 | private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
|
---|
36 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
37 | #region private classes
|
---|
38 | private class InterpreterState {
|
---|
39 | private double[] argumentStack;
|
---|
40 | private int argumentStackPointer;
|
---|
41 | private Instruction[] code;
|
---|
42 | private int pc;
|
---|
43 | public int ProgramCounter {
|
---|
44 | get { return pc; }
|
---|
45 | set { pc = value; }
|
---|
46 | }
|
---|
47 | internal InterpreterState(Instruction[] code, int argumentStackSize) {
|
---|
48 | this.code = code;
|
---|
49 | this.pc = 0;
|
---|
50 | if (argumentStackSize > 0) {
|
---|
51 | this.argumentStack = new double[argumentStackSize];
|
---|
52 | }
|
---|
53 | this.argumentStackPointer = 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | internal void Reset() {
|
---|
57 | this.pc = 0;
|
---|
58 | this.argumentStackPointer = 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | internal Instruction NextInstruction() {
|
---|
62 | return code[pc++];
|
---|
63 | }
|
---|
64 | private void Push(double val) {
|
---|
65 | argumentStack[argumentStackPointer++] = val;
|
---|
66 | }
|
---|
67 | private double Pop() {
|
---|
68 | return argumentStack[--argumentStackPointer];
|
---|
69 | }
|
---|
70 |
|
---|
71 | internal void CreateStackFrame(double[] argValues) {
|
---|
72 | // push in reverse order to make indexing easier
|
---|
73 | for (int i = argValues.Length - 1; i >= 0; i--) {
|
---|
74 | argumentStack[argumentStackPointer++] = argValues[i];
|
---|
75 | }
|
---|
76 | Push(argValues.Length);
|
---|
77 | }
|
---|
78 |
|
---|
79 | internal void RemoveStackFrame() {
|
---|
80 | int size = (int)Pop();
|
---|
81 | argumentStackPointer -= size;
|
---|
82 | }
|
---|
83 |
|
---|
84 | internal double GetStackFrameValue(ushort index) {
|
---|
85 | // layout of stack:
|
---|
86 | // [0] <- argumentStackPointer
|
---|
87 | // [StackFrameSize = N + 1]
|
---|
88 | // [Arg0] <- argumentStackPointer - 2 - 0
|
---|
89 | // [Arg1] <- argumentStackPointer - 2 - 1
|
---|
90 | // [...]
|
---|
91 | // [ArgN] <- argumentStackPointer - 2 - N
|
---|
92 | // <Begin of stack frame>
|
---|
93 | return argumentStack[argumentStackPointer - index - 2];
|
---|
94 | }
|
---|
95 | }
|
---|
96 | private class OpCodes {
|
---|
97 | public const byte Add = 1;
|
---|
98 | public const byte Sub = 2;
|
---|
99 | public const byte Mul = 3;
|
---|
100 | public const byte Div = 4;
|
---|
101 |
|
---|
102 | public const byte Sin = 5;
|
---|
103 | public const byte Cos = 6;
|
---|
104 | public const byte Tan = 7;
|
---|
105 |
|
---|
106 | public const byte Log = 8;
|
---|
107 | public const byte Exp = 9;
|
---|
108 |
|
---|
109 | public const byte IfThenElse = 10;
|
---|
110 |
|
---|
111 | public const byte GT = 11;
|
---|
112 | public const byte LT = 12;
|
---|
113 |
|
---|
114 | public const byte AND = 13;
|
---|
115 | public const byte OR = 14;
|
---|
116 | public const byte NOT = 15;
|
---|
117 |
|
---|
118 |
|
---|
119 | public const byte Average = 16;
|
---|
120 |
|
---|
121 | public const byte Call = 17;
|
---|
122 |
|
---|
123 | public const byte Variable = 18;
|
---|
124 | public const byte LagVariable = 19;
|
---|
125 | public const byte Constant = 20;
|
---|
126 | public const byte Arg = 21;
|
---|
127 |
|
---|
128 | public const byte Power = 22;
|
---|
129 | public const byte Root = 23;
|
---|
130 | public const byte TimeLag = 24;
|
---|
131 | public const byte Integral = 25;
|
---|
132 | public const byte Derivative = 26;
|
---|
133 |
|
---|
134 | public const byte VariableCondition = 27;
|
---|
135 |
|
---|
136 | public const byte Square = 28;
|
---|
137 | public const byte SquareRoot = 29;
|
---|
138 | public const byte Gamma = 30;
|
---|
139 | public const byte Psi = 31;
|
---|
140 | public const byte Dawson = 32;
|
---|
141 | public const byte ExponentialIntegralEi = 33;
|
---|
142 | public const byte CosineIntegral = 34;
|
---|
143 | public const byte SineIntegral = 35;
|
---|
144 | public const byte HyperbolicCosineIntegral = 36;
|
---|
145 | public const byte HyperbolicSineIntegral = 37;
|
---|
146 | public const byte FresnelCosineIntegral = 38;
|
---|
147 | public const byte FresnelSineIntegral = 39;
|
---|
148 | public const byte AiryA = 40;
|
---|
149 | public const byte AiryB = 41;
|
---|
150 | public const byte Norm = 42;
|
---|
151 | public const byte Erf = 43;
|
---|
152 | public const byte Bessel = 44;
|
---|
153 | }
|
---|
154 | #endregion
|
---|
155 |
|
---|
156 | private Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
|
---|
157 | { typeof(Addition), OpCodes.Add },
|
---|
158 | { typeof(Subtraction), OpCodes.Sub },
|
---|
159 | { typeof(Multiplication), OpCodes.Mul },
|
---|
160 | { typeof(Division), OpCodes.Div },
|
---|
161 | { typeof(Sine), OpCodes.Sin },
|
---|
162 | { typeof(Cosine), OpCodes.Cos },
|
---|
163 | { typeof(Tangent), OpCodes.Tan },
|
---|
164 | { typeof(Logarithm), OpCodes.Log },
|
---|
165 | { typeof(Exponential), OpCodes.Exp },
|
---|
166 | { typeof(IfThenElse), OpCodes.IfThenElse },
|
---|
167 | { typeof(GreaterThan), OpCodes.GT },
|
---|
168 | { typeof(LessThan), OpCodes.LT },
|
---|
169 | { typeof(And), OpCodes.AND },
|
---|
170 | { typeof(Or), OpCodes.OR },
|
---|
171 | { typeof(Not), OpCodes.NOT},
|
---|
172 | { typeof(Average), OpCodes.Average},
|
---|
173 | { typeof(InvokeFunction), OpCodes.Call },
|
---|
174 | { typeof(Variable), OpCodes.Variable },
|
---|
175 | { typeof(LaggedVariable), OpCodes.LagVariable },
|
---|
176 | { typeof(Constant), OpCodes.Constant },
|
---|
177 | { typeof(Argument), OpCodes.Arg },
|
---|
178 | { typeof(Power),OpCodes.Power},
|
---|
179 | { typeof(Root),OpCodes.Root},
|
---|
180 | { typeof(TimeLag), OpCodes.TimeLag},
|
---|
181 | { typeof(Integral), OpCodes.Integral},
|
---|
182 | { typeof(Derivative), OpCodes.Derivative},
|
---|
183 | { typeof(VariableCondition),OpCodes.VariableCondition},
|
---|
184 | { typeof(Square),OpCodes.Square},
|
---|
185 | { typeof(SquareRoot),OpCodes.SquareRoot},
|
---|
186 | { typeof(Gamma), OpCodes.Gamma },
|
---|
187 | { typeof(Psi), OpCodes.Psi },
|
---|
188 | { typeof(Dawson), OpCodes.Dawson},
|
---|
189 | { typeof(ExponentialIntegralEi), OpCodes.ExponentialIntegralEi },
|
---|
190 | { typeof(CosineIntegral), OpCodes.CosineIntegral },
|
---|
191 | { typeof(SineIntegral), OpCodes.SineIntegral },
|
---|
192 | { typeof(HyperbolicCosineIntegral), OpCodes.HyperbolicCosineIntegral },
|
---|
193 | { typeof(HyperbolicSineIntegral), OpCodes.HyperbolicSineIntegral },
|
---|
194 | { typeof(FresnelCosineIntegral), OpCodes.FresnelCosineIntegral },
|
---|
195 | { typeof(FresnelSineIntegral), OpCodes.FresnelSineIntegral },
|
---|
196 | { typeof(AiryA), OpCodes.AiryA },
|
---|
197 | { typeof(AiryB), OpCodes.AiryB },
|
---|
198 | { typeof(Norm), OpCodes.Norm},
|
---|
199 | { typeof(Erf), OpCodes.Erf},
|
---|
200 | { typeof(Bessel), OpCodes.Bessel}
|
---|
201 | };
|
---|
202 |
|
---|
203 | public override bool CanChangeName {
|
---|
204 | get { return false; }
|
---|
205 | }
|
---|
206 | public override bool CanChangeDescription {
|
---|
207 | get { return false; }
|
---|
208 | }
|
---|
209 |
|
---|
210 | #region parameter properties
|
---|
211 | public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
|
---|
212 | get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
|
---|
213 | }
|
---|
214 |
|
---|
215 | public IValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
216 | get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
217 | }
|
---|
218 | #endregion
|
---|
219 |
|
---|
220 | #region properties
|
---|
221 | public BoolValue CheckExpressionsWithIntervalArithmetic {
|
---|
222 | get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
|
---|
223 | set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
|
---|
224 | }
|
---|
225 |
|
---|
226 | public IntValue EvaluatedSolutions {
|
---|
227 | get { return EvaluatedSolutionsParameter.Value; }
|
---|
228 | set { EvaluatedSolutionsParameter.Value = value; }
|
---|
229 | }
|
---|
230 | #endregion
|
---|
231 |
|
---|
232 | [StorableConstructor]
|
---|
233 | private SymbolicDataAnalysisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { }
|
---|
234 | private SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
|
---|
235 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
236 | return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner);
|
---|
237 | }
|
---|
238 |
|
---|
239 | public SymbolicDataAnalysisExpressionTreeInterpreter()
|
---|
240 | : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
|
---|
241 | 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)));
|
---|
242 | Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
243 | }
|
---|
244 |
|
---|
245 | [StorableHook(HookType.AfterDeserialization)]
|
---|
246 | private void AfterDeserialization() {
|
---|
247 | if (!Parameters.ContainsKey(EvaluatedSolutionsParameterName))
|
---|
248 | Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
249 | }
|
---|
250 |
|
---|
251 | #region IStatefulItem
|
---|
252 | public void InitializeState() {
|
---|
253 | EvaluatedSolutions.Value = 0;
|
---|
254 | }
|
---|
255 |
|
---|
256 | public void ClearState() {
|
---|
257 | }
|
---|
258 | #endregion
|
---|
259 |
|
---|
260 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) {
|
---|
261 | if (CheckExpressionsWithIntervalArithmetic.Value)
|
---|
262 | throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
|
---|
263 | EvaluatedSolutions.Value++; // increment the evaluated solutions counter
|
---|
264 | var compiler = new SymbolicExpressionTreeCompiler();
|
---|
265 | Instruction[] code = compiler.Compile(tree, MapSymbolToOpCode);
|
---|
266 | int necessaryArgStackSize = 0;
|
---|
267 | for (int i = 0; i < code.Length; i++) {
|
---|
268 | Instruction instr = code[i];
|
---|
269 | if (instr.opCode == OpCodes.Variable) {
|
---|
270 | var variableTreeNode = instr.dynamicNode as VariableTreeNode;
|
---|
271 | instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
272 | code[i] = instr;
|
---|
273 | } else if (instr.opCode == OpCodes.LagVariable) {
|
---|
274 | var laggedVariableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;
|
---|
275 | instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
|
---|
276 | code[i] = instr;
|
---|
277 | } else if (instr.opCode == OpCodes.VariableCondition) {
|
---|
278 | var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
|
---|
279 | instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
|
---|
280 | } else if (instr.opCode == OpCodes.Call) {
|
---|
281 | necessaryArgStackSize += instr.nArguments + 1;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | var state = new InterpreterState(code, necessaryArgStackSize);
|
---|
285 |
|
---|
286 | foreach (var rowEnum in rows) {
|
---|
287 | int row = rowEnum;
|
---|
288 | state.Reset();
|
---|
289 | yield return Evaluate(dataset, ref row, state);
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | private double Evaluate(Dataset dataset, ref int row, InterpreterState state) {
|
---|
294 | Instruction currentInstr = state.NextInstruction();
|
---|
295 | switch (currentInstr.opCode) {
|
---|
296 | case OpCodes.Add: {
|
---|
297 | double s = Evaluate(dataset, ref row, state);
|
---|
298 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
299 | s += Evaluate(dataset, ref row, state);
|
---|
300 | }
|
---|
301 | return s;
|
---|
302 | }
|
---|
303 | case OpCodes.Sub: {
|
---|
304 | double s = Evaluate(dataset, ref row, state);
|
---|
305 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
306 | s -= Evaluate(dataset, ref row, state);
|
---|
307 | }
|
---|
308 | if (currentInstr.nArguments == 1) s = -s;
|
---|
309 | return s;
|
---|
310 | }
|
---|
311 | case OpCodes.Mul: {
|
---|
312 | double p = Evaluate(dataset, ref row, state);
|
---|
313 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
314 | p *= Evaluate(dataset, ref row, state);
|
---|
315 | }
|
---|
316 | return p;
|
---|
317 | }
|
---|
318 | case OpCodes.Div: {
|
---|
319 | double p = Evaluate(dataset, ref row, state);
|
---|
320 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
321 | p /= Evaluate(dataset, ref row, state);
|
---|
322 | }
|
---|
323 | if (currentInstr.nArguments == 1) p = 1.0 / p;
|
---|
324 | return p;
|
---|
325 | }
|
---|
326 | case OpCodes.Average: {
|
---|
327 | double sum = Evaluate(dataset, ref row, state);
|
---|
328 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
329 | sum += Evaluate(dataset, ref row, state);
|
---|
330 | }
|
---|
331 | return sum / currentInstr.nArguments;
|
---|
332 | }
|
---|
333 | case OpCodes.Cos: {
|
---|
334 | return Math.Cos(Evaluate(dataset, ref row, state));
|
---|
335 | }
|
---|
336 | case OpCodes.Sin: {
|
---|
337 | return Math.Sin(Evaluate(dataset, ref row, state));
|
---|
338 | }
|
---|
339 | case OpCodes.Tan: {
|
---|
340 | return Math.Tan(Evaluate(dataset, ref row, state));
|
---|
341 | }
|
---|
342 | case OpCodes.Square: {
|
---|
343 | return Math.Pow(Evaluate(dataset, ref row, state), 2);
|
---|
344 | }
|
---|
345 | case OpCodes.Power: {
|
---|
346 | double x = Evaluate(dataset, ref row, state);
|
---|
347 | double y = Math.Round(Evaluate(dataset, ref row, state));
|
---|
348 | return Math.Pow(x, y);
|
---|
349 | }
|
---|
350 | case OpCodes.SquareRoot: {
|
---|
351 | return Math.Sqrt(Evaluate(dataset, ref row, state));
|
---|
352 | }
|
---|
353 | case OpCodes.Root: {
|
---|
354 | double x = Evaluate(dataset, ref row, state);
|
---|
355 | double y = Math.Round(Evaluate(dataset, ref row, state));
|
---|
356 | return Math.Pow(x, 1 / y);
|
---|
357 | }
|
---|
358 | case OpCodes.Exp: {
|
---|
359 | return Math.Exp(Evaluate(dataset, ref row, state));
|
---|
360 | }
|
---|
361 | case OpCodes.Log: {
|
---|
362 | return Math.Log(Evaluate(dataset, ref row, state));
|
---|
363 | }
|
---|
364 | case OpCodes.Gamma: {
|
---|
365 | var x = Evaluate(dataset, ref row, state);
|
---|
366 | if (double.IsNaN(x)) return double.NaN;
|
---|
367 | else return alglib.gammafunction(x);
|
---|
368 | }
|
---|
369 | case OpCodes.Psi: {
|
---|
370 | var x = Evaluate(dataset, ref row, state);
|
---|
371 | if (double.IsNaN(x)) return double.NaN;
|
---|
372 | else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN;
|
---|
373 | return alglib.psi(x);
|
---|
374 | }
|
---|
375 | case OpCodes.Dawson: {
|
---|
376 | var x = Evaluate(dataset, ref row, state);
|
---|
377 | if (double.IsNaN(x)) return double.NaN;
|
---|
378 | return alglib.dawsonintegral(x);
|
---|
379 | }
|
---|
380 | case OpCodes.ExponentialIntegralEi: {
|
---|
381 | var x = Evaluate(dataset, ref row, state);
|
---|
382 | if (double.IsNaN(x)) return double.NaN;
|
---|
383 | return alglib.exponentialintegralei(x);
|
---|
384 | }
|
---|
385 | case OpCodes.SineIntegral: {
|
---|
386 | double si, ci;
|
---|
387 | var x = Evaluate(dataset, ref row, state);
|
---|
388 | if (double.IsNaN(x)) return double.NaN;
|
---|
389 | else {
|
---|
390 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
391 | return si;
|
---|
392 | }
|
---|
393 | }
|
---|
394 | case OpCodes.CosineIntegral: {
|
---|
395 | double si, ci;
|
---|
396 | var x = Evaluate(dataset, ref row, state);
|
---|
397 | if (double.IsNaN(x)) return double.NaN;
|
---|
398 | else {
|
---|
399 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
400 | return ci;
|
---|
401 | }
|
---|
402 | }
|
---|
403 | case OpCodes.HyperbolicSineIntegral: {
|
---|
404 | double shi, chi;
|
---|
405 | var x = Evaluate(dataset, ref row, state);
|
---|
406 | if (double.IsNaN(x)) return double.NaN;
|
---|
407 | else {
|
---|
408 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
409 | return shi;
|
---|
410 | }
|
---|
411 | }
|
---|
412 | case OpCodes.HyperbolicCosineIntegral: {
|
---|
413 | double shi, chi;
|
---|
414 | var x = Evaluate(dataset, ref row, state);
|
---|
415 | if (double.IsNaN(x)) return double.NaN;
|
---|
416 | else {
|
---|
417 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
418 | return chi;
|
---|
419 | }
|
---|
420 | }
|
---|
421 | case OpCodes.FresnelCosineIntegral: {
|
---|
422 | double c = 0, s = 0;
|
---|
423 | var x = Evaluate(dataset, ref row, state);
|
---|
424 | if (double.IsNaN(x)) return double.NaN;
|
---|
425 | else {
|
---|
426 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
427 | return c;
|
---|
428 | }
|
---|
429 | }
|
---|
430 | case OpCodes.FresnelSineIntegral: {
|
---|
431 | double c = 0, s = 0;
|
---|
432 | var x = Evaluate(dataset, ref row, state);
|
---|
433 | if (double.IsNaN(x)) return double.NaN;
|
---|
434 | else {
|
---|
435 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
436 | return s;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | case OpCodes.AiryA: {
|
---|
440 | double ai, aip, bi, bip;
|
---|
441 | var x = Evaluate(dataset, ref row, state);
|
---|
442 | if (double.IsNaN(x)) return double.NaN;
|
---|
443 | else {
|
---|
444 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
445 | return ai;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | case OpCodes.AiryB: {
|
---|
449 | double ai, aip, bi, bip;
|
---|
450 | var x = Evaluate(dataset, ref row, state);
|
---|
451 | if (double.IsNaN(x)) return double.NaN;
|
---|
452 | else {
|
---|
453 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
454 | return bi;
|
---|
455 | }
|
---|
456 | }
|
---|
457 | case OpCodes.Norm: {
|
---|
458 | var x = Evaluate(dataset, ref row, state);
|
---|
459 | if (double.IsNaN(x)) return double.NaN;
|
---|
460 | else return alglib.normaldistribution(x);
|
---|
461 | }
|
---|
462 | case OpCodes.Erf: {
|
---|
463 | var x = Evaluate(dataset, ref row, state);
|
---|
464 | if (double.IsNaN(x)) return double.NaN;
|
---|
465 | else return alglib.errorfunction(x);
|
---|
466 | }
|
---|
467 | case OpCodes.Bessel: {
|
---|
468 | var x = Evaluate(dataset, ref row, state);
|
---|
469 | if (double.IsNaN(x)) return double.NaN;
|
---|
470 | else return alglib.besseli0(x);
|
---|
471 | }
|
---|
472 | case OpCodes.IfThenElse: {
|
---|
473 | double condition = Evaluate(dataset, ref row, state);
|
---|
474 | double result;
|
---|
475 | if (condition > 0.0) {
|
---|
476 | result = Evaluate(dataset, ref row, state); SkipInstructions(state);
|
---|
477 | } else {
|
---|
478 | SkipInstructions(state); result = Evaluate(dataset, ref row, state);
|
---|
479 | }
|
---|
480 | return result;
|
---|
481 | }
|
---|
482 | case OpCodes.AND: {
|
---|
483 | double result = Evaluate(dataset, ref row, state);
|
---|
484 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
485 | if (result > 0.0) result = Evaluate(dataset, ref row, state);
|
---|
486 | else {
|
---|
487 | SkipInstructions(state);
|
---|
488 | }
|
---|
489 | }
|
---|
490 | return result > 0.0 ? 1.0 : -1.0;
|
---|
491 | }
|
---|
492 | case OpCodes.OR: {
|
---|
493 | double result = Evaluate(dataset, ref row, state);
|
---|
494 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
495 | if (result <= 0.0) result = Evaluate(dataset, ref row, state);
|
---|
496 | else {
|
---|
497 | SkipInstructions(state);
|
---|
498 | }
|
---|
499 | }
|
---|
500 | return result > 0.0 ? 1.0 : -1.0;
|
---|
501 | }
|
---|
502 | case OpCodes.NOT: {
|
---|
503 | return Evaluate(dataset, ref row, state) > 0.0 ? -1.0 : 1.0;
|
---|
504 | }
|
---|
505 | case OpCodes.GT: {
|
---|
506 | double x = Evaluate(dataset, ref row, state);
|
---|
507 | double y = Evaluate(dataset, ref row, state);
|
---|
508 | if (x > y) return 1.0;
|
---|
509 | else return -1.0;
|
---|
510 | }
|
---|
511 | case OpCodes.LT: {
|
---|
512 | double x = Evaluate(dataset, ref row, state);
|
---|
513 | double y = Evaluate(dataset, ref row, state);
|
---|
514 | if (x < y) return 1.0;
|
---|
515 | else return -1.0;
|
---|
516 | }
|
---|
517 | case OpCodes.TimeLag: {
|
---|
518 | var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
|
---|
519 | row += timeLagTreeNode.Lag;
|
---|
520 | double result = Evaluate(dataset, ref row, state);
|
---|
521 | row -= timeLagTreeNode.Lag;
|
---|
522 | return result;
|
---|
523 | }
|
---|
524 | case OpCodes.Integral: {
|
---|
525 | int savedPc = state.ProgramCounter;
|
---|
526 | var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
|
---|
527 | double sum = 0.0;
|
---|
528 | for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); i++) {
|
---|
529 | row += Math.Sign(timeLagTreeNode.Lag);
|
---|
530 | sum += Evaluate(dataset, ref row, state);
|
---|
531 | state.ProgramCounter = savedPc;
|
---|
532 | }
|
---|
533 | row -= timeLagTreeNode.Lag;
|
---|
534 | sum += Evaluate(dataset, ref row, state);
|
---|
535 | return sum;
|
---|
536 | }
|
---|
537 |
|
---|
538 | //mkommend: derivate calculation taken from:
|
---|
539 | //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
|
---|
540 | //one sided smooth differentiatior, N = 4
|
---|
541 | // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
|
---|
542 | case OpCodes.Derivative: {
|
---|
543 | int savedPc = state.ProgramCounter;
|
---|
544 | double f_0 = Evaluate(dataset, ref row, state); row--;
|
---|
545 | state.ProgramCounter = savedPc;
|
---|
546 | double f_1 = Evaluate(dataset, ref row, state); row -= 2;
|
---|
547 | state.ProgramCounter = savedPc;
|
---|
548 | double f_3 = Evaluate(dataset, ref row, state); row--;
|
---|
549 | state.ProgramCounter = savedPc;
|
---|
550 | double f_4 = Evaluate(dataset, ref row, state);
|
---|
551 | row += 4;
|
---|
552 |
|
---|
553 | return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
|
---|
554 | }
|
---|
555 | case OpCodes.Call: {
|
---|
556 | // evaluate sub-trees
|
---|
557 | double[] argValues = new double[currentInstr.nArguments];
|
---|
558 | for (int i = 0; i < currentInstr.nArguments; i++) {
|
---|
559 | argValues[i] = Evaluate(dataset, ref row, state);
|
---|
560 | }
|
---|
561 | // push on argument values on stack
|
---|
562 | state.CreateStackFrame(argValues);
|
---|
563 |
|
---|
564 | // save the pc
|
---|
565 | int savedPc = state.ProgramCounter;
|
---|
566 | // set pc to start of function
|
---|
567 | state.ProgramCounter = (ushort)currentInstr.iArg0;
|
---|
568 | // evaluate the function
|
---|
569 | double v = Evaluate(dataset, ref row, state);
|
---|
570 |
|
---|
571 | // delete the stack frame
|
---|
572 | state.RemoveStackFrame();
|
---|
573 |
|
---|
574 | // restore the pc => evaluation will continue at point after my subtrees
|
---|
575 | state.ProgramCounter = savedPc;
|
---|
576 | return v;
|
---|
577 | }
|
---|
578 | case OpCodes.Arg: {
|
---|
579 | return state.GetStackFrameValue((ushort)currentInstr.iArg0);
|
---|
580 | }
|
---|
581 | case OpCodes.Variable: {
|
---|
582 | if (row < 0 || row >= dataset.Rows)
|
---|
583 | return double.NaN;
|
---|
584 | var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
|
---|
585 | return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
|
---|
586 | }
|
---|
587 | case OpCodes.LagVariable: {
|
---|
588 | var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
|
---|
589 | int actualRow = row + laggedVariableTreeNode.Lag;
|
---|
590 | if (actualRow < 0 || actualRow >= dataset.Rows)
|
---|
591 | return double.NaN;
|
---|
592 | return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
|
---|
593 | }
|
---|
594 | case OpCodes.Constant: {
|
---|
595 | var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
|
---|
596 | return constTreeNode.Value;
|
---|
597 | }
|
---|
598 |
|
---|
599 | //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
|
---|
600 | //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
|
---|
601 | case OpCodes.VariableCondition: {
|
---|
602 | if (row < 0 || row >= dataset.Rows)
|
---|
603 | return double.NaN;
|
---|
604 | var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
|
---|
605 | double variableValue = ((IList<double>)currentInstr.iArg0)[row];
|
---|
606 | double x = variableValue - variableConditionTreeNode.Threshold;
|
---|
607 | double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
|
---|
608 |
|
---|
609 | double trueBranch = Evaluate(dataset, ref row, state);
|
---|
610 | double falseBranch = Evaluate(dataset, ref row, state);
|
---|
611 |
|
---|
612 | return trueBranch * p + falseBranch * (1 - p);
|
---|
613 | }
|
---|
614 | default: throw new NotSupportedException();
|
---|
615 | }
|
---|
616 | }
|
---|
617 |
|
---|
618 | private byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
|
---|
619 | if (symbolToOpcode.ContainsKey(treeNode.Symbol.GetType()))
|
---|
620 | return symbolToOpcode[treeNode.Symbol.GetType()];
|
---|
621 | else
|
---|
622 | throw new NotSupportedException("Symbol: " + treeNode.Symbol);
|
---|
623 | }
|
---|
624 |
|
---|
625 | // skips a whole branch
|
---|
626 | private void SkipInstructions(InterpreterState state) {
|
---|
627 | int i = 1;
|
---|
628 | while (i > 0) {
|
---|
629 | i += state.NextInstruction().nArguments;
|
---|
630 | i--;
|
---|
631 | }
|
---|
632 | }
|
---|
633 | }
|
---|
634 | }
|
---|