Changeset 18098 for branches/3040_VectorBasedGP
- Timestamp:
- 12/01/21 09:02:27 (3 years ago)
- 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 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 } -
branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/SegmentOptimization/SOPDataDescriptor.cs
r18097 r18098 40 40 41 41 internal string VariableName { get; set; } 42 internal int Row { get; set; }43 42 internal int Lower { get; set; } 44 43 internal int Upper { get; set; } 45 44 internal string Aggregation { get; set; } 46 internal SOPDataDescriptor(string name, string description, string variableName, int row, intlower, int upper, string aggregation) {45 internal SOPDataDescriptor(string name, string description, string variableName, int lower, int upper, string aggregation) { 47 46 Name = name; 48 47 Description = description; 49 48 50 49 VariableName = variableName; 51 Row = row;52 50 Lower = lower; 53 51 Upper = upper; -
branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/SegmentOptimization/SegmentOptimizationInstanceProvider.cs
r18097 r18098 47 47 return new[] { 48 48 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()), 50 50 Lower = 30, Upper = 50, Aggregation = "sum" 51 51 }), 52 52 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()), 54 54 Lower = 350, Upper = 500, Aggregation = "sum" 55 55 }), 56 56 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()), 58 58 Lower = 30, Upper = 50, Aggregation = "sum" 59 59 }), 60 60 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()), 62 62 Lower = 350, Upper = 500, Aggregation = "sum" 63 63 }), … … 68 68 var descriptor = (SOPDataDescriptor)id; 69 69 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; 70 77 } 71 78 } … … 88 95 public override IEnumerable<IDataDescriptor> GetDataDescriptors() { 89 96 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"), 96 103 }; 97 104 } … … 124 131 125 132 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 } 127 144 128 145 return new SOPData { 129 Values = data.ToArray(),146 Data = data, 130 147 Lower = descriptor.Lower, 131 148 Upper = descriptor.Upper, -
branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances/3.3/Types/SOPData.cs
r18092 r18098 7 7 public string Description { get; set; } 8 8 9 public double[ ] Values{ get; set; }9 public double[,] Data { get; set; } 10 10 public int Upper { get; set; } 11 11 public int Lower { get; set; }
Note: See TracChangeset
for help on using the changeset viewer.