Changeset 18098


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

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

Location:
branches/3040_VectorBasedGP
Files:
4 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}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/SegmentOptimization/SOPDataDescriptor.cs

    r18097 r18098  
    4040
    4141    internal string VariableName { get; set; }
    42     internal int Row { get; set; }
    4342    internal int  Lower { get; set; }
    4443    internal int Upper { get; set; }
    4544    internal string Aggregation { get; set; }
    46     internal SOPDataDescriptor(string name, string description, string variableName, int row, int lower, int upper, string aggregation) {
     45    internal SOPDataDescriptor(string name, string description, string variableName, int lower, int upper, string aggregation) {
    4746      Name = name;
    4847      Description = description;
    4948
    5049      VariableName = variableName;
    51       Row = row;
    5250      Lower = lower;
    5351      Upper = upper;
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/SegmentOptimization/SegmentOptimizationInstanceProvider.cs

    r18097 r18098  
    4747      return new[] {
    4848        new SOPDataDescriptor("[1:100]: i sum", "", new SOPData {
    49           Values = Enumerable.Range(1, 100).Select(i => (double)i).ToArray(),
     49          Data = ToNdimArray(Enumerable.Range(1, 100).Select(i => (double)i).ToArray()),
    5050          Lower = 30, Upper = 50, Aggregation = "sum"
    5151        }),
    5252        new SOPDataDescriptor("[1:1000]: i sum", "", new SOPData {
    53           Values = Enumerable.Range(1, 1000).Select(i => (double)i).ToArray(),
     53          Data = ToNdimArray(Enumerable.Range(1, 1000).Select(i => (double)i).ToArray()),
    5454          Lower = 350, Upper = 500, Aggregation = "sum"
    5555        }),
    5656        new SOPDataDescriptor("[1:100]: i^2 sum", "", new SOPData {
    57           Values = Enumerable.Range(1, 100).Select(i => (double)i * i).ToArray(),
     57          Data = ToNdimArray(Enumerable.Range(1, 100).Select(i => (double)i * i).ToArray()),
    5858          Lower = 30, Upper = 50, Aggregation = "sum"
    5959        }),
    6060        new SOPDataDescriptor("[1:1000]: i^2 sum", "", new SOPData {
    61           Values = Enumerable.Range(1, 1000).Select(i => (double)i * i).ToArray(),
     61          Data = ToNdimArray(Enumerable.Range(1, 1000).Select(i => (double)i * i).ToArray()),
    6262          Lower = 350, Upper = 500, Aggregation = "sum"
    6363        }),
     
    6868      var descriptor = (SOPDataDescriptor)id;
    6969      return descriptor.Data;
     70    }
     71
     72    public static T[,] ToNdimArray<T>(T[] array) {
     73      var matrix = new T[1, array.Length];
     74      for (int i = 0; i < array.Length; i++)
     75        matrix[0, i] = array[i];
     76      return matrix;
    7077    }
    7178  }
     
    8895    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
    8996      return new[] {
    90         new SOPDataDescriptor("v1", "", "v1", 0, 20, 60, "mean"),
    91         new SOPDataDescriptor("v2", "", "v2", 0, 20, 60, "mean"),
    92         new SOPDataDescriptor("v3", "", "v3", 0, 20, 60, "mean"),
    93         new SOPDataDescriptor("v4", "", "v4", 0, 20, 40, "mean"),
    94         new SOPDataDescriptor("v5", "", "v5", 0, 60, 80, "mean"),
    95         new SOPDataDescriptor("v6", "", "v6", 0, 40, 60, "mean"),
     97        new SOPDataDescriptor("v1", "", "v1", 20, 60, "mean"),
     98        new SOPDataDescriptor("v2", "", "v2", 20, 60, "mean"),
     99        new SOPDataDescriptor("v3", "", "v3", 20, 60, "mean"),
     100        new SOPDataDescriptor("v4", "", "v4", 20, 40, "mean"),
     101        new SOPDataDescriptor("v5", "", "v5", 60, 80, "mean"),
     102        new SOPDataDescriptor("v6", "", "v6", 40, 60, "mean"),
    96103      };
    97104    }
     
    124131
    125132    private SOPData LoadInstance(IDataset dataset, SOPDataDescriptor descriptor) {
    126       var data = dataset.GetDoubleVectorValue(descriptor.VariableName, descriptor.Row);
     133      var vectors = dataset.GetDoubleVectorValues(descriptor.VariableName).ToList();
     134      int length = vectors.FirstOrDefault()?.Count ?? 0;
     135      if (length == 0 || vectors.Any(v => v.Count != length))
     136        throw new ArgumentException("no or empty vectors or vectors with different lengths");
     137      var data = new double[vectors.Count, vectors.First().Count];
     138      for (int i = 0; i < vectors.Count; i++) {
     139        var v = vectors[i];
     140        for (int j = 0; j < v.Count; j++) {
     141          data[i, j] = v[j];
     142        }
     143      }
    127144
    128145      return new SOPData {
    129         Values = data.ToArray(),
     146        Data = data,
    130147        Lower = descriptor.Lower,
    131148        Upper = descriptor.Upper,
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances/3.3/Types/SOPData.cs

    r18092 r18098  
    77    public string Description { get; set; }
    88
    9     public double[] Values { get; set; }
     9    public double[,] Data { get; set; }
    1010    public int Upper { get; set; }
    1111    public int Lower { get; set; }
Note: See TracChangeset for help on using the changeset viewer.