Changeset 14818 for branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EgoUtilities.cs
- Timestamp:
- 04/04/17 12:37:52 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EgoUtilities.cs
r14768 r14818 94 94 95 95 public static double GetEstimation(this IRegressionModel model, RealVector r) { 96 var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) } );96 var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) }, false); 97 97 return model.GetEstimatedValues(dataset, new[] { 0 }).First(); 98 98 } 99 99 public static double GetVariance(this IConfidenceRegressionModel model, RealVector r) { 100 var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) } );100 var dataset = GetDataSet(new[] { new Tuple<RealVector, double>(r, 0.0) }, false); 101 101 return model.GetEstimatedVariances(dataset, new[] { 0 }).First(); 102 102 } 103 103 104 public static Dataset GetDataSet(IReadOnlyList<Tuple<RealVector, double>> samples) { 105 var n = samples[0].Item1.Length + 1; 106 var data = new double[samples.Count, n]; 107 var names = new string[n - 1]; 108 for (var i = 0; i < n; i++) 109 if (i < names.Length) { 110 names[i] = "input" + i; 111 for (var j = 0; j < samples.Count; j++) data[j, i] = samples[j].Item1[i]; 112 } else 113 for (var j = 0; j < samples.Count; j++) data[j, n - 1] = samples[j].Item2; 104 105 public static double GetDoubleValue(this IDataset dataset, int i, int j) { 106 return dataset.GetDoubleValue("input" + j, i); 107 } 108 public static Dataset GetDataSet(IReadOnlyList<Tuple<RealVector, double>> samples, bool removeDuplicates) { 109 if (removeDuplicates) 110 samples = RemoveDuplicates(samples); //TODO duplicates require heteroskedasticity in Models 111 112 113 var dimensions = samples[0].Item1.Length + 1; 114 var data = new double[samples.Count, dimensions]; 115 var names = new string[dimensions - 1]; 116 for (var i = 0; i < names.Length; i++) names[i] = "input" + i; 117 118 for (var j = 0; j < samples.Count; j++) { 119 for (var i = 0; i < names.Length; i++) data[j, i] = samples[j].Item1[i]; 120 data[j, dimensions - 1] = samples[j].Item2; 121 122 } 123 124 114 125 return new Dataset(names.Concat(new[] { "output" }).ToArray(), data); 115 126 } 127 128 private static IReadOnlyList<Tuple<RealVector, double>> RemoveDuplicates(IReadOnlyList<Tuple<RealVector, double>> samples) { 129 var res = new List<Tuple<RealVector, double, int>>(); 130 131 foreach (var sample in samples) { 132 if (res.Count == 0) { 133 res.Add(new Tuple<RealVector, double, int>(sample.Item1, sample.Item2, 1)); 134 continue; 135 } 136 137 var index = res.ArgMin(x => Euclidian(sample.Item1, x.Item1)); 138 var d = Euclidian(res[index].Item1, sample.Item1); 139 if (d > 0.0001) 140 res.Add(new Tuple<RealVector, double, int>(sample.Item1, sample.Item2, 1)); 141 else { 142 var t = res[index]; 143 res.RemoveAt(index); 144 res.Add(new Tuple<RealVector, double, int>(t.Item1, t.Item2 + sample.Item2, t.Item3 + 1)); 145 } 146 } 147 return res.Select(x => new Tuple<RealVector, double>(x.Item1, x.Item2 / x.Item3)).ToArray(); 148 } 149 150 private static double Euclidian(IEnumerable<double> a, IEnumerable<double> b) { 151 return Math.Sqrt(a.Zip(b, (d, d1) => d - d1).Sum(d => d * d)); 152 } 153 116 154 public static DoubleMatrix GetBoundingBox(IEnumerable<RealVector> vectors) { 117 155 DoubleMatrix res = null; … … 128 166 return res; 129 167 } 168 169 130 170 } 131 171 }
Note: See TracChangeset
for help on using the changeset viewer.