Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/22/19 16:31:11 (5 years ago)
Author:
pfleck
Message:

#3040

  • Added VectorVariable and its corresponding TreeNode.
  • Added symbols for basic vector operations and symbols for sum and avg of vectors.
  • Added a VectorEvaluate method in the SymbolicDataAnalysisExpressionTreeInterpreter that returns a DoubleVector instead of a double that is also called within the regular Evaluate method.
Location:
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs

    r17180 r17367  
    7979    CubeRoot = 51,
    8080    Tanh = 52,
     81    VectorVariable = 53,
     82    VectorAdd = 54,
     83    VectorSub = 55,
     84    VectorMul = 56,
     85    VectorDiv = 57,
     86    VectorSum = 58,
     87    VectorAvg = 59
    8188  };
    8289  public static class OpCodes {
    8390    // constants for API compatibility only
    8491    public const byte Add = (byte)OpCode.Add;
    85     public const byte Sub =(byte)OpCode.Sub;
    86     public const byte Mul =(byte)OpCode.Mul;
    87     public const byte Div =(byte)OpCode.Div;
    88     public const byte Sin =(byte)OpCode.Sin;
    89     public const byte Cos =(byte)OpCode.Cos;
    90     public const byte Tan =(byte)OpCode.Tan;
    91     public const byte Log =(byte)OpCode.Log;
     92    public const byte Sub = (byte)OpCode.Sub;
     93    public const byte Mul = (byte)OpCode.Mul;
     94    public const byte Div = (byte)OpCode.Div;
     95    public const byte Sin = (byte)OpCode.Sin;
     96    public const byte Cos = (byte)OpCode.Cos;
     97    public const byte Tan = (byte)OpCode.Tan;
     98    public const byte Log = (byte)OpCode.Log;
    9299    public const byte Exp = (byte)OpCode.Exp;
    93100    public const byte IfThenElse = (byte)OpCode.IfThenElse;
     
    134141    public const byte CubeRoot = (byte)OpCode.CubeRoot;
    135142    public const byte Tanh = (byte)OpCode.Tanh;
     143    public const byte VectorVariable = (byte)OpCode.VectorVariable;
     144    public const byte VectorAdd = (byte)OpCode.VectorAdd;
     145    public const byte VectorSub = (byte)OpCode.VectorSub;
     146    public const byte VectorMul = (byte)OpCode.VectorMul;
     147    public const byte VectorDiv = (byte)OpCode.VectorDiv;
     148    public const byte VectorSum = (byte)OpCode.VectorSum;
     149    public const byte VectorAvg = (byte)OpCode.VectorAvg;
    136150
    137151
    138152    private static Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
    139        { typeof(Addition), OpCodes.Add },
     153      { typeof(Addition), OpCodes.Add },
    140154      { typeof(Subtraction), OpCodes.Sub },
    141155      { typeof(Multiplication), OpCodes.Mul },
     
    189203      { typeof(AnalyticQuotient), OpCodes.AnalyticQuotient },
    190204      { typeof(Cube), OpCodes.Cube },
    191       { typeof(CubeRoot), OpCodes.CubeRoot }
     205      { typeof(CubeRoot), OpCodes.CubeRoot },
     206      { typeof(VectorVariable), OpCodes.VectorVariable },
     207      { typeof(VectorAddition), OpCodes.VectorAdd },
     208      { typeof(VectorSubtraction), OpCodes.VectorSub },
     209      { typeof(VectorMultiplication), OpCodes.VectorMul },
     210      { typeof(VectorDivision), OpCodes.VectorDiv },
     211      { typeof(VectorSum), OpCodes.VectorSum },
     212      { typeof(VectorAverage), OpCodes.VectorAvg }
    192213    };
    193214
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r17180 r17367  
    149149          var factorTreeNode = instr.dynamicNode as BinaryFactorVariableTreeNode;
    150150          instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
     151        } else if (instr.opCode == OpCodes.VectorVariable) {
     152          var vectorVariableTreeNode = (VectorVariableTreeNode)instr.dynamicNode;
     153          instr.data = dataset.GetReadOnlyDoubleVectorValues(vectorVariableTreeNode.VariableName);
    151154        } else if (instr.opCode == OpCodes.LagVariable) {
    152155          var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
     
    528531            }
    529532          }
     533
     534        case OpCodes.VectorSum: {
     535            DoubleVector v = VectorEvaluate(dataset, ref row, state);
     536            return v.Sum();
     537          }
     538        case OpCodes.VectorAvg: {
     539            DoubleVector v = VectorEvaluate(dataset, ref row, state);
     540            return v.Average();
     541          }
     542
    530543        default:
    531544          throw new NotSupportedException();
    532545      }
    533546    }
     547
     548    public virtual DoubleVector VectorEvaluate(IDataset dataset, ref int row, InterpreterState state) {
     549      Instruction currentInstr = state.NextInstruction();
     550      switch (currentInstr.opCode) {
     551        case OpCodes.VectorAdd: {
     552            DoubleVector s = VectorEvaluate(dataset, ref row, state);
     553            for (int i = 1; i < currentInstr.nArguments; i++) {
     554              s += Evaluate(dataset, ref row, state);
     555            }
     556            return s;
     557          }
     558        case OpCodes.VectorSub: {
     559            DoubleVector s = VectorEvaluate(dataset, ref row, state);
     560            for (int i = 1; i < currentInstr.nArguments; i++) {
     561              s -= Evaluate(dataset, ref row, state);
     562            }
     563            return s;
     564          }
     565        case OpCodes.VectorMul: {
     566            DoubleVector s = VectorEvaluate(dataset, ref row, state);
     567            for (int i = 1; i < currentInstr.nArguments; i++) {
     568              s *= Evaluate(dataset, ref row, state);
     569            }
     570            return s;
     571          }
     572        case OpCodes.VectorDiv: {
     573            DoubleVector s = VectorEvaluate(dataset, ref row, state);
     574            for (int i = 1; i < currentInstr.nArguments; i++) {
     575              s /= Evaluate(dataset, ref row, state);
     576            }
     577            return s;
     578          }
     579
     580        case OpCodes.VectorVariable: {
     581            if (row < 0 || row >= dataset.Rows) return new DoubleVector(new[] { double.NaN });
     582            var vectorVarTreeNode = currentInstr.dynamicNode as VectorVariableTreeNode;
     583            return ((IList<DoubleVector>)currentInstr.data)[row] * vectorVarTreeNode.Weight;
     584          }
     585        default:
     586          throw new NotSupportedException();
     587      }
     588    }
    534589  }
    535590}
Note: See TracChangeset for help on using the changeset viewer.