AgglomerativeClustering.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package de.jplag.clustering.algorithm;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. import org.apache.commons.math3.linear.RealMatrix;
  5. import de.jplag.clustering.ClusteringOptions;
  6. /**
  7. * Begin by assigning a cluster to each entity and then successively merge similar clusters.
  8. */
  9. public class AgglomerativeClustering implements GenericClusteringAlgorithm {
  10. private final ClusteringOptions options;
  11. public AgglomerativeClustering(ClusteringOptions options) {
  12. this.options = options;
  13. }
  14. @Override
  15. public Collection<Collection<Integer>> cluster(RealMatrix similarityMatrix) {
  16. int size = similarityMatrix.getRowDimension();
  17. // all clusters that do not have a parent yet
  18. Set<Cluster> clusters = new HashSet<>(size);
  19. // calculated similarities. Might contain connections to already visited
  20. // clusters, those need to be ignored.
  21. PriorityQueue<ClusterConnection> similarities;
  22. // initialization
  23. List<Cluster> initialClusters = new ArrayList<>(size);
  24. for (int i = 0; i < size; i++) {
  25. List<Integer> members = new ArrayList<>();
  26. members.add(i);
  27. Cluster cluster = new Cluster(members);
  28. initialClusters.add(cluster);
  29. clusters.add(cluster);
  30. }
  31. List<ClusterConnection> initialSimilarities = new ArrayList<>(size * (size - 1) / 2);
  32. for (int leftIndex = 0; leftIndex < initialClusters.size(); leftIndex++) {
  33. Cluster leftCluster = initialClusters.get(leftIndex);
  34. for (int rightIndex = leftIndex + 1; rightIndex < initialClusters.size(); rightIndex++) {
  35. Cluster rightCluster = initialClusters.get(rightIndex);
  36. initialSimilarities.add(new ClusterConnection(leftCluster, rightCluster, (float) similarityMatrix.getEntry(leftIndex, rightIndex)));
  37. }
  38. }
  39. similarities = new PriorityQueue<>(initialSimilarities);
  40. while (clusters.size() > 1) {
  41. ClusterConnection nearest = similarities.poll();
  42. // TODO Check that nearest is never null
  43. if (!(clusters.contains(nearest.left) && clusters.contains(nearest.right))) {
  44. // One cluster already part of another cluster
  45. continue;
  46. }
  47. if (nearest.similarity < options.getAgglomerativeThreshold()) {
  48. break;
  49. }
  50. clusters.remove(nearest.left);
  51. clusters.remove(nearest.right);
  52. nearest.left.submissions().addAll(nearest.right.submissions());
  53. Cluster combined = new Cluster(nearest.left.submissions());
  54. for (Cluster otherCluster : clusters) {
  55. float similarity = options.getAgglomerativeInterClusterSimilarity().clusterSimilarity(combined.submissions, otherCluster.submissions,
  56. similarityMatrix);
  57. similarities.add(new ClusterConnection(combined, otherCluster, similarity));
  58. }
  59. clusters.add(combined);
  60. }
  61. return clusters.stream().map(Cluster::submissions).collect(Collectors.toList());
  62. }
  63. private record ClusterConnection(Cluster left, Cluster right, float similarity) implements Comparable<ClusterConnection> {
  64. @Override
  65. public int compareTo(ClusterConnection other) {
  66. return (int) Math.signum(other.similarity - similarity);
  67. }
  68. }
  69. /**
  70. * Encapsulate a list in a class because we do not want hashing based on the members but on identity only. Also a
  71. * cluster need a different identity than the list because the lists are reused.
  72. */
  73. private record Cluster(List<Integer> submissions) {
  74. }
  75. }