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

ADD: distance measure in BuildTree, insert type as BuildTree parameter

parent 10bdf184
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,6 @@ package mhtree; ...@@ -3,7 +3,6 @@ package mhtree;
import cz.muni.fi.disa.similarityoperators.cover.AbstractRepresentation.PrecomputedDistances; import cz.muni.fi.disa.similarityoperators.cover.AbstractRepresentation.PrecomputedDistances;
import messif.buckets.BucketDispatcher; import messif.buckets.BucketDispatcher;
import messif.buckets.BucketStorageException; import messif.buckets.BucketStorageException;
import messif.buckets.LocalBucket;
import messif.objects.LocalAbstractObject; import messif.objects.LocalAbstractObject;
import java.util.*; import java.util.*;
...@@ -22,11 +21,13 @@ class BuildTree { ...@@ -22,11 +21,13 @@ class BuildTree {
private final PrecomputedDistances objectDistances; private final PrecomputedDistances objectDistances;
private final float[][] nodeDistances; private final float[][] nodeDistances;
private final InsertType insertType;
private final DistanceMeasure distanceMeasure;
private final BucketDispatcher bucketDispatcher; private final BucketDispatcher bucketDispatcher;
private Node root; private Node root;
BuildTree(List<LocalAbstractObject> objects, int leafCapacity, int arity, InsertType insertType, BucketDispatcher bucketDispatcher) throws BucketStorageException { BuildTree(List<LocalAbstractObject> objects, int leafCapacity, int arity, InsertType insertType, DistanceMeasure distanceMeasure, BucketDispatcher bucketDispatcher) throws BucketStorageException {
this.arity = arity; this.arity = arity;
this.leafCapacity = leafCapacity; this.leafCapacity = leafCapacity;
...@@ -38,17 +39,19 @@ class BuildTree { ...@@ -38,17 +39,19 @@ class BuildTree {
objectDistances = new PrecomputedDistances(objects); objectDistances = new PrecomputedDistances(objects);
nodeDistances = new float[nodes.length][nodes.length]; nodeDistances = new float[nodes.length][nodes.length];
this.insertType = insertType;
this.distanceMeasure = distanceMeasure;
this.bucketDispatcher = bucketDispatcher; this.bucketDispatcher = bucketDispatcher;
buildTree(insertType); buildTree();
} }
public Node getRoot() { public Node getRoot() {
return root; return root;
} }
private void buildTree(InsertType insertType) throws BucketStorageException { private void buildTree() throws BucketStorageException {
if (!initHullPoints(insertType)) return; if (!initHullPoints()) return;
precomputeHullDistances(); precomputeHullDistances();
...@@ -62,7 +65,7 @@ class BuildTree { ...@@ -62,7 +65,7 @@ class BuildTree {
notProcessedIndices.stream().forEach(restOfTheIndices::add); notProcessedIndices.stream().forEach(restOfTheIndices::add);
int mainIndex = restOfTheIndices.remove(0); int mainIndex = restOfTheIndices.remove(0);
mergeHulls(mainIndex, restOfTheIndices, insertType); mergeHulls(mainIndex, restOfTheIndices);
break; break;
} }
...@@ -78,16 +81,16 @@ class BuildTree { ...@@ -78,16 +81,16 @@ class BuildTree {
nnIndices.add(index); nnIndices.add(index);
} }
mergeHulls(furthestNodeIndex, nnIndices, insertType); mergeHulls(furthestNodeIndex, nnIndices);
} }
} }
root = nodes[validNodeIndices.nextSetBit(0)]; root = nodes[validNodeIndices.nextSetBit(0)];
} }
private boolean initHullPoints(InsertType insertType) throws BucketStorageException { private boolean initHullPoints() throws BucketStorageException {
if (objectDistances.getObjectCount() < leafCapacity) { if (objectDistances.getObjectCount() < leafCapacity) {
root = new LeafNode(objectDistances, bucketDispatcher.createBucket(), insertType); root = new LeafNode(objectDistances, bucketDispatcher.createBucket(), insertType, distanceMeasure);
return false; return false;
} }
...@@ -111,7 +114,7 @@ class BuildTree { ...@@ -111,7 +114,7 @@ class BuildTree {
// Select the rest of the objects up to the total of leafCapacity // Select the rest of the objects up to the total of leafCapacity
objects.addAll(findClosestObjects(furthestIndex, leafCapacity - 1, notProcessedObjectIndices)); objects.addAll(findClosestObjects(furthestIndex, leafCapacity - 1, notProcessedObjectIndices));
nodes[nodeIndex] = new LeafNode(objectDistances.getSubset(objects), bucketDispatcher.createBucket(), insertType); nodes[nodeIndex] = new LeafNode(objectDistances.getSubset(objects), bucketDispatcher.createBucket(), insertType, distanceMeasure);
} }
return true; return true;
...@@ -192,7 +195,7 @@ class BuildTree { ...@@ -192,7 +195,7 @@ class BuildTree {
} }
} }
private void mergeHulls(int mainHullIndex, List<Integer> otherHullIndices, InsertType insertType) { private void mergeHulls(int mainHullIndex, List<Integer> otherHullIndices) {
if (otherHullIndices.size() == 0) return; if (otherHullIndices.size() == 0) return;
List<Integer> indices = new ArrayList<>(otherHullIndices); List<Integer> indices = new ArrayList<>(otherHullIndices);
...@@ -200,7 +203,7 @@ class BuildTree { ...@@ -200,7 +203,7 @@ class BuildTree {
List<Node> nodes = indices.stream().map(i -> this.nodes[i]).collect(Collectors.toList()); List<Node> nodes = indices.stream().map(i -> this.nodes[i]).collect(Collectors.toList());
InternalNode parent = Node.createParent(nodes, objectDistances, insertType); InternalNode parent = Node.createParent(nodes, objectDistances, insertType, distanceMeasure);
nodes.forEach(node -> node.setParent(parent)); nodes.forEach(node -> node.setParent(parent));
parent.addChildren(nodes); parent.addChildren(nodes);
......
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