code: public static Assignments cluster(Matrix matrix,
                                        int numClusters,
                                        int numRepetitions,
                                        KMeansSeed seedType,
                                        CriterionFunction criterion) {
          int[] bestAssignment = null;
          double bestScore = (criterion.isMaximize()) ? 0 : Double.MAX_VALUE;
          for (int i = 0; i < numRepetitions; ++i) {
              clusterIteration(matrix, numClusters, seedType, criterion);
              if (criterion.isMaximize()) {
                  if (criterion.score() > bestScore) {
                      bestScore = criterion.score();
                      bestAssignment = criterion.assignments();
                  }
              } else {
                  if (criterion.score() < bestScore) {
                      bestScore = criterion.score();
                      bestAssignment = criterion.assignments();
                  }
              }
          }
  
          // Convert the array of assignments to an Assignments object.
          Assignment[] assignments = new Assignment[matrix.rows()];
          for (int i = 0; i < bestAssignment.length; ++i)
              assignments[i] = new HardAssignment(bestAssignment[i]);
  
          return new Assignments(numClusters, assignments, matrix);
      }
docs: Clusters {@link matrix} using the specified {@link SeedAlgorithm}
  and the specified {@link CriterionFunction}. The best scoring solution
  out of {@code numRepetitions} will be returned.