Просмотр исходного кода

Merge branch 'master' into 192-webreport

# Conflicts:
#	jplag/pom.xml
#	jplag/src/main/java/de/jplag/CLI.java
Evgeni 4 лет назад
Родитель
Сommit
1a20445547
54 измененных файлов с 3596 добавлено и 100 удалено
  1. 11 0
      jplag.frontend.java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java
  2. 11 0
      jplag/pom.xml
  3. 44 14
      jplag/src/main/java/de/jplag/CLI.java
  4. 33 0
      jplag/src/main/java/de/jplag/CliGroupHelper.java
  5. 131 31
      jplag/src/main/java/de/jplag/CommandLineArgument.java
  6. 4 0
      jplag/src/main/java/de/jplag/JPlag.java
  7. 11 0
      jplag/src/main/java/de/jplag/JPlagResult.java
  8. 115 0
      jplag/src/main/java/de/jplag/clustering/Cluster.java
  9. 68 0
      jplag/src/main/java/de/jplag/clustering/ClusteringAdapter.java
  10. 14 0
      jplag/src/main/java/de/jplag/clustering/ClusteringAlgorithm.java
  11. 56 0
      jplag/src/main/java/de/jplag/clustering/ClusteringFactory.java
  12. 253 0
      jplag/src/main/java/de/jplag/clustering/ClusteringOptions.java
  13. 23 0
      jplag/src/main/java/de/jplag/clustering/ClusteringPreprocessor.java
  14. 102 0
      jplag/src/main/java/de/jplag/clustering/ClusteringResult.java
  15. 51 0
      jplag/src/main/java/de/jplag/clustering/IntegerMapping.java
  16. 36 0
      jplag/src/main/java/de/jplag/clustering/PreprocessedClusteringAlgorithm.java
  17. 33 0
      jplag/src/main/java/de/jplag/clustering/Preprocessing.java
  18. 48 0
      jplag/src/main/java/de/jplag/clustering/PreprocessorHelper.java
  19. 120 0
      jplag/src/main/java/de/jplag/clustering/algorithm/AgglomerativeClustering.java
  20. 835 0
      jplag/src/main/java/de/jplag/clustering/algorithm/BFGS.java
  21. 199 0
      jplag/src/main/java/de/jplag/clustering/algorithm/BayesianOptimization.java
  22. 197 0
      jplag/src/main/java/de/jplag/clustering/algorithm/GaussianProcess.java
  23. 13 0
      jplag/src/main/java/de/jplag/clustering/algorithm/GenericClusteringAlgorithm.java
  24. 44 0
      jplag/src/main/java/de/jplag/clustering/algorithm/InterClusterSimilarity.java
  25. 141 0
      jplag/src/main/java/de/jplag/clustering/algorithm/SpectralClustering.java
  26. 53 0
      jplag/src/main/java/de/jplag/clustering/preprocessors/CumulativeDistributionFunctionPreprocessor.java
  27. 51 0
      jplag/src/main/java/de/jplag/clustering/preprocessors/PercentileThresholdProcessor.java
  28. 43 0
      jplag/src/main/java/de/jplag/clustering/preprocessors/ThresholdPreprocessor.java
  29. 16 1
      jplag/src/main/java/de/jplag/options/JPlagOptions.java
  30. 8 2
      jplag/src/main/java/de/jplag/options/SimilarityMetric.java
  31. 14 0
      jplag/src/main/resources/de/jplag/messages.properties
  32. 1 4
      jplag/src/test/java/de/jplag/NewJavaFeaturesTest.java
  33. 36 0
      jplag/src/test/java/de/jplag/cli/ClusteringTest.java
  34. 53 0
      jplag/src/test/java/de/jplag/clustering/ClusterTest.java
  35. 53 0
      jplag/src/test/java/de/jplag/clustering/ClusteringAdapterTest.java
  36. 225 0
      jplag/src/test/java/de/jplag/clustering/ClusteringRealDataTest.java
  37. 56 0
      jplag/src/test/java/de/jplag/clustering/ClusteringResultTest.java
  38. 17 0
      jplag/src/test/java/de/jplag/clustering/algorithm/AgglomerativeClusteringTest.java
  39. 26 0
      jplag/src/test/java/de/jplag/clustering/algorithm/BayesianOptimizationTest.java
  40. 68 0
      jplag/src/test/java/de/jplag/clustering/algorithm/ClusteringData.java
  41. 37 0
      jplag/src/test/java/de/jplag/clustering/algorithm/GaussianProcessTest.java
  42. 17 0
      jplag/src/test/java/de/jplag/clustering/algorithm/SpectralClusteringTest.java
  43. 40 0
      jplag/src/test/java/de/jplag/clustering/preprocessors/CumulativeDistributionFunctionPreprocessorTest.java
  44. 43 0
      jplag/src/test/java/de/jplag/clustering/preprocessors/PercentilePreprocessorTest.java
  45. 89 0
      jplag/src/test/java/de/jplag/clustering/preprocessors/PreprocessingTestBase.java
  46. 41 0
      jplag/src/test/java/de/jplag/clustering/preprocessors/ThresholdPreprocessorTest.java
  47. 0 17
      jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/A/Java11.java
  48. 1 1
      jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/A/Java12.java
  49. 2 2
      jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/A/Java14.java
  50. 2 2
      jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/B/Java10.java
  51. 1 18
      jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/B/Java11.java
  52. 1 1
      jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/B/Java12.java
  53. 7 5
      jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/B/Java14.java
  54. 2 2
      pom.xml

+ 11 - 0
jplag.frontend.java/src/main/java/de/jplag/java/TokenGeneratingTreeScanner.java

@@ -28,6 +28,7 @@ import com.sun.source.tree.PackageTree;
 import com.sun.source.tree.ProvidesTree;
 import com.sun.source.tree.RequiresTree;
 import com.sun.source.tree.ReturnTree;
+import com.sun.source.tree.SwitchExpressionTree;
 import com.sun.source.tree.SwitchTree;
 import com.sun.source.tree.SynchronizedTree;
 import com.sun.source.tree.ThrowTree;
@@ -182,6 +183,16 @@ final class TokenGeneratingTreeScanner extends TreeScanner<Object, Object> {
         return ret;
     }
 
+    @Override
+    public Object visitSwitchExpression(SwitchExpressionTree node, Object parameterValue) {
+        long start = positions.getStartPosition(ast, node);
+        long end = positions.getEndPosition(ast, node);
+        parser.add(JavaTokenConstants.J_SWITCH_BEGIN, filename, map.getLineNumber(start), map.getColumnNumber(start), 6);
+        Object result = super.visitSwitchExpression(node, parameterValue);
+        parser.add(JavaTokenConstants.J_SWITCH_END, filename, map.getLineNumber(end - 1), map.getColumnNumber(end - 1), 1);
+        return result;
+    }
+
     @Override
     public Object visitCase(CaseTree node, Object p) {
         long n = positions.getStartPosition(ast, node);

+ 11 - 0
jplag/pom.xml

@@ -25,6 +25,17 @@
             <artifactId>jackson-databind</artifactId>
             <version>2.13.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-math3</artifactId>
+            <version>3.6.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>4.0.0</version>
+            <scope>test</scope>
+        </dependency>
 
         <dependency>
             <groupId>de.jplag</groupId>

+ 44 - 14
jplag/src/main/java/de/jplag/CLI.java

@@ -1,19 +1,9 @@
 package de.jplag;
 
-import static de.jplag.CommandLineArgument.BASE_CODE;
-import static de.jplag.CommandLineArgument.COMPARISON_MODE;
-import static de.jplag.CommandLineArgument.DEBUG;
-import static de.jplag.CommandLineArgument.EXCLUDE_FILE;
-import static de.jplag.CommandLineArgument.LANGUAGE;
-import static de.jplag.CommandLineArgument.MIN_TOKEN_MATCH;
-import static de.jplag.CommandLineArgument.RESULT_FOLDER;
-import static de.jplag.CommandLineArgument.ROOT_DIRECTORY;
-import static de.jplag.CommandLineArgument.SHOWN_COMPARISONS;
-import static de.jplag.CommandLineArgument.SIMILARITY_THRESHOLD;
-import static de.jplag.CommandLineArgument.SUBDIRECTORY;
-import static de.jplag.CommandLineArgument.SUFFIXES;
-import static de.jplag.CommandLineArgument.VERBOSITY;
+import static de.jplag.CommandLineArgument.*;
 
+import java.io.File;
+import java.util.Optional;
 import java.util.Random;
 
 import net.sourceforge.argparse4j.ArgumentParsers;
@@ -21,9 +11,14 @@ import net.sourceforge.argparse4j.inf.ArgumentParser;
 import net.sourceforge.argparse4j.inf.ArgumentParserException;
 import net.sourceforge.argparse4j.inf.Namespace;
 
+import de.jplag.clustering.ClusteringAlgorithm;
+import de.jplag.clustering.ClusteringOptions;
+import de.jplag.clustering.Preprocessing;
+import de.jplag.clustering.algorithm.InterClusterSimilarity;
 import de.jplag.exceptions.ExitException;
 import de.jplag.options.JPlagOptions;
 import de.jplag.options.LanguageOption;
+import de.jplag.options.SimilarityMetric;
 import de.jplag.options.Verbosity;
 import de.jplag.reporting2.JsonReport;
 import de.jplag.reporting2.Report;
@@ -70,8 +65,9 @@ public class CLI {
      */
     public CLI() {
         parser = ArgumentParsers.newFor(PROGRAM_NAME).build().defaultHelp(true).description(generateDescription());
+        CliGroupHelper groupHelper = new CliGroupHelper(parser);
         for (CommandLineArgument argument : CommandLineArgument.values()) {
-            argument.parseWith(parser);
+            argument.parseWith(parser, groupHelper);
         }
     }
 
@@ -114,6 +110,40 @@ public class CLI {
         options.setMaximumNumberOfComparisons(SHOWN_COMPARISONS.getFrom(namespace));
         ComparisonMode.fromName(COMPARISON_MODE.getFrom(namespace)).ifPresentOrElse(it -> options.setComparisonMode(it),
                 () -> System.out.println("Unknown comparison mode, using default mode!"));
+
+        ClusteringOptions.Builder clusteringBuilder = new ClusteringOptions.Builder();
+        Optional.ofNullable((Boolean) CLUSTER_ENABLE.getFrom(namespace)).ifPresent(clusteringBuilder::enabled);
+        Optional.ofNullable((ClusteringAlgorithm) CLUSTER_ALGORITHM.getFrom(namespace)).ifPresent(clusteringBuilder::algorithm);
+        Optional.ofNullable((SimilarityMetric) CLUSTER_METRIC.getFrom(namespace)).ifPresent(clusteringBuilder::similarityMetric);
+        Optional.ofNullable((Float) CLUSTER_SPECTRAL_BANDWIDTH.getFrom(namespace)).ifPresent(clusteringBuilder::spectralKernelBandwidth);
+        Optional.ofNullable((Float) CLUSTER_SPECTRAL_NOISE.getFrom(namespace)).ifPresent(clusteringBuilder::spectralGaussianProcessVariance);
+        Optional.ofNullable((Integer) CLUSTER_SPECTRAL_MIN_RUNS.getFrom(namespace)).ifPresent(clusteringBuilder::spectralMinRuns);
+        Optional.ofNullable((Integer) CLUSTER_SPECTRAL_MAX_RUNS.getFrom(namespace)).ifPresent(clusteringBuilder::spectralMaxRuns);
+        Optional.ofNullable((Integer) CLUSTER_SPECTRAL_KMEANS_ITERATIONS.getFrom(namespace))
+                .ifPresent(clusteringBuilder::spectralMaxKMeansIterationPerRun);
+        Optional.ofNullable((Float) CLUSTER_AGGLOMERATIVE_THRESHOLD.getFrom(namespace)).ifPresent(clusteringBuilder::agglomerativeThreshold);
+        Optional.ofNullable((InterClusterSimilarity) CLUSTER_AGGLOMERATIVE_INTER_CLUSTER_SIMILARITY.getFrom(namespace))
+                .ifPresent(clusteringBuilder::agglomerativeInterClusterSimilarity);
+        Optional.ofNullable((Boolean) CLUSTER_PREPROCESSING_NONE.getFrom(namespace)).ifPresent(none -> {
+            if (none) {
+                clusteringBuilder.preprocessor(Preprocessing.NONE);
+            }
+        });
+        Optional.ofNullable((Boolean) CLUSTER_PREPROCESSING_CDF.getFrom(namespace)).ifPresent(cdf -> {
+            if (cdf) {
+                clusteringBuilder.preprocessor(Preprocessing.CUMULATIVE_DISTRIBUTION_FUNCTION);
+            }
+        });
+        Optional.ofNullable((Float) CLUSTER_PREPROCESSING_PERCENTILE.getFrom(namespace)).ifPresent(percentile -> {
+            clusteringBuilder.preprocessor(Preprocessing.PERCENTILE);
+            clusteringBuilder.preprocessorPercentile(percentile);
+        });
+        Optional.ofNullable((Float) CLUSTER_PREPROCESSING_THRESHOLD.getFrom(namespace)).ifPresent(threshold -> {
+            clusteringBuilder.preprocessor(Preprocessing.THRESHOLD);
+            clusteringBuilder.preprocessorPercentile(threshold);
+        });
+        options.setClusteringOptions(clusteringBuilder.build());
+
         return options;
     }
 

+ 33 - 0
jplag/src/main/java/de/jplag/CliGroupHelper.java

@@ -0,0 +1,33 @@
+package de.jplag;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import net.sourceforge.argparse4j.inf.ArgumentContainer;
+import net.sourceforge.argparse4j.inf.ArgumentGroup;
+import net.sourceforge.argparse4j.inf.ArgumentParser;
+import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
+
+/**
+ * Can be used to automatically create and reuse {@link ArgumentGroup}s and {@link MutuallyExclusiveGroup}s through
+ * their names only. This is useful when an {@link ArgumentParser} is not configured in an imperative fashion.
+ */
+public class CliGroupHelper {
+
+    private final ArgumentParser parser;
+    private Map<String, MutuallyExclusiveGroup> mutuallyExclusiveGroups = new HashMap<>();
+    private Map<String, ArgumentGroup> argumentGroups = new HashMap<>();
+
+    public CliGroupHelper(ArgumentParser parser) {
+        this.parser = parser;
+    }
+
+    public ArgumentContainer getMutuallyExclusiveGroup(String name) {
+        return mutuallyExclusiveGroups.computeIfAbsent(name, parser::addMutuallyExclusiveGroup);
+    }
+
+    public ArgumentContainer getArgumentGroup(String name) {
+        return argumentGroups.computeIfAbsent(name, parser::addArgumentGroup);
+    }
+
+}

+ 131 - 31
jplag/src/main/java/de/jplag/CommandLineArgument.java

@@ -1,5 +1,7 @@
 package de.jplag;
 
+import static de.jplag.CLI.CLUSTERING_GROUP_NAME;
+import static de.jplag.CLI.CLUSTERING_PREPROCESSING_GROUP_NAME;
 import static de.jplag.options.JPlagOptions.DEFAULT_COMPARISON_MODE;
 import static de.jplag.options.JPlagOptions.DEFAULT_SHOWN_COMPARISONS;
 import static de.jplag.options.JPlagOptions.DEFAULT_SIMILARITY_THRESHOLD;
@@ -9,11 +11,18 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 
+import net.sourceforge.argparse4j.impl.Arguments;
 import net.sourceforge.argparse4j.inf.Argument;
+import net.sourceforge.argparse4j.inf.ArgumentAction;
+import net.sourceforge.argparse4j.inf.ArgumentContainer;
 import net.sourceforge.argparse4j.inf.ArgumentParser;
 import net.sourceforge.argparse4j.inf.Namespace;
 
+import de.jplag.clustering.ClusteringAlgorithm;
+import de.jplag.clustering.ClusteringOptions;
+import de.jplag.clustering.algorithm.InterClusterSimilarity;
 import de.jplag.options.LanguageOption;
+import de.jplag.options.SimilarityMetric;
 import de.jplag.strategy.ComparisonMode;
 
 /**
@@ -21,50 +30,84 @@ import de.jplag.strategy.ComparisonMode;
  * @author Timur Saglam
  */
 public enum CommandLineArgument {
-    ROOT_DIRECTORY("rootDir", NumberOfArgumentValues.ONE_OR_MORE_VALUES, String.class),
-    LANGUAGE("-l", String.class, LanguageOption.getDefault().getDisplayName(), LanguageOption.getAllDisplayNames()),
+
+    ROOT_DIRECTORY(new Builder("rootDir", String.class).nargs(NumberOfArgumentValues.ONE_OR_MORE_VALUES)),
+    LANGUAGE(new Builder("-l", String.class).defaultsTo(LanguageOption.getDefault().getDisplayName()).choices(LanguageOption.getAllDisplayNames())),
     BASE_CODE("-bc", String.class),
-    VERBOSITY("-v", String.class, "quiet", List.of("quiet", "long")), // TODO SH: Replace verbosity when integrating a real logging library
+    VERBOSITY(new Builder("-v", String.class).defaultsTo("quiet").choices(List.of("quiet", "long"))), // TODO SH: Replace verbosity when integrating a
+                                                                                                      // real logging library
     DEBUG("-d", Boolean.class),
     SUBDIRECTORY("-S", String.class),
     SUFFIXES("-p", String.class),
     EXCLUDE_FILE("-x", String.class),
     MIN_TOKEN_MATCH("-t", Integer.class),
-    SIMILARITY_THRESHOLD("-m", Float.class, DEFAULT_SIMILARITY_THRESHOLD),
-    SHOWN_COMPARISONS("-n", Integer.class, DEFAULT_SHOWN_COMPARISONS),
-    RESULT_FOLDER("-r", String.class, "result"),
-    COMPARISON_MODE("-c", String.class, DEFAULT_COMPARISON_MODE.getName(), ComparisonMode.allNames());
+    SIMILARITY_THRESHOLD(new Builder("-m", Float.class).defaultsTo(DEFAULT_SIMILARITY_THRESHOLD)),
+    SHOWN_COMPARISONS(new Builder("-n", Integer.class).defaultsTo(DEFAULT_SHOWN_COMPARISONS)),
+    RESULT_FOLDER(new Builder("-r", String.class).defaultsTo("result")),
+    COMPARISON_MODE(new Builder("-c", String.class).defaultsTo(DEFAULT_COMPARISON_MODE.getName()).choices(ComparisonMode.allNames())),
+    CLUSTER_ENABLE(new Builder("--cluster-skip", Boolean.class).argumentGroup(CLUSTERING_GROUP_NAME).action(Arguments.storeTrue())),
+    CLUSTER_ALGORITHM(
+            new Builder("--cluster-alg", ClusteringAlgorithm.class).argumentGroup(CLUSTERING_GROUP_NAME)
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getAlgorithm())),
+    CLUSTER_METRIC(
+            new Builder("--cluster-metric", SimilarityMetric.class).argumentGroup(CLUSTERING_GROUP_NAME)
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getSimilarityMetric())),
+    CLUSTER_SPECTRAL_BANDWIDTH(
+            new Builder("--cluster-spectral-bandwidth", Float.class).argumentGroup(CLUSTERING_GROUP_NAME).metaVar("bandwidth")
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getSpectralKernelBandwidth())),
+    CLUSTER_SPECTRAL_NOISE(
+            new Builder("--cluster-spectral-noise", Float.class).argumentGroup(CLUSTERING_GROUP_NAME).metaVar("noise")
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getSpectralGaussianProcessVariance())),
+    CLUSTER_SPECTRAL_MIN_RUNS(
+            new Builder("--cluster-spectral-min-runs", Integer.class).argumentGroup(CLUSTERING_GROUP_NAME).metaVar("min")
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getSpectralMinRuns())),
+    CLUSTER_SPECTRAL_MAX_RUNS(
+            new Builder("--cluster-spectral-max-runs", Integer.class).argumentGroup(CLUSTERING_GROUP_NAME).metaVar("max")
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getSpectralMaxRuns())),
+    CLUSTER_SPECTRAL_KMEANS_ITERATIONS(
+            new Builder("--cluster-spectral-kmeans-interations", Integer.class).argumentGroup(CLUSTERING_GROUP_NAME).metaVar("iterations")
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getSpectralMaxKMeansIterationPerRun())),
+    CLUSTER_AGGLOMERATIVE_THRESHOLD(
+            new Builder("--cluster-agglomerative-threshold", Float.class).argumentGroup(CLUSTERING_GROUP_NAME).metaVar("threshold")
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getAgglomerativeThreshold())),
+    CLUSTER_AGGLOMERATIVE_INTER_CLUSTER_SIMILARITY(
+            new Builder("--cluster-agglomerative-inter-cluster-similarity", InterClusterSimilarity.class).argumentGroup(CLUSTERING_GROUP_NAME)
+                    .defaultsTo(ClusteringOptions.DEFAULTS.getAgglomerativeInterClusterSimilarity())),
+    CLUSTER_PREPROCESSING_NONE(
+            new Builder("--cluster-pp-none", Boolean.class).mutuallyExclusiveGroup(CLUSTERING_PREPROCESSING_GROUP_NAME)
+                    .action(Arguments.storeTrue())),
+    CLUSTER_PREPROCESSING_CDF(
+            new Builder("--cluster-pp-cdf", Boolean.class).mutuallyExclusiveGroup(CLUSTERING_PREPROCESSING_GROUP_NAME).action(Arguments.storeTrue())),
+    CLUSTER_PREPROCESSING_PERCENTILE(
+            new Builder("--cluster-pp-percentile", Float.class).mutuallyExclusiveGroup(CLUSTERING_PREPROCESSING_GROUP_NAME).metaVar("percentile")),
+    CLUSTER_PREPROCESSING_THRESHOLD(
+            new Builder("--cluster-pp-threshold", Float.class).mutuallyExclusiveGroup(CLUSTERING_PREPROCESSING_GROUP_NAME).metaVar("threshold"));
 
     private final String flag;
     private final NumberOfArgumentValues numberOfValues;
     private final String description;
     private final Optional<Object> defaultValue;
     private final Optional<Collection<String>> choices;
+    private final Optional<String> argumentGroup;
+    private final Optional<String> mutuallyExclusiveGroup;
+    private final Optional<ArgumentAction> action;
+    private final Optional<String> metaVar;
     private final Class<?> type;
 
     private CommandLineArgument(String flag, Class<?> type) {
-        this(flag, NumberOfArgumentValues.SINGLE_VALUE, type, Optional.empty(), Optional.empty());
-    }
-
-    private CommandLineArgument(String flag, NumberOfArgumentValues numberOfValues, Class<?> type) {
-        this(flag, numberOfValues, type, Optional.empty(), Optional.empty());
-    }
-
-    private CommandLineArgument(String flag, Class<?> type, Object defaultValue) {
-        this(flag, NumberOfArgumentValues.SINGLE_VALUE, type, Optional.of(defaultValue), Optional.empty());
-    }
-
-    private CommandLineArgument(String flag, Class<?> type, Object defaultValue, Collection<String> choices) {
-        this(flag, NumberOfArgumentValues.SINGLE_VALUE, type, Optional.of(defaultValue), Optional.of(choices));
+        this(new Builder(flag, type));
     }
 
-    private CommandLineArgument(String flag, NumberOfArgumentValues numberOfValues, Class<?> type, Optional<Object> defaultValue,
-            Optional<Collection<String>> choices) {
-        this.flag = flag;
-        this.numberOfValues = numberOfValues;
-        this.type = type;
-        this.defaultValue = defaultValue;
-        this.choices = choices;
+    private CommandLineArgument(Builder builder) {
+        this.flag = builder.flag;
+        this.type = builder.type;
+        this.defaultValue = builder.defaultValue;
+        this.choices = builder.choices;
+        this.argumentGroup = builder.argumentGroup;
+        this.mutuallyExclusiveGroup = builder.mutuallyExclusiveGroup;
+        this.action = builder.action;
+        this.metaVar = builder.metaVar;
+        this.numberOfValues = builder.nargs.orElse(NumberOfArgumentValues.SINGLE_VALUE);
         this.description = retrieveDescriptionFromMessages();
     }
 
@@ -76,10 +119,10 @@ public enum CommandLineArgument {
     }
 
     /**
-     * @return the flag name of the command line argument without leading dashes.
+     * @return the flag name of the command line argument without leading dashes and inner dashes replaced with underscores.
      */
     public String flagWithoutDash() {
-        return flag.replace("-", "");
+        return flag.replaceAll("^-+", "").replaceAll("-", "_");
     }
 
     /**
@@ -111,10 +154,15 @@ public enum CommandLineArgument {
      * Parses the command line argument with a specific parser.
      * @param parser is that parser.
      */
-    public void parseWith(ArgumentParser parser) {
-        Argument argument = parser.addArgument(flag).help(description);
+    public void parseWith(ArgumentParser parser, CliGroupHelper groupHelper) {
+        ArgumentContainer argContainer = mutuallyExclusiveGroup.map(groupHelper::getMutuallyExclusiveGroup)
+                .or(() -> argumentGroup.map(groupHelper::getArgumentGroup)).orElse(parser);
+
+        Argument argument = argContainer.addArgument(flag).help(description);
         choices.ifPresent(it -> argument.choices(it));
         defaultValue.ifPresent(it -> argument.setDefault(it));
+        action.ifPresent(argument::action);
+        metaVar.ifPresent(argument::metavar);
         argument.type(type);
         if (type == Boolean.class) {
             argument.action(storeTrue());
@@ -136,4 +184,56 @@ public enum CommandLineArgument {
         }
         return Messages.getString(getClass().getSimpleName() + "." + builder.toString());
     }
+
+    private static class Builder {
+        private final String flag;
+        private final Class<?> type;
+        private Optional<Object> defaultValue = Optional.empty();
+        private Optional<Collection<String>> choices = Optional.empty();
+        private Optional<String> argumentGroup = Optional.empty();
+        private Optional<String> mutuallyExclusiveGroup = Optional.empty();
+        private Optional<ArgumentAction> action = Optional.empty();
+        private Optional<String> metaVar = Optional.empty();
+        private Optional<NumberOfArgumentValues> nargs = Optional.empty();
+
+        public Builder(String flag, Class<?> type) {
+            this.flag = flag;
+            this.type = type;
+        }
+
+        public Builder defaultsTo(Object defaultValue) {
+            this.defaultValue = Optional.of(defaultValue);
+            return this;
+        }
+
+        public Builder choices(Collection<String> choices) {
+            this.choices = Optional.of(choices);
+            return this;
+        }
+
+        public Builder argumentGroup(String argumentGroup) {
+            this.argumentGroup = Optional.of(argumentGroup);
+            return this;
+        }
+
+        public Builder mutuallyExclusiveGroup(String mutuallyExclusiveGroup) {
+            this.mutuallyExclusiveGroup = Optional.of(mutuallyExclusiveGroup);
+            return this;
+        }
+
+        public Builder action(ArgumentAction action) {
+            this.action = Optional.of(action);
+            return this;
+        }
+
+        public Builder metaVar(String metaVar) {
+            this.metaVar = Optional.of(metaVar);
+            return this;
+        }
+
+        public Builder nargs(NumberOfArgumentValues nargs) {
+            this.nargs = Optional.of(nargs);
+            return this;
+        }
+    }
 }

+ 4 - 0
jplag/src/main/java/de/jplag/JPlag.java

@@ -12,6 +12,7 @@ import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
 
+import de.jplag.clustering.ClusteringFactory;
 import de.jplag.exceptions.ExitException;
 import de.jplag.exceptions.SubmissionException;
 import de.jplag.options.JPlagOptions;
@@ -90,6 +91,9 @@ public class JPlag {
         // Compare valid submissions.
         JPlagResult result = comparisonStrategy.compareSubmissions(submissionSet);
         errorCollector.print("\nTotal time for comparing submissions: " + TimeUtil.formatDuration(result.getDuration()), null);
+
+        result.setClusteringResult(ClusteringFactory.getClusterings(result.getComparisons(), options.getClusteringOptions()));
+
         return result;
     }
 

+ 11 - 0
jplag/src/main/java/de/jplag/JPlagResult.java

@@ -2,6 +2,7 @@ package de.jplag;
 
 import java.util.List;
 
+import de.jplag.clustering.ClusteringResult;
 import de.jplag.options.JPlagOptions;
 
 /**
@@ -19,6 +20,8 @@ public class JPlagResult {
 
     private final int[] similarityDistribution; // 10-element array representing the similarity distribution of the detected matches.
 
+    private List<ClusteringResult<Submission>> clusteringResult;
+
     public JPlagResult(List<JPlagComparison> comparisons, SubmissionSet submissions, long durationInMillis, JPlagOptions options) {
         this.comparisons = comparisons;
         this.submissions = submissions;
@@ -37,6 +40,10 @@ public class JPlagResult {
         this.comparisons = this.getComparisons(limit);
     }
 
+    public void setClusteringResult(List<ClusteringResult<Submission>> clustering) {
+        this.clusteringResult = clustering;
+    }
+
     /**
      * @return a list of all comparisons sorted by percentage (descending)
      */
@@ -95,6 +102,10 @@ public class JPlagResult {
         return similarityDistribution;
     }
 
+    public List<ClusteringResult<Submission>> getClusteringResult() {
+        return this.clusteringResult;
+    }
+
     @Override
     public String toString() {
         return String.format("JPlagResult { comparisons: %d, duration: %d ms, language: %s, submissions: %d }", getComparisons().size(),

+ 115 - 0
jplag/src/main/java/de/jplag/clustering/Cluster.java

@@ -0,0 +1,115 @@
+package de.jplag.clustering;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.stream.Collectors;
+
+/**
+ * Cluster part of a {@link ClusteringResult}.
+ * @param <T> type of the clusters members
+ */
+public class Cluster<T> {
+
+    private float communityStrength;
+    private Collection<T> members;
+    private ClusteringResult<T> clusteringResult = null;
+
+    public Cluster(Collection<T> members, float communityStrength) {
+        this.members = new ArrayList<>(members);
+        this.communityStrength = communityStrength;
+    }
+
+    public Collection<T> getMembers() {
+        return members;
+    }
+
+    /**
+     * See {@link ClusteringResult#getCommunityStrength}
+     * @return community strength of the cluster
+     */
+    public float getCommunityStrength() {
+        return communityStrength;
+    }
+
+    /**
+     * Sets this clusters clustering result. Should only be called by classes extending {@link ClusteringResult} on their
+     * own clusters.
+     * @param clusteringResult the clustering result
+     */
+    public void setClusteringResult(ClusteringResult<T> clusteringResult) {
+        this.clusteringResult = clusteringResult;
+    }
+
+    /**
+     * @return How much each member of this cluster contributes to the {@link ClusteringResult#getCommunityStrength}.
+     */
+    public float getCommunityStrengthPerConnection() {
+        int size = getMembers().size();
+        if (size < 2)
+            return 0;
+        return getCommunityStrength() / connections();
+    }
+
+    /**
+     * Computes a normalized community strength per connection. Can be used as measure for strength of evidence in
+     * comparison to other clusters in the same clustering. Guaranteed to be smaller than 1. Negative values indicate
+     * non-clusters. This method may only be called on clusters that are part of a ClusteringResult.
+     * @return normalized community strength per connection
+     */
+    public float getNormalizedCommunityStrengthPerConnection() {
+        List<Cluster<T>> goodClusters = clusteringResult.getClusters().stream().filter(cluster -> cluster.getCommunityStrength() > 0)
+                .collect(Collectors.toList());
+        float posCommunityStrengthSum = (float) goodClusters.stream().mapToDouble(Cluster::getCommunityStrengthPerConnection).sum();
+
+        int size = clusteringResult.getClusters().size();
+        if (size < 2)
+            return getCommunityStrengthPerConnection();
+        return getCommunityStrengthPerConnection() / posCommunityStrengthSum;
+    }
+
+    /**
+     * How much this cluster is worth during optimization.
+     */
+    public double getWorth(BiFunction<T, T, Float> similarity) {
+        double communityStrength = getCommunityStrength();
+        if (getMembers().size() > 1) {
+            communityStrength /= connections();
+        }
+        double averageSimilarity = averageSimilarity(similarity);
+        return communityStrength * averageSimilarity;
+    }
+
+    /**
+     * Computes the average similarity inside the cluster.
+     * @param similarity function that supplies the similarity of two cluster members.
+     * @return average similarity
+     */
+    public float averageSimilarity(BiFunction<T, T, Float> similarity) {
+        List<T> members = new ArrayList<>(getMembers());
+        if (members.size() < 2) {
+            return 1;
+        }
+        float similaritySum = 0;
+        for (int i = 0; i < members.size(); i++) {
+            for (int j = i + 1; j < members.size(); j++) {
+                similaritySum += similarity.apply(members.get(i), members.get(j));
+            }
+        }
+        return similaritySum / connections();
+    }
+
+    private int connections() {
+        int size = getMembers().size();
+        return ((size - 1) * size) / 2;
+    }
+
+    /**
+     * Whether this cluster is very uninformative or wrong and should be pruned as last step of the clustering process.
+     * @return is bad
+     */
+    public boolean isBadCluster() {
+        return getMembers().size() < 2 || getCommunityStrength() < 0;
+    }
+}

+ 68 - 0
jplag/src/main/java/de/jplag/clustering/ClusteringAdapter.java

@@ -0,0 +1,68 @@
+package de.jplag.clustering;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+
+import de.jplag.JPlagComparison;
+import de.jplag.Submission;
+import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
+
+/**
+ * This class acts as an adapter between
+ * <ul>
+ * <li>the clustering algorithms (that operate on collections of integers)</li>
+ * <li>and the rest of the code base (that operates on {@link ClusteringResult}s of {@link Submission}s)</li>
+ * </ul>
+ */
+public class ClusteringAdapter {
+
+    private RealMatrix similarityMatrix;
+    private IntegerMapping<Submission> mapping;
+
+    /**
+     * Creates the clustering adapter. Only submissions that appear in those similarities might also appear in
+     * {@link ClusteringResult}s obtained from this adapter.
+     * @param comparisons that should be included in the process of clustering
+     * @param metric function that assigns a similarity to each comparison
+     */
+    public ClusteringAdapter(Collection<JPlagComparison> comparisons, Function<JPlagComparison, Float> metric) {
+        mapping = new IntegerMapping<>(comparisons.size());
+        for (JPlagComparison comparison : comparisons) {
+            mapping.map(comparison.getFirstSubmission());
+            mapping.map(comparison.getSecondSubmission());
+        }
+        int size = mapping.size();
+
+        similarityMatrix = new Array2DRowRealMatrix(size, size);
+        for (JPlagComparison comparison : comparisons) {
+            int firstIndex = mapping.map(comparison.getFirstSubmission());
+            int secondIndex = mapping.map(comparison.getSecondSubmission());
+            float similarity = metric.apply(comparison);
+            similarityMatrix.setEntry(firstIndex, secondIndex, similarity);
+            similarityMatrix.setEntry(secondIndex, firstIndex, similarity);
+        }
+    }
+
+    /**
+     * Use a generic clustering algorithm to cluster the submissions, that were included in this {@link ClusteringAdapter}'s
+     * comparison.
+     * @param algorithm that is used for clustering
+     * @return the clustered submissions
+     */
+    public ClusteringResult<Submission> doClustering(GenericClusteringAlgorithm algorithm) {
+        Collection<Collection<Integer>> intResult = algorithm.cluster(similarityMatrix);
+        ClusteringResult<Integer> modularityClusterResult = ClusteringResult.fromIntegerCollections(new ArrayList<>(intResult), similarityMatrix);
+        List<Cluster<Submission>> mappedClusters = modularityClusterResult.getClusters().stream().map(unmappedCluster -> {
+            return new Cluster<Submission>(unmappedCluster.getMembers().stream().map(mapping::unmap).collect(Collectors.toList()),
+                    unmappedCluster.getCommunityStrength());
+        }).collect(Collectors.toList());
+        return new ClusteringResult<>(mappedClusters, modularityClusterResult.getCommunityStrength());
+    }
+
+}

+ 14 - 0
jplag/src/main/java/de/jplag/clustering/ClusteringAlgorithm.java

@@ -0,0 +1,14 @@
+package de.jplag.clustering;
+
+import de.jplag.clustering.algorithm.AgglomerativeClustering;
+import de.jplag.clustering.algorithm.SpectralClustering;
+
+/**
+ * Choosable clustering algorithms
+ */
+public enum ClusteringAlgorithm {
+    /** {@link AgglomerativeClustering} */
+    AGGLOMERATIVE,
+    /** {@link SpectralClustering} */
+    SPECTRAL
+}

+ 56 - 0
jplag/src/main/java/de/jplag/clustering/ClusteringFactory.java

@@ -0,0 +1,56 @@
+package de.jplag.clustering;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import de.jplag.JPlagComparison;
+import de.jplag.Submission;
+import de.jplag.clustering.algorithm.AgglomerativeClustering;
+import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
+import de.jplag.clustering.algorithm.SpectralClustering;
+
+/**
+ * Runs the clustering according to an options object.
+ */
+public class ClusteringFactory {
+    public static List<ClusteringResult<Submission>> getClusterings(Collection<JPlagComparison> comparisons, ClusteringOptions options) {
+        if (!options.isEnabled())
+            return Collections.emptyList();
+
+        // init algorithm
+        GenericClusteringAlgorithm clusteringAlgorithm = null;
+        if (options.getAlgorithm() == ClusteringAlgorithm.AGGLOMERATIVE) {
+            clusteringAlgorithm = new AgglomerativeClustering(options);
+        } else if (options.getAlgorithm() == ClusteringAlgorithm.SPECTRAL) {
+            clusteringAlgorithm = new SpectralClustering(options);
+        }
+
+        // init preprocessor
+        Optional<ClusteringPreprocessor> preprocessor = options.getPreprocessor().constructPreprocessor(options);
+
+        if (preprocessor.isPresent()) {
+            // Package preprocessor into a clustering algorithm
+            clusteringAlgorithm = new PreprocessedClusteringAlgorithm(clusteringAlgorithm, preprocessor.orElseThrow());
+        }
+
+        // init adapter
+        ClusteringAdapter adapter = new ClusteringAdapter(comparisons, options.getSimilarityMetric());
+
+        // run clustering
+        ClusteringResult<Submission> result = adapter.doClustering(clusteringAlgorithm);
+
+        // remove bad clusters
+        result = removeBadClusters(result);
+
+        return List.of(result);
+    }
+
+    private static ClusteringResult<Submission> removeBadClusters(final ClusteringResult<Submission> clustering) {
+        List<Cluster<Submission>> filtered = clustering.getClusters().stream().filter(cluster -> !cluster.isBadCluster())
+                .collect(Collectors.toList());
+        return new ClusteringResult<>(filtered, clustering.getCommunityStrength());
+    }
+}

+ 253 - 0
jplag/src/main/java/de/jplag/clustering/ClusteringOptions.java

@@ -0,0 +1,253 @@
+
+package de.jplag.clustering;
+
+import de.jplag.clustering.algorithm.InterClusterSimilarity;
+import de.jplag.options.SimilarityMetric;
+
+/**
+ * Collection of all possible parameters that describe how a clustering should be performed.
+ */
+public class ClusteringOptions {
+
+    public static final ClusteringOptions DEFAULTS = new Builder().build();
+
+    private final SimilarityMetric similarityMetric;
+    private final float spectralKernelBandwidth;
+    private final float spectralGaussianProcessVariance;
+    private final int spectralMinRuns;
+    private final int spectralMaxRuns;
+    private final int spectralMaxKMeansIterationPerRun;
+    private final float agglomerativeThreshold;
+    private final Preprocessing preprocessor;
+    private final boolean enabled;
+    private final ClusteringAlgorithm algorithm;
+    private final InterClusterSimilarity agglomerativeInterClusterSimilarity;
+    private final float preprocessorThreshold;
+    private final float preprocessorPercentile;
+
+    /**
+     * @return The similarity metric is used for clustering
+     */
+    public SimilarityMetric getSimilarityMetric() {
+        return similarityMetric;
+    }
+
+    /**
+     * The kernel bandwidth for the matern kernel used in the gaussian process for the automatic search for the number of
+     * clusters in spectral clustering. Affects the runtime and results of the spectral clustering.
+     * @return kernel bandwidth for spectral clustering
+     */
+    public float getSpectralKernelBandwidth() {
+        return spectralKernelBandwidth;
+    }
+
+    /**
+     * This is the assumed level of noise in the evaluation results of a spectral clustering. Acts as normalization
+     * parameter for the Gaussian Process. The default setting works well with similarity scores in the range between zero
+     * and one. Affects the results of the spectral clustering.
+     * @return assumed variance of noise in Gaussian Process during spectral clustering.
+     */
+    public float getSpectralGaussianProcessVariance() {
+        return spectralGaussianProcessVariance;
+    }
+
+    /**
+     * The minimal number of times the kMeans algorithm is run for the spectral clustering. These runs will use predefined
+     * numbers of clusters and will not use the bayesian optimization to determine the number of clusters.
+     * @return minimal kMeans runs during spectral clustering
+     */
+    public int getSpectralMinRuns() {
+        return spectralMinRuns;
+    }
+
+    /**
+     * The maximal number of times the kMeans algorithm is run during the bayesian optimization for spectral clustering. The
+     * bayesian optimization may be stopped before, when no more maxima of the acquisition-function are found.
+     * @return maximal kMeans runs during spectral clustering
+     */
+    public int getSpectralMaxRuns() {
+        return spectralMaxRuns;
+    }
+
+    /**
+     * Maximum number of iterations of the kMeans clustering per run during spectral clustering.
+     * @return maximal kMeans iterations
+     */
+    public int getSpectralMaxKMeansIterationPerRun() {
+        return spectralMaxKMeansIterationPerRun;
+    }
+
+    /**
+     * Agglomerative clustering will merge clusters that have a similarity higher than this threshold.
+     * @return merging threshold for agglomerative clustering
+     */
+    public float getAgglomerativeThreshold() {
+        return agglomerativeThreshold;
+    }
+
+    /**
+     * Preprocessing for the similarity values before clustering. Preprocessing is mandatory for spectral clustering and
+     * optional for agglomerative clustering.
+     * @return preprocessor
+     */
+    public Preprocessing getPreprocessor() {
+        return preprocessor;
+    }
+
+    /**
+     * @return whether clustering should be performed
+     */
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    /**
+     * @return the clustering algorithm to use
+     */
+    public ClusteringAlgorithm getAlgorithm() {
+        return algorithm;
+    }
+
+    /**
+     * Similarity measure between clusters in agglomerative clustering.
+     * @return similarity measure
+     */
+    public InterClusterSimilarity getAgglomerativeInterClusterSimilarity() {
+        return agglomerativeInterClusterSimilarity;
+    }
+
+    /**
+     * @return up to which similarity the threshold-preprocessor zeroes out the similarities
+     */
+    public float getPreprocessorThreshold() {
+        return preprocessorThreshold;
+    }
+
+    /**
+     * @return up to which percentile of similarities the percentile-preprocessor zeroes out the similarities
+     */
+    public float getPreprocessorPercentile() {
+        return preprocessorPercentile;
+    }
+
+    public static class Builder {
+
+        private SimilarityMetric similarityMetric;
+        private float spectralKernelBandwidth;
+        private float spectralGaussianProcessVariance;
+        private int spectralMinRuns;
+        private int spectralMaxRuns;
+        private int spectralMaxKMeansIterationPerRun;
+        private float agglomerativeThreshold;
+        private Preprocessing preprocessor;
+        private boolean enabled;
+        private ClusteringAlgorithm algorithm;
+        private InterClusterSimilarity agglomerativeInterClusterSimilarity;
+        private float preprocessorThreshold;
+        private float preprocessorPercentile;
+
+        public Builder() {
+            // Setting the defaults here
+            similarityMetric(SimilarityMetric.MAX);
+            spectralKernelBandwidth(20.f);
+            spectralGaussianProcessVariance(0.05f * 0.05f);
+            spectralMinRuns(5);
+            spectralMaxRuns(50);
+            spectralMaxKMeansIterationPerRun(200);
+            agglomerativeThreshold(0.2f);
+            preprocessor(Preprocessing.CUMULATIVE_DISTRIBUTION_FUNCTION);
+            enabled(true);
+            algorithm(ClusteringAlgorithm.SPECTRAL);
+            agglomerativeInterClusterSimilarity(InterClusterSimilarity.AVERAGE);
+            preprocessorThreshold(0.2f);
+            preprocessorPercentile(0.5f);
+        }
+
+        public Builder similarityMetric(SimilarityMetric similarityMetric) {
+            this.similarityMetric = similarityMetric;
+            return Builder.this;
+        }
+
+        public Builder spectralKernelBandwidth(float spectralKernelBandwidth) {
+            this.spectralKernelBandwidth = spectralKernelBandwidth;
+            return Builder.this;
+        }
+
+        public Builder spectralGaussianProcessVariance(float spectralGPVariance) {
+            this.spectralGaussianProcessVariance = spectralGPVariance;
+            return Builder.this;
+        }
+
+        public Builder spectralMinRuns(int spectralMinRuns) {
+            this.spectralMinRuns = spectralMinRuns;
+            return Builder.this;
+        }
+
+        public Builder spectralMaxRuns(int spectralMaxRuns) {
+            this.spectralMaxRuns = spectralMaxRuns;
+            return Builder.this;
+        }
+
+        public Builder spectralMaxKMeansIterationPerRun(int spectralMaxKMeansIterationPerRun) {
+            this.spectralMaxKMeansIterationPerRun = spectralMaxKMeansIterationPerRun;
+            return Builder.this;
+        }
+
+        public Builder agglomerativeThreshold(float agglomerativeThreshold) {
+            this.agglomerativeThreshold = agglomerativeThreshold;
+            return Builder.this;
+        }
+
+        public Builder preprocessor(Preprocessing preprocessor) {
+            this.preprocessor = preprocessor;
+            return Builder.this;
+        }
+
+        public Builder enabled(boolean enabled) {
+            this.enabled = enabled;
+            return Builder.this;
+        }
+
+        public Builder algorithm(ClusteringAlgorithm algorithm) {
+            this.algorithm = algorithm;
+            return Builder.this;
+        }
+
+        public Builder agglomerativeInterClusterSimilarity(InterClusterSimilarity agglomerativeInterClusterSimilarity) {
+            this.agglomerativeInterClusterSimilarity = agglomerativeInterClusterSimilarity;
+            return Builder.this;
+        }
+
+        public Builder preprocessorThreshold(float preprocessorThreshold) {
+            this.preprocessorThreshold = preprocessorThreshold;
+            return Builder.this;
+        }
+
+        public Builder preprocessorPercentile(float preprocessorPercentile) {
+            this.preprocessorPercentile = preprocessorPercentile;
+            return Builder.this;
+        }
+
+        public ClusteringOptions build() {
+
+            return new ClusteringOptions(this);
+        }
+    }
+
+    private ClusteringOptions(Builder builder) {
+        this.similarityMetric = builder.similarityMetric;
+        this.spectralKernelBandwidth = builder.spectralKernelBandwidth;
+        this.spectralGaussianProcessVariance = builder.spectralGaussianProcessVariance;
+        this.spectralMinRuns = builder.spectralMinRuns;
+        this.spectralMaxRuns = builder.spectralMaxRuns;
+        this.spectralMaxKMeansIterationPerRun = builder.spectralMaxKMeansIterationPerRun;
+        this.agglomerativeThreshold = builder.agglomerativeThreshold;
+        this.preprocessor = builder.preprocessor;
+        this.enabled = builder.enabled;
+        this.algorithm = builder.algorithm;
+        this.agglomerativeInterClusterSimilarity = builder.agglomerativeInterClusterSimilarity;
+        this.preprocessorThreshold = builder.preprocessorThreshold;
+        this.preprocessorPercentile = builder.preprocessorPercentile;
+    }
+
+}

+ 23 - 0
jplag/src/main/java/de/jplag/clustering/ClusteringPreprocessor.java

@@ -0,0 +1,23 @@
+package de.jplag.clustering;
+
+/**
+ * Interface for classes that process similarity matrices before any clustering. Classes implementing this interface
+ * must ensure that they do not produce zero rows/columns inside the similarity matrix. They must also be able to
+ * calculate the original indices of rows/columns after use through their {@link ClusteringPreprocessor#originalIndexOf}
+ * method.
+ */
+public interface ClusteringPreprocessor {
+    /**
+     * Applies some preprocessing defined by the implementing class.
+     * @param similarityMatrix original similarities
+     * @return preprocessed similarities
+     */
+    double[][] preprocessSimilarities(double[][] similarityMatrix);
+
+    /**
+     * Maps the indices of the preprocessed similarity matrix back to indices in the original matrix.
+     * @param index after preprocessing
+     * @return corresponding index before preprocessing
+     */
+    int originalIndexOf(int index);
+}

+ 102 - 0
jplag/src/main/java/de/jplag/clustering/ClusteringResult.java

@@ -0,0 +1,102 @@
+package de.jplag.clustering;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.BiFunction;
+import java.util.stream.DoubleStream;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+
+/**
+ * Set of clusters dividing a set of entities.
+ * @param <T> type of the clustered entities (e.g. Submission)
+ */
+public class ClusteringResult<T> {
+
+    private final List<Cluster<T>> clusters;
+    private float communityStrength = 0;
+
+    public ClusteringResult(Collection<Cluster<T>> clusters, float communityStrength) {
+        this.clusters = Collections.unmodifiableList(new ArrayList<>(clusters));
+        this.communityStrength = communityStrength;
+        for (Cluster<T> cluster : clusters) {
+            cluster.setClusteringResult(this);
+        }
+    }
+
+    public Collection<Cluster<T>> getClusters() {
+        return Collections.unmodifiableList(clusters);
+    }
+
+    /**
+     * Community strength of the clustering. The expectation of community strength in a random graph is zero and it can not
+     * exceed one. It's the sum of it's clusters {@link Cluster#getCommunityStrength}. If the underlying network is not
+     * changed, a higher community strength denotes a better clustering. See: Finding and evaluating community structure in
+     * networks, M. E. J. Newman and M. Girvan, Phys. Rev. E 69, 026113 – Published 26 February 2004, Doi:
+     * 10.1103/PhysRevE.69.026113 It's called modularity in that paper.
+     * @return community strength
+     */
+    public float getCommunityStrength() {
+        return communityStrength;
+    }
+
+    /**
+     * How much this clustering result is worth during optimization.
+     * @param similarity
+     * @return worth
+     */
+    public float getWorth(BiFunction<T, T, Float> similarity) {
+        return (float) getClusters().stream().mapToDouble(c -> c.getWorth(similarity)).map(worth -> Double.isFinite(worth) ? worth : 0).average()
+                .getAsDouble();
+    }
+
+    /**
+     * Responsible for calculating the {@link ClusteringResult#getCommunityStrength} of a new clustering on integers and
+     * it's clusters.
+     */
+    public static ClusteringResult<Integer> fromIntegerCollections(List<Collection<Integer>> clustering, RealMatrix similarity) {
+        int numberOfSubmissions = similarity.getRowDimension();
+        Map<Integer, Integer> clusterIndicesOfSubmissionIndices = new HashMap<>();
+        int clusterIdx = 0;
+        for (Collection<Integer> cluster : clustering) {
+            for (Integer submissionIdx : cluster) {
+                clusterIndicesOfSubmissionIndices.put(submissionIdx, clusterIdx);
+            }
+            clusterIdx++;
+        }
+        List<Cluster<Integer>> clusters = new ArrayList<>(clustering.size());
+        float communityStrength = 0;
+        if (clustering.size() > 0) {
+            RealMatrix percentagesOfSimilaritySums = new Array2DRowRealMatrix(clustering.size(), clustering.size());
+            percentagesOfSimilaritySums = percentagesOfSimilaritySums.scalarMultiply(0);
+            for (int i = 0; i < numberOfSubmissions; i++) {
+                if (!clusterIndicesOfSubmissionIndices.containsKey(i))
+                    continue;
+                int clusterA = clusterIndicesOfSubmissionIndices.get(i);
+                for (int j = i + 1; j < numberOfSubmissions; j++) {
+                    if (!clusterIndicesOfSubmissionIndices.containsKey(j))
+                        continue;
+                    int clusterB = clusterIndicesOfSubmissionIndices.get(j);
+                    percentagesOfSimilaritySums.addToEntry(clusterA, clusterB, similarity.getEntry(i, j));
+                    percentagesOfSimilaritySums.addToEntry(clusterB, clusterA, similarity.getEntry(i, j));
+                }
+            }
+            percentagesOfSimilaritySums = percentagesOfSimilaritySums
+                    .scalarMultiply(1 / Arrays.stream(similarity.getData()).flatMapToDouble(DoubleStream::of).sum());
+            for (int i = 0; i < clustering.size(); i++) {
+                double outWeightSum = percentagesOfSimilaritySums.getRowVector(i).getL1Norm();
+                double clusterCommunityStrength = percentagesOfSimilaritySums.getEntry(i, i) - outWeightSum * outWeightSum;
+                clusters.add(new Cluster<Integer>(clustering.get(i), (float) clusterCommunityStrength));
+                communityStrength += clusterCommunityStrength;
+            }
+        }
+        return new ClusteringResult<>(clusters, communityStrength);
+    }
+
+}

+ 51 - 0
jplag/src/main/java/de/jplag/clustering/IntegerMapping.java

@@ -0,0 +1,51 @@
+package de.jplag.clustering;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Creates a mapping of any set to integers
+ */
+public class IntegerMapping<T> {
+
+    private HashMap<T, Integer> mapping = new HashMap<>();
+    private List<T> backMapping = new ArrayList<>();
+    private int size = 0;
+
+    public IntegerMapping(int initialCapacity) {
+        mapping = new HashMap<>(initialCapacity);
+        backMapping = new ArrayList<>(initialCapacity);
+    }
+
+    /**
+     * @param value is added to the mapping (if not already present)
+     * @return the associated integer
+     */
+    public int map(T value) {
+        Integer result = mapping.get(value);
+        if (result == null) {
+            int newIndex = size++;
+            mapping.put(value, newIndex);
+            backMapping.add(value);
+            return newIndex;
+        }
+        return result;
+    }
+
+    /**
+     * Maps the integer back to the original set.
+     * @param index the integer
+     * @return the original value
+     */
+    public T unmap(int index) {
+        return backMapping.get(index);
+    }
+
+    /**
+     * @return Number of unique values in the mapping
+     */
+    public int size() {
+        return size;
+    }
+}

+ 36 - 0
jplag/src/main/java/de/jplag/clustering/PreprocessedClusteringAlgorithm.java

@@ -0,0 +1,36 @@
+package de.jplag.clustering;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.stream.Collectors;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+
+import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
+
+/**
+ * Adapter class to put a preprocessor before any clustering algorithm.
+ */
+public class PreprocessedClusteringAlgorithm implements GenericClusteringAlgorithm {
+
+    private final GenericClusteringAlgorithm base;
+    private final ClusteringPreprocessor preprocessor;
+
+    public PreprocessedClusteringAlgorithm(GenericClusteringAlgorithm base, ClusteringPreprocessor preprocessor) {
+        this.base = base;
+        this.preprocessor = preprocessor;
+    }
+
+    @Override
+    public Collection<Collection<Integer>> cluster(RealMatrix similarityMatrix) {
+        double[][] data = preprocessor.preprocessSimilarities(similarityMatrix.getData());
+        if (data.length > 2) {
+            Collection<Collection<Integer>> preliminaryResult = base.cluster(new Array2DRowRealMatrix(data, false));
+            return preliminaryResult.stream().map(cluster -> cluster.stream().map(preprocessor::originalIndexOf).collect(Collectors.toList()))
+                    .collect(Collectors.toList());
+        }
+        return Collections.emptyList();
+    }
+
+}

+ 33 - 0
jplag/src/main/java/de/jplag/clustering/Preprocessing.java

@@ -0,0 +1,33 @@
+package de.jplag.clustering;
+
+import java.util.Optional;
+import java.util.function.Function;
+
+import antlr.preprocessor.Preprocessor;
+
+import de.jplag.clustering.preprocessors.CumulativeDistributionFunctionPreprocessor;
+import de.jplag.clustering.preprocessors.PercentileThresholdProcessor;
+import de.jplag.clustering.preprocessors.ThresholdPreprocessor;
+
+/**
+ * List of all usable {@link Preprocessor}s.
+ */
+public enum Preprocessing {
+    NONE(options -> null),
+    /** {@link CumulativeDistributionFunctionPreprocessor} */
+    CUMULATIVE_DISTRIBUTION_FUNCTION(options -> new CumulativeDistributionFunctionPreprocessor()),
+    /** {@link ThresholdPreprocessor} */
+    THRESHOLD(options -> new ThresholdPreprocessor(options.getPreprocessorThreshold())),
+    /** {@link PercentileThresholdProcessor} */
+    PERCENTILE(options -> new PercentileThresholdProcessor(options.getPreprocessorPercentile()));
+
+    private final Function<ClusteringOptions, ClusteringPreprocessor> constructor;
+
+    private Preprocessing(Function<ClusteringOptions, ClusteringPreprocessor> constructor) {
+        this.constructor = constructor;
+    }
+
+    public Optional<ClusteringPreprocessor> constructPreprocessor(ClusteringOptions options) {
+        return Optional.ofNullable(constructor.apply(options));
+    }
+}

+ 48 - 0
jplag/src/main/java/de/jplag/clustering/PreprocessorHelper.java

@@ -0,0 +1,48 @@
+package de.jplag.clustering;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.DoubleStream;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+
+/**
+ * Implements removal of zero rows / columns and calculation of original indices for classes implementing
+ * {@link ClusteringPreprocessor}.
+ */
+public class PreprocessorHelper {
+    private IntegerMapping<Integer> mapping;
+
+    /**
+     * Removes disconnected edges from the input matrix.
+     * @param connections similarity matrix
+     * @return similarity matrix without zero rows / cols
+     */
+    public double[][] removeDisconnectedEntries(double[][] connections) {
+        List<Integer> rowList = new ArrayList<>();
+        mapping = new IntegerMapping<>(connections.length);
+        RealMatrix similarity = new Array2DRowRealMatrix(connections, true);
+        for (int i = 0; i < similarity.getRowDimension(); i++) {
+            if (DoubleStream.of(similarity.getRow(i)).filter(x -> x > 0).findAny().isPresent()) {
+                rowList.add(i);
+                mapping.map(i);
+            }
+        }
+        int[] preservedRows = rowList.stream().mapToInt(Integer::intValue).toArray();
+        if (preservedRows.length == 0) {
+            return new double[0][];
+        }
+        similarity = similarity.getSubMatrix(preservedRows, preservedRows);
+        return similarity.getData();
+    }
+
+    /**
+     * Implements the logic for {@link ClusteringPreprocessor#originalIndexOf}
+     * @param index after preprocessing
+     * @return corresponding index before preprocessing
+     */
+    public int postProcessResult(int index) {
+        return mapping.unmap(index);
+    }
+}

+ 120 - 0
jplag/src/main/java/de/jplag/clustering/algorithm/AgglomerativeClustering.java

@@ -0,0 +1,120 @@
+package de.jplag.clustering.algorithm;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.commons.math3.linear.RealMatrix;
+
+import de.jplag.clustering.ClusteringOptions;
+
+/**
+ * Begin by assigning a cluster to each entity and then successively merge similar clusters.
+ */
+public class AgglomerativeClustering implements GenericClusteringAlgorithm {
+
+    private ClusteringOptions options;
+
+    public AgglomerativeClustering(ClusteringOptions options) {
+        this.options = options;
+    }
+
+    @Override
+    public Collection<Collection<Integer>> cluster(RealMatrix similarityMatrix) {
+        int size = similarityMatrix.getRowDimension();
+        // all clusters that do not have a parent yet
+        Set<Cluster> clusters = new HashSet<>(size);
+        // calculated similarities. Might contain connections to already visited
+        // clusters, those need to be ignored.
+        PriorityQueue<ClusterConnection> similarities;
+
+        {
+            // initialization
+
+            List<Cluster> initialClusters = new ArrayList<>(size);
+            for (int i = 0; i < size; i++) {
+                List<Integer> members = new ArrayList<>();
+                members.add(i);
+                Cluster cluster = new Cluster(members);
+                initialClusters.add(cluster);
+                clusters.add(cluster);
+            }
+
+            List<ClusterConnection> initialSimilarities = new ArrayList<>(size * (size - 1) / 2);
+
+            for (int leftIndex = 0; leftIndex < initialClusters.size(); leftIndex++) {
+                Cluster leftCluster = initialClusters.get(leftIndex);
+                for (int rightIndex = leftIndex + 1; rightIndex < initialClusters.size(); rightIndex++) {
+                    Cluster rightCluster = initialClusters.get(rightIndex);
+                    initialSimilarities
+                            .add(new ClusterConnection(leftCluster, rightCluster, (float) similarityMatrix.getEntry(leftIndex, rightIndex)));
+                }
+            }
+
+            similarities = new PriorityQueue<>(initialSimilarities);
+        }
+
+        while (clusters.size() > 1) {
+            ClusterConnection nearest = similarities.poll();
+            if (!(clusters.contains(nearest.left) && clusters.contains(nearest.right))) {
+                // One cluster already part of another cluster
+                continue;
+            }
+            if (nearest.similarity < options.getAgglomerativeThreshold()) {
+                break;
+            }
+            clusters.remove(nearest.left);
+            clusters.remove(nearest.right);
+            nearest.left.getSubmissions().addAll(nearest.right.getSubmissions());
+            Cluster combined = new Cluster(nearest.left.getSubmissions());
+            for (Cluster otherCluster : clusters) {
+                float similarity = options.getAgglomerativeInterClusterSimilarity().clusterSimilarity(combined.submissions, otherCluster.submissions,
+                        similarityMatrix);
+                similarities.add(new ClusterConnection(combined, otherCluster, similarity));
+            }
+            clusters.add(combined);
+        }
+
+        return clusters.stream().map(Cluster::getSubmissions).collect(Collectors.toList());
+    }
+
+    private class ClusterConnection implements Comparable<ClusterConnection> {
+
+        private Cluster left;
+        private Cluster right;
+        private float similarity;
+
+        public ClusterConnection(Cluster left, Cluster right, float similarity) {
+            this.left = left;
+            this.right = right;
+            this.similarity = similarity;
+        }
+
+        @Override
+        public int compareTo(ClusterConnection other) {
+            return (int) Math.signum(other.similarity - similarity);
+        }
+
+    }
+
+    /**
+     * Encapsulate a list in a class because we do not want hashing based on the members but on identity only. Also a
+     * cluster need a different identity than the list because the lists are reused.
+     */
+    private class Cluster {
+        private List<Integer> submissions;
+
+        public Cluster(List<Integer> submissions) {
+            this.submissions = submissions;
+        }
+
+        public List<Integer> getSubmissions() {
+            return submissions;
+        }
+    }
+
+}

+ 835 - 0
jplag/src/main/java/de/jplag/clustering/algorithm/BFGS.java

@@ -0,0 +1,835 @@
+/**
+ * The original version of this file originates from SMILE: https://github.com/haifengl/smile
+ */
+
+/*******************************************************************************
+ * Copyright (c) 2010-2020 Haifeng Li. All rights reserved. Smile is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version. Smile is distributed in the hope that it will
+ * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU
+ * Lesser General Public License along with Smile. If not, see <https://www.gnu.org/licenses/>.
+ ******************************************************************************/
+
+package de.jplag.clustering.algorithm;
+
+import static java.lang.Math.abs;
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+import static java.lang.Math.sqrt;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.commons.math3.linear.DecompositionSolver;
+import org.apache.commons.math3.linear.LUDecomposition;
+import org.apache.commons.math3.linear.MatrixUtils;
+import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.util.Pair;
+
+/**
+ * The Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm is an iterative method for solving unconstrained nonlinear
+ * optimization problems.
+ * <p>
+ * The BFGS method belongs to quasi-Newton methods, a class of hill-climbing optimization techniques that seek a
+ * stationary point of a (preferably twice continuously differentiable) function. For such problems, a necessary
+ * condition for optimality is that the gradient be zero. Newton's method and the BFGS methods are not guaranteed to
+ * converge unless the function has a quadratic Taylor expansion near an optimum. However, BFGS has proven to have good
+ * performance even for non-smooth optimizations.
+ * <p>
+ * In quasi-Newton methods, the Hessian matrix of second derivatives is not computed. Instead, the Hessian matrix is
+ * approximated using updates specified by gradient evaluations (or approximate gradient evaluations). Quasi-Newton
+ * methods are generalizations of the secant method to find the root of the first derivative for multidimensional
+ * problems. In multi-dimensional problems, the secant equation does not specify a unique solution, and quasi-Newton
+ * methods differ in how they constrain the solution. The BFGS method is one of the most popular members of this class.
+ * <p>
+ * Like the original BFGS, the limited-memory BFGS (L-BFGS) uses an estimation to the inverse Hessian matrix to steer
+ * its search through variable space, but where BFGS stores a dense <code>n × n</code> approximation to the inverse
+ * Hessian (<code>n</code> being the number of variables in the problem), L-BFGS stores only a few vectors that
+ * represent the approximation implicitly. Due to its resulting linear memory requirement, the L-BFGS method is
+ * particularly well suited for optimization problems with a large number of variables (e.g., &gt; 1000). Instead of the
+ * inverse Hessian <code>H_k</code>, L-BFGS maintains * a history of the past <code>m</code> updates of the position
+ * <code>x</code> and gradient <code>∇f(x)</code>, where generally the history size <code>m</code> can be small (often
+ * <code>m &lt; 10</code>). These updates are used to implicitly do operations requiring the <code>H_k</code>-vector
+ * product.
+ * <h2>References</h2>
+ * <ol>
+ * <li>Roger Fletcher. Practical methods of optimization.</li>
+ * <li>D. Liu and J. Nocedal. On the limited memory BFGS method for large scale optimization. Mathematical Programming B
+ * 45 (1989) 503-528.</li>
+ * <li>Richard H. Byrd, Peihuang Lu, Jorge Nocedal and Ciyou Zhu. A limited memory algorithm for bound constrained
+ * optimization.</li>
+ * </ol>
+ * @author Haifeng Li
+ */
+public class BFGS {
+    // private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(BFGS.class);
+    /** A number close to zero, between machine epsilon and its square root. */
+    private static final double EPSILON = Double.valueOf(System.getProperty("smile.bfgs.epsilon", "1E-10"));
+    /** The convergence criterion on function value. */
+    private static final double TOLF = 4 * EPSILON;
+    /** The scaled maximum step length allowed in line searches. */
+    private static final double STPMX = 100.0;
+
+    /**
+     * Minimize a function along a search direction by find a step which satisfies a sufficient decrease condition and a
+     * curvature condition.
+     * <p>
+     * At each stage this function updates an interval of uncertainty with endpoints <code>stx</code> and <code>sty</code>.
+     * The interval of uncertainty is initially chosen so that it contains a minimizer of the modified function
+     * <p>
+     * 
+     * <pre>
+     *      f(x+stp*s) - f(x) - ftol*stp*(gradf(x)'s).
+     * </pre>
+     * 
+     * If a step is obtained for which the modified function has a nonpositive function value and nonnegative derivative,
+     * then the interval of uncertainty is chosen so that it contains a minimizer of <code>f(x+stp*s)</code>.
+     * <p>
+     * The algorithm is designed to find a step which satisfies the sufficient decrease condition
+     * <p>
+     * 
+     * <pre>
+     *       f(x+stp*s) &lt;= f(X) + ftol*stp*(gradf(x)'s),
+     * </pre>
+     * 
+     * and the curvature condition
+     * <p>
+     * 
+     * <pre>
+     *       abs(gradf(x+stp*s)'s)) &lt;= gtol*abs(gradf(x)'s).
+     * </pre>
+     * 
+     * If <code>ftol</code> is less than <code>gtol</code> and if, for example, the function is bounded below, then there is
+     * always a step which satisfies both conditions. If no step can be found which satisfies both conditions, then the
+     * algorithm usually stops when rounding errors prevent further progress. In this case <code>stp</code> only satisfies
+     * the sufficient decrease condition.
+     * @param xold on input this contains the base point for the line search.
+     * @param fold on input this contains the value of the objective function at <code>x</code>.
+     * @param g on input this contains the gradient of the objective function at <code>x</code>.
+     * @param p the search direction.
+     * @param x on output, it contains <code>xold + &gamma;*p</code>, where &gamma; > 0 is the step length.
+     * @param stpmax specify upper bound for the step in the line search so that we do not try to evaluate the function in
+     * regions where it is undefined or subject to overflow.
+     * @return the new function value.
+     */
+    private static double linesearch(MultivariateFunction func, double[] xold, double fold, double[] g, double[] p, double[] x, double stpmax) {
+        if (stpmax <= 0) {
+            throw new IllegalArgumentException("Invalid upper bound of linear search step: " + stpmax);
+        }
+
+        // Termination occurs when the relative width of the interval
+        // of uncertainty is at most xtol.
+        final double xtol = EPSILON;
+        // Tolerance for the sufficient decrease condition.
+        final double ftol = 1.0E-4;
+
+        int n = xold.length;
+
+        // Scale if attempted step is too big
+        double pnorm = norm(p);
+        if (pnorm > stpmax) {
+            double r = stpmax / pnorm;
+            for (int i = 0; i < n; i++) {
+                p[i] *= r;
+            }
+        }
+
+        // Check if s is a descent direction.
+        double slope = 0.0;
+        for (int i = 0; i < n; i++) {
+            slope += g[i] * p[i];
+        }
+
+        if (slope >= 0) {
+            // throw new IllegalArgumentException("Line Search: the search direction is not a descent direction, which may be caused
+            // by roundoff problem.");
+        }
+
+        // Calculate minimum step.
+        double test = 0.0;
+        for (int i = 0; i < n; i++) {
+            double temp = abs(p[i]) / max(xold[i], 1.0);
+            if (temp > test) {
+                test = temp;
+            }
+        }
+
+        double alammin = xtol / test;
+        double alam = 1.0;
+
+        double alam2 = 0.0, f2 = 0.0;
+        double a, b, disc, rhs1, rhs2, tmpalam;
+        int runs = 50;
+        while (true) {
+            // Evaluate the function and gradient at stp
+            // and compute the directional derivative.
+            for (int i = 0; i < n; i++) {
+                x[i] = xold[i] + alam * p[i];
+            }
+
+            double f = func.f(x);
+
+            // Convergence on &Delta; x.
+            if (alam < alammin) {
+                System.arraycopy(xold, 0, x, 0, n);
+                return f;
+            } else if (f <= fold + ftol * alam * slope) {
+                // Sufficient function decrease.
+                return f;
+            } else if (runs-- < 0) {
+                return f;
+            } else {
+                // Backtrack
+                if (alam == 1.0) {
+                    // First time
+                    tmpalam = -slope / (2.0 * (f - fold - slope));
+                } else {
+                    // Subsequent backtracks.
+                    rhs1 = f - fold - alam * slope;
+                    rhs2 = f2 - fold - alam2 * slope;
+                    a = (rhs1 / (alam * alam) - rhs2 / (alam2 * alam2)) / (alam - alam2);
+                    b = (-alam2 * rhs1 / (alam * alam) + alam * rhs2 / (alam2 * alam2)) / (alam - alam2);
+                    if (a == 0.0) {
+                        tmpalam = -slope / (2.0 * b);
+                    } else {
+                        disc = b * b - 3.0 * a * slope;
+                        if (disc < 0.0) {
+                            tmpalam = 0.5 * alam;
+                        } else if (b <= 0.0) {
+                            tmpalam = (-b + sqrt(disc)) / (3.0 * a);
+                        } else {
+                            tmpalam = -slope / (b + sqrt(disc));
+                        }
+                    }
+                    if (tmpalam > 0.5 * alam) {
+                        tmpalam = 0.5 * alam;
+                    }
+                }
+            }
+            alam2 = alam;
+            f2 = f;
+            alam = max(tmpalam, 0.1 * alam);
+        }
+    }
+
+    /**
+     * This method solves the bound constrained minimization problem using the L-BFGS-B method. The L-BFGS-B algorithm
+     * extends L-BFGS to handle simple box constraints on variables; that is, constraints of the form li ≤ xi ≤ ui where li
+     * and ui are per-variable constant lower and upper bounds, respectively (for each xi, either or both bounds may be
+     * omitted). The method works by identifying fixed and free variables at every step (using a simple gradient method),
+     * and then using the L-BFGS method on the free variables only to get higher accuracy, and then repeating the process.
+     * @param func the function to be minimized.
+     * @param m the number of corrections used in the L-BFGS update. Values of <code>m</code> less than 3 are not
+     * recommended; large values of <code>m</code> will result in excessive computing time. <code>3 &lt;= m &lt;= 7</code>
+     * is recommended. A common choice for m is m = 5.
+     * @param x on initial entry this must be set by the user to the values of the initial estimate of the solution vector.
+     * On exit with <code>iflag = 0</code>, it contains the values of the variables at the best point found (usually a
+     * solution).
+     * @param l the lower bound.
+     * @param u the upper bound.
+     * @param gtol the convergence tolerance on zeroing the gradient.
+     * @param maxIter the maximum number of iterations.
+     * @return the minimum value of the function.
+     */
+    public static double minimize(DifferentiableMultivariateFunction func, int m, double[] x, double[] l, double[] u, double gtol, int maxIter) {
+        if (gtol <= 0.0) {
+            throw new IllegalArgumentException("Invalid gradient tolerance: " + gtol);
+        }
+
+        if (maxIter <= 0) {
+            throw new IllegalArgumentException("Invalid maximum number of iterations: " + maxIter);
+        }
+
+        if (m <= 0) {
+            throw new IllegalArgumentException("Invalid m: " + m);
+        }
+
+        if (l.length != x.length) {
+            throw new IllegalArgumentException("Invalid lower bound size: " + l.length);
+        }
+
+        if (u.length != x.length) {
+            throw new IllegalArgumentException("Invalid upper bound size: " + u.length);
+        }
+
+        int n = x.length;
+        double theta = 1.0;
+
+        Matrix Y = null, S = null;
+        Matrix W = new Matrix(n, 1);
+        Matrix M = new Matrix(1, 1);
+
+        ArrayList<double[]> yHistory = new ArrayList<>();
+        ArrayList<double[]> sHistory = new ArrayList<>();
+        double[] y = new double[n];
+        double[] s = new double[n];
+
+        double[] p = new double[n];
+        double[] g = new double[n];
+        double[] cauchy = new double[n];
+        double f = func.g(x, g);
+
+        double[] x_old = new double[n];
+        double[] g_old = new double[n];
+
+        // Upper limit for line search step.
+        double stpmax = STPMX * max(norm(x), n);
+
+        for (int iter = 1; iter <= maxIter; iter++) {
+            double f_old = f;
+            System.arraycopy(x, 0, x_old, 0, n);
+            System.arraycopy(g, 0, g_old, 0, n);
+
+            // STEP 2: compute the cauchy point
+            System.arraycopy(x, 0, cauchy, 0, n);
+            double[] c = cauchy(x, g, cauchy, l, u, theta, W, M);
+            clampToBound(cauchy, l, u);
+
+            // STEP 3: compute a search direction d_k by the primal method for the sub-problem
+            double[] subspaceMin = subspaceMinimization(x, g, cauchy, c, l, u, theta, W, M);
+            clampToBound(subspaceMin, l, u);
+
+            // STEP 4: perform line search
+            for (int i = 0; i < n; i++) {
+                p[i] = subspaceMin[i] - x[i];
+            }
+            linesearch(func, x_old, f_old, g, p, x, stpmax);
+            clampToBound(x, l, u);
+
+            for (double xi : x) {
+                if (Double.isNaN(xi) || Double.isInfinite(xi)) {
+                    // logger.warn("L-BFGS-B: bad x produced by line search, return previous good x");
+                    System.arraycopy(x_old, 0, x, 0, n);
+                    return f_old;
+                }
+            }
+
+            // STEP 5: compute gradient update current guess and function information
+            f = func.g(x, g);
+            if (Double.isNaN(f) || Double.isInfinite(f)) {
+                // logger.warn("L-BFGS-B: bad f(x) produced by line search, return previous good x");
+                System.arraycopy(x_old, 0, x, 0, n);
+                return f_old;
+            }
+
+            if (gnorm(x, g, l, u) < gtol) {
+                // logger.info(String.format("L-BFGS-B converges on gradient after %d iterations: %.5f", iter, f));
+                return f;
+            }
+
+            if (iter % 100 == 0) {
+                // logger.info(String.format("L-BFGS-B: the function value after %3d iterations: %.5f", iter, f));
+            }
+
+            // prepare for next iteration
+            for (int i = 0; i < n; i++) {
+                y[i] = g[i] - g_old[i];
+                s[i] = x[i] - x_old[i];
+            }
+
+            // STEP 6
+            double sy = dot(s, y);
+            double yy = dot(y, y);
+            double test = abs(sy);
+            if (test > EPSILON * yy) {
+                if (yHistory.size() >= m) {
+                    yHistory.remove(0);
+                    sHistory.remove(0);
+                }
+
+                yHistory.add(y);
+                sHistory.add(s);
+
+                int h = yHistory.size();
+                if (iter <= m) {
+                    Y = new Matrix(n, h);
+                    S = new Matrix(n, h);
+                    W = new Matrix(n, 2 * h);
+                    M = new Matrix(2 * h, 2 * h);
+                }
+
+                // STEP 7
+                theta = yy / sy;
+                for (int j = 0; j < h; j++) {
+                    double[] yj = yHistory.get(j);
+                    double[] sj = sHistory.get(j);
+
+                    for (int i = 0; i < n; i++) {
+                        Y.set(i, j, yj[i]);
+                        S.set(i, j, sj[i]);
+                        W.set(i, j, yj[i]);
+                        W.set(i, h + j, sj[i] * theta);
+                    }
+                }
+
+                Matrix SY = S.tm(Y);
+                Matrix SS = S.ata();
+                for (int j = 0; j < h; j++) {
+                    M.set(j, j, -SY.get(j, j));
+                    for (int i = 0; i <= j; i++) {
+                        M.set(h + i, j, 0.0);
+                        M.set(j, h + i, 0.0);
+                    }
+
+                    for (int i = j + 1; i < h; i++) {
+                        M.set(h + i, j, SY.get(i, j));
+                        M.set(j, h + i, SY.get(i, j));
+                    }
+
+                    for (int i = 0; i < h; i++) {
+                        M.set(h + i, h + j, theta * SS.get(i, j));
+                    }
+                }
+
+                // M.uplo(UPLO.LOWER);
+                M = M.inverse();
+            }
+
+            // logger.debug("L-BFGS-B iteration {} moves from {} to {} where f(x) = {}", iter, Arrays.toString(x_old),
+            // Arrays.toString(x), f);
+
+            if (abs(f_old - f) < TOLF) {
+                // logger.info(String.format("L-BFGS-B converges on f(x) after %d iterations: %.5f", iter, f));
+                return f;
+            }
+        }
+
+        // logger.warn(String.format("L-BFGS-B reaches maximum %d iterations: %.5f", maxIter, f));
+        return f;
+    }
+
+    private static double dot(double[] a, double[] b) {
+        if (a.length != b.length) {
+            throw new IllegalArgumentException("a and b have different lengths");
+        }
+        double sum = 0;
+        for (int i = 0; i < a.length; i++) {
+            sum += a[i] * b[i];
+        }
+        return sum;
+    }
+
+    private static double norm(double[] a) {
+        return Math.sqrt(dot(a, a));
+    }
+
+    /**
+     * Algorithm CP: Computation of the Generalized Cauchy Point. See page 8.
+     * @param x the parameter vector
+     * @param g the gradient vector
+     */
+    private static double[] cauchy(double[] x, double[] g, double[] cauchy, double[] l, double[] u, double theta, Matrix W, Matrix M) {
+        int n = x.length;
+        double[] t = new double[n];
+        double[] d = new double[n];
+
+        for (int i = 0; i < n; i++) {
+            t[i] = g[i] == 0 ? Double.MAX_VALUE : (g[i] < 0 ? (x[i] - u[i]) / g[i] : (x[i] - l[i]) / g[i]);
+            if (t[i] != 0.0)
+                d[i] = -g[i];
+        }
+
+        int[] index = sortWithIndex(t);
+        double[] p = W.tv(d);
+        double[] c = new double[p.length];
+        double fPrime = -dot(d, d);
+        double fDoublePrime = max(-theta * fPrime - M.xAx(p), EPSILON);
+        double f_dp_orig = fDoublePrime;
+        double dt_min = -fPrime / fDoublePrime;
+        double t_old = 0.0;
+
+        int i = 0;
+        for (; i < n; i++) {
+            if (t[index[i]] >= 0)
+                break;
+        }
+
+        double dt = i < n ? t[i] : 0;
+        for (; dt_min >= dt && i < n; i++) {
+            int b = index[i];
+            double tb = t[i];
+            dt = tb - t_old;
+
+            cauchy[b] = d[b] > 0 ? u[b] : (d[b] < 0 ? l[b] : cauchy[b]);
+            double zb = cauchy[b] - x[b];
+            for (int j = 0; j < c.length; j++) {
+                c[j] += p[j] * dt;
+            }
+
+            double gb = g[b];
+            double[] wbt = W.row(b);
+            fPrime += dt * fDoublePrime + gb * gb + theta * gb * zb - gb * dot(wbt, M.mv(c));
+            fDoublePrime -= theta * gb * gb + 2.0 * gb * dot(wbt, M.mv(p)) + gb * gb * M.xAx(wbt);
+            fDoublePrime = max(fDoublePrime, EPSILON * f_dp_orig);
+            for (int j = 0; j < p.length; j++) {
+                p[j] += wbt[j] * gb;
+            }
+
+            d[b] = 0;
+            dt_min = -fPrime / fDoublePrime;
+            t_old = tb;
+        }
+
+        dt_min = max(dt_min, 0.0);
+        t_old += dt_min;
+
+        for (int ii = i; ii < n; ii++) {
+            int si = index[ii];
+            cauchy[si] = x[si] + t_old * d[si];
+        }
+
+        for (int j = 0; j < c.length; j++) {
+            c[j] += p[j] * dt_min;
+        }
+
+        return c;
+    }
+
+    /**
+     * Minimization of the subspace of free variables. See Section 5 on page 9.
+     * @param x the parameter vector
+     * @param g the gradient vector
+     * @param cauchy the cauchy points
+     * @param c vector obtained from gcp() used to initialize the subspace minimization process
+     */
+    private static double[] subspaceMinimization(double[] x, double[] g, double[] cauchy, double[] c, double[] l, double[] u, double theta, Matrix W,
+            Matrix M) {
+        int n = x.length;
+        double thetaInverse = 1.0 / theta;
+        ArrayList<Integer> freeVarIdx = new ArrayList<>();
+        for (int i = 0; i < n; i++) {
+            if (cauchy[i] != u[i] && cauchy[i] != l[i]) {
+                freeVarIdx.add(i);
+            }
+        }
+
+        if (freeVarIdx.isEmpty()) {
+            return cauchy.clone();
+        }
+
+        int freeVarCount = freeVarIdx.size();
+        int[] freeVar = new int[freeVarCount];
+        for (int i = 0; i < freeVarCount; i++) {
+            freeVar[i] = freeVarIdx.get(i);
+        }
+
+        double[] wmc = W.mv(M.mv(c));
+        double[] r = new double[freeVarCount];
+        for (int i = 0; i < freeVarCount; i++) {
+            int fi = freeVar[i];
+            r[i] = g[fi] + (cauchy[fi] - x[fi]) * theta - wmc[fi];
+        }
+
+        Matrix WZ = W.row(freeVar);
+        double[] v = M.mv(WZ.tv(r));
+        Matrix N = WZ.ata().mul(-thetaInverse);
+        N = M.mm(N);
+        int n1 = N.nrows();
+        for (int i = 0; i < n1; i++) {
+            N.add(i, i, 1.0);
+        }
+
+        DecompositionSolver lu = N.lu();
+        v = lu.solve(new ArrayRealVector(v)).toArray();
+
+        double[] wzv = WZ.mv(v);
+
+        double[] du = new double[freeVarCount];
+        for (int i = 0; i < freeVarCount; i++) {
+            du[i] = -thetaInverse * (r[i] + wzv[i] * thetaInverse);
+        }
+        double alphaStar = findAlpha(cauchy, du, l, u, freeVar);
+        double[] dStar = new double[freeVarCount];
+        for (int i = 0; i < freeVarCount; i++) {
+            dStar[i] = du[i] * alphaStar;
+        }
+
+        double[] subspaceMin = cauchy.clone();
+        for (int i = 0; i < freeVarCount; i++) {
+            subspaceMin[freeVar[i]] += dStar[i];
+        }
+
+        return subspaceMin;
+    }
+
+    /**
+     * Find the alpha* parameter, a positive scalar. See Equation 5.8 on page 11.
+     * @param cauchy cauchy point
+     * @param du intermediate results used to find alpha*
+     * @param freeVar the indices of free variable
+     */
+    private static double findAlpha(double[] cauchy, double[] du, double[] l, double[] u, int[] freeVar) {
+        double alphaStar = 1.0;
+        int n = freeVar.length;
+
+        for (int i = 0; i < n; i++) {
+            int fi = freeVar[i];
+            alphaStar = du[i] > 0 ? min(alphaStar, (u[fi] - cauchy[fi]) / du[i]) : min(alphaStar, (l[fi] - cauchy[fi]) / du[i]);
+        }
+
+        return alphaStar;
+    }
+
+    /**
+     * Returns the infinity norm of gradient.
+     * @param x the parameter vector
+     * @param g the gradient vector
+     */
+    private static double gnorm(double[] x, double[] g, double[] l, double[] u) {
+        double norm = 0.0;
+        int n = x.length;
+
+        for (int i = 0; i < n; i++) {
+            double gi = g[i];
+            if (gi < 0)
+                gi = max(x[i] - u[i], gi);
+            else
+                gi = min(x[i] - l[i], gi);
+            norm = max(norm, abs(gi));
+        }
+
+        return norm;
+    }
+
+    /**
+     * Clamps the values of v to stay within the pre-defined bounds.
+     * @param v the vector to be adjusted
+     */
+    private static void clampToBound(double[] v, double[] l, double[] u) {
+        int n = v.length;
+
+        for (int i = 0; i < n; i++) {
+            if (v[i] > u[i])
+                v[i] = u[i];
+            else if (v[i] < l[i])
+                v[i] = l[i];
+        }
+    }
+
+    public interface MultivariateFunction {
+        /**
+         * Computes the value of the function at x.
+         * @param x a real vector.
+         * @return the function value.
+         */
+        double f(double[] x);
+    }
+
+    public interface DifferentiableMultivariateFunction extends MultivariateFunction {
+        /** A number close to zero, between machine epsilon and its square root. */
+        double EPSILON = Double.parseDouble(System.getProperty("smile.gradient.epsilon", "1E-8"));
+
+        /**
+         * Computes the value and gradient at x. The default implementation uses finite differences to calculate the gradient.
+         * When possible, the subclass should compute the gradient analytically.
+         * @param x a real vector.
+         * @param gradient the output variable of gradient.
+         * @return the function value.
+         */
+        default double g(double[] x, double[] gradient) {
+            double fx = f(x);
+
+            int n = x.length;
+            double[] xh = new double[n];
+            for (int i = 0; i < n; i++) {
+                System.arraycopy(x, 0, xh, 0, n);
+                double xi = x[i];
+                double h = EPSILON * Math.abs(xi);
+                if (h == 0.0) {
+                    h = EPSILON;
+                }
+                xh[i] = xi + h; // trick to reduce finite-precision error.
+                h = xh[i] - xi;
+
+                double fh = f(xh);
+                xh[i] = xi;
+                gradient[i] = (fh - fx) / h;
+            }
+
+            return fx;
+        }
+    }
+
+    private static int[] sortWithIndex(double[] array) {
+        List<Pair<Integer, Double>> sortedPairs = IntStream.range(0, array.length).mapToObj(idx -> Pair.create(idx, array[idx]))
+                .sorted(Comparator.comparingDouble(Pair::getValue)).collect(Collectors.toList());
+        double[] sortedDoubles = sortedPairs.stream().mapToDouble(Pair::getValue).toArray();
+        int[] sortedIndices = sortedPairs.stream().mapToInt(Pair::getKey).toArray();
+        System.arraycopy(sortedDoubles, 0, array, 0, array.length);
+        return sortedIndices;
+    }
+
+    /**
+     * This is a substitute for SMILE's matrix implementation.
+     */
+    private static class Matrix {
+
+        private RealMatrix d;
+        private int m;
+        private int n;
+
+        /**
+         * Constructor of zero matrix.
+         * @param m the number of rows.
+         * @param n the number of columns.
+         */
+        public Matrix(RealMatrix delegate) {
+            d = delegate;
+            this.m = delegate.getRowDimension();
+            this.n = delegate.getColumnDimension();
+        }
+
+        /**
+         * Constructor of zero matrix.
+         * @param m the number of rows.
+         * @param n the number of columns.
+         */
+        public Matrix(int m, int n) {
+            d = new Array2DRowRealMatrix(m, n);
+            this.m = m;
+            this.n = n;
+        }
+
+        /**
+         * Sets {@code A[i,j] = x}.
+         * @param i the row index.
+         * @param j the column index.
+         * @param x the matrix cell value.
+         */
+        public void set(int i, int j, double x) {
+            d.setEntry(i, j, x);
+        }
+
+        /**
+         * Returns matrix multiplication {@code A' * B}.
+         * @param B the operand.
+         * @return the multiplication.
+         */
+        public Matrix tm(Matrix B) {
+            if (d.getRowDimension() != B.d.getRowDimension()) {
+                throw new IllegalArgumentException(String.format("Matrix multiplication A' * B: %d x %d vs %d x %d", m, n, B.m, B.n));
+            }
+
+            return new Matrix(d.transpose().multiply(B.d));
+        }
+
+        /**
+         * Returns {@code A' * A}.
+         * @return {@code A' * A}.
+         */
+        public Matrix ata() {
+            Matrix C = new Matrix(d.transpose().multiply(d));
+            // C.mm(TRANSPOSE, 1this, NO_TRANSPOSE, this);
+            // C.uplo(LOWER);
+            return C;
+        }
+
+        /**
+         * Returns {@code A[i,j]}.
+         * @param i the row index.
+         * @param j the column index.
+         * @return the matrix cell value.
+         */
+        public double get(int i, int j) {
+            return d.getEntry(i, j);
+        }
+
+        /**
+         * Returns the inverse of matrix. For pseudo inverse, use QRDecomposition.
+         * @return the inverse of matrix.
+         */
+        public Matrix inverse() {
+            return new Matrix(MatrixUtils.inverse(d));
+        }
+
+        /**
+         * Returns Matrix-vector multiplication {@code A' * x}.
+         * @param x the vector.
+         * @return the matrix-vector multiplication {@code A' * x}.
+         */
+        public double[] tv(double[] x) {
+            return d.transpose().operate(x);
+        }
+
+        /**
+         * Returns the quadratic form {@code x' * A * x}. The left upper sub-matrix of A is used in the computation based on the
+         * size of x.
+         * @param x the vector.
+         * @return the quadratic form.
+         */
+        public double xAx(double[] x) {
+            if (m != n) {
+                throw new IllegalArgumentException(String.format("The matrix is not square: %d x %d", m, n));
+            }
+
+            if (n != x.length) {
+                throw new IllegalArgumentException(String.format("Matrix: %d x %d, Vector: %d", m, n, x.length));
+            }
+
+            double[] Ax = mv(x);
+            return new ArrayRealVector(x).dotProduct(new ArrayRealVector(Ax));
+        }
+
+        /**
+         * Returns the matrix-vector multiplication {@code A * x}.
+         * @param x the vector.
+         * @return the matrix-vector multiplication {@code A * x}.
+         */
+        public double[] mv(double[] x) {
+            return d.operate(x);
+        }
+
+        /**
+         * Returns the i-th row. Negative index -i means the i-th row from the end.
+         * @param i the row index.
+         * @return the row.
+         */
+        public double[] row(int i) {
+            i = i < 0 ? m + i : i;
+            return d.getRow(i);
+        }
+
+        /**
+         * Returns the matrix of selected rows. Negative index -i means the i-th row from the end.
+         * @param rows the row indices.
+         * @return the sub-matrix.
+         */
+        public Matrix row(int... rows) {
+            double[][] data = new double[rows.length][n];
+
+            for (int i = 0; i < rows.length; i++) {
+                double[] row = row(rows[i]);
+                System.arraycopy(row, 0, data[i], 0, n);
+            }
+
+            return new Matrix(new Array2DRowRealMatrix(data, false));
+        }
+
+        public Matrix mul(double e) {
+            return new Matrix(d.scalarMultiply(e));
+        }
+
+        public Matrix mm(Matrix n2) {
+            return new Matrix(d.multiply(n2.d));
+        }
+
+        public int nrows() {
+            return m;
+        }
+
+        public void add(int m, int n, double e) {
+            d.addToEntry(m, n, e);
+        }
+
+        public DecompositionSolver lu() {
+            return new LUDecomposition(d).getSolver();
+        }
+
+    }
+}

+ 199 - 0
jplag/src/main/java/de/jplag/clustering/algorithm/BayesianOptimization.java

@@ -0,0 +1,199 @@
+package de.jplag.clustering.algorithm;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Spliterator;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+import org.apache.commons.math3.distribution.NormalDistribution;
+import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.OutOfRangeException;
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.commons.math3.linear.RealVector;
+import org.apache.commons.math3.random.HaltonSequenceGenerator;
+import org.apache.commons.math3.random.RandomVectorGenerator;
+
+/**
+ * Maximizes a function using bayesian optimization.
+ */
+public class BayesianOptimization {
+
+    private static final int STOP_AFTER_CONSECUTIVE_RANDOM_PICKS = 3;
+    private static final int MAX_NON_ZERO_ACQ_FN_EVALS_PER_ITERATION = 50;
+    private static final int MAXIMUM_ACQ_FN_EVALS_PER_ITERATION = 1000;
+
+    private RealVector minima;
+    private RealVector maxima;
+    private int maxEvaluations;
+    private int initialPoints;
+    private double noise;
+    private RealVector lengthScale;
+    private boolean debug = false;
+
+    /**
+     * @param minima of the explored parameters
+     * @param maxima of the explored parameters
+     * @param initPoints points that are initially sampled for exploration
+     * @param maxEvaluations maximal evaluations of the fitted function
+     * @param noise of the explored function
+     * @param lengthScale width parameter for the matern kernel
+     */
+    public BayesianOptimization(RealVector minima, RealVector maxima, int initPoints, int maxEvaluations, double noise, RealVector lengthScale) {
+        if (minima.getDimension() != maxima.getDimension()) {
+            throw new DimensionMismatchException(minima.getDimension(), maxima.getDimension());
+        }
+        if (initPoints < 1 || initPoints > maxEvaluations) {
+            throw new OutOfRangeException(initPoints, 1, maxEvaluations);
+        }
+        this.maxima = maxima;
+        this.minima = minima;
+        this.initialPoints = initPoints;
+        this.maxEvaluations = maxEvaluations;
+        this.noise = noise;
+        this.lengthScale = lengthScale;
+    }
+
+    public void setDebugLogging(boolean debug) {
+        this.debug = debug;
+    }
+
+    private Stream<RealVector> sampleSolutionSpace() {
+        RandomVectorGenerator generator = new HaltonSequenceGenerator(minima.getDimension());
+        RealVector size = maxima.subtract(minima);
+        return Stream.generate(generator::nextVector).map(haltonPoint -> new ArrayRealVector(haltonPoint).ebeMultiply(size).add(minima));
+
+    }
+
+    private GaussianProcess fit(List<RealVector> listOfCoordinates, List<Double> observations) {
+        return GaussianProcess.fit(listOfCoordinates, observations.stream().mapToDouble(Double::doubleValue).toArray(), noise, true,
+                lengthScale.toArray());
+    }
+
+    private double acquisitionFunction(GaussianProcess gaussianProcess, double[] coordinates, double yMax) {
+        // expected improvement
+        double[] meanAndStandardDeviation = gaussianProcess.predict(new ArrayRealVector(coordinates));
+        double mean = meanAndStandardDeviation[0];
+        double standardDeviation = meanAndStandardDeviation[1];
+        double neededImprovement = mean - yMax;
+        double normalizedNeededImprovement = neededImprovement / standardDeviation;
+        NormalDistribution normalDistribution = new NormalDistribution(null, 0, 1);
+        return neededImprovement * normalDistribution.cumulativeProbability(normalizedNeededImprovement)
+                + standardDeviation * normalDistribution.density(normalizedNeededImprovement);
+    }
+
+    private static <T> Optional<T> getNext(Spliterator<T> spliterator) {
+        final List<T> result = new ArrayList<>(1);
+
+        if (spliterator.tryAdvance(result::add)) {
+            return Optional.of(result.get(0));
+        } else {
+            return Optional.empty();
+        }
+    }
+
+    /**
+     * Numerically optimize acquisition function
+     */
+    private RealVector maxAcq(GaussianProcess gaussianProcess, double yMax, Spliterator<RealVector> samples, double[] randomPicks) {
+        double bestScore = Double.NEGATIVE_INFINITY;
+        double[] bestSolution = getNext(samples).orElseThrow().toArray();
+        double[] min = minima.toArray();
+        double[] max = maxima.toArray();
+
+        int nonZeroAcquisitions = 0;
+        for (int i = 0; i < MAXIMUM_ACQ_FN_EVALS_PER_ITERATION && nonZeroAcquisitions < MAX_NON_ZERO_ACQ_FN_EVALS_PER_ITERATION; i++) {
+            double[] location = getNext(samples).orElseThrow().toArray();
+            if (acquisitionFunction(gaussianProcess, location, yMax) == 0)
+                continue;
+            nonZeroAcquisitions++;
+            double acquisition = -BFGS.minimize(coordinates -> -acquisitionFunction(gaussianProcess, coordinates, yMax), 5, location, min, max,
+                    0.00001, 1000);
+            // Sometimes result is out of bounds (might be due to numerical errors?)
+            for (int j = 0; j < location.length; j++) {
+                location[j] = Math.min(max[j], location[j]);
+                location[j] = Math.max(min[j], location[j]);
+            }
+            if (acquisition > bestScore) {
+                bestSolution = location;
+                bestScore = acquisition;
+            }
+        }
+        if (nonZeroAcquisitions == 0) {
+            randomPicks[0]++;
+        } else {
+            randomPicks[0] = 0;
+        }
+        return new ArrayRealVector(bestSolution);
+    }
+
+    /**
+     * Optimizes a real-valued function and returns a result associated with the optimal value.
+     * @param <T> type of the result
+     * @param objectiveFunction function to optimize
+     * @return result
+     */
+    public <T> OptimizationResult<T> maximize(Function<RealVector, OptimizationResult<T>> objectiveFunction) {
+        List<Double> observations = new ArrayList<>(maxEvaluations);
+        List<RealVector> testedCoordinates = new ArrayList<>(maxEvaluations);
+        OptimizationResult<T> best = null;
+
+        // the first couple of executions are reserved for exploration
+        sampleSolutionSpace().limit(initialPoints).forEach(testedCoordinates::add);
+
+        Spliterator<RealVector> poiSampler = sampleSolutionSpace().spliterator();
+        double[] zeroAcquisitionsCounter = new double[1];
+
+        while (observations.size() < maxEvaluations && zeroAcquisitionsCounter[0] < STOP_AFTER_CONSECUTIVE_RANDOM_PICKS) {
+            int idx = observations.size();
+            RealVector coordinates;
+            if (idx < testedCoordinates.size()) {
+                // hard coded exploration
+                coordinates = testedCoordinates.get(idx);
+            } else {
+                // GPR
+                GaussianProcess gpr = fit(testedCoordinates, observations);
+                if (debug) {
+                    System.out.println(gpr.toString(minima, maxima, 100, 25, 0));
+                }
+                coordinates = maxAcq(gpr, best.score, poiSampler, zeroAcquisitionsCounter);
+                testedCoordinates.add(coordinates);
+            }
+            OptimizationResult<T> result = objectiveFunction.apply(coordinates);
+            result.params = coordinates;
+            observations.add(result.getScore());
+            if (best == null) {
+                best = result;
+            } else if (result.score > best.score) {
+                best = result;
+            }
+        }
+
+        return best;
+    }
+
+    public static final class OptimizationResult<T> {
+
+        private double score;
+        private T value;
+        private RealVector params;
+
+        public OptimizationResult(double score, T value) {
+            this.score = score;
+            this.value = value;
+        }
+
+        public T getValue() {
+            return value;
+        }
+
+        public double getScore() {
+            return score;
+        }
+
+        public RealVector getParams() {
+            return params;
+        }
+    }
+}

+ 197 - 0
jplag/src/main/java/de/jplag/clustering/algorithm/GaussianProcess.java

@@ -0,0 +1,197 @@
+package de.jplag.clustering.algorithm;
+
+import java.text.MessageFormat;
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.function.DoubleToIntFunction;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.commons.math3.linear.CholeskyDecomposition;
+import org.apache.commons.math3.linear.DiagonalMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.linear.RealVector;
+import org.apache.commons.math3.stat.StatUtils;
+
+/**
+ * Implementation of a gaussian process with a matern kernel. This class can be used to fit any real-valued function
+ * with noisy evaluations. Predictions come in the form of expectations and standard deviations.
+ */
+public class GaussianProcess {
+
+    private List<RealVector> listOfCoordinates;
+    private RealVector weight;
+    private double mean;
+    private double standardDeviation;
+    private CholeskyDecomposition cholesky;
+    private RealVector lengthScale;
+
+    private GaussianProcess(List<RealVector> listOfCoordinates, RealVector weight, double mean, double standardDeviation,
+            CholeskyDecomposition cholesky, RealVector lengthScale) {
+        this.listOfCoordinates = listOfCoordinates;
+        this.weight = weight;
+        this.mean = mean;
+        this.standardDeviation = standardDeviation;
+        this.cholesky = cholesky;
+        this.lengthScale = lengthScale;
+    }
+
+    /**
+     * @param coordinates coordinate to predict at
+     * @return array containing the predicted [mean, standard deviation] at x.
+     */
+    public double[] predict(RealVector coordinates) {
+        RealVector kernelizedCoordinates = maternKernel(listOfCoordinates, coordinates, lengthScale);
+        RealVector kernelMatrixTimesCoordinates = cholesky.getSolver().solve(kernelizedCoordinates);
+
+        double predictedMean = weight.dotProduct(kernelizedCoordinates);
+        double predictedStandardDeviation = Math
+                .sqrt(maternKernel(coordinates, coordinates, lengthScale) - kernelMatrixTimesCoordinates.dotProduct(kernelizedCoordinates));
+
+        double[] out = new double[2];
+        out[0] = predictedMean * this.standardDeviation + this.mean;
+        out[1] = predictedStandardDeviation * this.standardDeviation;
+
+        if (Double.isNaN(out[1])) {
+            out[1] = 0;
+        }
+
+        return out;
+    }
+
+    private static final double SQRT_5 = Math.sqrt(5);
+
+    /**
+     * Fit Gaussian Process using a matern kernel.
+     * @param observedCoordinates
+     * @param observations expected to have zero mean, unit variance if normalize is false
+     * @param noise variance of noise in Y
+     * @param normalize if Y should be normalized
+     * @param lengthScale X values are divided by these values before calculating their euclidean distance. If all entries
+     * are equal the kernel is isometric.
+     */
+    public static GaussianProcess fit(List<RealVector> observedCoordinates, double[] observations, double noise, boolean normalize,
+            double[] lengthScale) {
+        if (observedCoordinates.size() < 1)
+            throw new IllegalArgumentException("Observed coordinates are empty");
+        if (observedCoordinates.size() != observations.length) {
+            throw new IllegalArgumentException(MessageFormat.format("Observed coordinates and observations are of different dimensions {0} and {1}",
+                    observedCoordinates.size(), observations.length));
+        }
+        OptionalInt brokenXIndex = IntStream.range(1, observedCoordinates.size())
+                .filter(i -> observedCoordinates.get(i).getDimension() != observedCoordinates.get(i - 1).getDimension()).findFirst();
+        brokenXIndex.ifPresent(brokenIndex -> {
+            throw new IllegalArgumentException(MessageFormat.format(
+                    "Observed coordinates has different dimensions at index {0} ({2}) and index {1} ({3})", brokenIndex - 1, brokenXIndex,
+                    observedCoordinates.get(brokenIndex - 1).getDimension(), observedCoordinates.get(brokenIndex).getDimension()));
+        });
+        if (noise <= 0) {
+            throw new IllegalArgumentException(MessageFormat.format("noise must be strictly positive, got {0}", noise));
+        }
+        if (lengthScale.length != observedCoordinates.get(0).getDimension()) {
+            throw new IllegalArgumentException(MessageFormat.format("lengthScale is of different dimension {0} than the coordinates values {1}",
+                    lengthScale.length, observedCoordinates.get(0).getDimension()));
+        }
+        double mean = 0;
+        double standardDeviation = 1;
+        if (normalize) {
+            mean = StatUtils.mean(observations);
+            standardDeviation = Math.sqrt(StatUtils.variance(observations, mean));
+            for (int i = 0; i < observations.length; i++) {
+                observations[i] = (observations[i] - mean) / standardDeviation;
+            }
+        }
+        RealVector observationVector = new ArrayRealVector(observations);
+        RealVector lengthScaleVector = new ArrayRealVector(lengthScale);
+
+        RealMatrix k = maternKernel(observedCoordinates, lengthScaleVector);
+        k = k.add(new DiagonalMatrix(DoubleStream.generate(() -> noise).limit(observedCoordinates.size()).toArray(), false));
+
+        CholeskyDecomposition decomposition = new CholeskyDecomposition(k);
+        RealVector w = decomposition.getSolver().solve(observationVector);
+
+        return new GaussianProcess(observedCoordinates, w, mean, standardDeviation, decomposition, lengthScaleVector);
+    }
+
+    /**
+     * Matern kernel for nu=2.5 (we get a twice differentiable gp)
+     */
+    private static RealMatrix maternKernel(List<RealVector> observedCoordinates, RealVector lengthScale) {
+        RealMatrix k = new Array2DRowRealMatrix(observedCoordinates.size(), observedCoordinates.size());
+        for (int i = 0; i < observedCoordinates.size(); i++) {
+            RealVector left = observedCoordinates.get(i);
+            for (int j = i; j < observedCoordinates.size(); j++) {
+                RealVector right = observedCoordinates.get(j);
+                double maternVal = maternKernel(left, right, lengthScale);
+                k.setEntry(i, j, maternVal);
+                k.setEntry(j, i, maternVal);
+            }
+        }
+        return k;
+    }
+
+    private static RealVector maternKernel(List<RealVector> observedCoordinates, RealVector vector, RealVector lengthScale) {
+        RealVector out = new ArrayRealVector(observedCoordinates.size());
+        for (int i = 0; i < observedCoordinates.size(); i++) {
+            out.setEntry(i, maternKernel(observedCoordinates.get(i), vector, lengthScale));
+        }
+        return out;
+    }
+
+    private static double maternKernel(RealVector left, RealVector right, RealVector lengthScale) {
+        double dist = left.ebeDivide(lengthScale).getDistance(right.ebeDivide(lengthScale));
+        dist *= SQRT_5;
+        return (1 + dist) * Math.exp(-dist);
+    }
+
+    /**
+     * ascii graph for debugging
+     */
+    public String toString(RealVector min, RealVector max, int width, int height, double minY) {
+        char[][] out = new char[height][width];
+        for (int i = 0; i < height; i++) {
+            for (int j = 0; j < width; j++) {
+                out[i][j] = ' ';
+            }
+        }
+        double[] mean = new double[width];
+        double[] std = new double[width];
+        for (int i = 0; i < width; i++) {
+            double t = (i + 0.5) / width;
+            RealVector coordinates = min.add(max.subtract(min).mapMultiplyToSelf(t));
+            double[] meanStd = predict(coordinates);
+            mean[i] = meanStd[0];
+            std[i] = meanStd[1];
+        }
+        double[] upperBorder = IntStream.range(0, width).mapToDouble(i -> mean[i] + std[i]).toArray();
+        double[] lowerBorder = IntStream.range(0, width).mapToDouble(i -> mean[i] - std[i]).toArray();
+        double minVal = Math.max(DoubleStream.of(lowerBorder).min().orElseThrow(), minY);
+        double maxVal = DoubleStream.of(upperBorder).max().orElseThrow();
+        double range = maxVal - minVal;
+        DoubleToIntFunction toCharPos = x -> (int) Math.round(height - height * (x - minVal) / range);
+        int[] upperCharPos = DoubleStream.of(upperBorder).mapToInt(toCharPos).toArray();
+        int[] lowerCharPos = DoubleStream.of(lowerBorder).mapToInt(toCharPos).toArray();
+        int[] mindCharPos = DoubleStream.of(mean).mapToInt(toCharPos).toArray();
+
+        for (int i = 0; i < width; i++) {
+            if (upperCharPos[i] > 0 && upperCharPos[i] < height)
+                out[upperCharPos[i]][i] = '-';
+            if (lowerCharPos[i] > 0 && lowerCharPos[i] < height)
+                out[lowerCharPos[i]][i] = '-';
+            if (mindCharPos[i] > 0 && mindCharPos[i] < height)
+                out[mindCharPos[i]][i] = '+';
+        }
+
+        StringBuilder stringBuilder = new StringBuilder();
+        for (int i = 0; i < height; i++) {
+            stringBuilder.append(out[i]);
+            if (i < 98) {
+                stringBuilder.append('\n');
+            }
+        }
+        return stringBuilder.toString();
+    }
+
+}

+ 13 - 0
jplag/src/main/java/de/jplag/clustering/algorithm/GenericClusteringAlgorithm.java

@@ -0,0 +1,13 @@
+package de.jplag.clustering.algorithm;
+
+import java.util.Collection;
+
+import org.apache.commons.math3.linear.RealMatrix;
+
+/**
+ * Interface for algorithms that can perform clustering using only a symmetric matrix of similarities. The integers in
+ * the returned collection of integer collections denote the rows / columns of the similarity matrix.
+ */
+public interface GenericClusteringAlgorithm {
+    Collection<Collection<Integer>> cluster(RealMatrix similarityMatrix);
+}

+ 44 - 0
jplag/src/main/java/de/jplag/clustering/algorithm/InterClusterSimilarity.java

@@ -0,0 +1,44 @@
+package de.jplag.clustering.algorithm;
+
+import java.util.List;
+import java.util.function.DoubleBinaryOperator;
+
+import org.apache.commons.math3.linear.RealMatrix;
+
+public enum InterClusterSimilarity {
+    MIN(Float.MAX_VALUE, Math::min),
+    MAX(Float.MIN_VALUE, Math::max),
+    AVERAGE(0, (a, b) -> a + b);
+
+    private final float neutralElement;
+    private final DoubleBinaryOperator accumulator;
+
+    private InterClusterSimilarity(float neutralElement, DoubleBinaryOperator accumulator) {
+        this.neutralElement = neutralElement;
+        this.accumulator = accumulator;
+    }
+
+    /**
+     * Calculates the distance between two clusters.
+     * @param leftCluster list of cluster indices in left cluster
+     * @param rightCluster list of cluster indices in right cluster
+     * @param similarityMatrix matrix containing similarities
+     * @return similarity between the two clusters
+     */
+    public float clusterSimilarity(List<Integer> leftCluster, List<Integer> rightCluster, RealMatrix similarityMatrix) {
+        float similarity = this.neutralElement;
+
+        for (int leftIndex = 0; leftIndex < leftCluster.size(); leftIndex++) {
+            int leftSubmission = leftCluster.get(leftIndex);
+            for (int rightIndex = 0; rightIndex < rightCluster.size(); rightIndex++) {
+                float submissionSimilarity = (float) similarityMatrix.getEntry(leftSubmission, rightCluster.get(rightIndex));
+                similarity = (float) this.accumulator.applyAsDouble(similarity, submissionSimilarity);
+            }
+        }
+
+        if (this == InterClusterSimilarity.AVERAGE) {
+            similarity /= leftCluster.size() * rightCluster.size();
+        }
+        return similarity;
+    }
+}

+ 141 - 0
jplag/src/main/java/de/jplag/clustering/algorithm/SpectralClustering.java

@@ -0,0 +1,141 @@
+package de.jplag.clustering.algorithm;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor;
+import org.apache.commons.math3.linear.DiagonalMatrix;
+import org.apache.commons.math3.linear.EigenDecomposition;
+import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.linear.RealVector;
+import org.apache.commons.math3.ml.clustering.Cluster;
+import org.apache.commons.math3.ml.clustering.Clusterable;
+import org.apache.commons.math3.ml.clustering.Clusterer;
+import org.apache.commons.math3.ml.clustering.KMeansPlusPlusClusterer;
+
+import de.jplag.clustering.ClusteringOptions;
+import de.jplag.clustering.ClusteringResult;
+
+/**
+ * Spectral clustering is a clustering algorithm for graph data. Each node is represented as k-dimensional vector,
+ * afterwards k-Means is used to generate a clustering with k on that representation. This implementation uses Bayesian
+ * Optimization to find an appropriate number for k.
+ */
+public class SpectralClustering implements GenericClusteringAlgorithm {
+
+    private static final double MULTIPLICITY_EPSILON = 0.05;
+    private final ClusteringOptions options;
+
+    public SpectralClustering(ClusteringOptions options) {
+        this.options = options;
+    }
+
+    @Override
+    public Collection<Collection<Integer>> cluster(RealMatrix similarityMatrix) {
+        // Calculate points to cluster according to "On spectral clustering: analysis and an algorithm" by Ng, Jordan & Weiss
+        // 2001
+        int dimension = similarityMatrix.getRowDimension();
+
+        // We don't use the similarity function, we already have some kind of similarity
+        RealMatrix weights = similarityMatrix.copy();
+        weights.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
+            @Override
+            public double visit(int row, int column, double value) {
+                if (row == column)
+                    return 0;
+                return similarityMatrix.getEntry(row, column);
+            }
+        });
+
+        DiagonalMatrix diagonalPowMinus1Over2 = new DiagonalMatrix(dimension);
+        diagonalPowMinus1Over2.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
+            @Override
+            public double visit(int row, int column, double value) {
+                if (row != column)
+                    return 0;
+                return 1 / Math.sqrt(weights.getRowVector(row).getL1Norm());
+            }
+        });
+
+        RealMatrix identity = new Array2DRowRealMatrix(dimension, dimension);
+        identity.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
+            @Override
+            public double visit(int row, int column, double value) {
+                return row == column ? 1 : 0;
+            }
+        });
+        RealMatrix laplacian = identity.subtract(diagonalPowMinus1Over2.multiply(weights).multiply(diagonalPowMinus1Over2));
+        EigenDecomposition eigenDecomposition = new EigenDecomposition(laplacian);
+
+        List<Integer> eigenValueIds = new ArrayList<>(dimension);
+        for (int i = 0; i < dimension; i++) {
+            eigenValueIds.add(i);
+        }
+        eigenValueIds.sort(Comparator.comparingDouble(eigenDecomposition::getRealEigenvalue));
+
+        // find number of clusters as the multiplicity of eigenvalue 0
+        int minClusters = Math.max(2, (int) DoubleStream.of(eigenDecomposition.getRealEigenvalues()).filter(x -> x < MULTIPLICITY_EPSILON).count());
+        int maxClusters = (int) Math.ceil(dimension / 2.0);
+
+        // Find number of clusters using bayesian optimization
+        RealVector lengthScale = new ArrayRealVector(1, options.getSpectralKernelBandwidth());
+        BayesianOptimization bo = new BayesianOptimization(new ArrayRealVector(1, minClusters), new ArrayRealVector(1, maxClusters),
+                options.getSpectralMinRuns(), options.getSpectralMaxRuns(), options.getSpectralGaussianProcessVariance(), lengthScale);
+        // bo.debug = true;
+        BayesianOptimization.OptimizationResult<Collection<Collection<Integer>>> bayesianOptimizationResult = bo.maximize(r -> {
+            int clusters = (int) Math.round(r.getEntry(0));
+            clusters = Math.max(minClusters, clusters);
+            clusters = Math.min(maxClusters, clusters);
+            Collection<Collection<Integer>> clustering = cluster(clusters, dimension, eigenValueIds, eigenDecomposition);
+            ClusteringResult<Integer> modularityRes = ClusteringResult.fromIntegerCollections(new ArrayList<>(clustering), similarityMatrix);
+            return new BayesianOptimization.OptimizationResult<>(modularityRes.getWorth((a, b) -> (float) similarityMatrix.getEntry(a, b)),
+                    clustering);
+        });
+
+        return bayesianOptimizationResult.getValue();
+    }
+
+    private Collection<Collection<Integer>> cluster(int numberOfClusters, int dimension, List<Integer> eigenValueIds, EigenDecomposition ed) {
+        RealMatrix concatenatedEigenVectors = new Array2DRowRealMatrix(dimension, numberOfClusters);
+        concatenatedEigenVectors.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
+            public double visit(int row, int column, double value) {
+                int eigenVectorId = eigenValueIds.get(column);
+                RealVector eigenVector = ed.getEigenvector(eigenVectorId);
+                return eigenVector.getEntry(row);
+            };
+        });
+
+        List<ClusterableEigenVector> normRows = IntStream.range(0, dimension).filter(i -> concatenatedEigenVectors.getRowVector(i).getNorm() > 0)
+                .mapToObj(row -> new ClusterableEigenVector(row, concatenatedEigenVectors.getRowVector(row).unitVector()))
+                .collect(Collectors.toList());
+
+        Clusterer<ClusterableEigenVector> clusterer = new KMeansPlusPlusClusterer<>(numberOfClusters, options.getSpectralMaxKMeansIterationPerRun());
+        List<? extends Cluster<ClusterableEigenVector>> clusters = clusterer.cluster(normRows);
+        return clusters.stream().map(cluster -> {
+            return cluster.getPoints().stream().map(eigenVector -> eigenVector.id).collect(Collectors.toList());
+        }).collect(Collectors.toList());
+    }
+
+    private static class ClusterableEigenVector implements Clusterable {
+        private int id;
+        private double[] eigenVector;
+
+        public ClusterableEigenVector(int id, RealVector eigenVector) {
+            this.id = id;
+            this.eigenVector = eigenVector.toArray();
+        }
+
+        @Override
+        public double[] getPoint() {
+            return eigenVector;
+        }
+    }
+
+}

+ 53 - 0
jplag/src/main/java/de/jplag/clustering/preprocessors/CumulativeDistributionFunctionPreprocessor.java

@@ -0,0 +1,53 @@
+package de.jplag.clustering.preprocessors;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor;
+import org.apache.commons.math3.linear.DefaultRealMatrixPreservingVisitor;
+import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.random.EmpiricalDistribution;
+
+import de.jplag.clustering.ClusteringPreprocessor;
+import de.jplag.clustering.PreprocessorHelper;
+
+/**
+ * Multiplies entries of the similarity matrix by their cumulative probability. - Weights that are very low will be
+ * (close to) zero - Weights that are very high will be (close to) unchanged
+ */
+public class CumulativeDistributionFunctionPreprocessor implements ClusteringPreprocessor {
+
+    private PreprocessorHelper helper = new PreprocessorHelper();
+
+    @Override
+    public double[][] preprocessSimilarities(double[][] similarityMatrix) {
+        RealMatrix similarity = new Array2DRowRealMatrix(similarityMatrix, true);
+        int connections = (similarity.getColumnDimension() * (similarity.getColumnDimension() - 1)) / 2;
+        EmpiricalDistribution dist = new EmpiricalDistribution(Math.max(100, connections / 100));
+        double[] allWeights = new double[connections];
+        similarity.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
+
+            int index = 0;
+
+            @Override
+            public void visit(int row, int column, double value) {
+                // collect upper triangle
+                if (row > column) {
+                    allWeights[this.index++] = value;
+                }
+            }
+        });
+        dist.load(allWeights);
+        similarity.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
+            @Override
+            public double visit(int row, int column, double value) {
+                return dist.cumulativeProbability(value) * value;
+            }
+        });
+        return helper.removeDisconnectedEntries(similarity.getData());
+    }
+
+    @Override
+    public int originalIndexOf(int result) {
+        return helper.postProcessResult(result);
+    }
+
+}

+ 51 - 0
jplag/src/main/java/de/jplag/clustering/preprocessors/PercentileThresholdProcessor.java

@@ -0,0 +1,51 @@
+package de.jplag.clustering.preprocessors;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.DefaultRealMatrixPreservingVisitor;
+import org.apache.commons.math3.stat.descriptive.rank.Percentile;
+import org.apache.commons.math3.stat.descriptive.rank.Percentile.EstimationType;
+
+import de.jplag.clustering.ClusteringPreprocessor;
+
+/**
+ * Suppresses all similarities below a given percentile.
+ */
+public class PercentileThresholdProcessor implements ClusteringPreprocessor {
+
+    private float percentile;
+    private ThresholdPreprocessor thresholdPreprocessor;
+
+    public PercentileThresholdProcessor(float percentile) {
+        this.percentile = percentile;
+    }
+
+    @Override
+    public double[][] preprocessSimilarities(double[][] similarityMatrix) {
+        Array2DRowRealMatrix similarity = new Array2DRowRealMatrix(similarityMatrix, false);
+        Percentile percentileEstimator = new Percentile().withEstimationType(EstimationType.R_2);
+        int connections = (similarity.getColumnDimension() * (similarity.getColumnDimension() - 1)) / 2;
+        double[] allWeights = new double[connections];
+        similarity.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
+
+            int index = 0;
+
+            @Override
+            public void visit(int row, int column, double value) {
+                // collect upper triangle
+                if (row > column) {
+                    allWeights[this.index++] = value;
+                }
+            }
+        });
+        double threshold = percentileEstimator.evaluate(allWeights, percentile);
+        thresholdPreprocessor = new ThresholdPreprocessor(threshold);
+
+        return thresholdPreprocessor.preprocessSimilarities(similarityMatrix);
+    }
+
+    @Override
+    public int originalIndexOf(int result) {
+        return thresholdPreprocessor.originalIndexOf(result);
+    }
+
+}

+ 43 - 0
jplag/src/main/java/de/jplag/clustering/preprocessors/ThresholdPreprocessor.java

@@ -0,0 +1,43 @@
+package de.jplag.clustering.preprocessors;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor;
+import org.apache.commons.math3.linear.RealMatrix;
+
+import de.jplag.clustering.ClusteringPreprocessor;
+import de.jplag.clustering.PreprocessorHelper;
+
+/**
+ * Suppresses all similarities below a given threshold.
+ */
+public class ThresholdPreprocessor implements ClusteringPreprocessor {
+
+    private double threshold;
+    private PreprocessorHelper helper = new PreprocessorHelper();
+
+    public ThresholdPreprocessor(double threshold) {
+        this.threshold = threshold;
+    }
+
+    @Override
+    public double[][] preprocessSimilarities(double[][] similarityMatrix) {
+        RealMatrix similarity = new Array2DRowRealMatrix(similarityMatrix, true);
+        similarity.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() {
+            @Override
+            public double visit(int row, int column, double value) {
+                return value >= threshold ? value : 0;
+            }
+        });
+        return helper.removeDisconnectedEntries(similarity.getData());
+    }
+
+    @Override
+    public int originalIndexOf(int result) {
+        return helper.postProcessResult(result);
+    }
+
+    public double getThreshold() {
+        return threshold;
+    }
+
+}

+ 16 - 1
jplag/src/main/java/de/jplag/options/JPlagOptions.java

@@ -11,6 +11,7 @@ import java.util.Optional;
 import java.util.Set;
 
 import de.jplag.Language;
+import de.jplag.clustering.ClusteringOptions;
 import de.jplag.strategy.ComparisonMode;
 
 public class JPlagOptions {
@@ -64,7 +65,7 @@ public class JPlagOptions {
 
     /**
      * Tunes the comparison sensitivity by adjusting the minimum token required to be counted as matching section. A smaller
-     * <n> increases the sensitivity but might lead to more false-positves.
+     * <n> increases the sensitivity but might lead to more false-positives.
      */
     private Integer minimumTokenMatch;
 
@@ -115,6 +116,11 @@ public class JPlagOptions {
      */
     private Verbosity verbosity;
 
+    /**
+     * Clustering options
+     */
+    private ClusteringOptions clusteringOptions = new ClusteringOptions.Builder().build();
+
     /**
      * Constructor with required attributes.
      */
@@ -187,6 +193,10 @@ public class JPlagOptions {
         return debugParser;
     }
 
+    public ClusteringOptions getClusteringOptions() {
+        return this.clusteringOptions;
+    }
+
     public void setBaseCodeSubmissionName(String baseCodeSubmissionName) {
         if (baseCodeSubmissionName == null || baseCodeSubmissionName.isEmpty()) {
             this.baseCodeSubmissionName = Optional.empty();
@@ -283,6 +293,10 @@ public class JPlagOptions {
         this.verbosity = verbosity;
     }
 
+    public void setClusteringOptions(ClusteringOptions clusteringOptions) {
+        this.clusteringOptions = clusteringOptions;
+    }
+
     private boolean hasFileSuffixes() {
         return fileSuffixes != null && fileSuffixes.length > 0;
     }
@@ -290,4 +304,5 @@ public class JPlagOptions {
     private boolean hasMinimumTokenMatch() {
         return minimumTokenMatch != null;
     }
+
 }

+ 8 - 2
jplag/src/main/java/de/jplag/options/SimilarityMetric.java

@@ -4,10 +4,11 @@ import java.util.function.Function;
 
 import de.jplag.JPlagComparison;
 
-public enum SimilarityMetric {
+public enum SimilarityMetric implements Function<JPlagComparison, Float> {
     AVG(it -> it.similarity()),
     MIN(it -> it.minimalSimilarity()),
-    MAX(it -> it.maximalSimilarity());
+    MAX(it -> it.maximalSimilarity()),
+    INTERSECTION(it -> (float) it.getNumberOfMatchedTokens());
 
     private final Function<JPlagComparison, Float> similarityFunction;
 
@@ -18,4 +19,9 @@ public enum SimilarityMetric {
     public boolean isAboveThreshold(JPlagComparison comparison, float similarityThreshold) {
         return similarityFunction.apply(comparison) >= similarityThreshold;
     }
+
+    @Override
+    public Float apply(JPlagComparison comparison) {
+        return similarityFunction.apply(comparison);
+    }
 }

+ 14 - 0
jplag/src/main/resources/de/jplag/messages.properties

@@ -11,3 +11,17 @@ CommandLineArgument.ShownComparisons=The maximum number of comparisons that will
 CommandLineArgument.SimilarityThreshold=Comparison similarity threshold [0-100]: All comparisons above this threshold will be saved
 CommandLineArgument.Subdirectory=Look in directories <root-dir>/*/<dir> for programs
 CommandLineArgument.Verbosity=Verbosity of the logging
+CommandLineArgument.ClusterEnable=Skips the clustering
+CommandLineArgument.ClusterAlgorithm=Which clustering algorithm to use. Agglomerative merges similar submissions bottom up. Spectral clustering is combined with Bayesian Optimization to execute the k-Means clustering algorithm multiple times, hopefully finding a "good" clustering automatically.
+CommandLineArgument.ClusterMetric=The metric used for clustering. AVG is intersection over union, MAX can expose some attempts of obfuscation.
+CommandLineArgument.ClusterSpectralBandwidth=Bandwidth of the matern kernel in the Gaussian Process used during the search for a good number of clusters for spectral clustering. If a good clustering result is found during the search, numbers of clusters that differ by something in range of the bandwidth are also expected to good.
+CommandLineArgument.ClusterSpectralNoise=The result of each run in the search for good clusterings are random. The noise level models the variance in the "worth" of these results. It also acts as a regularization constant.
+CommandLineArgument.ClusterSpectralMinRuns=Minimum number of k-Means executions during spectral clustering. With these initial clustering sizes are explored.
+CommandLineArgument.ClusterSpectralMaxRuns=Maximum number of k-Means executions during spectral clustering. Any execution after the initial runs tries to balance between exploration of unknown clustering sizes and exploitation of clustering sizes known as good.
+CommandLineArgument.ClusterSpectralKmeansIterations=Maximum number of iterations during each execution of the k-Means algorithm.
+CommandLineArgument.ClusterAgglomerativeThreshold=Only clusters with an inter-cluster-similarity greater than this threshold are merged during agglomerative clustering.
+CommandLineArgument.ClusterAgglomerativeInterClusterSimilarity=How to measure the similarity of two clusters during agglomerative clustering. Minimum, maximum or average similarity between the submissions in each cluster.
+CommandLineArgument.ClusterPreprocessingNone=Do not use any preprocessing before clustering. Not recommended for spectral clustering.
+CommandLineArgument.ClusterPreprocessingCdf=Before clustering, the value of the cumulative distribution function of all similarities is estimated. The similarities are multiplied with these estimates. This has the effect of supressing similarities that are low compared to other similarities.
+CommandLineArgument.ClusterPreprocessingPercentile=Any similarity smaller than the given percentile will be suppressed during clustering.
+CommandLineArgument.ClusterPreprocessingThreshold=Any similarity smaller than the given threshold value will be suppressed during clustering.

+ 1 - 4
jplag/src/test/java/de/jplag/NewJavaFeaturesTest.java

@@ -18,13 +18,10 @@ public class NewJavaFeaturesTest extends TestBase {
         assertEquals(2, result.getNumberOfSubmissions());
         assertEquals(1, result.getComparisons().size());
         var comparison = result.getComparisons().get(0);
-        assertEquals(100, comparison.similarity(), Double.MIN_NORMAL);
+        assertEquals(92.1, comparison.similarity(), DELTA);
 
         // Five matches that each start at the same position:
         assertEquals(4, comparison.getMatches().size());
-        for (Match match : comparison.getMatches()) {
-            assertEquals(match.getStartOfFirst(), match.getStartOfSecond());
-        }
     }
 
 }

+ 36 - 0
jplag/src/test/java/de/jplag/cli/ClusteringTest.java

@@ -0,0 +1,36 @@
+package de.jplag.cli;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import de.jplag.CommandLineArgument;
+import de.jplag.clustering.Preprocessing;
+
+public class ClusteringTest extends CommandLineInterfaceTest {
+
+    private static final double EPSILON = 0.000001;
+
+    @Test
+    public void parsePercentilePreProcessor() {
+        String argument = CommandLineArgument.CLUSTER_PREPROCESSING_PERCENTILE.flag() + "=" + Float.toString(0.5f);
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(Preprocessing.PERCENTILE, options.getClusteringOptions().getPreprocessor());
+        assertEquals(0.5, options.getClusteringOptions().getPreprocessorPercentile(), EPSILON);
+    }
+
+    @Test
+    public void parseCdfPreProcessor() {
+        String argument = CommandLineArgument.CLUSTER_PREPROCESSING_CDF.flag();
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(Preprocessing.CUMULATIVE_DISTRIBUTION_FUNCTION, options.getClusteringOptions().getPreprocessor());
+    }
+
+    @Test
+    public void parseNoPreProcessor() {
+        String argument = CommandLineArgument.CLUSTER_PREPROCESSING_NONE.flag();
+        buildOptionsFromCLI(argument, CURRENT_DIRECTORY);
+        assertEquals(Preprocessing.NONE, options.getClusteringOptions().getPreprocessor());
+    }
+
+}

+ 53 - 0
jplag/src/test/java/de/jplag/clustering/ClusterTest.java

@@ -0,0 +1,53 @@
+package de.jplag.clustering;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+
+import org.junit.Test;
+
+public class ClusterTest {
+
+    private static final double EPSILON = 0.00001;
+    Cluster<Character> cluster;
+
+    @Test
+    public void testAverageSimilarity() {
+        cluster = new Cluster<>(List.of('a', 'b', 'c'), 0);
+        float averageSimilarity = cluster.averageSimilarity((a, b) -> {
+            return Math.abs((float) (((int) a) - ((int) b)));
+        });
+        assertEquals((1.f + 2.f + 1.f + 1.f + 2.f + 1.f) / 6, averageSimilarity, EPSILON);
+    }
+
+    @Test
+    public void testCommunityStrengthPerConnectionOneMember() {
+        cluster = new Cluster<>(List.of('a'), 10);
+        assertEquals(0.0, cluster.getCommunityStrengthPerConnection(), EPSILON);
+    }
+
+    @Test
+    public void testCommunityStrengthPerConnectionTwoMembers() {
+        cluster = new Cluster<>(List.of('a', 'b'), 10);
+        assertEquals(10.0, cluster.getCommunityStrengthPerConnection(), EPSILON);
+    }
+
+    @Test
+    public void testCommunityStrengthPerConnectionThreeMembers() {
+        cluster = new Cluster<>(List.of('a', 'b', 'c'), 10);
+        assertEquals(10.0 / 3, cluster.getCommunityStrengthPerConnection(), EPSILON);
+    }
+
+    @Test
+    public void testNormalizedCommunityStrength() {
+        cluster = new Cluster<>(List.of('a', 'b', 'c'), 10);
+        @SuppressWarnings("unchecked")
+        ClusteringResult<Character> clusteringResult = mock(ClusteringResult.class);
+        when(clusteringResult.getClusters()).thenReturn(List.of(cluster, cluster));
+        cluster.setClusteringResult(clusteringResult);
+        assertEquals(0.5, cluster.getNormalizedCommunityStrengthPerConnection(), EPSILON);
+    }
+
+}

+ 53 - 0
jplag/src/test/java/de/jplag/clustering/ClusteringAdapterTest.java

@@ -0,0 +1,53 @@
+package de.jplag.clustering;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import org.apache.commons.math3.linear.RealMatrix;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+
+import de.jplag.JPlagComparison;
+import de.jplag.Submission;
+import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
+
+public class ClusteringAdapterTest {
+
+    @Test
+    public void testClustering() {
+        List<Submission> submissions = IntStream.range(0, 4).mapToObj(x -> mock(Submission.class)).collect(Collectors.toList());
+        List<JPlagComparison> comparisons = new ArrayList<>(6);
+        for (int i = 0; i < submissions.size(); i++) {
+            for (int j = i + 1; j < submissions.size(); j++) {
+                JPlagComparison comparison = mock(JPlagComparison.class);
+                when(comparison.getFirstSubmission()).thenReturn(submissions.get(i));
+                when(comparison.getSecondSubmission()).thenReturn(submissions.get(j));
+                comparisons.add(comparison);
+            }
+        }
+
+        // Mock algorithm that returns everything in a single cluster
+        GenericClusteringAlgorithm algorithm = mock(GenericClusteringAlgorithm.class);
+        when(algorithm.cluster(any(RealMatrix.class))).then((InvocationOnMock invocation) -> {
+            RealMatrix arg = invocation.getArgument(0);
+            Collection<Collection<Integer>> result = List.of(IntStream.range(0, arg.getRowDimension()).boxed().collect(Collectors.toList()));
+            return result;
+        });
+
+        ClusteringAdapter clustering = new ClusteringAdapter(comparisons, x -> 0.f);
+        ClusteringResult<Submission> clusteringResult = clustering.doClustering(algorithm);
+
+        Collection<Collection<Submission>> expectedResult = List.of(submissions);
+
+        assertEquals(expectedResult, clusteringResult.getClusters().stream().map(Cluster::getMembers).collect(Collectors.toList()));
+    }
+
+}

+ 225 - 0
jplag/src/test/java/de/jplag/clustering/ClusteringRealDataTest.java

@@ -0,0 +1,225 @@
+package de.jplag.clustering;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assume.assumeTrue;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Deque;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Scanner;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+import org.junit.Test;
+
+import de.jplag.clustering.algorithm.GenericClusteringAlgorithm;
+import de.jplag.clustering.algorithm.SpectralClustering;
+import de.jplag.clustering.preprocessors.CumulativeDistributionFunctionPreprocessor;
+
+/**
+ * These test are not meant to be run during normal unit testing. They can be used to test the clustering algorithms
+ * against data from the private pseudomized reports repository. These tests test PROBABILISTIC behavior, so use with
+ * caution!
+ */
+public class ClusteringRealDataTest {
+
+    private static class TestFile {
+
+        private String uri;
+        private Optional<List<String>> expected;
+
+        public TestFile(String uri, Optional<List<String>> expected) {
+            this.uri = uri;
+            this.expected = expected;
+        }
+    }
+
+    private static List<String> B_POSITIVE = Arrays.asList(new String[] {"Student (31)", "Student (223)"});
+    private static List<String> C_POSITIVE = Arrays.asList(new String[] {"Student (166)", "Student (212)", "Student (236)", "Student (229)"});
+
+    private static final TestFile[] OLD_CLUSTERING_DATA = {new TestFile("de/jplag/PseudonymizedReports/alt/A_1000_matches_max.csv", Optional.empty()),
+            new TestFile("de/jplag/PseudonymizedReports/alt/B_1000_matches_max.csv", Optional.of(B_POSITIVE)),
+            new TestFile("de/jplag/PseudonymizedReports/alt/C_1000_matches_max.csv", Optional.of(C_POSITIVE)),};
+
+    private static final TestFile[] NEW_CLUSTERING_DATA = {new TestFile("de/jplag/PseudonymizedReports/neu/A_matches_avg.csv", Optional.empty()),
+            new TestFile("de/jplag/PseudonymizedReports/neu/B_matches_avg.csv", Optional.of(B_POSITIVE)),
+            new TestFile("de/jplag/PseudonymizedReports/neu/C_matches_avg.csv", Optional.of(C_POSITIVE)),};
+
+    private String str(float f) {
+        return String.format("%.4f", f);
+    }
+
+    private URL loadFromClasspath(String file) throws FileNotFoundException {
+        URL url = getClass().getClassLoader().getResource(file);
+        if (url == null) {
+            assumeTrue(file + " not found. 'de/jpag/PseudonymizedReports' must contain the data from the PseudonymizedReports repository.", false);
+        }
+        return url;
+    }
+
+    private void doTesting(ReadResult readResult, Optional<List<String>> expected) {
+        RealMatrix clusteringSimilarity = new Array2DRowRealMatrix(readResult.similarity.getData());
+
+        /*
+         * AgglomerativeClustering.ClusteringOptions options = new AgglomerativeClustering.ClusteringOptions();
+         * options.minimalSimilarity = 0.15f; options.similarity = AgglomerativeClustering.InterClusterSimilarity.AVERAGE;
+         * ClusteringAlgorithm clusteringAlg = new AgglomerativeClustering(options);
+         */
+
+        SpectralClustering clusteringAlg = new SpectralClustering(ClusteringOptions.DEFAULTS);
+        ClusteringPreprocessor preprocessor = new CumulativeDistributionFunctionPreprocessor();
+        GenericClusteringAlgorithm preprocessedClusteringAlg = new PreprocessedClusteringAlgorithm(clusteringAlg, preprocessor);
+        Collection<Collection<Integer>> clustering = preprocessedClusteringAlg.cluster(clusteringSimilarity);
+        ClusteringResult<Integer> mRes = ClusteringResult.fromIntegerCollections(new ArrayList<>(clustering), readResult.similarity);
+        List<Cluster<Integer>> clusters = new ArrayList<>(mRes.getClusters());
+        clusters.sort(Comparator.comparingDouble(c -> -c.getNormalizedCommunityStrengthPerConnection()));
+
+        System.out.println("cs\tncsm\tavgSim\tcombined\tmembers");
+        for (Cluster<Integer> c : clusters) {
+            float ncsm = c.getCommunityStrengthPerConnection();
+            float avgSim = c.averageSimilarity((a, b) -> (float) readResult.similarity.getEntry(a, b));
+            System.out.println(str(c.getCommunityStrength()) + "\t" + str(ncsm) + "\t" + str(avgSim) + "\t"
+                    + c.getMembers().stream().map(readResult.mapping::unmap).collect(Collectors.toList()));
+        }
+        System.out.println("Community Strength: " + mRes.getCommunityStrength());
+        System.out.println("Clusters: " + clusters.size());
+
+        expected.ifPresent(expectedIdentifiers -> {
+            Set<String> expectedIdentifiersSet = new HashSet<>(expectedIdentifiers);
+            Set<String> bestClusters = clusters.get(0).getMembers().stream().map(readResult.mapping::unmap).collect(Collectors.toSet());
+            assertEquals(expectedIdentifiersSet, bestClusters);
+            System.out.println("hey");
+        });
+    }
+
+    @Test
+    public void aClusteringOld() throws FileNotFoundException, URISyntaxException {
+        for (TestFile testFile : OLD_CLUSTERING_DATA) {
+            URL url = loadFromClasspath(testFile.uri);
+            File file = new File(url.toURI());
+            ReadResult r = readOldCsv(file);
+            doTesting(r, testFile.expected);
+        }
+    }
+
+    @Test
+    public void aClusteringNew() throws FileNotFoundException, URISyntaxException {
+        for (TestFile testFile : NEW_CLUSTERING_DATA) {
+            URL url = loadFromClasspath(testFile.uri);
+            File file = new File(url.toURI());
+            ReadResult r = readNewCsv(file);
+            doTesting(r, testFile.expected);
+        }
+    }
+
+    private static class ReadComparison {
+        int left;
+        int right;
+        float similarity;
+    }
+
+    private static class ReadResult {
+        IntegerMapping<String> mapping;
+        RealMatrix similarity;
+    }
+
+    private static ReadResult readNewCsv(File fileName) throws FileNotFoundException {
+        IntegerMapping<String> mapping = new IntegerMapping<>(512);
+        List<ReadComparison> comparisons = new ArrayList<>(512);
+        try (CSVReader reader = new CSVReader(fileName, ";")) {
+            while (reader.hasNext()) {
+                List<String> records = reader.next();
+                if (records.isEmpty())
+                    continue;
+                String leftStudent = records.get(1);
+                String rightStudent = records.get(2);
+                String similarity = records.get(3);
+                ReadComparison comparison = new ReadComparison();
+                comparison.left = mapping.map(leftStudent);
+                comparison.right = mapping.map(rightStudent);
+                comparison.similarity = Float.parseFloat(similarity) / 100;
+                comparisons.add(comparison);
+            }
+        }
+        RealMatrix matrix = new Array2DRowRealMatrix(mapping.size(), mapping.size());
+        for (ReadComparison comparison : comparisons) {
+            matrix.setEntry(comparison.left, comparison.right, comparison.similarity);
+            matrix.setEntry(comparison.right, comparison.left, comparison.similarity);
+        }
+        ReadResult r = new ReadResult();
+        r.similarity = matrix;
+        r.mapping = mapping;
+        return r;
+    }
+
+    private static ReadResult readOldCsv(File fileName) throws FileNotFoundException {
+        IntegerMapping<String> mapping = new IntegerMapping<>(512);
+        List<ReadComparison> comparisons = new ArrayList<>(512);
+        try (CSVReader reader = new CSVReader(fileName, ";")) {
+            while (reader.hasNext()) {
+                List<String> records = reader.next();
+                if (records.isEmpty())
+                    continue;
+                Deque<String> stuff = new ArrayDeque<>(records);
+                String leftStudent = stuff.removeFirst();
+                int leftID = mapping.map(leftStudent);
+                while (stuff.size() >= 3) {
+                    ReadComparison comparison = new ReadComparison();
+                    comparison.left = leftID;
+                    stuff.removeFirst(); // comparison ID not needed
+                    String rightStudent = stuff.removeFirst();
+                    String similarity = stuff.removeFirst();
+                    comparison.right = mapping.map(rightStudent);
+                    comparison.similarity = Float.parseFloat(similarity) / 100;
+                    comparisons.add(comparison);
+                }
+            }
+        }
+        RealMatrix matrix = new Array2DRowRealMatrix(mapping.size(), mapping.size());
+        for (ReadComparison comparison : comparisons) {
+            matrix.setEntry(comparison.left, comparison.right, comparison.similarity);
+            matrix.setEntry(comparison.right, comparison.left, comparison.similarity);
+        }
+        ReadResult r = new ReadResult();
+        r.similarity = matrix;
+        r.mapping = mapping;
+        return r;
+    }
+
+    private static class CSVReader implements AutoCloseable {
+        private String delimiter;
+        private Scanner scanner;
+
+        private CSVReader(File fileName, String delimiter) throws FileNotFoundException {
+            this.delimiter = delimiter;
+            scanner = new Scanner(fileName);
+        }
+
+        @Override
+        public void close() {
+            scanner.close();
+        }
+
+        List<String> next() {
+            String line = scanner.nextLine();
+            String[] records = line.split(delimiter);
+            return Arrays.asList(records);
+        }
+
+        boolean hasNext() {
+            return scanner.hasNextLine();
+        }
+    }
+}

+ 56 - 0
jplag/src/test/java/de/jplag/clustering/ClusteringResultTest.java

@@ -0,0 +1,56 @@
+package de.jplag.clustering;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+import org.junit.Test;
+
+public class ClusteringResultTest {
+
+    @Test
+    public void perfectClustering() {
+        RealMatrix similarity = new Array2DRowRealMatrix(4, 4);
+
+        // These are similar
+        setEntries(similarity, 0, 1, 1f);
+        setEntries(similarity, 2, 3, 1f);
+
+        // Others are dissimilar
+
+        ClusteringResult<Integer> result = ClusteringResult.fromIntegerCollections(List.of(List.of(0, 1), List.of(2, 3)), similarity);
+
+        // The maximum of the metric is 1 - 1/k for k clusters
+        assertEquals(0.5, result.getCommunityStrength(), 0.00001);
+    }
+
+    @Test
+    public void uniformClustering() {
+        RealMatrix similarity = new Array2DRowRealMatrix(4, 4);
+
+        // We'd obtain such weights by pre-selecting the clusters,
+        // then randomly picking two clusters and adding weight between random of each
+        // cluster
+
+        // These are similar
+        setEntries(similarity, 0, 1, 0.1f);
+        setEntries(similarity, 2, 3, 0.1f);
+
+        // Others are dissimilar
+        setEntries(similarity, 0, 2, 0.05f);
+        setEntries(similarity, 0, 3, 0.05f);
+        setEntries(similarity, 1, 2, 0.05f);
+        setEntries(similarity, 1, 3, 0.05f);
+
+        ClusteringResult<Integer> result = ClusteringResult.fromIntegerCollections(List.of(List.of(0, 1), List.of(2, 3)), similarity);
+
+        assertEquals(0.0, result.getCommunityStrength(), 0.00001);
+    }
+
+    private static void setEntries(RealMatrix matrix, int i, int j, double value) {
+        matrix.setEntry(i, j, value);
+        matrix.setEntry(j, i, value);
+    }
+}

+ 17 - 0
jplag/src/test/java/de/jplag/clustering/algorithm/AgglomerativeClusteringTest.java

@@ -0,0 +1,17 @@
+package de.jplag.clustering.algorithm;
+
+import java.util.Collection;
+
+import org.junit.Test;
+
+public class AgglomerativeClusteringTest {
+
+    @Test
+    public void test() {
+        for (ClusteringData testData : ClusteringData.values()) {
+            AgglomerativeClustering clustering = new AgglomerativeClustering(testData.getOptions());
+            Collection<Collection<Integer>> result = clustering.cluster(testData.getSimilarity());
+            testData.assertValid(result);
+        }
+    }
+}

+ 26 - 0
jplag/src/test/java/de/jplag/clustering/algorithm/BayesianOptimizationTest.java

@@ -0,0 +1,26 @@
+package de.jplag.clustering.algorithm;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.commons.math3.linear.RealVector;
+import org.junit.Test;
+
+public class BayesianOptimizationTest {
+
+    @Test
+    public void findParabolaMax() {
+        RealVector minima = new ArrayRealVector(new double[] {-20});
+        RealVector maxima = new ArrayRealVector(new double[] {10});
+        RealVector lengthScale = new ArrayRealVector(new double[] {5});
+        double maximumAt = 1;
+        BayesianOptimization bo = new BayesianOptimization(minima, maxima, 3, 15, 1.0 / 12.0, lengthScale);
+        double maximumPoint = bo.maximize(v -> {
+            double val = v.getEntry(0);
+            double result = -(val - maximumAt) * (val - maximumAt);
+            result += 0.2 * (Math.random() - 0.5);
+            return new BayesianOptimization.OptimizationResult<Double>(result, result);
+        }).getParams().getEntry(0);
+        assertEquals(maximumAt, maximumPoint, 1);
+    }
+}

+ 68 - 0
jplag/src/test/java/de/jplag/clustering/algorithm/ClusteringData.java

@@ -0,0 +1,68 @@
+package de.jplag.clustering.algorithm;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.RealMatrix;
+
+import de.jplag.clustering.ClusteringOptions;
+
+public enum ClusteringData {
+
+    FOUR_POINTS(() -> {
+        RealMatrix similarity = new Array2DRowRealMatrix(4, 4);
+        for (int i = 0; i < 4; i++) {
+            similarity.setEntry(i, i, 1);
+        }
+        // These are similar
+        setEntries(similarity, 0, 1, 0.5);
+        setEntries(similarity, 2, 3, 0.5f);
+
+        // Others are dissimilar
+        setEntries(similarity, 0, 2, 0.1f);
+        setEntries(similarity, 0, 3, 0.1f);
+        setEntries(similarity, 1, 2, 0.1f);
+        setEntries(similarity, 1, 3, 0.1f);
+
+        return similarity;
+    }, new int[][] {{0, 1}, {2, 3}}, new ClusteringOptions.Builder().agglomerativeThreshold(0.4f));
+
+    private final RealMatrix similarity;
+    private final Set<Set<Integer>> expected;
+    private final ClusteringOptions options;
+
+    private ClusteringData(Supplier<RealMatrix> similarity, int[][] expected, ClusteringOptions.Builder options) {
+        this.similarity = similarity.get();
+        this.expected = makeSets(
+                Arrays.stream(expected).map(intArray -> Arrays.stream(intArray).boxed().collect(Collectors.toList())).collect(Collectors.toList()));
+        this.options = options.build();
+    }
+
+    public ClusteringOptions getOptions() {
+        return options;
+    }
+
+    public RealMatrix getSimilarity() {
+        return similarity;
+    }
+
+    private Set<Set<Integer>> makeSets(Collection<? extends Collection<Integer>> collections) {
+        return collections.stream().map(HashSet::new).collect(Collectors.toSet());
+    }
+
+    public void assertValid(Collection<? extends Collection<Integer>> actual) {
+        assertEquals(this.name() + " not clustered correctly", expected, makeSets(actual));
+    }
+
+    private static void setEntries(RealMatrix matrix, int i, int j, double value) {
+        matrix.setEntry(i, j, value);
+        matrix.setEntry(j, i, value);
+    }
+}

+ 37 - 0
jplag/src/test/java/de/jplag/clustering/algorithm/GaussianProcessTest.java

@@ -0,0 +1,37 @@
+package de.jplag.clustering.algorithm;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.commons.math3.linear.RealVector;
+import org.junit.Test;
+
+public class GaussianProcessTest {
+
+    @Test
+    public void noisyLinearFunction() {
+        List<RealVector> X = new ArrayList<>();
+        double[] Y = new double[20];
+        int idx = 0;
+        for (int i = 0; i < 10; i++) {
+            RealVector x = new ArrayRealVector(1);
+            x.setEntry(0, i);
+            X.add(x);
+            X.add(x);
+            Y[idx++] = i + Math.random() - 0.5;
+            Y[idx++] = i + Math.random() - 0.5;
+        }
+        GaussianProcess gp = GaussianProcess.fit(X, Y, 1 / 12.0, true, new double[] {1});
+        for (int i = 0; i < 19; i++) {
+            RealVector vx = new ArrayRealVector(1);
+            double x = i / 2.0;
+            vx.setEntry(0, x);
+            double[] prediction = gp.predict(vx);
+            assertTrue("The prediction error can't very high", Math.abs(prediction[0] - x) < 1);
+            assertTrue("The standard deviation must be greater than 0", prediction[1] > 0);
+        }
+    }
+}

+ 17 - 0
jplag/src/test/java/de/jplag/clustering/algorithm/SpectralClusteringTest.java

@@ -0,0 +1,17 @@
+package de.jplag.clustering.algorithm;
+
+import java.util.Collection;
+
+import org.junit.Test;
+
+public class SpectralClusteringTest {
+
+    @Test
+    public void test() {
+        for (ClusteringData testData : ClusteringData.values()) {
+            SpectralClustering clustering = new SpectralClustering(testData.getOptions());
+            Collection<Collection<Integer>> result = clustering.cluster(testData.getSimilarity());
+            testData.assertValid(result);
+        }
+    }
+}

+ 40 - 0
jplag/src/test/java/de/jplag/clustering/preprocessors/CumulativeDistributionFunctionPreprocessorTest.java

@@ -0,0 +1,40 @@
+package de.jplag.clustering.preprocessors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class CumulativeDistributionFunctionPreprocessorTest extends PreprocessingTestBase {
+
+    private static final double EPSILON = 0.0000001;
+
+    CumulativeDistributionFunctionPreprocessor preprocessor;
+
+    @Before
+    public void init() {
+        preprocessor = new CumulativeDistributionFunctionPreprocessor();
+    }
+
+    @Test
+    public void satisfiesInterface() {
+        double[][] original = createTestData();
+        double[][] result = preprocessor.preprocessSimilarities(original);
+        validPreprocessing(original, result, preprocessor::originalIndexOf);
+    }
+
+    @Test
+    public void mayDecreaseOnly() {
+        double[][] original = createTestData();
+        double[][] result = preprocessor.preprocessSimilarities(original);
+        withAllValues(preprocessor, original, result, (originalValue, preprocessed) -> {
+            if (originalValue == 0.0) {
+                assertEquals(0.0, preprocessed.orElse(0.0), EPSILON);
+            } else {
+                assertTrue(preprocessed.get() <= originalValue);
+            }
+        });
+    }
+
+}

+ 43 - 0
jplag/src/test/java/de/jplag/clustering/preprocessors/PercentilePreprocessorTest.java

@@ -0,0 +1,43 @@
+package de.jplag.clustering.preprocessors;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Optional;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class PercentilePreprocessorTest extends PreprocessingTestBase {
+
+    private static final double EPSILON = 0.0000001;
+
+    PercentileThresholdProcessor preprocessor;
+
+    @Before
+    public void init() {
+        preprocessor = new PercentileThresholdProcessor(0.5f);
+    }
+
+    @Test
+    public void satisfiesInterface() {
+        double[][] original = createTestData();
+        double[][] result = preprocessor.preprocessSimilarities(original);
+        validPreprocessing(original, result, preprocessor::originalIndexOf);
+    }
+
+    @Test
+    public void removedBelowPercentile() {
+        double[][] original = createTestData();
+        double[][] result = preprocessor.preprocessSimilarities(original);
+        withAllValues(preprocessor, original, result, (originalValue, preprocessed) -> {
+            // Values are only 0.1 and 0.5; percentile 0.5 should only preserve values of
+            // 0.5.
+            if (originalValue > 0.1) {
+                assertEquals(Optional.of(originalValue), preprocessed);
+            } else {
+                assertEquals(0.0, preprocessed.orElse(0.0), EPSILON);
+            }
+        });
+    }
+
+}

+ 89 - 0
jplag/src/test/java/de/jplag/clustering/preprocessors/PreprocessingTestBase.java

@@ -0,0 +1,89 @@
+package de.jplag.clustering.preprocessors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.BiConsumer;
+import java.util.function.IntUnaryOperator;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.ArrayRealVector;
+import org.apache.commons.math3.linear.RealMatrix;
+
+import de.jplag.clustering.ClusteringPreprocessor;
+
+public class PreprocessingTestBase {
+
+    private static final double EPSILON = 0.000001;
+
+    public double[][] createTestData() {
+        RealMatrix similarity = new Array2DRowRealMatrix(5, 5);
+        for (int i = 0; i < 4; i++) {
+            similarity.setEntry(i, i, 1);
+        }
+        // These are similar
+        setEntries(similarity, 0, 1, 0.5);
+        setEntries(similarity, 2, 3, 0.5f);
+
+        // Others are dissimilar
+        setEntries(similarity, 0, 2, 0.1f);
+        setEntries(similarity, 0, 3, 0.1f);
+        setEntries(similarity, 1, 2, 0.1f);
+        setEntries(similarity, 1, 3, 0.1f);
+
+        // last row is empty
+
+        return similarity.getData();
+    }
+
+    public void validPreprocessing(double[][] originalArray, double[][] resultArray, IntUnaryOperator originalIndex) {
+        RealMatrix result = new Array2DRowRealMatrix(resultArray, false);
+        RealMatrix original = new Array2DRowRealMatrix(originalArray, false);
+        assertEquals("not a square matrix", result.getColumnDimension(), result.getRowDimension());
+
+        List<Integer> usedOriginalIndices = IntStream.range(0, result.getColumnDimension()).map(originalIndex).boxed().collect(Collectors.toList());
+
+        assertEquals("original indices not unique", usedOriginalIndices.size(), new HashSet<>(usedOriginalIndices).size());
+
+        assertTrue("original indices valid", usedOriginalIndices.stream().allMatch(index -> index >= 0 && index < original.getColumnDimension()));
+
+        double[] columnNorms = IntStream.range(0, result.getColumnDimension()).mapToObj(index -> result.getColumn(index))
+                .mapToDouble(array -> new ArrayRealVector(array, false).getNorm()).toArray();
+
+        for (int i = 0; i < columnNorms.length; i++) {
+            assertTrue("produced zero column in column " + i, columnNorms[i] > EPSILON);
+        }
+    }
+
+    public double[][] withAllValues(ClusteringPreprocessor preprocessor, double[][] original, double[][] result,
+            BiConsumer<Double, Optional<Double>> originalAndPreprocessedConsumer) {
+
+        // Construct mapping from original indices to preprocessed indices as implied by
+        // originalIndexOf
+        Map<Integer, Integer> mappedIndices = IntStream.range(0, result.length).collect(HashMap::new,
+                (map, index) -> map.put(preprocessor.originalIndexOf(index), index), HashMap::putAll);
+        for (int j = 0; j < original.length; j++) {
+            double[] row = original[j];
+            Optional<double[]> mappedRow = Optional.ofNullable(mappedIndices.get(j)).map(index -> result[index]);
+            for (int i = 0; i < row.length; i++) {
+                double originalValue = row[i];
+                Optional<Double> value = Optional.ofNullable(mappedIndices.get(i)).flatMap(index -> mappedRow.map(x -> x[index]));
+                originalAndPreprocessedConsumer.accept(originalValue, value);
+            }
+        }
+
+        return result;
+    }
+
+    private static void setEntries(RealMatrix matrix, int i, int j, double value) {
+        matrix.setEntry(i, j, value);
+        matrix.setEntry(j, i, value);
+    }
+}

+ 41 - 0
jplag/src/test/java/de/jplag/clustering/preprocessors/ThresholdPreprocessorTest.java

@@ -0,0 +1,41 @@
+package de.jplag.clustering.preprocessors;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Optional;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class ThresholdPreprocessorTest extends PreprocessingTestBase {
+
+    private static final double EPSILON = 0.0000001;
+
+    ThresholdPreprocessor preprocessor;
+
+    @Before
+    public void init() {
+        preprocessor = new ThresholdPreprocessor(0.2);
+    }
+
+    @Test
+    public void satisfiesInterface() {
+        double[][] original = createTestData();
+        double[][] result = preprocessor.preprocessSimilarities(original);
+        validPreprocessing(original, result, preprocessor::originalIndexOf);
+    }
+
+    @Test
+    public void removedBelowThreshold() {
+        double[][] original = createTestData();
+        double[][] result = preprocessor.preprocessSimilarities(original);
+        withAllValues(preprocessor, original, result, (originalValue, preprocessed) -> {
+            if (originalValue > preprocessor.getThreshold()) {
+                assertEquals(Optional.of(originalValue), preprocessed);
+            } else {
+                assertEquals(0.0, preprocessed.orElse(0.0), EPSILON);
+            }
+        });
+    }
+
+}

+ 0 - 17
jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/A/Java11.java

@@ -13,23 +13,6 @@ public class Java11 {
         Cal cal = (var a, var b) -> a + b;
         int result = cal.sum(10, 20);
         System.out.println(result);
-        
-        // New HTTP Client API:
-        HttpClient client = HttpClient.newBuilder()
-                .version(Version.HTTP_1_1)
-                .followRedirects(Redirect.NORMAL)
-                .connectTimeout(Duration.ofSeconds(20))
-                .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
-                .authenticator(Authenticator.getDefault())
-                .build();
-        System.out.println(client);
-        
-        // New string methods:
-        String text = "ABCD";
-        text.repeat(2);
-        text.isBlank();
-        text.strip();
-        text.lines();
     }
     
     interface Cal {

+ 1 - 1
jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/A/Java12.java

@@ -1,7 +1,7 @@
 import java.text.NumberFormat;
 import java.util.Locale;
 
-public class Java12 {
+public class Java12 { // Java 12 has no new relevant structural features
     public static void main(String[] args) {
         // New String methods:
         String text = "ABCD";

+ 2 - 2
jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/A/Java14.java

@@ -8,8 +8,8 @@ public enum Java14 {
     public static void main(String[] args) {
         // Advanced switch expressions (JEP 361):
         boolean isWeekend = switch (MONDAY) {
-        case SATURDAY, SUNDAY -> true;
-        default -> false;
+            case SATURDAY, SUNDAY -> true;
+            default -> false;
         };
         System.out.println(isWeekend);
     }

+ 2 - 2
jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/B/Java10.java

@@ -7,9 +7,9 @@ public class Java10 {
     List<Optional<String>> test;
     
     public Java10() {
-        // Var type for local variables (JEP 286):
+        // Typed variables as usual:
         test = new ArrayList<>();
-        var variable = test;
+        List<Optional<String>> variable = test;
         int length = variable.toString().length();
         test = variable;
         test.add(Optional.of(variable.toString()));

+ 1 - 18
jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/B/Java11.java

@@ -10,26 +10,9 @@ public class Java11 {
 
     public static void main(String[] args) {
         // Var type allowed in lambda paramters (JEP 323):
-        Cal cal = (a, b) -> a + b;
+        Cal cal = (int a, int b) -> a + b;
         int result = cal.sum(10, 20);
         System.out.println(result);
-        
-        // New HTTP Client API:
-        HttpClient client = HttpClient.newBuilder()
-                .version(Version.HTTP_1_1)
-                .followRedirects(Redirect.ALWAYS)
-                .connectTimeout(Duration.ofSeconds(15))
-                .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.de", 90)))
-                .authenticator(Authenticator.getDefault())
-                .build();
-        System.out.println(client);
-        
-        // New string methods:
-        String text = "ABCD";
-        text.repeat(4);
-        text.isBlank();
-        text.strip();
-        text.lines();
     }
     
     interface Cal {

+ 1 - 1
jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/B/Java12.java

@@ -1,7 +1,7 @@
 import java.text.NumberFormat;
 import java.util.Locale;
 
-public class Java12 {
+public class Java12 { // Java 12 has no new relevant structural features
     public static void main(String[] args) {
         // New String methods:
         String text = "ABCD";

+ 7 - 5
jplag/src/test/resources/de/jplag/samples/NewJavaFeatures/B/Java14.java

@@ -6,11 +6,13 @@ public enum Java14 {
     SUNDAY;
 
     public static void main(String[] args) {
-        // Advanced switch expressions (JEP 361):
-        boolean isWeekend = switch (MONDAY) {
-        case FRIDAY, SATURDAY, SUNDAY -> true;
-        default -> false;
-        };
+        // Old switch expressions:
+        boolean isWeekend;
+        switch (MONDAY) {
+            case SATURDAY:
+            case SUNDAY: isWeekend = true; break;
+            default: isWeekend = false;
+        }
         System.out.println(isWeekend);
     }
 

+ 2 - 2
pom.xml

@@ -81,7 +81,7 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <maven.compiler.source>14</maven.compiler.source>
         <maven.compiler.target>14</maven.compiler.target>
-        <spotless.version>2.20.2</spotless.version>
+        <spotless.version>2.21.0</spotless.version>
     </properties>
     <dependencyManagement>
         <dependencies>
@@ -328,7 +328,7 @@
             <plugin>
                 <groupId>org.sonatype.plugins</groupId>
                 <artifactId>nexus-staging-maven-plugin</artifactId>
-                <version>1.6.8</version>
+                <version>1.6.12</version>
                 <extensions>true</extensions>
                 <configuration>
                     <serverId>ossrh</serverId>