Search in sources :

Example 1 with MTreeEntry

use of de.lmu.ifi.dbs.elki.index.tree.metrical.mtreevariants.MTreeEntry in project elki by elki-project.

the class MetricalIndexKNNQuery method getKNNForObject.

@Override
public KNNList getKNNForObject(O q, int k) {
    if (k < 1) {
        throw new IllegalArgumentException("At least one object has to be requested!");
    }
    index.statistics.countKNNQuery();
    KNNHeap knnList = DBIDUtil.newHeap(k);
    double d_k = Double.POSITIVE_INFINITY;
    final ComparableMinHeap<MTreeSearchCandidate> pq = new ComparableMinHeap<>();
    // Push the root node
    pq.add(new MTreeSearchCandidate(0., index.getRootID(), null, 0.));
    // search in tree
    while (!pq.isEmpty()) {
        MTreeSearchCandidate pqNode = pq.poll();
        if (knnList.size() >= k && pqNode.mindist > d_k) {
            break;
        }
        AbstractMTreeNode<?, ?, ?> node = index.getNode(pqNode.nodeID);
        DBID id_p = pqNode.routingObjectID;
        double d1 = pqNode.routingDistance;
        // directory node
        if (!node.isLeaf()) {
            for (int i = 0; i < node.getNumEntries(); i++) {
                MTreeEntry entry = node.getEntry(i);
                DBID o_r = entry.getRoutingObjectID();
                double r_or = entry.getCoveringRadius();
                double d2 = id_p != null ? entry.getParentDistance() : 0.;
                double diff = Math.abs(d1 - d2);
                double sum = d_k + r_or;
                if (diff <= sum) {
                    double d3 = distanceQuery.distance(o_r, q);
                    index.statistics.countDistanceCalculation();
                    double d_min = Math.max(d3 - r_or, 0.);
                    if (d_min <= d_k) {
                        pq.add(new MTreeSearchCandidate(d_min, ((DirectoryEntry) entry).getPageID(), o_r, d3));
                    }
                }
            }
        } else // data node
        {
            for (int i = 0; i < node.getNumEntries(); i++) {
                MTreeEntry entry = node.getEntry(i);
                DBID o_j = entry.getRoutingObjectID();
                double d2 = id_p != null ? entry.getParentDistance() : 0.;
                double diff = Math.abs(d1 - d2);
                if (diff <= d_k) {
                    double d3 = distanceQuery.distance(o_j, q);
                    index.statistics.countDistanceCalculation();
                    if (d3 <= d_k) {
                        knnList.insert(d3, o_j);
                        d_k = knnList.getKNNDistance();
                    }
                }
            }
        }
    }
    return knnList.toKNNList();
}
Also used : ComparableMinHeap(de.lmu.ifi.dbs.elki.utilities.datastructures.heap.ComparableMinHeap) DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) MTreeEntry(de.lmu.ifi.dbs.elki.index.tree.metrical.mtreevariants.MTreeEntry) DirectoryEntry(de.lmu.ifi.dbs.elki.index.tree.DirectoryEntry) KNNHeap(de.lmu.ifi.dbs.elki.database.ids.KNNHeap)

Example 2 with MTreeEntry

use of de.lmu.ifi.dbs.elki.index.tree.metrical.mtreevariants.MTreeEntry in project elki by elki-project.

the class MTreeIndex method insertAll.

@Override
public void insertAll(DBIDs ids) {
    List<MTreeEntry> objs = new ArrayList<>(ids.size());
    for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) {
        DBID id = DBIDUtil.deref(iter);
        final O object = relation.get(id);
        objs.add(createNewLeafEntry(id, object, Double.NaN));
    }
    insertAll(objs);
}
Also used : DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) MTreeEntry(de.lmu.ifi.dbs.elki.index.tree.metrical.mtreevariants.MTreeEntry) ArrayList(java.util.ArrayList) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 3 with MTreeEntry

use of de.lmu.ifi.dbs.elki.index.tree.metrical.mtreevariants.MTreeEntry in project elki by elki-project.

the class MetricalIndexRangeQuery method doRangeQuery.

/**
 * Performs a range query on the specified subtree. It recursively traverses
 * all paths from the specified node, which cannot be excluded from leading to
 * qualifying objects.
 *
 * @param o_p the routing object of the specified node
 * @param node the root of the subtree to be traversed
 * @param q the query object
 * @param r_q the query range
 * @param result the list holding the query results
 */
private void doRangeQuery(DBID o_p, AbstractMTreeNode<O, ?, ?> node, O q, double r_q, ModifiableDoubleDBIDList result) {
    double d1 = 0.;
    if (o_p != null) {
        d1 = distanceQuery.distance(o_p, q);
        index.statistics.countDistanceCalculation();
    }
    if (!node.isLeaf()) {
        for (int i = 0; i < node.getNumEntries(); i++) {
            MTreeEntry entry = node.getEntry(i);
            DBID o_r = entry.getRoutingObjectID();
            double r_or = entry.getCoveringRadius();
            double d2 = o_p != null ? entry.getParentDistance() : 0.;
            double diff = Math.abs(d1 - d2);
            double sum = r_q + r_or;
            if (diff <= sum) {
                double d3 = distanceQuery.distance(o_r, q);
                index.statistics.countDistanceCalculation();
                if (d3 <= sum) {
                    AbstractMTreeNode<O, ?, ?> child = index.getNode(((DirectoryEntry) entry).getPageID());
                    doRangeQuery(o_r, child, q, r_q, result);
                }
            }
        }
    } else {
        for (int i = 0; i < node.getNumEntries(); i++) {
            MTreeEntry entry = node.getEntry(i);
            DBID o_j = entry.getRoutingObjectID();
            double d2 = o_p != null ? entry.getParentDistance() : 0.;
            double diff = Math.abs(d1 - d2);
            if (diff <= r_q) {
                double d3 = distanceQuery.distance(o_j, q);
                index.statistics.countDistanceCalculation();
                if (d3 <= r_q) {
                    result.add(d3, o_j);
                }
            }
        }
    }
}
Also used : DBID(de.lmu.ifi.dbs.elki.database.ids.DBID) MTreeEntry(de.lmu.ifi.dbs.elki.index.tree.metrical.mtreevariants.MTreeEntry)

Aggregations

DBID (de.lmu.ifi.dbs.elki.database.ids.DBID)3 MTreeEntry (de.lmu.ifi.dbs.elki.index.tree.metrical.mtreevariants.MTreeEntry)3 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)1 KNNHeap (de.lmu.ifi.dbs.elki.database.ids.KNNHeap)1 DirectoryEntry (de.lmu.ifi.dbs.elki.index.tree.DirectoryEntry)1 ComparableMinHeap (de.lmu.ifi.dbs.elki.utilities.datastructures.heap.ComparableMinHeap)1 ArrayList (java.util.ArrayList)1