use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class RdKNNTree method doBulkReverseKNN.
/**
* Performs a bulk reverse knn query in the specified subtree.
*
* @param node the root node of the current subtree
* @param ids the object ids for which the rknn query is performed
* @param result the map containing the query results for each object
*/
private void doBulkReverseKNN(RdKNNNode node, DBIDs ids, Map<DBID, ModifiableDoubleDBIDList> result) {
if (node.isLeaf()) {
for (int i = 0; i < node.getNumEntries(); i++) {
RdKNNLeafEntry entry = (RdKNNLeafEntry) node.getEntry(i);
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
DBID id = DBIDUtil.deref(iter);
double distance = distanceQuery.distance(entry.getDBID(), id);
if (distance <= entry.getKnnDistance()) {
result.get(id).add(distance, entry.getDBID());
}
}
}
} else // node is a inner node
{
for (int i = 0; i < node.getNumEntries(); i++) {
RdKNNDirectoryEntry entry = (RdKNNDirectoryEntry) node.getEntry(i);
ModifiableDBIDs candidates = DBIDUtil.newArray();
for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
DBID id = DBIDUtil.deref(iter);
double minDist = distanceQuery.minDist(entry, id);
if (minDist <= entry.getKnnDistance()) {
candidates.add(id);
}
if (!candidates.isEmpty()) {
doBulkReverseKNN(getNode(entry), candidates, result);
}
}
}
}
}
use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class RdKNNTree method adjustKNNDistance.
/**
* Adjusts the knn distance in the subtree of the specified root entry.
*
* @param entry the root entry of the current subtree
* @param ids <em>Sorted</em> list of IDs
* @param knnLists a map of knn lists for each leaf entry
*/
private void adjustKNNDistance(RdKNNEntry entry, ArrayDBIDs ids, List<? extends KNNList> knnLists) {
RdKNNNode node = getNode(entry);
double knnDist_node = 0.;
if (node.isLeaf()) {
for (int i = 0; i < node.getNumEntries(); i++) {
RdKNNEntry leafEntry = node.getEntry(i);
DBID id = ((LeafEntry) leafEntry).getDBID();
int pos = ids.binarySearch(id);
if (pos >= 0) {
leafEntry.setKnnDistance(knnLists.get(pos).getKNNDistance());
}
knnDist_node = Math.max(knnDist_node, leafEntry.getKnnDistance());
}
} else {
for (int i = 0; i < node.getNumEntries(); i++) {
RdKNNEntry dirEntry = node.getEntry(i);
adjustKNNDistance(dirEntry, ids, knnLists);
knnDist_node = Math.max(knnDist_node, dirEntry.getKnnDistance());
}
}
entry.setKnnDistance(knnDist_node);
}
use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class VAFile method initialize.
@Override
public void initialize() {
setPartitions(relation);
for (DBIDIter iter = relation.iterDBIDs(); iter.valid(); iter.advance()) {
DBID id = DBIDUtil.deref(iter);
vectorApprox.add(calculateApproximation(id, relation.get(id)));
}
}
use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class SimplifiedCoverTree method bulkConstruct.
/**
* Bulk-load the cover tree.
*
* This bulk-load is slightly simpler than the one used in the original
* cover-tree source: We do not look back into the "far" set of candidates.
*
* @param cur Current routing object
* @param maxScale Maximum scale
* @param elems Candidates
* @return Root node of subtree
*/
protected Node bulkConstruct(DBIDRef cur, int maxScale, ModifiableDoubleDBIDList elems) {
assert (!elems.contains(cur));
final double max = maxDistance(elems);
final int scale = Math.min(distToScale(max) - 1, maxScale);
final int nextScale = scale - 1;
// elements remaining:
if (max <= 0 || scale <= scaleBottom || elems.size() < truncate) {
return new Node(cur, max, elems);
}
// Find neighbors in the cover of the current object:
ModifiableDoubleDBIDList candidates = DBIDUtil.newDistanceDBIDList();
excludeNotCovered(elems, scaleToDist(scale), candidates);
// If no elements were not in the cover, build a compact tree:
if (candidates.size() == 0) {
LOG.warning("Scale not chosen appropriately? " + max + " " + scaleToDist(scale));
return bulkConstruct(cur, nextScale, elems);
}
// We will have at least one other child, so build the parent:
Node node = new Node(cur, max);
// Routing element now is a singleton:
final boolean curSingleton = elems.size() == 0;
if (!curSingleton) {
// Add node for the routing object:
node.children.add(bulkConstruct(cur, nextScale, elems));
}
final double fmax = scaleToDist(nextScale);
// Build additional cover nodes:
for (DoubleDBIDListIter it = candidates.iter(); it.valid(); ) {
assert (it.getOffset() == 0);
DBID t = DBIDUtil.deref(it);
// Recycle.
elems.clear();
collectByCover(it, candidates, fmax, elems);
assert (DBIDUtil.equal(t, it)) : "First element in candidates must not change!";
if (elems.size() == 0) {
// Singleton
node.singletons.add(it);
} else {
// Build a full child node:
node.children.add(bulkConstruct(it, nextScale, elems));
}
candidates.removeSwap(0);
}
assert (candidates.size() == 0);
// Routing object is not yet handled:
if (curSingleton) {
if (node.isLeaf()) {
// First in leaf is enough.
node.children = null;
} else {
// Add as regular singleton.
node.singletons.add(cur);
}
}
// TODO: improve recycling of lists?
return node;
}
use of de.lmu.ifi.dbs.elki.database.ids.DBID in project elki by elki-project.
the class SimplifiedCoverTree method bulkLoad.
/**
* Bulk-load the index.
*
* @param ids IDs to load
*/
public void bulkLoad(DBIDs ids) {
if (ids.size() == 0) {
return;
}
assert (root == null) : "Tree already initialized.";
DBIDIter it = ids.iter();
DBID first = DBIDUtil.deref(it);
// Compute distances to all neighbors:
ModifiableDoubleDBIDList candidates = DBIDUtil.newDistanceDBIDList(ids.size() - 1);
for (it.advance(); it.valid(); it.advance()) {
candidates.add(distance(first, it), it);
}
root = bulkConstruct(first, Integer.MAX_VALUE, candidates);
}
Aggregations