Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/15/18 10:41:20 (6 years ago)
Author:
gkronber
Message:

#2886: added clustering of functions and output of clusters, fixed bug in evaluation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/ExpressionClustering/Flann.cs

    r15841 r15842  
    8686      public int checks;                /* how many leafs (features) to check in one search */
    8787      public float cb_index;            /* cluster boundary index. Used when searching the kmeans tree */
    88       public float eps; 
     88      public float eps;
    8989
    9090      /*  kdtree index parameters */
     
    153153    [DllImport("flann-1.7.1.dll")]
    154154    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    }
    155187
    156188    public static int FindNearestNeighbours(List<double[]> dataset, List<double[]> queryset, out List<int> results, out List<double> distances, int nearestNeighbours = 3) {
     
    176208      flann_set_distance_type(flann_distance_t.FLANN_DIST_EUCLIDEAN, 0);
    177209
    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;
    183218      // _ds must be a rows × cols matrix stored in row-major order (one feature on each row)                         
    184219      //var index = flann_build_index(_ds, rows: dataset.Count, cols: _dim, speedup: ref speedup, flann_params: ref p);
    185      
     220
    186221
    187222      // copy testset
    188223      var _testset = new float[_tRows * _dim];
    189224      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) {
    192227          _testset[i++] = (float)e[d];
    193228        }
Note: See TracChangeset for help on using the changeset viewer.