Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5295 was 5288, checked in by mkommend, 14 years ago

Implemented Power symbol for GP (ticket #1374).

File size: 13.1 KB
RevLine 
[3253]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
[4068]23using System.Collections.Generic;
[4722]24using HeuristicLab.Common;
[3253]25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[4068]27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Compiler;
[3462]28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
[4068]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[3373]30using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
[3253]31
[3373]32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[3253]33  [StorableClass]
[3462]34  [Item("SimpleArithmeticExpressionInterpreter", "Interpreter for arithmetic symbolic expression trees including function calls.")]
[4722]35  public sealed class SimpleArithmeticExpressionInterpreter : NamedItem, ISymbolicExpressionTreeInterpreter {
[3462]36    private class OpCodes {
37      public const byte Add = 1;
38      public const byte Sub = 2;
39      public const byte Mul = 3;
40      public const byte Div = 4;
[3841]41
42      public const byte Sin = 5;
43      public const byte Cos = 6;
44      public const byte Tan = 7;
45
46      public const byte Log = 8;
47      public const byte Exp = 9;
48
49      public const byte IfThenElse = 10;
50
51      public const byte GT = 11;
52      public const byte LT = 12;
53
54      public const byte AND = 13;
55      public const byte OR = 14;
56      public const byte NOT = 15;
57
58
59      public const byte Average = 16;
60
61      public const byte Call = 17;
62
63      public const byte Variable = 18;
64      public const byte LagVariable = 19;
65      public const byte Constant = 20;
66      public const byte Arg = 21;
[5288]67
68      public const byte Power = 22;
[3462]69    }
70
[3841]71    private Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
72      { typeof(Addition), OpCodes.Add },
73      { typeof(Subtraction), OpCodes.Sub },
74      { typeof(Multiplication), OpCodes.Mul },
75      { typeof(Division), OpCodes.Div },
76      { typeof(Sine), OpCodes.Sin },
77      { typeof(Cosine), OpCodes.Cos },
78      { typeof(Tangent), OpCodes.Tan },
79      { typeof(Logarithm), OpCodes.Log },
80      { typeof(Exponential), OpCodes.Exp },
81      { typeof(IfThenElse), OpCodes.IfThenElse },
82      { typeof(GreaterThan), OpCodes.GT },
83      { typeof(LessThan), OpCodes.LT },
84      { typeof(And), OpCodes.AND },
85      { typeof(Or), OpCodes.OR },
86      { typeof(Not), OpCodes.NOT},
87      { typeof(Average), OpCodes.Average},
88      { typeof(InvokeFunction), OpCodes.Call },
89      { typeof(HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable), OpCodes.Variable },
90      { typeof(LaggedVariable), OpCodes.LagVariable },
91      { typeof(Constant), OpCodes.Constant },
92      { typeof(Argument), OpCodes.Arg },
[5288]93      { typeof(Power),OpCodes.Power},
[3841]94    };
[3491]95    private const int ARGUMENT_STACK_SIZE = 1024;
[3513]96
[3545]97    public override bool CanChangeName {
98      get { return false; }
99    }
100    public override bool CanChangeDescription {
101      get { return false; }
102    }
103
[4722]104    [StorableConstructor]
105    private SimpleArithmeticExpressionInterpreter(bool deserializing) : base(deserializing) { }
106    private SimpleArithmeticExpressionInterpreter(SimpleArithmeticExpressionInterpreter original, Cloner cloner) : base(original, cloner) { }
107    public override IDeepCloneable Clone(Cloner cloner) {
108      return new SimpleArithmeticExpressionInterpreter(this, cloner);
109    }
110
[3513]111    public SimpleArithmeticExpressionInterpreter()
112      : base() {
113    }
114
[3462]115    public IEnumerable<double> GetSymbolicExpressionTreeValues(SymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) {
[3294]116      var compiler = new SymbolicExpressionTreeCompiler();
[5223]117      Instruction[] code = compiler.Compile(tree, MapSymbolToOpCode);
118
119      for (int i = 0; i < code.Length; i++) {
120        Instruction instr = code[i];
121        if (instr.opCode == OpCodes.Variable) {
122          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
123          instr.iArg0 = (ushort)dataset.GetVariableIndex(variableTreeNode.VariableName);
124          code[i] = instr;
125        } else if (instr.opCode == OpCodes.LagVariable) {
126          var variableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;
127          instr.iArg0 = (ushort)dataset.GetVariableIndex(variableTreeNode.VariableName);
128          code[i] = instr;
129        }
[3253]130      }
131
[5223]132      double[] argumentStack = new double[ARGUMENT_STACK_SIZE];
133      foreach (var rowEnum in rows) {
134        int row = rowEnum;
135        int pc = 0;
136        int argStackPointer = 0;
137        yield return Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]138      }
[3462]139    }
140
[5223]141    private double Evaluate(Dataset dataset, ref int row, Instruction[] code, ref int pc, double[] argumentStack, ref int argStackPointer) {
[4022]142      Instruction currentInstr = code[pc++];
[3462]143      switch (currentInstr.opCode) {
144        case OpCodes.Add: {
[5223]145            double s = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3996]146            for (int i = 1; i < currentInstr.nArguments; i++) {
[5223]147              s += Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3294]148            }
149            return s;
150          }
[3462]151        case OpCodes.Sub: {
[5223]152            double s = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3294]153            for (int i = 1; i < currentInstr.nArguments; i++) {
[5223]154              s -= Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3294]155            }
[3733]156            if (currentInstr.nArguments == 1) s = -s;
[3294]157            return s;
158          }
[3462]159        case OpCodes.Mul: {
[5223]160            double p = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3294]161            for (int i = 1; i < currentInstr.nArguments; i++) {
[5223]162              p *= Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3294]163            }
164            return p;
165          }
[3462]166        case OpCodes.Div: {
[5223]167            double p = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3294]168            for (int i = 1; i < currentInstr.nArguments; i++) {
[5223]169              p /= Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3294]170            }
[3733]171            if (currentInstr.nArguments == 1) p = 1.0 / p;
[3294]172            return p;
173          }
[3841]174        case OpCodes.Average: {
[5223]175            double sum = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]176            for (int i = 1; i < currentInstr.nArguments; i++) {
[5223]177              sum += Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]178            }
179            return sum / currentInstr.nArguments;
180          }
181        case OpCodes.Cos: {
[5223]182            return Math.Cos(Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer));
[3841]183          }
184        case OpCodes.Sin: {
[5223]185            return Math.Sin(Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer));
[3841]186          }
187        case OpCodes.Tan: {
[5223]188            return Math.Tan(Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer));
[3841]189          }
[5288]190        case OpCodes.Power: {
191            double x = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
192            double y = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
193            return Math.Pow(x, y);
194          }
[3841]195        case OpCodes.Exp: {
[5223]196            return Math.Exp(Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer));
[3841]197          }
198        case OpCodes.Log: {
[5223]199            return Math.Log(Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer));
[3841]200          }
201        case OpCodes.IfThenElse: {
[5223]202            double condition = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]203            double result;
204            if (condition > 0.0) {
[5223]205              result = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer); SkipBakedCode(code, ref pc);
[3841]206            } else {
[5223]207              SkipBakedCode(code, ref pc); result = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]208            }
209            return result;
210          }
211        case OpCodes.AND: {
[5223]212            double result = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]213            for (int i = 1; i < currentInstr.nArguments; i++) {
[5223]214              if (result <= 0.0) SkipBakedCode(code, ref pc);
[3841]215              else {
[5223]216                result = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]217              }
218            }
219            return result <= 0.0 ? -1.0 : 1.0;
220          }
221        case OpCodes.OR: {
[5223]222            double result = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]223            for (int i = 1; i < currentInstr.nArguments; i++) {
[5223]224              if (result > 0.0) SkipBakedCode(code, ref pc);
[3841]225              else {
[5223]226                result = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]227              }
228            }
229            return result > 0.0 ? 1.0 : -1.0;
230          }
231        case OpCodes.NOT: {
[5223]232            return -Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]233          }
234        case OpCodes.GT: {
[5223]235            double x = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
236            double y = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]237            if (x > y) return 1.0;
238            else return -1.0;
239          }
240        case OpCodes.LT: {
[5223]241            double x = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
242            double y = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3841]243            if (x < y) return 1.0;
244            else return -1.0;
245          }
[3462]246        case OpCodes.Call: {
[3409]247            // evaluate sub-trees
[3491]248            // push on argStack in reverse order
[3409]249            for (int i = 0; i < currentInstr.nArguments; i++) {
[5223]250              argumentStack[argStackPointer + currentInstr.nArguments - i] = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3409]251            }
[3747]252            argStackPointer += currentInstr.nArguments;
[3491]253
[3409]254            // save the pc
255            int nextPc = pc;
256            // set pc to start of function 
257            pc = currentInstr.iArg0;
258            // evaluate the function
[5223]259            double v = Evaluate(dataset, ref row, code, ref pc, argumentStack, ref argStackPointer);
[3491]260
261            // decrease the argument stack pointer by the number of arguments pushed
262            // to set the argStackPointer back to the original location
263            argStackPointer -= currentInstr.nArguments;
264
[3409]265            // restore the pc => evaluation will continue at point after my subtrees 
266            pc = nextPc;
267            return v;
268          }
[3462]269        case OpCodes.Arg: {
[3491]270            return argumentStack[argStackPointer - currentInstr.iArg0];
[3409]271          }
[3462]272        case OpCodes.Variable: {
[3373]273            var variableTreeNode = currentInstr.dynamicNode as VariableTreeNode;
[3462]274            return dataset[row, currentInstr.iArg0] * variableTreeNode.Weight;
275          }
[3841]276        case OpCodes.LagVariable: {
[5223]277            var laggedVariableTreeNode = currentInstr.dynamicNode as LaggedVariableTreeNode;
278            int actualRow = row + laggedVariableTreeNode.Lag;
[3841]279            if (actualRow < 0 || actualRow >= dataset.Rows) throw new ArgumentException("Out of range access to dataset row: " + row);
[5223]280            return dataset[actualRow, currentInstr.iArg0] * laggedVariableTreeNode.Weight;
[3841]281          }
[3462]282        case OpCodes.Constant: {
[3373]283            var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
[3462]284            return constTreeNode.Value;
[3294]285          }
286        default: throw new NotSupportedException();
[3253]287      }
288    }
[3841]289
[5223]290    private byte MapSymbolToOpCode(SymbolicExpressionTreeNode treeNode) {
291      if (symbolToOpcode.ContainsKey(treeNode.Symbol.GetType()))
292        return symbolToOpcode[treeNode.Symbol.GetType()];
293      else
294        throw new NotSupportedException("Symbol: " + treeNode.Symbol);
295    }
296
[3841]297    // skips a whole branch
[5223]298    private void SkipBakedCode(Instruction[] code, ref int pc) {
[3841]299      int i = 1;
300      while (i > 0) {
301        i += code[pc++].nArguments;
302        i--;
303      }
304    }
[3253]305  }
306}
Note: See TracBrowser for help on using the repository browser.