Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Crossovers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs @ 7489

Last change on this file since 7489 was 7486, checked in by mkommend, 12 years ago

#1682: Added forgotten AfterDeserializationHook in the SymbolicDataAnalysisExpressionTreeInterpreter to add the EvaluatedSolutionsParameter if it is not present.

File size: 19.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace 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    #endregion
137
138    private Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
139      { typeof(Addition), OpCodes.Add },
140      { typeof(Subtraction), OpCodes.Sub },
141      { typeof(Multiplication), OpCodes.Mul },
142      { typeof(Division), OpCodes.Div },
143      { typeof(Sine), OpCodes.Sin },
144      { typeof(Cosine), OpCodes.Cos },
145      { typeof(Tangent), OpCodes.Tan },
146      { typeof(Logarithm), OpCodes.Log },
147      { typeof(Exponential), OpCodes.Exp },
148      { typeof(IfThenElse), OpCodes.IfThenElse },
149      { typeof(GreaterThan), OpCodes.GT },
150      { typeof(LessThan), OpCodes.LT },
151      { typeof(And), OpCodes.AND },
152      { typeof(Or), OpCodes.OR },
153      { typeof(Not), OpCodes.NOT},
154      { typeof(Average), OpCodes.Average},
155      { typeof(InvokeFunction), OpCodes.Call },
156      { typeof(HeuristicLab.Problems.DataAnalysis.Symbolic.Variable), OpCodes.Variable },
157      { typeof(LaggedVariable), OpCodes.LagVariable },
158      { typeof(Constant), OpCodes.Constant },
159      { typeof(Argument), OpCodes.Arg },
160      { typeof(Power),OpCodes.Power},
161      { typeof(Root),OpCodes.Root},
162      { typeof(TimeLag), OpCodes.TimeLag},
163      { typeof(Integral), OpCodes.Integral},
164      { typeof(Derivative), OpCodes.Derivative},
165      { typeof(VariableCondition),OpCodes.VariableCondition}
166    };
167
168    public override bool CanChangeName {
169      get { return false; }
170    }
171    public override bool CanChangeDescription {
172      get { return false; }
173    }
174
175    #region parameter properties
176    public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
177      get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
178    }
179
180    public IValueParameter<IntValue> EvaluatedSolutionsParameter {
181      get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
182    }
183    #endregion
184
185    #region properties
186    public BoolValue CheckExpressionsWithIntervalArithmetic {
187      get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
188      set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
189    }
190
191    public IntValue EvaluatedSolutions {
192      get { return EvaluatedSolutionsParameter.Value; }
193      set { EvaluatedSolutionsParameter.Value = value; }
194    }
195    #endregion
196
197    [StorableConstructor]
198    private SymbolicDataAnalysisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { }
199    private SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
200    public override IDeepCloneable Clone(Cloner cloner) {
201      return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner);
202    }
203
204    public SymbolicDataAnalysisExpressionTreeInterpreter()
205      : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
206      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)));
207      Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
208    }
209
210    [StorableHook(HookType.AfterDeserialization)]
211    private void AfterDeserialization() {
212      if (!Parameters.ContainsKey(EvaluatedSolutionsParameterName))
213        Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
214    }
215
216    #region IStatefulItem
217    public void InitializeState() {
218      EvaluatedSolutions.Value = 0;
219    }
220
221    public void ClearState() {
222    }
223    #endregion
224
225    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) {
226      EvaluatedSolutions.Value++; // increment the evaluated solutions counter
227      if (CheckExpressionsWithIntervalArithmetic.Value)
228        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
229      var compiler = new SymbolicExpressionTreeCompiler();
230      Instruction[] code = compiler.Compile(tree, MapSymbolToOpCode);
231      int necessaryArgStackSize = 0;
232      for (int i = 0; i < code.Length; i++) {
233        Instruction instr = code[i];
234        if (instr.opCode == OpCodes.Variable) {
235          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
236          instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
237          code[i] = instr;
238        } else if (instr.opCode == OpCodes.LagVariable) {
239          var laggedVariableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;
240          instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
241          code[i] = instr;
242        } else if (instr.opCode == OpCodes.VariableCondition) {
243          var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
244          instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
245        } else if (instr.opCode == OpCodes.Call) {
246          necessaryArgStackSize += instr.nArguments + 1;
247        }
248      }
249      var state = new InterpreterState(code, necessaryArgStackSize);
250
251      foreach (var rowEnum in rows) {
252        int row = rowEnum;
253        state.Reset();
254        yield return Evaluate(dataset, ref row, state);
255      }
256    }
257
258    private double Evaluate(Dataset dataset, ref int row, InterpreterState state) {
259      Instruction currentInstr = state.NextInstruction();
260      switch (currentInstr.opCode) {
261        case OpCodes.Add: {
262            double s = Evaluate(dataset, ref row, state);
263            for (int i = 1; i < currentInstr.nArguments; i++) {
264              s += Evaluate(dataset, ref row, state);
265            }
266            return s;
267          }
268        case OpCodes.Sub: {
269            double s = Evaluate(dataset, ref row, state);
270            for (int i = 1; i < currentInstr.nArguments; i++) {
271              s -= Evaluate(dataset, ref row, state);
272            }
273            if (currentInstr.nArguments == 1) s = -s;
274            return s;
275          }
276        case OpCodes.Mul: {
277            double p = Evaluate(dataset, ref row, state);
278            for (int i = 1; i < currentInstr.nArguments; i++) {
279              p *= Evaluate(dataset, ref row, state);
280            }
281            return p;
282          }
283        case OpCodes.Div: {
284            double p = Evaluate(dataset, ref row, state);
285            for (int i = 1; i < currentInstr.nArguments; i++) {
286              p /= Evaluate(dataset, ref row, state);
287            }
288            if (currentInstr.nArguments == 1) p = 1.0 / p;
289            return p;
290          }
291        case OpCodes.Average: {
292            double sum = Evaluate(dataset, ref row, state);
293            for (int i = 1; i < currentInstr.nArguments; i++) {
294              sum += Evaluate(dataset, ref row, state);
295            }
296            return sum / currentInstr.nArguments;
297          }
298        case OpCodes.Cos: {
299            return Math.Cos(Evaluate(dataset, ref row, state));
300          }
301        case OpCodes.Sin: {
302            return Math.Sin(Evaluate(dataset, ref row, state));
303          }
304        case OpCodes.Tan: {
305            return Math.Tan(Evaluate(dataset, ref row, state));
306          }
307        case OpCodes.Power: {
308            double x = Evaluate(dataset, ref row, state);
309            double y = Math.Round(Evaluate(dataset, ref row, state));
310            return Math.Pow(x, y);
311          }
312        case OpCodes.Root: {
313            double x = Evaluate(dataset, ref row, state);
314            double y = Math.Round(Evaluate(dataset, ref row, state));
315            return Math.Pow(x, 1 / y);
316          }
317        case OpCodes.Exp: {
318            return Math.Exp(Evaluate(dataset, ref row, state));
319          }
320        case OpCodes.Log: {
321            return Math.Log(Evaluate(dataset, ref row, state));
322          }
323        case OpCodes.IfThenElse: {
324            double condition = Evaluate(dataset, ref row, state);
325            double result;
326            if (condition > 0.0) {
327              result = Evaluate(dataset, ref row, state); SkipInstructions(state);
328            } else {
329              SkipInstructions(state); result = Evaluate(dataset, ref row, state);
330            }
331            return result;
332          }
333        case OpCodes.AND: {
334            double result = Evaluate(dataset, ref row, state);
335            for (int i = 1; i < currentInstr.nArguments; i++) {
336              if (result > 0.0) result = Evaluate(dataset, ref row, state);
337              else {
338                SkipInstructions(state);
339              }
340            }
341            return result > 0.0 ? 1.0 : -1.0;
342          }
343        case OpCodes.OR: {
344            double result = Evaluate(dataset, ref row, state);
345            for (int i = 1; i < currentInstr.nArguments; i++) {
346              if (result <= 0.0) result = Evaluate(dataset, ref row, state);
347              else {
348                SkipInstructions(state);
349              }
350            }
351            return result > 0.0 ? 1.0 : -1.0;
352          }
353        case OpCodes.NOT: {
354            return Evaluate(dataset, ref row, state) > 0.0 ? -1.0 : 1.0;
355          }
356        case OpCodes.GT: {
357            double x = Evaluate(dataset, ref row, state);
358            double y = Evaluate(dataset, ref row, state);
359            if (x > y) return 1.0;
360            else return -1.0;
361          }
362        case OpCodes.LT: {
363            double x = Evaluate(dataset, ref row, state);
364            double y = Evaluate(dataset, ref row, state);
365            if (x < y) return 1.0;
366            else return -1.0;
367          }
368        case OpCodes.TimeLag: {
369            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
370            row += timeLagTreeNode.Lag;
371            double result = Evaluate(dataset, ref row, state);
372            row -= timeLagTreeNode.Lag;
373            return result;
374          }
375        case OpCodes.Integral: {
376            int savedPc = state.ProgramCounter;
377            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
378            double sum = 0.0;
379            for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); i++) {
380              row += Math.Sign(timeLagTreeNode.Lag);
381              sum += Evaluate(dataset, ref row, state);
382              state.ProgramCounter = savedPc;
383            }
384            row -= timeLagTreeNode.Lag;
385            sum += Evaluate(dataset, ref row, state);
386            return sum;
387          }
388
389        //mkommend: derivate calculation taken from:
390        //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
391        //one sided smooth differentiatior, N = 4
392        // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
393        case OpCodes.Derivative: {
394            int savedPc = state.ProgramCounter;
395            double f_0 = Evaluate(dataset, ref row, state); row--;
396            state.ProgramCounter = savedPc;
397            double f_1 = Evaluate(dataset, ref row, state); row -= 2;
398            state.ProgramCounter = savedPc;
399            double f_3 = Evaluate(dataset, ref row, state); row--;
400            state.ProgramCounter = savedPc;
401            double f_4 = Evaluate(dataset, ref row, state);
402            row += 4;
403
404            return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
405          }
406        case OpCodes.Call: {
407            // evaluate sub-trees
408            double[] argValues = new double[currentInstr.nArguments];
409            for (int i = 0; i < currentInstr.nArguments; i++) {
410              argValues[i] = Evaluate(dataset, ref row, state);
411            }
412            // push on argument values on stack
413            state.CreateStackFrame(argValues);
414
415            // save the pc
416            int savedPc = state.ProgramCounter;
417            // set pc to start of function 
418            state.ProgramCounter = (ushort)currentInstr.iArg0;
419            // evaluate the function
420            double v = Evaluate(dataset, ref row, state);
421
422            // delete the stack frame
423            state.RemoveStackFrame();
424
425            // restore the pc => evaluation will continue at point after my subtrees 
426            state.ProgramCounter = savedPc;
427            return v;
428          }
429        case OpCodes.Arg: {
430            return state.GetStackFrameValue((ushort)currentInstr.iArg0);
431          }
432        case OpCodes.Variable: {
433            if (row < 0 || row >= dataset.Rows)
434              return double.NaN;
435            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
436            return ((IList<double>)currentInstr.iArg0)[row] * 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            return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
444          }
445        case OpCodes.Constant: {
446            var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
447            return constTreeNode.Value;
448          }
449
450        //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
451        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
452        case OpCodes.VariableCondition: {
453            if (row < 0 || row >= dataset.Rows)
454              return double.NaN;
455            var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
456            double variableValue = ((IList<double>)currentInstr.iArg0)[row];
457            double x = variableValue - variableConditionTreeNode.Threshold;
458            double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
459
460            double trueBranch = Evaluate(dataset, ref row, state);
461            double falseBranch = Evaluate(dataset, ref row, state);
462
463            return trueBranch * p + falseBranch * (1 - p);
464          }
465        default: throw new NotSupportedException();
466      }
467    }
468
469    private byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
470      if (symbolToOpcode.ContainsKey(treeNode.Symbol.GetType()))
471        return symbolToOpcode[treeNode.Symbol.GetType()];
472      else
473        throw new NotSupportedException("Symbol: " + treeNode.Symbol);
474    }
475
476    // skips a whole branch
477    private void SkipInstructions(InterpreterState state) {
478      int i = 1;
479      while (i > 0) {
480        i += state.NextInstruction().nArguments;
481        i--;
482      }
483    }
484  }
485}
Note: See TracBrowser for help on using the repository browser.