Search in sources :

Example 31 with DBID

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);
                }
            }
        }
    }
}
Also used : DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) ModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 32 with DBID

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);
}
Also used : DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) LeafEntry(de.lmu.ifi.dbs.elki.index.tree.LeafEntry)

Example 33 with DBID

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)));
    }
}
Also used : DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 34 with DBID

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;
}
Also used : DoubleDBIDListIter(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter) ModifiableDoubleDBIDList(de.lmu.ifi.dbs.elki.database.ids.ModifiableDoubleDBIDList) DBID(de.lmu.ifi.dbs.elki.database.ids.DBID)

Example 35 with DBID

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);
}
Also used : ModifiableDoubleDBIDList(de.lmu.ifi.dbs.elki.database.ids.ModifiableDoubleDBIDList) DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Aggregations

DBID (de.lmu.ifi.dbs.elki.database.ids.DBID)42 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)20 ModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs)11 ArrayList (java.util.ArrayList)10 KNNList (de.lmu.ifi.dbs.elki.database.ids.KNNList)9 ModifiableDoubleDBIDList (de.lmu.ifi.dbs.elki.database.ids.ModifiableDoubleDBIDList)9 DoubleDBIDListIter (de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDListIter)6 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)5 HashSetModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs)5 Database (de.lmu.ifi.dbs.elki.database.Database)4 ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)4 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)4 HashMap (java.util.HashMap)4 KNNHeap (de.lmu.ifi.dbs.elki.database.ids.KNNHeap)3 MTreeEntry (de.lmu.ifi.dbs.elki.index.tree.metrical.mtreevariants.MTreeEntry)3 Test (org.junit.Test)3 AbstractClusterAlgorithmTest (de.lmu.ifi.dbs.elki.algorithm.clustering.AbstractClusterAlgorithmTest)2 SLINK (de.lmu.ifi.dbs.elki.algorithm.clustering.hierarchical.SLINK)2 CutDendrogramByHeight (de.lmu.ifi.dbs.elki.algorithm.clustering.hierarchical.extraction.CutDendrogramByHeight)2 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)2