Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/SimpleArithmeticExpressionInterpreter.cs @ 6675

Last change on this file since 6675 was 5467, checked in by gkronber, 14 years ago

#1325: Merged r5060 from branch into trunk.

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