Changeset 18060 for branches


Ignore:
Timestamp:
09/28/21 13:37:43 (9 months ago)
Author:
pfleck
Message:

#3040 Added a subvector symbol with ranges as subtrees.

Location:
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/VectorTreeSimplifier.cs

    r17629 r18060  
    6666    private static readonly StandardDeviation standardDeviationSymbol = new StandardDeviation();
    6767    private static readonly Variance varianceSymbol = new Variance();
     68    private static readonly Min minSymbol = new Min();
     69    private static readonly Max maxSymbol = new Max();
    6870
    6971    private readonly SymbolicDataAnalysisExpressionTreeVectorInterpreter interpreter;
     
    281283    private static bool IsVariance(ISymbolicExpressionTreeNode node) {
    282284      return node.Symbol is Variance;
     285    }
     286
     287    private static bool IsMin(ISymbolicExpressionTreeNode node) {
     288      return node.Symbol is Min;
     289    }
     290
     291    private static bool IsMax(ISymbolicExpressionTreeNode node) {
     292      return node.Symbol is Max;
    283293    }
    284294    #endregion
     
    363373      } else if (IsVariance(original)) {
    364374        return SimplifyVarianceAggregation(original);
     375      } else if (IsMin(original)) {
     376        return SimplifyMinAggregation(original);
     377      } else if (IsMax(original)) {
     378        return SimplifyMaxAggregation(original);
    365379      } else {
    366380        return SimplifyAny(original);
     
    574588    private ISymbolicExpressionTreeNode SimplifyVarianceAggregation(ISymbolicExpressionTreeNode original) {
    575589      return MakeVarianceAggregation(GetSimplifiedTree(original.GetSubtree(0)));
     590    }
     591
     592    private ISymbolicExpressionTreeNode SimplifyMinAggregation(ISymbolicExpressionTreeNode original) {
     593      return MakeMinAggregation(GetSimplifiedTree(original.GetSubtree(0)));
     594    }
     595
     596    private ISymbolicExpressionTreeNode SimplifyMaxAggregation(ISymbolicExpressionTreeNode original) {
     597      return MakeMaxAggregation(GetSimplifiedTree(original.GetSubtree(0)));
    576598    }
    577599    #endregion
     
    15081530        });
    15091531    }
     1532
     1533    private ISymbolicExpressionTreeNode MakeMinAggregation(ISymbolicExpressionTreeNode node) {
     1534      return MakeAggregation(node, meanSymbol,
     1535        simplifyScalar: n => n,
     1536        simplifyAdditiveTerms: (vectorNode, scalarNode) => {
     1537          var minNode = MakeMinAggregation(vectorNode);
     1538          return MakeSum(scalarNode, minNode);
     1539        },
     1540        simplifyMultiplicativeFactors: (vectorNode, scalarNode) => {
     1541          var minNode = MakeMinAggregation(vectorNode);
     1542          return MakeProduct(scalarNode, minNode);
     1543        });
     1544    }
     1545
     1546    private ISymbolicExpressionTreeNode MakeMaxAggregation(ISymbolicExpressionTreeNode node) {
     1547      return MakeAggregation(node, meanSymbol,
     1548        simplifyScalar: n => n,
     1549        simplifyAdditiveTerms: (vectorNode, scalarNode) => {
     1550          var maxNode = MakeMaxAggregation(vectorNode);
     1551          return MakeSum(scalarNode, maxNode);
     1552        },
     1553        simplifyMultiplicativeFactors: (vectorNode, scalarNode) => {
     1554          var maxNode = MakeMaxAggregation(vectorNode);
     1555          return MakeProduct(scalarNode, maxNode);
     1556        });
     1557    }
    15101558    #endregion
    15111559
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentVectorExpressionGrammar.cs

    r17752 r18060  
    5050
    5151    private const string VectorManipulationSymbolsName = "Vector Manipulation Symbols";
     52    private const string VectorSubVectorSymbolsName = "Vector SubVector Symbols";
    5253
    5354    private const string RealValuedSymbolsName = "Real Valued Symbols";
     
    127128
    128129      #region vector manipulation symbol declaration
    129       var subvector = new SubVector() { EnableWindowing = true };
     130      var subvectorLocal = new SubVector();
     131      var subvectorSubtree = new SubVectorSubtree();
    130132      #endregion
    131133
     
    148150      var vectorSymbols = new GroupSymbol(VectorSymbolsName, new List<ISymbol>() { vectorarithmeticSymbols, vectortrigonometricSymbols, vectorexponentialAndLogarithmicSymbols, vectorpowerSymbols, vectorterminalSymbols });
    149151
    150       var vectorManipulationSymbols = new GroupSymbol(VectorManipulationSymbolsName, new List<ISymbol>() { subvector });
     152      var vectorSubVectorSymbols = new GroupSymbol(VectorSubVectorSymbolsName, new List<ISymbol>() { subvectorLocal, subvectorSubtree });
     153      var vectorManipulationSymbols = new GroupSymbol(VectorManipulationSymbolsName, new List<ISymbol>() { vectorSubVectorSymbols });
     154     
    151155
    152156      //var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol> { scalarSymbols, vectorSymbols });
     
    185189      SetSubtreeCount(vectorterminalSymbols, 0, 0);
    186190
    187       SetSubtreeCount(subvector, 1, 1);
     191      SetSubtreeCount(subvectorLocal, 1, 1);
     192      SetSubtreeCount(subvectorSubtree, 3, 3);
    188193      #endregion
    189194
     
    198203      AddAllowedChildSymbol(root, constant, 1);
    199204      AddAllowedChildSymbol(aggregationSymbols, vectorSymbols);
    200       AddAllowedChildSymbol(statisticsSymbols, subvector);
     205      AddAllowedChildSymbol(statisticsSymbols, vectorSubVectorSymbols);
    201206
    202207      AddAllowedChildSymbol(vectorarithmeticSymbols, vectorSymbols);
     
    208213      AddAllowedChildSymbol(vectorroot, constant, 1);
    209214
    210       AddAllowedChildSymbol(subvector, vectorSymbols);
     215      AddAllowedChildSymbol(subvectorLocal, vectorSymbols);
     216      AddAllowedChildSymbol(subvectorSubtree, vectorSymbols, 0);
     217      AddAllowedChildSymbol(subvectorSubtree, scalarSymbols, 1);
     218      AddAllowedChildSymbol(subvectorSubtree, scalarSymbols, 2);
    211219      #endregion
    212220
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentVectorTimeSeriesExpressionGrammar.cs

    r17830 r18060  
    5252
    5353    private const string VectorManipulationSymbolsName = "Vector Manipulation Symbols";
     54    private const string VectorSubVectorSymbolsName = "Vector SubVector Symbols";
    5455
    5556    private const string RealValuedSymbolsName = "Real Valued Symbols";
     
    167168
    168169      #region vector manipulation symbol declaration
    169       var subvector = new SubVector() { EnableWindowing = true };
     170      var subvectorLocal = new SubVector();
     171      var subvectorSubtree = new SubVectorSubtree();
    170172      #endregion
    171173
     
    199201      var vectorSymbols = new GroupSymbol(VectorSymbolsName, new List<ISymbol>() { vectorarithmeticSymbols, vectortrigonometricSymbols, vectorexponentialAndLogarithmicSymbols, vectorpowerSymbols, vectorterminalSymbols });
    200202
    201       var vectorManipulationSymbols = new GroupSymbol(VectorManipulationSymbolsName, new List<ISymbol>() { subvector });
     203      var vectorSubVectorSymbols = new GroupSymbol(VectorSubVectorSymbolsName, new List<ISymbol>() { subvectorLocal, subvectorSubtree });
     204      var vectorManipulationSymbols = new GroupSymbol(VectorManipulationSymbolsName, new List<ISymbol>() { vectorSubVectorSymbols });
    202205
    203206      //var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol> { scalarSymbols, vectorSymbols });
     
    260263      SetSubtreeCount(vectorterminalSymbols, 0, 0);
    261264
    262       SetSubtreeCount(subvector, 1, 1);
     265      SetSubtreeCount(subvectorLocal, 1, 1);
     266      SetSubtreeCount(subvectorSubtree, 3, 3);
    263267      #endregion
    264268
     
    273277      AddAllowedChildSymbol(root, constant, 1);
    274278      AddAllowedChildSymbol(aggregationSymbols, vectorSymbols, 0);
    275       AddAllowedChildSymbol(statisticsSymbols, subvector, 0);
     279      AddAllowedChildSymbol(statisticsSymbols, vectorSubVectorSymbols, 0);
    276280      AddAllowedChildSymbol(quantile, constantZeroToOne, 1);
    277281      AddAllowedChildSymbol(distancesSymbols, vectorSymbols, 1);
    278282      AddAllowedChildSymbol(distributionCharacteristicsSymbols, vectorSymbols, 0);
    279       AddAllowedChildSymbol(distributionCharacteristicsSymbols, subvector, 0);
     283      AddAllowedChildSymbol(distributionCharacteristicsSymbols, vectorSubVectorSymbols, 0);
    280284      AddAllowedChildSymbol(distributionCharacteristicsSymbols, constantZeroToOne, 1);
    281285      AddAllowedChildSymbol(timeSeriesDynamicsSymbols, vectorSymbols, 0);
    282       AddAllowedChildSymbol(timeSeriesDynamicsSymbols, subvector, 0);
     286      AddAllowedChildSymbol(timeSeriesDynamicsSymbols, vectorSubVectorSymbols, 0);
    283287      AddAllowedChildSymbol(timeSeriesDynamicsSymbols, constantZeroToOne, 1);
    284288      AddAllowedChildSymbol(timeSeriesDynamicsSymbols, constantZeroToOne, 2);
     
    292296      AddAllowedChildSymbol(vectorroot, constantZeroToOne, 1);
    293297
    294       AddAllowedChildSymbol(subvector, vectorSymbols);
     298      AddAllowedChildSymbol(subvectorLocal, vectorSymbols);
     299      AddAllowedChildSymbol(subvectorSubtree, vectorSymbols, 0);
     300      AddAllowedChildSymbol(subvectorSubtree, scalarSymbols, 1);
     301      AddAllowedChildSymbol(subvectorSubtree, scalarSymbols, 2);
    295302      #endregion
    296303
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs

    r17830 r18060  
    9090    Covariance = 63,
    9191    SubVector = 64,
     92    SubVectorSubtree = 65,
    9293
    9394    #region Time Series Symbols
     
    195196    public const byte Covariance = (byte)OpCode.Covariance;
    196197    public const byte SubVector = (byte)OpCode.SubVector;
     198    public const byte SubVectorSubtree = (byte)OpCode.SubVectorSubtree;
    197199    #region Time Series Symbols
    198200    public const byte Median = (byte)OpCode.Median;
     
    300302      { typeof(Covariance), OpCodes.Covariance },
    301303      { typeof(SubVector), OpCodes.SubVector },
     304      { typeof(SubVectorSubtree), OpCodes.SubVectorSubtree },
    302305
    303306      #region Time Series Symbols
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs

    r18058 r18060  
    728728          }
    729729        case OpCodes.SubVector: {
    730             var cur = Evaluate(dataset, ref row, state, traceDict);
    731             return WindowedFunctionApply(cur, (WindowedSymbolTreeNode)currentInstr.dynamicNode,
    732               s => s,
    733               v => v);
    734           }
     730          DoubleVector SubVector(DoubleVector v , double start, double end) {
     731            int Mod(int x, int m) => (x % m + m) % m;
     732            int startIdx = Mod((int)Math.Round(start * v.Count), v.Count);
     733            int endIdx = Mod((int)Math.Round(end * v.Count), v.Count);
     734              int size = v.Count;
     735            if (startIdx < endIdx) {
     736              return v.SubVector(startIdx, count: endIdx - startIdx);
     737            } else { // wrap around
     738              var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx));
     739              v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx]
     740              v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx);   // copy [0:endIdx]      to [size-startIdx:size]
     741              return resultVector;
     742            }
     743          }
     744          var cur = Evaluate(dataset, ref row, state, traceDict);
     745          TraceEvaluation(currentInstr, cur);
     746          return FunctionApply(cur,
     747            s => s,
     748            v => {
     749              var node = (IWindowedSymbolTreeNode)currentInstr.dynamicNode;
     750              return SubVector(v, node.Offset, node.Length);
     751            });
     752          }
     753        case OpCodes.SubVectorSubtree: {
     754          DoubleVector SubVector(DoubleVector v, double start, double end) {
     755            int Mod(int x, int m) => (x % m + m) % m;
     756            int startIdx = Mod((int)Math.Round(start * v.Count), v.Count);
     757            int endIdx = Mod((int)Math.Round(end * v.Count), v.Count);
     758            int size = v.Count;
     759            if (startIdx < endIdx) {
     760              return v.SubVector(startIdx, count: endIdx - startIdx);
     761            } else { // wrap around
     762              var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx));
     763              v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx]
     764              v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx);   // copy [0:endIdx]      to [size-startIdx:size]
     765              return resultVector;
     766            }
     767          }
     768          var cur = Evaluate(dataset, ref row, state, traceDict);
     769          var offset = Evaluate(dataset, ref row, state, traceDict);
     770          var length = Evaluate(dataset, ref row, state, traceDict);
     771          TraceEvaluation(currentInstr, cur);
     772          return FunctionApply(cur,
     773            s => s,
     774            v => SubVector(v, offset.Scalar, length.Scalar)
     775          );
     776        }
    735777        case OpCodes.Variable: {
    736778            if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN;
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubVector.cs

    r17726 r18060  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HEAL.Attic;
     26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2527
    2628namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    2729  [StorableType("4E9511C6-0FA4-496D-9610-35D9F779F899")]
    28   [Item("SubVector", "Symbol that represents SubVector sum function.")]
    29   public sealed class SubVector : WindowedSymbol {
    30     private const int minimumArity = 1;
    31     private const int maximumArity = 1;
    32 
    33     public override int MinimumArity {
    34       get { return minimumArity; }
    35     }
    36     public override int MaximumArity {
    37       get { return maximumArity; }
    38     }
     30  [Item("SubVector", "Symbol that represent the SubVector function with local parameters.")]
     31  public class SubVector : WindowedSymbol {
     32    public override int MinimumArity => 1;
     33    public override int MaximumArity => 1;
    3934
    4035    [StorableConstructor]
    41     private SubVector(StorableConstructorFlag _) : base(_) { }
    42     private SubVector(SubVector original, Cloner cloner) : base(original, cloner) { }
     36    protected SubVector(StorableConstructorFlag _) : base(_) { }
     37
     38    protected SubVector(SubVector original, Cloner cloner) : base(original, cloner) { }
     39
    4340    public override IDeepCloneable Clone(Cloner cloner) {
    4441      return new SubVector(this, cloner);
    4542    }
    46     public SubVector() : base("SubVector", "Symbol that represents the SubVector function.") { }
     43
     44    public SubVector() : base("SubVector", "Symbol that represent the SubVector function with local parameters.") {
     45      EnableWindowing = true;
     46    }
     47  }
     48
     49  [StorableType("BA11A829-3236-46C3-AE83-9FA8511D8E8C")]
     50  [Item("SubVectorSubtree", "Symbol that represent the SubVector function with subtrees.")]
     51  public sealed class SubVectorSubtree : Symbol {
     52    public override int MinimumArity => 3;
     53    public override int MaximumArity => 3;
     54
     55    [StorableConstructor]
     56    private SubVectorSubtree(StorableConstructorFlag _) : base(_) { }
     57    private SubVectorSubtree(SubVectorSubtree original, Cloner cloner) : base(original, cloner) { }
     58    public override IDeepCloneable Clone(Cloner cloner) { return new SubVectorSubtree(this, cloner); }
     59    public SubVectorSubtree() : base("SubVectorSubtree", "Symbol that represent the SubVector function with subtrees.") { }
    4760  }
    4861}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/WindowedSymbol.cs

    r17726 r18060  
    158158
    159159      offsetMu = 0.0;
    160       offsetSigma = 0.05;
     160      offsetSigma = 0.2;
    161161      lengthMu = 1.0;
    162       lengthSigma = 0.05;
     162      lengthSigma = 0.2;
    163163
    164164      manipulatorOffsetMu = 0.0;
Note: See TracChangeset for help on using the changeset viewer.