Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/01/21 09:02:27 (3 years ago)
Author:
pfleck
Message:

#3040 Added support for multi-row data for SOP instances where the segment aggregation results are averaged over multiple rows.

File:
1 edited

Legend:

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

    r18096 r18098  
    5050
    5151    [Storable]
    52     private IValueParameter<DoubleArray> dataVectorParameter;
    53     public IValueParameter<DoubleArray> DataVectorParameter {
    54       get { return dataVectorParameter; }
     52    private IValueParameter<DoubleMatrix> dataParameter;
     53    public IValueParameter<DoubleMatrix> DataParameter {
     54      get { return dataParameter; }
    5555    }
    5656    [Storable]
     
    6868      Encoding = new IntegerVectorEncoding("bounds");
    6969
    70       Parameters.Add(dataVectorParameter = new ValueParameter<DoubleArray>("Data Vector", ""));
     70      Parameters.Add(dataParameter = new ValueParameter<DoubleMatrix>("Data", ""));
    7171      Parameters.Add(knownBoundsParameter = new ValueParameter<IntRange>("Known Bounds", ""));
    7272      Parameters.Add(aggregationParameter = new ValueParameter<EnumValue<Aggregation>>("Aggregation Function", ""));
     
    7676      #region Default Instance
    7777      Load(new SOPData() {
    78         Values = Enumerable.Range(1, 50).Select(x => (double)x * x).ToArray(),
     78        Data = ToNdimArray(Enumerable.Range(1, 50).Select(x => (double)x * x).ToArray()),
    7979        Lower = 20, Upper = 30,
    8080        Aggregation = "mean"
     
    8484    private SegmentOptimizationProblem(SegmentOptimizationProblem original, Cloner cloner)
    8585      : base(original, cloner) {
    86       dataVectorParameter = cloner.Clone(original.dataVectorParameter);
     86      dataParameter = cloner.Clone(original.dataParameter);
    8787      knownBoundsParameter = cloner.Clone(original.knownBoundsParameter);
    8888      aggregationParameter = cloner.Clone(original.aggregationParameter);
     
    9898    [StorableHook(HookType.AfterDeserialization)]
    9999    private void AfterDeserialization() {
     100      if (Parameters.ContainsKey("Data Vector") && Parameters["Data Vector"] is ValueParameter<DoubleArray> arrayParameter) {
     101        Parameters.Remove(arrayParameter);
     102        var array = arrayParameter.Value;
     103        var matrix = new DoubleMatrix(1, array.Length);
     104        for (int i = 0; i < array.Length; i++) matrix[0, i] = array[i];
     105        Parameters.Add(dataParameter = new ValueParameter<DoubleMatrix>("Data", "", matrix));
     106      }
     107
    100108      RegisterEventHandlers();
    101109    }
    102110   
    103111    private void RegisterEventHandlers() {
    104       dataVectorParameter.ValueChanged += DataVectorChanged;
     112      dataParameter.ValueChanged += DataChanged;
    105113      knownBoundsParameter.ValueChanged += KnownBoundsChanged;
    106114      aggregationParameter.Value.ValueChanged += AggregationFunctionChanged;
    107115    }
    108     private void DataVectorChanged(object sender, EventArgs eventArgs) {
    109       Encoding.Bounds = new IntMatrix(new[,] { { 0, DataVectorParameter.Value.Length } });
     116    private void DataChanged(object sender, EventArgs eventArgs) {
     117      Encoding.Bounds = new IntMatrix(new[,] { { 0, DataParameter.Value.Columns } });
    110118    }
    111119    private void KnownBoundsChanged(object sender, EventArgs e) {
     
    115123
    116124    public override double Evaluate(Individual individual, IRandom random) {
    117       var data = DataVectorParameter.Value;
     125      var data = DataParameter.Value;
    118126      var knownBounds = KnownBoundsParameter.Value;
    119127      var aggregation = aggregationParameter.Value.Value;
     
    134142      var bounds = new IntRange(best.Min(), best.Max());
    135143
    136       var data = DataVectorParameter.Value;
     144      var data = DataParameter.Value;
    137145      var knownBounds = KnownBoundsParameter.Value;
    138146      var aggregation = aggregationParameter.Value.Value;
     
    153161
    154162    private static double BoundedAggregation(DoubleArray data, IntRange bounds, Aggregation aggregation) {
     163      var matrix = new DoubleMatrix(1, data.Length);
     164      for (int i = 0; i < data.Length; i++) matrix[0, i] = data[i];
     165      return BoundedAggregation(matrix, bounds, aggregation);
     166    }
     167
     168    private static double BoundedAggregation(DoubleMatrix data, IntRange bounds, Aggregation aggregation) {
    155169      if (bounds.Size == 0) {
    156170        return 0;
    157171      }
    158172
    159       var vector = DoubleVector.Build.DenseOfEnumerable(data);
    160       var segment = vector.SubVector(bounds.Start, bounds.Size);
    161 
    162       switch (aggregation) {
    163         case Aggregation.Sum:
    164           return segment.Sum();
    165         case Aggregation.Mean:
    166          return segment.Average();
     173      var resultValues = new double[data.Rows];
     174      var array = new double[data.Columns];
     175      for (int row = 0; row < data.Rows; row++) {
     176        for (int i = 0; i < array.Length; i++) array[i] = data[row, i];
     177
     178        var vector = DoubleVector.Build.DenseOfArray(array);
     179        var segment = vector.SubVector(bounds.Start, bounds.Size);
     180
     181        switch (aggregation) {
     182          case Aggregation.Sum:
     183            resultValues[row] = segment.Sum();
     184            break;
     185          case Aggregation.Mean:
     186            resultValues[row] = segment.Average();
     187            break;
    167188          case Aggregation.StandardDeviation:
    168           return segment.StandardDeviationPop();
    169         default:
    170           throw new NotImplementedException();
    171       }
     189            resultValues[row] = segment.StandardDeviationPop();
     190            break;
     191          default:
     192            throw new NotImplementedException();
     193        }
     194      }
     195
     196      return resultValues.Average();
    172197    }
    173198
    174199    public void Load(SOPData data) {
    175       DataVectorParameter.Value = new DoubleArray(data.Values);
     200      DataParameter.Value = new DoubleMatrix(data.Data);
    176201      KnownBoundsParameter.Value = new IntRange(data.Lower, data.Upper);
    177202      switch (data.Aggregation.ToLower()) {
     
    193218                                           
    194219      Encoding.Length = 2;
    195       Encoding.Bounds = new IntMatrix(new[,] { { 0, DataVectorParameter.Value.Length } });
     220      Encoding.Bounds = new IntMatrix(new[,] { { 0, DataParameter.Value.Columns } });
    196221
    197222      BestKnownQuality = 0;
     
    200225      Description = data.Description;
    201226    }
     227
     228    public static T[,] ToNdimArray<T>(T[] array) {
     229      var matrix = new T[1, array.Length];
     230      for (int i = 0; i < array.Length; i++)
     231        matrix[0, i] = array[i];
     232      return matrix;
     233    }
    202234  }
    203235}
Note: See TracChangeset for help on using the changeset viewer.