diff --git a/src/mhtree/MHTree.java b/src/mhtree/MHTree.java index e4a217fba55a23d3bfbd4f27b1261b2eedc01852..a053329a68b35a07dcfec58ae4d923589613e235 100644 --- a/src/mhtree/MHTree.java +++ b/src/mhtree/MHTree.java @@ -11,7 +11,6 @@ import messif.operations.data.InsertOperation; import messif.operations.query.ApproxKNNQueryOperation; import messif.operations.query.KNNQueryOperation; import messif.statistics.StatisticCounter; -import mhtree.benchmarking.Utils; import java.io.Serializable; import java.util.*; diff --git a/src/mhtree/Utils.java b/src/mhtree/Utils.java new file mode 100644 index 0000000000000000000000000000000000000000..b453edb2e616af278b4bc33d4777943d9ff4436e --- /dev/null +++ b/src/mhtree/Utils.java @@ -0,0 +1,46 @@ +package mhtree; + +import messif.objects.util.DistanceRankedObject; +import messif.objects.util.RankedAbstractObject; +import messif.operations.query.ApproxKNNQueryOperation; +import messif.operations.query.KNNQueryOperation; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class Utils { + + // comparing done based on distances, counts how many of the same distances of KNNQueryOperation were presents in the answer of ApproxKNNQueryOperation + public static double measureRecall(ApproxKNNQueryOperation approxKNNQueryOperation, KNNQueryOperation knnQueryOperation) { + if (approxKNNQueryOperation.getAnswerCount() == 0) return 0d; + if (knnQueryOperation.getAnswerCount() == 0) return -1d; + + List<RankedAbstractObject> objects = new ArrayList<>(knnQueryOperation.getAnswerCount()); + for (RankedAbstractObject object : knnQueryOperation) + objects.add(object); + + Map<Float, Long> frequencyMap = objects.parallelStream() + .map(DistanceRankedObject::getDistance) + .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); + + long trueCount = 0; + + for (RankedAbstractObject approxObject : approxKNNQueryOperation) { + float distance = approxObject.getDistance(); + if (frequencyMap.containsKey(distance)) { + long count = frequencyMap.get(distance); + if (count == 1) { + frequencyMap.remove(distance); + } else { + frequencyMap.replace(distance, count - 1); + } + trueCount++; + } + } + + return trueCount / (double) knnQueryOperation.getAnswerCount(); + } +}