Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/07/16 11:00:55 (8 years ago)
Author:
gkronber
Message:

#2653: merged r14236,r14314:14315 and r14322 from trunk to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.DataAnalysis

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs

    r14308 r14327  
    3636  public sealed class NearestNeighbourModel : ClassificationModel, INearestNeighbourModel {
    3737
     38    private readonly object kdTreeLockObject = new object();
    3839    private alglib.nearestneighbor.kdtree kdTree;
    3940    public alglib.nearestneighbor.kdtree KDTree {
     
    4748      }
    4849    }
     50
    4951
    5052    public override IEnumerable<string> VariablesUsedForPrediction {
     
    200202          x[column] = inputData[row, column];
    201203        }
    202         int actNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);
    203         alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists);
    204         alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours); // gkronber: this call changes the kdTree data structure
     204        int numNeighbours;
     205        lock (kdTreeLockObject) { // gkronber: the following calls change the kdTree data structure
     206          numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);
     207          alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists);
     208          alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours);
     209        }
    205210
    206211        double distanceWeightedValue = 0.0;
    207212        double distsSum = 0.0;
    208         for (int i = 0; i < actNeighbours; i++) {
     213        for (int i = 0; i < numNeighbours; i++) {
    209214          distanceWeightedValue += neighbours[i, columns] / dists[i];
    210215          distsSum += 1.0 / dists[i];
     
    233238          x[column] = inputData[row, column];
    234239        }
    235         int actNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);
    236         alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists);
    237         alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours);
    238 
     240        int numNeighbours;
     241        lock (kdTreeLockObject) {
     242          // gkronber: the following calls change the kdTree data structure
     243          numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);
     244          alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists);
     245          alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours);
     246        }
    239247        Array.Clear(y, 0, y.Length);
    240         for (int i = 0; i < actNeighbours; i++) {
     248        for (int i = 0; i < numNeighbours; i++) {
    241249          int classValue = (int)Math.Round(neighbours[i, columns]);
    242250          y[classValue]++;
Note: See TracChangeset for help on using the changeset viewer.