/*
* Copyright (c) 2004-2005 Massachusetts Institute of Technology.
* All Rights Reserved.
*
* MIT grants permission to use, copy, modify, and distribute this software and
* its documentation for NON-COMMERCIAL purposes and without fee, provided that
* this copyright notice appears in all copies.
*
* MIT provides this software "as is," without representations or warranties of
* any kind, either expressed or implied, including but not limited to the
* implied warranties of merchantability, fitness for a particular purpose, and
* noninfringement. MIT shall not be liable for any damages arising from any
* use of this software.
*
* Author: Alexandr Andoni ([email protected]), Piotr Indyk ([email protected])
*/
/*
The main functionality of the LSH scheme is in this file (all except
the hashing of the buckets). This file includes all the functions
for processing a PRNearNeighborStructT data structure, which is the
main R-NN data structure based on LSH scheme. The particular
functions are: initializing a DS, adding new points to the DS, and
responding to queries on the DS.
*/
#include "headers.h"
void printRNNParameters(FILE *output, RNNParametersT parameters){
ASSERT(output != NULL);
fprintf(output, "R\n");
fprintf(output, "%0.9lf\n", parameters.parameterR);
fprintf(output, "Success probability\n");
fprintf(output, "%0.9lf\n", parameters.successProbability);
fprintf(output, "Dimension\n");
fprintf(output, "%d\n", parameters.dimension);
fprintf(output, "R^2\n");
fprintf(output, "%0.9lf\n", parameters.parameterR2);
fprintf(output, "Use <u> functions\n");
fprintf(output, "%d\n", parameters.useUfunctions);
fprintf(output, "k\n");
fprintf(output, "%d\n", parameters.parameterK);
fprintf(output, "m [# independent tuples of LSH functions]\n");
fprintf(output, "%d\n", parameters.parameterM);
fprintf(output, "L\n");
fprintf(output, "%d\n", parameters.parameterL);
fprintf(output, "W\n");
fprintf(output, "%0.9lf\n", parameters.parameterW);
fprintf(output, "T\n");
fprintf(output, "%d\n", parameters.parameterT);
fprintf(output, "typeHT\n");
fprintf(output, "%d\n", parameters.typeHT);
}
RNNParametersT readRNNParameters(FILE *input){
ASSERT(input != NULL);
RNNParametersT parameters;
char s[1000];// TODO: possible buffer overflow
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
FSCANF_REAL(input, ¶meters.parameterR);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
FSCANF_REAL(input, ¶meters.successProbability);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
fscanf(input, "%d", ¶meters.dimension);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
FSCANF_REAL(input, ¶meters.parameterR2);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
fscanf(input, "%d", ¶meters.useUfunctions);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
fscanf(input, "%d", ¶meters.parameterK);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
fscanf(input, "%d", ¶meters.parameterM);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
fscanf(input, "%d", ¶meters.parameterL);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
FSCANF_REAL(input, ¶meters.parameterW);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
fscanf(input, "%d", ¶meters.parameterT);
fscanf(input, "\n");fscanf(input, "%[^\n]\n", s);
fscanf(input, "%d", ¶meters.typeHT);
return parameters;
}
// Creates the LSH hash functions for the R-near neighbor structure
// <nnStruct>. The functions fills in the corresponding field of
// <nnStruct>.
void initHashFunctions(PRNearNeighborStructT nnStruct){
ASSERT(nnStruct != NULL);
LSHFunctionT **lshFunctions;
// allocate memory for the functions
FAILIF(NULL == (lshFunctions = (LSHFunctionT**)MALLOC(nnStruct->nHFTuples * sizeof(LSHFunctionT*))));
for(IntT i = 0; i < nnStruct->nHFTuples; i++){
FAILIF(NULL == (lshFunctions[i] = (LSHFunctionT*)MALLOC(nnStruct->hfTuplesLength * sizeof(LSHFunctionT))));
for(IntT j = 0; j < nnStruct->hfTuplesLength; j++){
FAILIF(NULL == (lshFunctions[i][j].a = (RealT*)MALLOC(nnStruct->dimension * sizeof(RealT))));
}
}
// initialize the LSH functions
for(IntT i = 0; i < nnStruct->nHFTuples; i++){
for(IntT j = 0; j < nnStruct->hfTuplesLength; j++){
// vector a
for(IntT d = 0; d < nnStruct->dimension; d++){
#ifdef USE_L1_DISTANCE
lshFunctions[i][j].a[d] = genCauchyRandom();
#else
lshFunctions[i][j].a[d] = genGaussianRandom();
#endif
}
// b
lshFunctions[i][j].b = genUniformRandom(0, nnStruct->parameterW);
}
}
nnStruct->lshFunctions = lshFunctions;
}
// Initializes the fields of a R-near neighbors data structure except
// the hash tables for storing the buckets.
PRNearNeighborStructT initializePRNearNeighborFields(RNNParametersT algParameters, Int32T nPointsEstimate){
PRNearNeighborStructT nnStruct;
FAILIF(NULL == (nnStruct = (PRNearNeighborStructT)MALLOC(sizeof(RNearNeighborStructT))));
nnStruct->parameterR = algParameters.parameterR;
nnStruct->parameterR2 = algParameters.parameterR2;
nnStruct->useUfunctions = algParameters.useUfunctions;
nnStruct->parameterK = algParameters.parameterK;
if (!algParameters.useUfunctions) {
// Use normal <g> functions.
nnStruct->parameterL = algParameters.parameterL;
nnStruct->nHFTuples = algParameters.parameterL;
nnStruct->hfTuplesLength = algParameters.parameterK;
}else{
// Use <u> hash functions; a <g> function is a pair of 2 <u> functions.
nnStruct->parameterL = algParameters.parameterL;
nnStruct->nHFTuples = algParameters.parameterM;
nnStruct->hfTuplesLength = algParameters.parameterK / 2;
}
nnStruct->parameterT = algParameters.parameterT;
nnStruct->dimension = algParameters.dimension;
nnStruct->parameterW = algParameters.parameterW;
nnStruct->nPoints = 0;
nnStruct->pointsArraySize = nPointsEstimate;
FAILIF(NULL == (nnStruct->points = (PPointT*)MALLOC(nnStruct->pointsArraySize * sizeof(PPointT))));
// create the hash functions
initHashFunctions(nnStruct);
// init fields that are used only in operations ("temporary" variables for operations).
// init the vector <pointULSHVectors> and the vector
// <precomputedHashesOfULSHs>
FAILIF(NULL == (nnStruct->pointULSHVectors = (Uns32T**)MALLOC(nnStruct->nHFTuples * sizeof(Uns32T*))));
for(IntT i = 0; i < nnStruct->nHFTuples; i++){
FAILIF(NULL == (nnStruct->pointULSHVectors[i] = (Uns32T*)MALLOC(nnStruct->hfTuplesLength * sizeof(Uns32T))));
}
FAILIF(NULL == (nnStruct->precomputedHashesOfULSHs = (Uns32T**)MALLOC(nnStruct->nHFTuples * sizeof(Uns32T*))));
for(IntT i = 0; i < nnStruct->nHFTuples; i++){
FAILIF(NULL == (nnStruct->precomputedHashesOfULSHs[i] = (Uns32T*)MALLOC(N_PRECOMPUTED_HASHES_NEEDED * sizeof(Uns32T))));
}
// init the vector <reducedPoint>
FAILIF(NULL == (nnStruct->reducedPoint = (RealT*)MALLOC(nnStruct->dimension * sizeof(RealT))));
// init the vector <nearPoints>
nnStruct->sizeMarkedPoints = nPointsEstimate;
FAILIF(NULL == (nnStruct->markedPoints = (BooleanT*)MALLOC(nnStruct->sizeMarkedPoints * sizeof(BooleanT))));
for(IntT i = 0; i < nnStruct->sizeMarkedPoints; i++){
nnStruct->markedPoints[i] = FALSE;
}
// init the vector <nearPointsIndeces>
FAILIF(NULL == (nnStruct->markedPointsIndeces = (Int32T*)MALLOC(nnStruct->sizeMarkedPoints * sizeof(Int32T))));
nnStruct->reportingResult = TRUE;
return nnStruct;
}
// Constructs a new empty R-near-neighbor data structure.
PRNearNeighborStructT initLSH(RNNParametersT algParameters, Int32T nPointsEstimate){
ASSERT(algParameters.typeHT == HT_LINKED_LIST || algParameters.typeHT == HT_STATISTICS);
PRNearNeighborStructT nnStruct = initializePRNearNeighborFields(algParameters, nPointsEstimate);
// initialize second level hashing (bucket hashing)
FAILIF(NULL == (nnStruct->hashedBuckets = (PUHashStructureT*)MALLOC(nnStruct->parameterL * sizeof(PUHashStructureT)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
E2LSH.zip (51个子文件)
E2LSH
bin
lsh 1KB
compile 911B
exact 420B
lsh_fromParams 618B
lsh_computeParams 991B
manual.ps 224KB
mnist1k.q 62KB
mnist1k.dts 6.73MB
Makefile 2KB
sources
SelfTuning.h 2KB
tags 30KB
SelfTuning.cpp 17KB
Random.h 1KB
BucketHashing.h 7KB
Util.cpp 2KB
Random.h.~1.2.~ 1001B
Geometry.cpp~ 2KB
LocalitySensitiveHashing.h 6KB
BasicDefinitions.h.~1.8.~ 7KB
compareOutputs.cpp 3KB
BasicDefinitions.h 7KB
Geometry.h.~1.2.~ 1KB
LSHMain.cpp 13KB
GlobalVars.cpp 770B
Util.h 1KB
GlobalVars.h 2KB
SelfTuning.cpp~ 18KB
BucketHashing.cpp 27KB
testFloat.cpp 801B
Random.h~ 1001B
exactNNs.cpp.~1.4.~ 5KB
LocalitySensitiveHashing.cpp 29KB
Random.cpp 3KB
LSHMain.cpp~ 13KB
convertMNIST.cpp 5KB
LSHMain.cpp.~1.17.~ 13KB
LocalitySensitiveHashing.cpp~ 29KB
exactNNs.cpp~ 5KB
LocalitySensitiveHashing.cpp.~1.7.~ 29KB
Geometry.cpp.~1.3.~ 2KB
Geometry.h 1KB
NearNeighbors.cpp 8KB
exactNNs.cpp 5KB
SelfTuning.cpp.~1.6.~ 18KB
Geometry.h~ 1KB
BasicDefinitions.h~ 7KB
NearNeighbors.h 1KB
Geometry.cpp 2KB
genPlantedDS.cpp 2KB
headers.h 1KB
genDS.cpp 2KB
共 51 条
- 1
JasonDing1354
- 粉丝: 1045
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页