initialClusters function

List<Cluster> initialClusters(
  1. int k,
  2. List<Instance> instances, {
  3. int? seed,
})

Creates an list of clusters.

An attempt is made to spread the clusters by making each instance in instances a candidate for the next cluster position with an associated probability proportional to the distance from the most recent cluster.

Implementation

List<Cluster> initialClusters(int k, List<Instance> instances, {int? seed}) {
  final Random rand = seed == null ? Random() : Random(seed);

  Cluster nextCluster(Cluster? prev, String id) {
    List<num> ds = instances
        .map((Instance instance) => prev?.distanceFrom(instance) ?? 1)
        .toList();
    num sum = ds.fold(0.0, (a, b) => a + b);
    List<num> ps = ds.map((x) => x / sum).toList();
    int instanceIndex = 0;
    num r = rand.nextDouble(), cum = 0;
    while (true) {
      cum += ps[instanceIndex];
      if (cum > r) break;
      instanceIndex++;
    }
    return Cluster(
      location: List<num>.from(instances[instanceIndex].location),
      id: id,
    );
  }

  Cluster? prev;
  var clusters =
      List<Cluster>.generate(k, (i) => nextCluster(prev, "cluster[$i]"));

  instances.forEach((instance) {
    instance.reallocate(clusters);
  });
  return clusters;
}