Free cookie consent management tool by TermsFeed Policy Generator

source: branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/VantagePointTree.cs @ 14787

Last change on this file since 14787 was 14787, checked in by gkronber, 7 years ago

#2700: more changes while reviewing

File size: 7.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21//Code is based on an implementation from Laurens van der Maaten
22
23/*
24*
25* Copyright (c) 2014, Laurens van der Maaten (Delft University of Technology)
26* All rights reserved.
27*
28* Redistribution and use in source and binary forms, with or without
29* modification, are permitted provided that the following conditions are met:
30* 1. Redistributions of source code must retain the above copyright
31*    notice, this list of conditions and the following disclaimer.
32* 2. Redistributions in binary form must reproduce the above copyright
33*    notice, this list of conditions and the following disclaimer in the
34*    documentation and/or other materials provided with the distribution.
35* 3. All advertising materials mentioning features or use of this software
36*    must display the following acknowledgement:
37*    This product includes software developed by the Delft University of Technology.
38* 4. Neither the name of the Delft University of Technology nor the names of
39*    its contributors may be used to endorse or promote products derived from
40*    this software without specific prior written permission.
41*
42* THIS SOFTWARE IS PROVIDED BY LAURENS VAN DER MAATEN ''AS IS'' AND ANY EXPRESS
43* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
45* EVENT SHALL LAURENS VAN DER MAATEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
47* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
48* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
50* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
51* OF SUCH DAMAGE.
52*
53*/
54#endregion
55
56using System.Collections.Generic;
57using System.Linq;
58using HeuristicLab.Collections;
59using HeuristicLab.Random;
60
61namespace HeuristicLab.Algorithms.DataAnalysis {
62  /// <summary>
63  /// Vantage point tree  (or VP tree) is a metric tree that segregates data in a metric space by choosing
64  /// a position in the space (the "vantage point") and partitioning the data points into two parts:
65  /// those points that are nearer to the vantage point than a threshold, and those points that are not.
66  /// By recursively applying this procedure to partition the data into smaller and smaller sets, a tree
67  /// data structure is created where neighbors in the tree are likely to be neighbors in the space.
68  /// </summary>
69  /// <typeparam name="T"></typeparam>
70  public class VantagePointTree<T> : IVantagePointTree<T> {
71    #region properties
72    private readonly List<T> items;
73    private readonly Node root;
74    private readonly IDistance<T> distance;
75    #endregion
76
77    public VantagePointTree(IDistance<T> distance, IEnumerable<T> items) : base() {
78      root = null;
79      this.distance = distance;
80      this.items = items.Select(x => x).ToList();
81      root = BuildFromPoints(0, this.items.Count);
82    }
83
84    public void Search(T target, int k, out IList<T> results, out IList<double> distances) {
85      var heap = new PriorityQueue<double, IndexedItem<double>>(double.MaxValue, double.MinValue, k);
86      Search(root, target, k, heap, double.MaxValue);
87      var res = new List<T>();
88      var dist = new List<double>();
89      while (heap.Size > 0) {
90        res.Add(items[heap.PeekMinValue().Index]);
91        dist.Add(heap.PeekMinValue().Value);
92        heap.RemoveMin();
93      }
94      res.Reverse();
95      dist.Reverse();
96      results = res;
97      distances = dist;
98    }
99
100    private void Search(Node node, T target, int k, PriorityQueue<double, IndexedItem<double>> heap, double tau = double.MaxValue) {
101      if (node == null) return;
102      var dist = distance.Get(items[node.index], target);
103      if (dist < tau) {
104        if (heap.Size == k) heap.RemoveMin();
105        heap.Insert(-dist, new IndexedItem<double>(node.index, dist));//TODO check if minheap or maxheap should be used here
106        if (heap.Size == k) tau = heap.PeekMinValue().Value;
107      }
108      if (node.left == null && node.right == null) return;
109
110      if (dist < node.threshold) {
111        if (dist - tau <= node.threshold) Search(node.left, target, k, heap, tau);   // if there can still be neighbors inside the ball, recursively search left child first
112        if (dist + tau >= node.threshold) Search(node.right, target, k, heap, tau);  // if there can still be neighbors outside the ball, recursively search right child
113      } else {
114        if (dist + tau >= node.threshold) Search(node.right, target, k, heap, tau);  // if there can still be neighbors outside the ball, recursively search right child first
115        if (dist - tau <= node.threshold) Search(node.left, target, k, heap, tau);   // if there can still be neighbors inside the ball, recursively search left child
116      }
117    }
118
119    private Node BuildFromPoints(int lower, int upper) {
120      if (upper == lower)      // indicates that we're done here!
121        return null;
122
123      // Lower index is center of current node
124      var node = new Node { index = lower };
125      var r = new MersenneTwister(); //outer behaviour does not change with the random seed => no need to take the IRandom from the algorithm
126      if (upper - lower <= 1) return node; // if we did not arrive at leaf yet
127
128      // Choose an arbitrary point and move it to the start
129      var i = (int)(r.NextDouble() / 1 * (upper - lower - 1)) + lower;
130      items.Swap(lower, i);
131
132      // Partition around the median distance
133      var median = (upper + lower) / 2;
134      items.NthElement(lower + 1, upper - 1, median, distance.GetDistanceComparer(items[lower]));
135
136      // Threshold of the new node will be the distance to the median
137      node.threshold = distance.Get(items[lower], items[median]);
138
139      // Recursively build tree
140      node.index = lower;
141      node.left = BuildFromPoints(lower + 1, median);
142      node.right = BuildFromPoints(median, upper);
143
144      // Return result
145      return node;
146    }
147
148    private sealed class Node {
149      public int index;
150      public double threshold;
151      public Node left;
152      public Node right;
153
154      internal Node() {
155        index = 0;
156        threshold = 0;
157        left = null;
158        right = null;
159      }
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.