sampleSupplyCounts static method

List<int> sampleSupplyCounts(
  1. DynamicPath path,
  2. int totalPointsCount, {
  3. bool weightBased = true,
  4. List<int>? oldCounts,
})

Implementation

static List<int> sampleSupplyCounts(DynamicPath path, int totalPointsCount,
    {bool weightBased = true, List<int>? oldCounts}) {
  int length = path.nodes.length;

  int newPointsCount = totalPointsCount - length;

  if (newPointsCount == 0) return List.generate(length, (index) => 0);

  List<double> weights = [];
  double totalWeights = 0.0;
  for (int i = 0; i < length; i++) {
    if (weightBased && oldCounts == null) {
      weights.add(path.getPathLengthAt(i));
    } else {
      weights.add(1.0);
    }
  }
  for (int i = 0; i < length; i++) {
    totalWeights += weights[i];
  }

  List<int> counts;
  int chooseIndex;

  if (oldCounts == null) {
    double scale = totalWeights / newPointsCount;
    counts = weights.map((w) => (w / scale).ceil()).toList();
  } else {
    counts = oldCounts.map((e) => (e + 1)).toList();
  }

  while (counts.total() > newPointsCount) {
    chooseIndex = randomChoose(weights);

    if (counts[chooseIndex] > 0) {
      counts[chooseIndex] -= 1;
    }
  }

  return counts;
}