[15840] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Runtime.InteropServices;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Threading.Tasks;
|
---|
| 7 |
|
---|
| 8 | namespace ExpressionClustering {
|
---|
| 9 | // /* file flann_example.c */
|
---|
| 10 | // # include "flann.h"
|
---|
| 11 | // # include <stdio.h>
|
---|
| 12 | // # include <assert.h>
|
---|
| 13 | // /* Function that reads a dataset */
|
---|
| 14 | // float* read_points(char* filename, int* rows, int* cols);
|
---|
| 15 | // int main(int argc, char** argv) {
|
---|
| 16 | // int rows, cols;
|
---|
| 17 | // int t_rows, t_cols;
|
---|
| 18 | // float speedup;
|
---|
| 19 | // /* read dataset points from file dataset.dat */
|
---|
| 20 | // float* dataset = read_points("dataset.dat", &rows, &cols);
|
---|
| 21 | // float* testset = read_points("testset.dat", &t_rows, &t_cols);
|
---|
| 22 | // /* points in dataset and testset should have the same dimensionality */
|
---|
| 23 | // assert(cols == t_cols);
|
---|
| 24 | // /* number of nearest neighbors to search */
|
---|
| 25 | // int nn = 3;
|
---|
| 26 | // /* allocate memory for the nearest-neighbors indices */
|
---|
| 27 | // int* result = (int*)malloc(t_rows * nn * sizeof(int));
|
---|
| 28 | // /* allocate memory for the distances */
|
---|
| 29 | // float* dists = (float*)malloc(t_rows * nn * sizeof(float));
|
---|
| 30 | // /* index parameters are stored here */
|
---|
| 31 | // struct FLANNParameters p = DEFAULT_FLANN_PARAMETERS;
|
---|
| 32 | // p.algorithm = FLANN_INDEX_AUTOTUNED; /* or FLANN_INDEX_KDTREE, FLANN_INDEX_KMEANS, ... /*
|
---|
| 33 | // p.target_precision = 0.9; /* want 90% target precision */
|
---|
| 34 | // /* compute the 3 nearest-neighbors of each point in the testset */
|
---|
| 35 | // flann_find_nearest_neighbors(dataset, rows, cols, testset, t_rows,
|
---|
| 36 | // result, dists, nn, &p);
|
---|
| 37 | // ...
|
---|
| 38 | // free(dataset);
|
---|
| 39 | // free(testset);
|
---|
| 40 | // free(result);
|
---|
| 41 | // free(dists);
|
---|
| 42 | // return 0;
|
---|
| 43 | // }
|
---|
| 44 |
|
---|
| 45 | public static class Flann {
|
---|
| 46 |
|
---|
| 47 | public enum flann_algorithm_t {
|
---|
| 48 | FLANN_INDEX_LINEAR = 0,
|
---|
| 49 | FLANN_INDEX_KDTREE = 1,
|
---|
| 50 | FLANN_INDEX_KMEANS = 2,
|
---|
| 51 | FLANN_INDEX_COMPOSITE = 3,
|
---|
| 52 | FLANN_INDEX_KDTREE_SINGLE = 3,
|
---|
| 53 | FLANN_INDEX_SAVED = 254,
|
---|
| 54 | FLANN_INDEX_AUTOTUNED = 255
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | public enum flann_centers_init_t {
|
---|
| 59 | FLANN_CENTERS_RANDOM = 0,
|
---|
| 60 | FLANN_CENTERS_GONZALES = 1,
|
---|
| 61 | FLANN_CENTERS_KMEANSPP = 2
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | public enum flann_log_level_t {
|
---|
| 65 | FLANN_LOG_NONE = 0,
|
---|
| 66 | FLANN_LOG_FATAL = 1,
|
---|
| 67 | FLANN_LOG_ERROR = 2,
|
---|
| 68 | FLANN_LOG_WARN = 3,
|
---|
| 69 | FLANN_LOG_INFO = 4
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | public enum flann_distance_t {
|
---|
| 73 | FLANN_DIST_EUCLIDEAN = 1, // squared euclidean distance
|
---|
| 74 | FLANN_DIST_MANHATTAN = 2,
|
---|
| 75 | FLANN_DIST_MINKOWSKI = 3,
|
---|
| 76 | FLANN_DIST_HIST_INTERSECT = 5,
|
---|
| 77 | FlANN_DIST_HELLINGER = 6,
|
---|
| 78 | FLANN_DIST_CHI_SQUARE = 7, // chi-square
|
---|
| 79 | FLANN_DIST_KULLBACK_LEIBLER = 8, // kullback-leibler divergence
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | public struct FLANNParameters {
|
---|
| 83 | public flann_algorithm_t algorithm; /* the algorithm to use */
|
---|
| 84 |
|
---|
| 85 | /* search time parameters */
|
---|
| 86 | public int checks; /* how many leafs (features) to check in one search */
|
---|
| 87 | public float cb_index; /* cluster boundary index. Used when searching the kmeans tree */
|
---|
[15842] | 88 | public float eps;
|
---|
[15840] | 89 |
|
---|
| 90 | /* kdtree index parameters */
|
---|
| 91 | public int trees; /* number of randomized trees to use (for kdtree) */
|
---|
[15841] | 92 | public int leaf_max_size;
|
---|
[15840] | 93 |
|
---|
| 94 | /* kmeans index parameters */
|
---|
| 95 | public int branching; /* branching factor (for kmeans tree) */
|
---|
| 96 | public int iterations; /* max iterations to perform in one kmeans cluetering (kmeans tree) */
|
---|
| 97 | public flann_centers_init_t centers_init; /* algorithm used for picking the initial cluster centers for kmeans tree */
|
---|
| 98 |
|
---|
| 99 | /* autotuned index parameters */
|
---|
| 100 | public float target_precision; /* precision desired (used for autotuning, -1 otherwise) */
|
---|
| 101 | public float build_weight; /* build tree time weighting factor */
|
---|
| 102 | public float memory_weight; /* index memory weigthing factor */
|
---|
| 103 | public float sample_fraction; /* what fraction of the dataset to use for autotuning */
|
---|
| 104 |
|
---|
[15841] | 105 | public uint table_number_;
|
---|
| 106 | public uint key_size_;
|
---|
| 107 | public uint multi_probe_level_;
|
---|
| 108 |
|
---|
[15840] | 109 | /* other parameters */
|
---|
| 110 | public flann_log_level_t log_level; /* determines the verbosity of each flann function */
|
---|
| 111 | public long random_seed; /* random seed to use */
|
---|
| 112 | };
|
---|
| 113 |
|
---|
| 114 | // struct FLANNParameters DEFAULT_FLANN_PARAMETERS = {
|
---|
| 115 | // FLANN_INDEX_KDTREE,
|
---|
| 116 | // 32, 0.2f, 0.0f,
|
---|
| 117 | // 4, 4,
|
---|
| 118 | // 32, 11, FLANN_CENTERS_RANDOM,
|
---|
| 119 | // 0.9f, 0.01f, 0, 0.1f,
|
---|
| 120 | // FLANN_LOG_NONE, 0
|
---|
| 121 | // };
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | public static FLANNParameters DEFAULT_FLANN_PARAMETERS = new FLANNParameters() {
|
---|
| 125 | algorithm = flann_algorithm_t.FLANN_INDEX_KDTREE,
|
---|
| 126 | checks = 32,
|
---|
| 127 | cb_index = 0.2f,
|
---|
| 128 | trees = 4,
|
---|
| 129 | branching = 32,
|
---|
| 130 | iterations = 11,
|
---|
| 131 | centers_init = flann_centers_init_t.FLANN_CENTERS_RANDOM,
|
---|
| 132 | target_precision = 0.9f,
|
---|
| 133 | build_weight = 0.01f,
|
---|
| 134 | memory_weight = 0,
|
---|
| 135 | sample_fraction = 0.1f,
|
---|
| 136 | log_level = flann_log_level_t.FLANN_LOG_NONE,
|
---|
| 137 | random_seed = 0,
|
---|
| 138 | };
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | [DllImport("flann-1.7.1.dll")]
|
---|
| 142 | public static extern int flann_find_nearest_neighbors(float[] dataset, int rows, int cols, float[] testset, int t_rows, int[] result, float[] dist, int nn, ref FLANNParameters flann_params);
|
---|
| 143 |
|
---|
| 144 | [DllImport("flann-1.7.1.dll")]
|
---|
[15841] | 145 | public static extern IntPtr flann_build_index(float[] dataset, int rows, int cols, ref float speedup, ref FLANNParameters flann_params);
|
---|
[15840] | 146 |
|
---|
| 147 | [DllImport("flann-1.7.1.dll")]
|
---|
[15841] | 148 | public static extern int flann_find_nearest_neighbors_index(IntPtr index_id, float[] testset, int trows, int[] indices, float[] dists, int nn, int checks, ref FLANNParameters flann_params);
|
---|
| 149 |
|
---|
| 150 | [DllImport("flann-1.7.1.dll")]
|
---|
[15840] | 151 | public static extern void flann_set_distance_type(flann_distance_t distance_type, int order);
|
---|
| 152 |
|
---|
| 153 | [DllImport("flann-1.7.1.dll")]
|
---|
| 154 | public static extern int flann_compute_cluster_centers(float[] dataset, int rows, int cols, int clusters, float[] result, ref FLANNParameters flann_params);
|
---|
| 155 |
|
---|
[15842] | 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 | }
|
---|
| 187 |
|
---|
[15840] | 188 | public static int FindNearestNeighbours(List<double[]> dataset, List<double[]> queryset, out List<int> results, out List<double> distances, int nearestNeighbours = 3) {
|
---|
| 189 | var _nn = nearestNeighbours;
|
---|
| 190 | var _tRows = queryset.Count;
|
---|
| 191 | var _dists = new float[_nn * _tRows];
|
---|
| 192 | var _result = new int[_nn * _tRows];
|
---|
| 193 | var _dim = dataset.First().Length;
|
---|
| 194 | FLANNParameters p = DEFAULT_FLANN_PARAMETERS;
|
---|
[15841] | 195 | p.algorithm = flann_algorithm_t.FLANN_INDEX_LINEAR;
|
---|
[15840] | 196 | p.centers_init = flann_centers_init_t.FLANN_CENTERS_RANDOM;
|
---|
| 197 | p.target_precision = 0.9f;
|
---|
| 198 | p.log_level = flann_log_level_t.FLANN_LOG_INFO;
|
---|
| 199 | // copy training set
|
---|
| 200 | var _ds = new float[dataset.Count * _dim];
|
---|
| 201 | var i = 0;
|
---|
| 202 | for (int d = 0; d < _dim; d++) {
|
---|
| 203 | foreach (var e in dataset) {
|
---|
| 204 | _ds[i++] = (float)e[d];
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | flann_set_distance_type(flann_distance_t.FLANN_DIST_EUCLIDEAN, 0);
|
---|
| 209 |
|
---|
[15842] | 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);
|
---|
[15840] | 213 |
|
---|
[15842] | 214 |
|
---|
| 215 | // for each point in the training set find the nearest cluster
|
---|
| 216 |
|
---|
| 217 | // float speedup = -1.0f;
|
---|
[15841] | 218 | // _ds must be a rows × cols matrix stored in row-major order (one feature on each row)
|
---|
| 219 | //var index = flann_build_index(_ds, rows: dataset.Count, cols: _dim, speedup: ref speedup, flann_params: ref p);
|
---|
[15840] | 220 |
|
---|
[15842] | 221 |
|
---|
[15840] | 222 | // copy testset
|
---|
| 223 | var _testset = new float[_tRows * _dim];
|
---|
| 224 | i = 0;
|
---|
[15842] | 225 | for (int d = 0; d < _dim; d++) {
|
---|
| 226 | foreach (var e in queryset) {
|
---|
[15841] | 227 | _testset[i++] = (float)e[d];
|
---|
[15840] | 228 | }
|
---|
| 229 | }
|
---|
[15841] | 230 |
|
---|
| 231 | //var res = flann_find_nearest_neighbors_index(index, _testset, _tRows, _result, _dists, _nn, 10, ref p);
|
---|
| 232 |
|
---|
| 233 |
|
---|
[15840] | 234 | var res =
|
---|
| 235 | flann_find_nearest_neighbors(
|
---|
| 236 | _ds, dataset.Count, _dim,
|
---|
| 237 | _testset, _tRows,
|
---|
| 238 | _result, _dists, _nn, ref p);
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | distances = _dists.Select(fi => (double)fi).ToList();
|
---|
| 242 | results = _result.ToList();
|
---|
| 243 | return res;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | }
|
---|