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 */
|
---|
88 | public float eps;
|
---|
89 |
|
---|
90 | /* kdtree index parameters */
|
---|
91 | public int trees; /* number of randomized trees to use (for kdtree) */
|
---|
92 | public int leaf_max_size;
|
---|
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 |
|
---|
105 | public uint table_number_;
|
---|
106 | public uint key_size_;
|
---|
107 | public uint multi_probe_level_;
|
---|
108 |
|
---|
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")]
|
---|
145 | public static extern IntPtr flann_build_index(float[] dataset, int rows, int cols, ref float speedup, ref FLANNParameters flann_params);
|
---|
146 |
|
---|
147 | [DllImport("flann-1.7.1.dll")]
|
---|
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")]
|
---|
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 |
|
---|
156 | public static int FindNearestNeighbours(List<double[]> dataset, List<double[]> queryset, out List<int> results, out List<double> distances, int nearestNeighbours = 3) {
|
---|
157 | var _nn = nearestNeighbours;
|
---|
158 | var _tRows = queryset.Count;
|
---|
159 | var _dists = new float[_nn * _tRows];
|
---|
160 | var _result = new int[_nn * _tRows];
|
---|
161 | var _dim = dataset.First().Length;
|
---|
162 | FLANNParameters p = DEFAULT_FLANN_PARAMETERS;
|
---|
163 | p.algorithm = flann_algorithm_t.FLANN_INDEX_LINEAR;
|
---|
164 | p.centers_init = flann_centers_init_t.FLANN_CENTERS_RANDOM;
|
---|
165 | p.target_precision = 0.9f;
|
---|
166 | p.log_level = flann_log_level_t.FLANN_LOG_INFO;
|
---|
167 | // copy training set
|
---|
168 | var _ds = new float[dataset.Count * _dim];
|
---|
169 | var i = 0;
|
---|
170 | for (int d = 0; d < _dim; d++) {
|
---|
171 | foreach (var e in dataset) {
|
---|
172 | _ds[i++] = (float)e[d];
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | flann_set_distance_type(flann_distance_t.FLANN_DIST_EUCLIDEAN, 0);
|
---|
177 |
|
---|
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;
|
---|
183 | // _ds must be a rows × cols matrix stored in row-major order (one feature on each row)
|
---|
184 | //var index = flann_build_index(_ds, rows: dataset.Count, cols: _dim, speedup: ref speedup, flann_params: ref p);
|
---|
185 |
|
---|
186 |
|
---|
187 | // copy testset
|
---|
188 | var _testset = new float[_tRows * _dim];
|
---|
189 | i = 0;
|
---|
190 | for(int d = 0; d < _dim; d++) {
|
---|
191 | foreach(var e in queryset) {
|
---|
192 | _testset[i++] = (float)e[d];
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | //var res = flann_find_nearest_neighbors_index(index, _testset, _tRows, _result, _dists, _nn, 10, ref p);
|
---|
197 |
|
---|
198 |
|
---|
199 | var res =
|
---|
200 | flann_find_nearest_neighbors(
|
---|
201 | _ds, dataset.Count, _dim,
|
---|
202 | _testset, _tRows,
|
---|
203 | _result, _dists, _nn, ref p);
|
---|
204 |
|
---|
205 |
|
---|
206 | distances = _dists.Select(fi => (double)fi).ToList();
|
---|
207 | results = _result.ToList();
|
---|
208 | return res;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|