private void TestSelector(ISelector selector, int tolerance, params double[] fitnesses) { int runCount = 100000; Population candidates = this.GenerateCandidates(fitnesses.ToArray()); Dictionary <double, int> proportions = fitnesses.ToDictionary(i => i, i => 0); selector.SetCandidates(candidates); candidates.UpdateFitnessProportions(); for (int i = 0; i <= runCount - 1; i++) { var candidate = selector.PickCandidates(1)[0]; proportions[candidate.Fitness] += 1; } double totalFitness = fitnesses.Sum(); foreach (KeyValuePair <double, int> proportion in proportions) { double fitnessProportion = proportion.Key / totalFitness; Assert.AreEqual(fitnessProportion * runCount, proportion.Value, runCount * fitnessProportion / tolerance); // within 1/tolerance % } }
private void TestSelector(ISelector selector, int tolerance, params double[] fitnesses) { int runCount = 100000; Population candidates = this.GenerateCandidates(fitnesses.ToArray()); Dictionary<double, int> proportions = fitnesses.ToDictionary(i => i, i => 0); selector.SetCandidates(candidates); candidates.UpdateFitnessProportions(); for (int i = 0; i <= runCount - 1; i++) { var candidate = selector.PickCandidates(1)[0]; proportions[candidate.Fitness] += 1; } double totalFitness = fitnesses.Sum(); foreach (KeyValuePair<double, int> proportion in proportions) { double fitnessProportion = proportion.Key / totalFitness; Assert.AreEqual(fitnessProportion * runCount, proportion.Value, runCount * fitnessProportion / tolerance); // within 1/tolerance % } }