Changeset 18098 for branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SegmentOptimization/SegmentOptimizationProblem.cs
- Timestamp:
- 12/01/21 09:02:27 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SegmentOptimization/SegmentOptimizationProblem.cs
r18096 r18098 50 50 51 51 [Storable] 52 private IValueParameter<Double Array> dataVectorParameter;53 public IValueParameter<Double Array> DataVectorParameter {54 get { return data VectorParameter; }52 private IValueParameter<DoubleMatrix> dataParameter; 53 public IValueParameter<DoubleMatrix> DataParameter { 54 get { return dataParameter; } 55 55 } 56 56 [Storable] … … 68 68 Encoding = new IntegerVectorEncoding("bounds"); 69 69 70 Parameters.Add(data VectorParameter = new ValueParameter<DoubleArray>("Data Vector", ""));70 Parameters.Add(dataParameter = new ValueParameter<DoubleMatrix>("Data", "")); 71 71 Parameters.Add(knownBoundsParameter = new ValueParameter<IntRange>("Known Bounds", "")); 72 72 Parameters.Add(aggregationParameter = new ValueParameter<EnumValue<Aggregation>>("Aggregation Function", "")); … … 76 76 #region Default Instance 77 77 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()), 79 79 Lower = 20, Upper = 30, 80 80 Aggregation = "mean" … … 84 84 private SegmentOptimizationProblem(SegmentOptimizationProblem original, Cloner cloner) 85 85 : base(original, cloner) { 86 data VectorParameter = cloner.Clone(original.dataVectorParameter);86 dataParameter = cloner.Clone(original.dataParameter); 87 87 knownBoundsParameter = cloner.Clone(original.knownBoundsParameter); 88 88 aggregationParameter = cloner.Clone(original.aggregationParameter); … … 98 98 [StorableHook(HookType.AfterDeserialization)] 99 99 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 100 108 RegisterEventHandlers(); 101 109 } 102 110 103 111 private void RegisterEventHandlers() { 104 data VectorParameter.ValueChanged += DataVectorChanged;112 dataParameter.ValueChanged += DataChanged; 105 113 knownBoundsParameter.ValueChanged += KnownBoundsChanged; 106 114 aggregationParameter.Value.ValueChanged += AggregationFunctionChanged; 107 115 } 108 private void Data VectorChanged(object sender, EventArgs eventArgs) {109 Encoding.Bounds = new IntMatrix(new[,] { { 0, Data VectorParameter.Value.Length} });116 private void DataChanged(object sender, EventArgs eventArgs) { 117 Encoding.Bounds = new IntMatrix(new[,] { { 0, DataParameter.Value.Columns } }); 110 118 } 111 119 private void KnownBoundsChanged(object sender, EventArgs e) { … … 115 123 116 124 public override double Evaluate(Individual individual, IRandom random) { 117 var data = Data VectorParameter.Value;125 var data = DataParameter.Value; 118 126 var knownBounds = KnownBoundsParameter.Value; 119 127 var aggregation = aggregationParameter.Value.Value; … … 134 142 var bounds = new IntRange(best.Min(), best.Max()); 135 143 136 var data = Data VectorParameter.Value;144 var data = DataParameter.Value; 137 145 var knownBounds = KnownBoundsParameter.Value; 138 146 var aggregation = aggregationParameter.Value.Value; … … 153 161 154 162 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) { 155 169 if (bounds.Size == 0) { 156 170 return 0; 157 171 } 158 172 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; 167 188 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(); 172 197 } 173 198 174 199 public void Load(SOPData data) { 175 Data VectorParameter.Value = new DoubleArray(data.Values);200 DataParameter.Value = new DoubleMatrix(data.Data); 176 201 KnownBoundsParameter.Value = new IntRange(data.Lower, data.Upper); 177 202 switch (data.Aggregation.ToLower()) { … … 193 218 194 219 Encoding.Length = 2; 195 Encoding.Bounds = new IntMatrix(new[,] { { 0, Data VectorParameter.Value.Length} });220 Encoding.Bounds = new IntMatrix(new[,] { { 0, DataParameter.Value.Columns } }); 196 221 197 222 BestKnownQuality = 0; … … 200 225 Description = data.Description; 201 226 } 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 } 202 234 } 203 235 }
Note: See TracChangeset
for help on using the changeset viewer.