Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18204


Ignore:
Timestamp:
01/18/22 16:35:16 (3 years ago)
Author:
pfleck
Message:

#3040 Improved performance by avoiding allocating memory for vector segments & fixed some index bounds corner cases.

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

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/SegmentOptimization/SamplingSegmentOptimizationManipulator.cs

    r18193 r18204  
    3737
    3838    [StorableType("65B2E870-7EED-4DD0-918E-94A346671784")]
    39     protected enum DimensionType {
     39    public enum DimensionType {
    4040      All,
    4141      Single
    4242    }
    4343    [StorableType("C73064F2-4E2F-473F-8DBA-DD47881726D4")]
    44     protected enum SearchRangeType {
     44    public enum SearchRangeType {
    4545      None, // Fixed on current value
    4646      Full,
     
    4949    }
    5050    [StorableType("A9EBE01E-53DE-4EB7-94B3-26C624034A08")]
    51     protected enum SamplingType {
     51    public enum SamplingType {
    5252      Exhaustive,
    5353      RandomSampling,
     
    5757    [StorableType("4EBF0DFE-90A1-4DFE-8CC7-D358EF8AF96B")]
    5858    [Flags]
    59     protected enum SamplingPointsType {
     59    public enum SamplingPointsType {
    6060      None = 0,
    6161      BeforeCombinations = 1,
     
    6363    }
    6464
    65     protected ValueParameter<EnumValue<DimensionType>> Dimension => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"];
    66     protected ValueParameter<EnumValue<SearchRangeType>> SearchRange => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"];
    67     protected ValueParameter<EnumValue<SamplingType>> Sampling => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"];
    68     protected ValueParameter<IntValue> SampleCount => (ValueParameter<IntValue>)Parameters["SampleCount"];
    69     protected ValueParameter<EnumValue<SamplingPointsType>> SamplingPoints => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"];
    70    
     65    #region Properties
     66    public DimensionType Dimension { get { return DimensionParameter.Value.Value; } set { DimensionParameter.Value.Value = value; } }
     67    public SearchRangeType SearchRange { get { return SearchRangeParameter.Value.Value; } set { SearchRangeParameter.Value.Value = value; } }
     68    public SamplingType Sampling { get { return SamplingParameter.Value.Value; } set { SamplingParameter.Value.Value = value; } }
     69    public int SampleCount { get { return SampleCountParameter.Value.Value; } set { SampleCountParameter.Value.Value = value; } }
     70    public SamplingPointsType SamplingPoints { get { return SamplingPointsParameter.Value.Value; } set { SamplingPointsParameter.Value.Value = value; } }
     71    #endregion
     72
     73    #region Parameter Properties
     74    public ValueParameter<EnumValue<DimensionType>> DimensionParameter => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"];
     75    public ValueParameter<EnumValue<SearchRangeType>> SearchRangeParameter => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"];
     76    public ValueParameter<EnumValue<SamplingType>> SamplingParameter => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"];
     77    public ValueParameter<IntValue> SampleCountParameter => (ValueParameter<IntValue>)Parameters["SampleCount"];
     78    public ValueParameter<EnumValue<SamplingPointsType>> SamplingPointsParameter => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"];
     79    #endregion
    7180
    7281    public SamplingSegmentOptimizationManipulator() {
     
    8695   
    8796    protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
    88       var indices = CreateIndices(random, integerVector, bounds, Dimension.Value.Value, SearchRange.Value.Value);
    89 
    90       if (SamplingPoints.Value.Value.HasFlag(SamplingPointsType.BeforeCombinations))
    91         indices = indices.Select(i => SampleIndices(i, Sampling.Value.Value, random, SampleCount.Value.Value).ToList()).ToArray();
     97      var indices = CreateIndices(random, new IntegerVector(new [] { integerVector.Min(), integerVector.Max() }), bounds, Dimension, SearchRange);
     98     
     99      if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations))
     100        indices = indices.Select(i => SampleIndices(i, Sampling, random, SampleCount).ToList()).ToArray();
    92101
    93102      var solutions = CreateCombinations(indices[0], indices[1]);
     103      if (!solutions.Any()) {
     104        if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations))
     105          return; // no valid combinations -> no mutation
     106        throw new InvalidOperationException("no indices!");
     107      }
    94108
    95       if (SamplingPoints.Value.Value.HasFlag(SamplingPointsType.AfterCombinations))
    96         solutions = SampleIndices(solutions, Sampling.Value.Value, random, SampleCount.Value.Value);
     109      if (SamplingPoints.HasFlag(SamplingPointsType.AfterCombinations))
     110        solutions = SampleIndices(solutions, Sampling, random, SampleCount);
    97111
    98112      var best = FindBest(solutions);
     
    105119      var indices = new IEnumerable<int>[integerVector.Length];
    106120      int targetIndex = random.Next(indices.Length); // first or second index
     121      integerVector = new IntegerVector(new[] { integerVector.Min(), integerVector.Max() });
    107122      for (int i = 0; i < indices.Length; i++) {
    108123        var searchRange = dimension == DimensionType.All || targetIndex == i ? indexRange : SearchRangeType.None;
     
    122137        case SearchRangeType.None:
    123138          return new[] { currentIndex };
    124         case SearchRangeType.RandomDirection:
    125           return random.Next(2) == 0 ? Enumerable.Range(0, currentIndex) : Enumerable.Range(currentIndex, length - currentIndex);
    126         case SearchRangeType.RandomRange:
    127           int start = random.Next(0, length - 1), end = random.Next(start + 1, length);
    128           return Enumerable.Range(start, end - start);
     139        case SearchRangeType.RandomDirection: // including currentIndex
     140          return random.Next(2) == 0
     141            ? Enumerable.Range(0, currentIndex + 1) // left
     142            : Enumerable.Range(currentIndex, length - currentIndex); // right
     143        case SearchRangeType.RandomRange: // random range around current index, not completely random range
     144          int start = random.Next(0, currentIndex + 1), end = random.Next(currentIndex, length);
     145          return Enumerable.Range(start, end - start + 1);
    129146        default:
    130147          throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null);
     
    148165        case SamplingType.LinearSelection:
    149166          var indicesList = indices.ToList();
    150           var selected = MathNet.Numerics.Generate.LinearSpaced(count, 0, indicesList.Count)
    151             .Select(i => (int)Math.Round(i))
     167          var selected = MathNet.Numerics.Generate.LinearSpaced(count, 0, indicesList.Count - 1) // LinearSpaced stop is inclusive
     168            .Select(i => (int)Math.Floor(i))
    152169            .Distinct();
    153170          return selected.Select(i => indicesList[i]);
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SegmentOptimization/SegmentOptimizationProblem.cs

    r18202 r18204  
    183183
    184184      var resultValues = new double[data.Rows];
    185       var array = new double[data.Columns];
    186185      for (int row = 0; row < data.Rows; row++) {
    187         for (int i = 0; i < array.Length; i++)
    188           array[i] = data[row, i];
    189                                                        
    190         //var vector = DoubleVector.Build.DenseOfArray(array);
    191         //var segment = vector.SubVector(bounds.Start, bounds.Size);
    192         var segment = DoubleVector.Build.DenseOfEnumerable(array.Skip(bounds.Start).Take(bounds.Size));
    193 
     186        var vector = data.GetRow(row);
     187        var segment = vector.Skip(bounds.Start).Take(bounds.Size);
    194188        switch (aggregation) {
    195189          case Aggregation.Sum:
Note: See TracChangeset for help on using the changeset viewer.