Skip to content
Snippets Groups Projects
Verified Commit 42fb9595 authored by David Procházka's avatar David Procházka
Browse files

ADD: LocalBucket into LeafNode

parent 1b7aa389
No related branches found
No related tags found
No related merge requests found
package mhtree;
import cz.muni.fi.disa.similarityoperators.cover.AbstractRepresentation;
import messif.buckets.BucketDispatcher;
import messif.buckets.BucketStorageException;
import messif.buckets.LocalBucket;
import messif.objects.LocalAbstractObject;
import java.util.*;
......@@ -18,10 +21,11 @@ class BuildTree {
private final AbstractRepresentation.PrecomputedDistances objectDistances;
private final float[][] nodeDistances;
private Node root;
private final BucketDispatcher bucketDispatcher;
private Node root;
BuildTree(List<LocalAbstractObject> objects, int leafCapacity, int numberOfChildren) {
BuildTree(List<LocalAbstractObject> objects, int leafCapacity, int numberOfChildren, BucketDispatcher bucketDispatcher) throws BucketStorageException {
this.numberOfChildren = numberOfChildren;
this.leafCapacity = leafCapacity;
......@@ -33,6 +37,8 @@ class BuildTree {
objectDistances = new AbstractRepresentation.PrecomputedDistances(objects);
nodeDistances = new float[nodes.length][nodes.length];
this.bucketDispatcher = bucketDispatcher;
buildTree();
}
......@@ -40,7 +46,7 @@ class BuildTree {
return root;
}
private void buildTree() {
private void buildTree() throws BucketStorageException {
if (!initHullPoints()) return;
precomputeHullDistances();
......@@ -78,9 +84,9 @@ class BuildTree {
root = nodes[validNodeIndices.nextSetBit(0)];
}
private boolean initHullPoints() {
private boolean initHullPoints() throws BucketStorageException {
if (objectDistances.getObjectCount() < leafCapacity) {
root = new LeafNode(objectDistances);
root = new LeafNode(objectDistances, bucketDispatcher.createBucket());
return false;
}
......@@ -89,7 +95,8 @@ class BuildTree {
for (int nodeIndex = 0; !notProcessedObjectIndices.isEmpty(); nodeIndex++) {
if (notProcessedObjectIndices.cardinality() < leafCapacity) {
notProcessedObjectIndices.stream().forEach(this::addObjectToClosestNode);
for (int i = notProcessedObjectIndices.nextSetBit(0); i >= 0; i = notProcessedObjectIndices.nextSetBit(i + 1))
addObjectToClosestNode(i);
return true;
}
......@@ -104,19 +111,20 @@ class BuildTree {
findClosestObjectIndices(furthestIndex, leafCapacity - 1, notProcessedObjectIndices)
.forEach(i -> objects.add(objectDistances.getObject(i)));
nodes[nodeIndex] = new LeafNode(objects);
nodes[nodeIndex] = new LeafNode(objects, bucketDispatcher.createBucket());
}
return true;
}
private void addObjectToClosestNode(int objectIndex) {
private void addObjectToClosestNode(int objectIndex) throws BucketStorageException {
int[] hullObjectIndices = getEveryHullObjectIndex();
int closestHullObjectIndex = hullObjectIndices[getNNIndex(objectIndex, hullObjectIndices)];
int nodeIndex = findCorrespondingHullIndex(objectDistances.getObject(closestHullObjectIndex));
nodes[nodeIndex] = LeafNode.addObject((LeafNode) nodes[nodeIndex], objectDistances.getObject(objectIndex));
LeafNode node = (LeafNode) nodes[nodeIndex];
node.addObject(objectDistances.getObject(objectIndex));
}
private int getNNIndex(int centerIndex, int[] dataIndices) {
......
package mhtree;
import messif.buckets.BucketStorageException;
import messif.buckets.impl.MemoryStorageBucket;
import messif.objects.AbstractObject;
import messif.objects.LocalAbstractObject;
import messif.objects.impl.ObjectFloatVectorNeuralNetworkL2;
......@@ -26,7 +28,7 @@ public class BuildTreeApp {
AbstractStreamObjectIterator<ObjectFloatVectorNeuralNetworkL2> iter = new StreamGenericAbstractObjectIterator<>(ObjectFloatVectorNeuralNetworkL2.class, arg);
AbstractObjectList<LocalAbstractObject> objects = new AbstractObjectList<>(iter);
MHTree tree = new MHTree(objects, 10, 5);
MHTree tree = new MHTree(objects, 10, 5, MemoryStorageBucket.class, null);
for (LocalAbstractObject object : objects) {
......@@ -47,7 +49,7 @@ public class BuildTreeApp {
}
}
} catch (IOException ex) {
} catch (IOException | BucketStorageException ex) {
Logger.getLogger("BuildTree").log(Level.SEVERE, null, ex);
}
}
......
......@@ -8,6 +8,7 @@ import java.util.List;
import java.util.stream.Stream;
public class InternalNode extends Node implements Serializable {
/**
* Serialization ID
*/
......
package mhtree;
import cz.muni.fi.disa.similarityoperators.cover.AbstractRepresentation;
import cz.muni.fi.disa.similarityoperators.cover.HullOptimizedRepresentationV3;
import messif.buckets.BucketStorageException;
import messif.buckets.LocalBucket;
import messif.objects.LocalAbstractObject;
import java.io.Serializable;
......@@ -8,23 +11,31 @@ import java.util.ArrayList;
import java.util.List;
public class LeafNode extends Node implements Serializable {
/**
* Serialization ID
*/
private static final long serialVersionUID = 1L;
private LocalBucket bucket;
LeafNode(List<LocalAbstractObject> objects) {
LeafNode(List<LocalAbstractObject> objects, LocalBucket bucket) throws BucketStorageException {
super(objects);
this.bucket = bucket;
this.bucket.addObjects(objects);
}
LeafNode(AbstractRepresentation.PrecomputedDistances distances) {
super(distances);
LeafNode(AbstractRepresentation.PrecomputedDistances distances, LocalBucket bucket) throws BucketStorageException {
this(distances.getObjects(), bucket);
}
public static LeafNode addObject(LeafNode node, LocalAbstractObject object) {
List<LocalAbstractObject> objects = new ArrayList<>(node.getObjects());
objects.add(object);
public void addObject(LocalAbstractObject object) throws BucketStorageException {
bucket.addObject(object);
return new LeafNode(objects);
if (isCovered(object)) return;
List<LocalAbstractObject> objects = new ArrayList<>(getObjects());
objects.add(object);
hull = new HullOptimizedRepresentationV3(objects);
}
}
package mhtree;
import messif.algorithms.Algorithm;
import messif.buckets.BucketDispatcher;
import messif.buckets.BucketStorageException;
import messif.buckets.LocalBucket;
import messif.objects.LocalAbstractObject;
import messif.operations.data.InsertOperation;
import messif.operations.query.ApproxKNNQueryOperation;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
public class MHTree extends Algorithm implements Serializable {
......@@ -20,19 +24,23 @@ public class MHTree extends Algorithm implements Serializable {
private final int numberOfChildren;
private final Node root;
private final BucketDispatcher bucketDispatcher;
@AlgorithmConstructor(description = "MH-Tree", arguments = {
"list of objects",
"number of objects in leaf node",
"number of children in internal node"
"number of children in internal node",
"storage class for buckets",
"storage class parameters"
})
MHTree(List<LocalAbstractObject> objects, int leafCapacity, int numberOfChildren) {
MHTree(List<LocalAbstractObject> objects, int leafCapacity, int numberOfChildren, Class<? extends LocalBucket> defaultBucketClass, Map<String, Object> bucketClassParams) throws BucketStorageException {
super("MH-Tree");
this.leafCapacity = leafCapacity;
this.numberOfChildren = numberOfChildren;
bucketDispatcher = new BucketDispatcher(Integer.MAX_VALUE, Long.MAX_VALUE, leafCapacity, 0, false, defaultBucketClass, bucketClassParams);
root = new BuildTree(objects, leafCapacity, numberOfChildren).getRoot();
root = new BuildTree(objects, leafCapacity, numberOfChildren, bucketDispatcher).getRoot();
}
public void approxKNN(ApproxKNNQueryOperation operation) {
......
......@@ -10,13 +10,14 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
public abstract class Node implements Serializable {
/**
* Serialization ID
*/
private static final long serialVersionUID = 420L;
private Node parent;
private HullOptimizedRepresentationV3 hull;
protected HullOptimizedRepresentationV3 hull;
Node(HullOptimizedRepresentationV3 hull) {
hull.build();
......
......@@ -3,6 +3,7 @@ package mhtree;
import messif.objects.LocalAbstractObject;
public class ObjectToNodeDistanceRank implements Comparable<ObjectToNodeDistanceRank> {
private final Node node;
private final LocalAbstractObject object;
private final float distance;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment