 Timestamp:
 03/15/18 10:41:20 (6 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/2886_SymRegGrammarEnumeration/ExpressionClustering/Flann.cs
r15841 r15842 86 86 public int checks; /* how many leafs (features) to check in one search */ 87 87 public float cb_index; /* cluster boundary index. Used when searching the kmeans tree */ 88 public float eps; 88 public float eps; 89 89 90 90 /* kdtree index parameters */ … … 153 153 [DllImport("flann1.7.1.dll")] 154 154 public static extern int flann_compute_cluster_centers(float[] dataset, int rows, int cols, int clusters, float[] result, ref FLANNParameters flann_params); 155 156 public static int FindClusters(List<double[]> dataset, out List<int> results, out List<double> distances, int nClusters) { 157 var _nRows = dataset.Count; 158 var _dists = new float[_nRows]; 159 var _result = new int[_nRows]; 160 var _dim = dataset.First().Length; 161 FLANNParameters p = DEFAULT_FLANN_PARAMETERS; 162 p.algorithm = flann_algorithm_t.FLANN_INDEX_LINEAR; 163 p.centers_init = flann_centers_init_t.FLANN_CENTERS_RANDOM; 164 p.target_precision = 0.9f; 165 p.log_level = flann_log_level_t.FLANN_LOG_INFO; 166 // copy training set 167 var _ds = new float[dataset.Count * _dim]; 168 var i = 0; 169 foreach (var e in dataset) { 170 for (int d = 0; d < _dim; d++) { 171 _ds[i++] = (float)e[d]; 172 } 173 } 174 175 flann_set_distance_type(flann_distance_t.FLANN_DIST_EUCLIDEAN, 0); 176 177 float[] centers = new float[nClusters * _dim]; 178 int actualClusters = flann_compute_cluster_centers(_ds, rows: dataset.Count, cols: _dim, clusters: nClusters, result: centers, flann_params: ref p); 179 180 var res = flann_find_nearest_neighbors(centers, actualClusters, _dim, _ds, _nRows, _result, _dists, 1, ref p); 181 182 183 distances = _dists.Select(fi => (double)fi).ToList(); 184 results = _result.ToList(); 185 return res; 186 } 155 187 156 188 public static int FindNearestNeighbours(List<double[]> dataset, List<double[]> queryset, out List<int> results, out List<double> distances, int nearestNeighbours = 3) { … … 176 208 flann_set_distance_type(flann_distance_t.FLANN_DIST_EUCLIDEAN, 0); 177 209 178 int nClusters = 100; 179 float[] centers = new float[nClusters * _dim]; 180 flann_compute_cluster_centers(_ds, rows: dataset.Count, cols: _dim, clusters: nClusters, result: centers, flann_params: ref p); 181 182 float speedup = 1.0f; 210 // int nClusters = 100; 211 // float[] centers = new float[nClusters * _dim]; 212 // flann_compute_cluster_centers(_ds, rows: dataset.Count, cols: _dim, clusters: nClusters, result: centers, flann_params: ref p); 213 214 215 // for each point in the training set find the nearest cluster 216 217 // float speedup = 1.0f; 183 218 // _ds must be a rows × cols matrix stored in rowmajor order (one feature on each row) 184 219 //var index = flann_build_index(_ds, rows: dataset.Count, cols: _dim, speedup: ref speedup, flann_params: ref p); 185 220 186 221 187 222 // copy testset 188 223 var _testset = new float[_tRows * _dim]; 189 224 i = 0; 190 for (int d = 0; d < _dim; d++) {191 foreach (var e in queryset) {225 for (int d = 0; d < _dim; d++) { 226 foreach (var e in queryset) { 192 227 _testset[i++] = (float)e[d]; 193 228 }
Note: See TracChangeset
for help on using the changeset viewer.