Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17367


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
Files:
4 edited
8 copied

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}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VectorAddition.cs

    r17362 r17367  
    2525using HEAL.Attic;
    2626namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    27   [StorableType("483AAB50-D2E9-472E-A2FA-625F621B7CA9")]
    28   [Item("Addition", "Symbol that represents the + operator.")]
    29   public sealed class Addition : Symbol {
     27  [StorableType("FD0FD140-7AED-483F-ACFC-A65FFCD4216A")]
     28  [Item("VectorAddition", "Symbol that represents the + operator.")]
     29  public sealed class VectorAddition : Symbol {
    3030    private const int minimumArity = 1;
    3131    private const int maximumArity = byte.MaxValue;
     
    3939
    4040    [StorableConstructor]
    41     private Addition(StorableConstructorFlag _) : base(_) { }
    42     private Addition(Addition original, Cloner cloner) : base(original, cloner) { }
     41    private VectorAddition(StorableConstructorFlag _) : base(_) { }
     42    private VectorAddition(VectorAddition original, Cloner cloner) : base(original, cloner) { }
    4343    public override IDeepCloneable Clone(Cloner cloner) {
    44       return new Addition(this, cloner);
     44      return new VectorAddition(this, cloner);
    4545    }
    46     public Addition() : base("Addition", "Symbol that represents the + operator.") { }
     46    public VectorAddition() : base("VectorAddition", "Symbol that represents the + operator.") { }
    4747  }
    4848}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VectorAverage.cs

    r17362 r17367  
    2525using HEAL.Attic;
    2626namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    27   [StorableType("8234F316-1816-41C5-94A9-11A5F3D36A77")]
    28   [Item("Sine", "Symbol that represents the sine function.")]
    29   public sealed class Sine : Symbol {
     27  [StorableType("85FF9A2E-207C-4008-BB8B-0C0363885443")]
     28  [Item("VectorAverage", "Symbol that represents the average function.")]
     29  public sealed class VectorAverage : Symbol {
    3030    private const int minimumArity = 1;
    3131    private const int maximumArity = 1;
     
    3939
    4040    [StorableConstructor]
    41     private Sine(StorableConstructorFlag _) : base(_) { }
    42     private Sine(Sine original, Cloner cloner) : base(original, cloner) { }
     41    private VectorAverage(StorableConstructorFlag _) : base(_) { }
     42    private VectorAverage(VectorAverage original, Cloner cloner) : base(original, cloner) { }
    4343    public override IDeepCloneable Clone(Cloner cloner) {
    44       return new Sine(this, cloner);
     44      return new VectorAverage(this, cloner);
    4545    }
    46     public Sine() : base("Sine", "Symbol that represents the sine function.") { }
     46    public VectorAverage() : base("VectorAverage", "Symbol that represents the average function.") { }
    4747  }
    4848}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VectorDivision.cs

    r17362 r17367  
    2525using HEAL.Attic;
    2626namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    27   [StorableType("EE4A9CF4-483A-4671-AEA2-03E3DAF2F976")]
    28   [Item("Division", "Symbol that represents the / operator.")]
    29   public sealed class Division : Symbol {
     27  [StorableType("AC2706D7-40FF-4BB2-A7C9-1EE83EFF2D83")]
     28  [Item("VectorDivision", "Symbol that represents the / operator.")]
     29  public sealed class VectorDivision : Symbol {
    3030    private const int minimumArity = 1;
    3131    private const int maximumArity = byte.MaxValue;
     
    3939
    4040    [StorableConstructor]
    41     private Division(StorableConstructorFlag _) : base(_) { }
    42     private Division(Division original, Cloner cloner) : base(original, cloner) { }
     41    private VectorDivision(StorableConstructorFlag _) : base(_) { }
     42    private VectorDivision(VectorDivision original, Cloner cloner) : base(original, cloner) { }
    4343    public override IDeepCloneable Clone(Cloner cloner) {
    44       return new Division(this, cloner);
     44      return new VectorDivision(this, cloner);
    4545    }
    46     public Division() : base("Division", "Symbol that represents the / operator.") { }
     46    public VectorDivision() : base("VectorDivision", "Symbol that represents the / operator.") { }
    4747  }
    4848}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VectorMultiplication.cs

    r17362 r17367  
    2525using HEAL.Attic;
    2626namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    27   [StorableType("8F054D7F-82D0-400A-BAE3-8C90EA709C34")]
    28   [Item("Multiplication", "Symbol that represents the * operator.")]
    29   public sealed class Multiplication : Symbol {
     27  [StorableType("B3CD0680-162D-49F6-9D23-0F8730704050")]
     28  [Item("VectorMultiplication", "Symbol that represents the * operator.")]
     29  public sealed class VectorMultiplication : Symbol {
    3030    private const int minimumArity = 1;
    3131    private const int maximumArity = byte.MaxValue;
     
    3939
    4040    [StorableConstructor]
    41     private Multiplication(StorableConstructorFlag _) : base(_) { }
    42     private Multiplication(Multiplication original, Cloner cloner) : base(original, cloner) { }
     41    private VectorMultiplication(StorableConstructorFlag _) : base(_) { }
     42    private VectorMultiplication(VectorMultiplication original, Cloner cloner) : base(original, cloner) { }
    4343    public override IDeepCloneable Clone(Cloner cloner) {
    44       return new Multiplication(this, cloner);
     44      return new VectorMultiplication(this, cloner);
    4545    }
    46     public Multiplication() : base("Multiplication", "Symbol that represents the * operator.") { }
     46    public VectorMultiplication() : base("VectorMultiplication", "Symbol that represents the * operator.") { }
    4747  }
    4848}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VectorSubtraction.cs

    r17362 r17367  
    2525using HEAL.Attic;
    2626namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    27   [StorableType("F50263D4-2B07-42C8-8AB6-297A54AE00CC")]
    28   [Item("Subtraction", "Symbol that represents the - operator.")]
    29   public sealed class Subtraction : Symbol {
     27  [StorableType("8700841D-3303-42E5-A71A-A941B4CF707E")]
     28  [Item("VectorSubtraction", "Symbol that represents the - operator.")]
     29  public sealed class VectorSubtraction : Symbol {
    3030    private const int minimumArity = 1;
    3131    private const int maximumArity = byte.MaxValue;
     
    3939
    4040    [StorableConstructor]
    41     private Subtraction(StorableConstructorFlag _) : base(_) { }
    42     private Subtraction(Subtraction original, Cloner cloner) : base(original, cloner) { }
     41    private VectorSubtraction(StorableConstructorFlag _) : base(_) { }
     42    private VectorSubtraction(VectorSubtraction original, Cloner cloner) : base(original, cloner) { }
    4343    public override IDeepCloneable Clone(Cloner cloner) {
    44       return new Subtraction(this, cloner);
     44      return new VectorSubtraction(this, cloner);
    4545    }
    46     public Subtraction() : base("Subtraction", "Symbol that represents the - operator.") { }
     46    public VectorSubtraction() : base("VectorSubtraction", "Symbol that represents the - operator.") { }
    4747  }
    4848}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VectorSum.cs

    r17362 r17367  
    2525using HEAL.Attic;
    2626namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    27   [StorableType("8234F316-1816-41C5-94A9-11A5F3D36A77")]
    28   [Item("Sine", "Symbol that represents the sine function.")]
    29   public sealed class Sine : Symbol {
     27  [StorableType("FD700096-C848-4841-BAF3-B89C4200EE5C")]
     28  [Item("VectorSum", "Symbol that represents the sum function.")]
     29  public sealed class VectorSum : Symbol {
    3030    private const int minimumArity = 1;
    3131    private const int maximumArity = 1;
     
    3939
    4040    [StorableConstructor]
    41     private Sine(StorableConstructorFlag _) : base(_) { }
    42     private Sine(Sine original, Cloner cloner) : base(original, cloner) { }
     41    private VectorSum(StorableConstructorFlag _) : base(_) { }
     42    private VectorSum(VectorSum original, Cloner cloner) : base(original, cloner) { }
    4343    public override IDeepCloneable Clone(Cloner cloner) {
    44       return new Sine(this, cloner);
     44      return new VectorSum(this, cloner);
    4545    }
    46     public Sine() : base("Sine", "Symbol that represents the sine function.") { }
     46    public VectorSum() : base("VectorSum", "Symbol that represents the sum function.") { }
    4747  }
    4848}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VectorVariable.cs

    r17362 r17367  
    2525using HEAL.Attic;
    2626namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    27   [StorableType("9072AE0E-FA62-48D2-A416-F678D6CC2CBC")]
     27  [StorableType("B7E7393D-95BC-434D-862C-670C9FCF8328")]
    2828  [Item("Variable", "Represents a variable value.")]
    29   public sealed class Variable : VariableBase {
     29  public sealed class VectorVariable : VariableBase {
    3030
    3131    [StorableConstructor]
    32     private Variable(StorableConstructorFlag _) : base(_) {
     32    private VectorVariable(StorableConstructorFlag _) : base(_) {
    3333    }
    34     private Variable(Variable original, Cloner cloner)
     34    private VectorVariable(Variable original, Cloner cloner)
    3535      : base(original, cloner) {
    3636    }
    37     public Variable() : base("Variable", "Represents a variable value.") { }
    38     public Variable(string name, string description) : base(name, description) { }
     37    public VectorVariable() : base("VectorVariable", "Represents a variable value.") { }
     38    public VectorVariable(string name, string description) : base(name, description) { }
    3939
    4040    public override ISymbolicExpressionTreeNode CreateTreeNode() {
    41       return new VariableTreeNode(this);
     41      return new VectorVariableTreeNode(this);
    4242    }
    4343
    4444    public override IDeepCloneable Clone(Cloner cloner) {
    45       return new Variable(this, cloner);
     45      return new VectorVariable(this, cloner);
    4646    }
    4747  }
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VectorVariableTreeNode.cs

    r17362 r17367  
    2323using HEAL.Attic;
    2424namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    25   [StorableType("E60C4216-A6BA-48DE-BA66-389B9946B70A")]
    26   public sealed class VariableTreeNode : VariableTreeNodeBase {
    27     public new Variable Symbol {
    28       get { return (Variable)base.Symbol; }
     25  [StorableType("C0C60572-E014-43D5-B06C-53F3DE6FD54A")]
     26  public sealed class VectorVariableTreeNode : VariableTreeNodeBase {
     27    public new VectorVariable Symbol {
     28      get { return (VectorVariable)base.Symbol; }
    2929    }
    3030    [StorableConstructor]
    31     private VariableTreeNode(StorableConstructorFlag _) : base(_) { }
    32     private VariableTreeNode(VariableTreeNode original, Cloner cloner)
     31    private VectorVariableTreeNode(StorableConstructorFlag _) : base(_) { }
     32    private VectorVariableTreeNode(VectorVariableTreeNode original, Cloner cloner)
    3333      : base(original, cloner) {
    3434    }
    35     private VariableTreeNode() { }
    36     public VariableTreeNode(Variable variableSymbol) : base(variableSymbol) { }
     35    private VectorVariableTreeNode() { }
     36    public VectorVariableTreeNode(VectorVariable variableSymbol) : base(variableSymbol) { }
    3737
    3838    public override IDeepCloneable Clone(Cloner cloner) {
    39       return new VariableTreeNode(this, cloner);
     39      return new VectorVariableTreeNode(this, cloner);
    4040    }
    4141  }
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Vectors/DoubleVector.cs

    r17365 r17367  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
     24using System.Linq;
     25using System.Windows.Markup;
    2326using HEAL.Attic;
    2427
     
    3538      : base(_) { }
    3639
     40    public static DoubleVector operator +(DoubleVector lhs, DoubleVector rhs) {
     41      return new DoubleVector(VecCalc(lhs, rhs, (l, r) => l + r));
     42    }
     43    public static DoubleVector operator -(DoubleVector lhs, DoubleVector rhs) {
     44      return new DoubleVector(VecCalc(lhs, rhs, (l, r) => l - r));
     45    }
     46    public static DoubleVector operator *(DoubleVector lhs, DoubleVector rhs) {
     47      return new DoubleVector(VecCalc(lhs, rhs, (l, r) => l * r));
     48    }
     49    public static DoubleVector operator /(DoubleVector lhs, DoubleVector rhs) {
     50      return new DoubleVector(VecCalc(lhs, rhs, (l, r) => l / r));
     51    }
     52    private static IEnumerable<double> VecCalc(IReadOnlyList<double> lhs, IReadOnlyList<double> rhs, Func<double, double, double> resultFunc) {
     53      if (lhs.Count != rhs.Count) throw new InvalidOperationException("Vectors are not of equal length.");
     54      return lhs.Zip(rhs, resultFunc);
     55    }
     56
     57    public static DoubleVector operator +(DoubleVector lhs, double rhs) {
     58      return new DoubleVector(lhs.Select(l => l + rhs));
     59    }
     60    public static DoubleVector operator -(DoubleVector lhs, double rhs) {
     61      return new DoubleVector(lhs.Select(l => l - rhs));
     62    }
     63    public static DoubleVector operator *(DoubleVector lhs, double rhs) {
     64      return new DoubleVector(lhs.Select(l => l * rhs));
     65    }
     66    public static DoubleVector operator /(DoubleVector lhs, double rhs) {
     67      return new DoubleVector(lhs.Select(l => l / rhs));
     68    }
     69
     70    public double Sum() {
     71      return values.Sum();
     72    }
     73
     74    public double Average() {
     75      return values.Average();
     76    }
    3777  }
    3878}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Vectors/Vector.cs

    r17365 r17367  
    2828  [StorableType("3574E222-1167-4F1B-93D2-01B15A7F1E84")]
    2929  public abstract class Vector<T> : IVector<T> {
    30     private readonly List<T> values;
     30    [Storable]
     31    protected readonly List<T> values;
    3132
    3233    protected Vector(IEnumerable<T> values) {
Note: See TracChangeset for help on using the changeset viewer.