001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030 031package org.openimaj.knn; 032 033import java.util.List; 034 035/** 036 * Interface for k-nearest-neighbour calculations with some data. 037 * 038 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 039 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 040 * 041 * @param <DATA> 042 * The type of data 043 * @param <DISTANCES> 044 * The type of distances measured (usually an array type) 045 * @param <PAIR_TYPE> 046 * The type of distance-index pair returned by the search methods 047 */ 048public interface NearestNeighbours<DATA, DISTANCES, PAIR_TYPE> { 049 050 /** 051 * Search for the nearest neighbour to each of the N queries, and return the 052 * index of each nearest neighbour and the respective distance. 053 * <p> 054 * For efficiency, to use this method, you need to pre-construct the arrays 055 * for storing the results outside of the method and pass them in as 056 * arguments. 057 * <p> 058 * <b>If a nearest-neighbour cannot be determined, it will have an index 059 * value of -1</b> 060 * 061 * @param qus 062 * An array of N query vectors 063 * @param indices 064 * The return N-dimensional array for holding the indices of the 065 * nearest neighbour of each respective query. 066 * @param distances 067 * The return N-dimensional array for holding the distances of 068 * the nearest neighbour to each respective query. 069 */ 070 public abstract void searchNN(final DATA[] qus, int[] indices, DISTANCES distances); 071 072 /** 073 * Search for the K nearest neighbours to each of the N queries, and return 074 * the indices of each nearest neighbour and their respective distances. 075 * <p> 076 * For efficiency, to use this method, you need to pre-construct the arrays 077 * for storing the results outside of the method and pass them in as 078 * arguments. 079 * <p> 080 * <b>If a k-th nearest-neighbour cannot be determined, it will have an 081 * index value of -1</b> 082 * 083 * @param qus 084 * An array of N query vectors 085 * @param K 086 * the number of neighbours to find 087 * @param indices 088 * The return N*K-dimensional array for holding the indices of 089 * the K nearest neighbours of each respective query. 090 * @param distances 091 * The return N*K-dimensional array for holding the distances of 092 * the nearest neighbours of each respective query. 093 */ 094 public abstract void searchKNN(final DATA[] qus, int K, int[][] indices, DISTANCES[] distances); 095 096 /** 097 * Search for the nearest neighbour to each of the N queries, and return the 098 * index of each nearest neighbour and the respective distance. 099 * <p> 100 * For efficiency, to use this method, you need to pre-construct the arrays 101 * for storing the results outside of the method and pass them in as 102 * arguments. 103 * <p> 104 * <b>If a nearest-neighbour cannot be determined, it will have an index 105 * value of -1</b> 106 * 107 * @param qus 108 * An array of N query vectors 109 * @param indices 110 * The return N-dimensional array for holding the indices of the 111 * nearest neighbour of each respective query. 112 * @param distances 113 * The return N-dimensional array for holding the distances of 114 * the nearest neighbour to each respective query. 115 */ 116 public abstract void searchNN(final List<DATA> qus, int[] indices, DISTANCES distances); 117 118 /** 119 * Search for the K nearest neighbours to each of the N queries, and return 120 * the indices of each nearest neighbour and their respective distances. 121 * <p> 122 * For efficiency, to use this method, you need to pre-construct the arrays 123 * for storing the results outside of the method and pass them in as 124 * arguments. 125 * <p> 126 * <b>If a k-th nearest-neighbour cannot be determined, it will have an 127 * index value of -1</b> 128 * 129 * @param qus 130 * An array of N query vectors 131 * @param K 132 * the number of neighbours to find 133 * @param indices 134 * The return N*K-dimensional array for holding the indices of 135 * the K nearest neighbours of each respective query. 136 * @param distances 137 * The return N*K-dimensional array for holding the distances of 138 * the nearest neighbours of each respective query. 139 */ 140 public abstract void searchKNN(final List<DATA> qus, int K, int[][] indices, DISTANCES[] distances); 141 142 /** 143 * Search for the K nearest neighbours to the given query and return an 144 * ordered list of pairs containing the distance and index of each 145 * neighbour. 146 * <p> 147 * If k neighbours cannot be determined, then the resultant list might have 148 * fewer than k elements. 149 * 150 * @param query 151 * the query vector 152 * @param K 153 * the number of neighbours to search for 154 * @return the top K nearest neighbours ordered by increasing distance 155 */ 156 public abstract List<PAIR_TYPE> searchKNN(DATA query, int K); 157 158 /** 159 * Search for the nearest neighbour to the given query and return a pair 160 * containing the distance and index of that neighbour. 161 * <p> 162 * If the nearest-neighbour cannot be determined <code>null</code> will be 163 * returned. 164 * 165 * @param query 166 * the query vector 167 * @return the distance and index of the nearest neighbour 168 */ 169 public abstract PAIR_TYPE searchNN(final DATA query); 170 171 /** 172 * Get the size of the dataset 173 * 174 * @return the dataset size 175 */ 176 public abstract int size(); 177}