public void IndexOf2(int size) { var random = RandomRegistry.GetRandom(); var props = new double[size]; var divisor = props.Length * (props.Length + 1) / 2.0; for (var i = 0; i < props.Length; ++i) { props[i] = (i + 1) / divisor; } Randomize(props, random); var incremental = ProbabilitySelector.Incremental((double[])props.Clone()); const int samples = 100000; for (var i = 0; i < samples; ++i) { var value = random.NextDouble(); var index1 = ProbabilitySelector.IndexOf(incremental, value); var index2 = IndexOf(props, value); Assert.Equal(index2, index1); } }
public void IndexOf1() { var random = RandomRegistry.GetRandom(); var props = new double[10]; var divisor = props.Length * (props.Length + 1) / 2.0; for (var i = 0; i < props.Length; ++i) { props[i] = (i + 1) / divisor; } Randomize(props, random); var incremental = ProbabilitySelector.Incremental((double[])props.Clone()); const double samples = 1000000; var indices = new double[props.Length]; Array.Fill(indices, 0); for (var i = 0; i < samples; ++i) { indices[ProbabilitySelector.IndexOf(incremental, random.NextDouble())] += 1; } for (var i = 0; i < props.Length; ++i) { indices[i] /= samples; } for (var i = 0; i < props.Length; ++i) { Assert.Equal(props[i], indices[i], 1); } }
public virtual Population <TGene, TAllele> Select(Population <TGene, TAllele> population, int count, Optimize opt) { if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count), $"Selection count must be greater or equal then zero, but was {count}."); } var selection = new Population <TGene, TAllele>(count); if (count > 0 && !population.IsEmpty) { var pop = Copy(population); var prob = Probabilities(pop, count, opt); CheckAndCorrect(prob); ProbabilitySelector.Incremental(prob); var random = RandomRegistry.GetRandom(); selection.Fill(() => pop[ProbabilitySelector.IndexOf(prob, random.NextDouble())], count); } return(selection); }
public void IndexOfSerialEqualBinary(int size) { var probabilities = Array(size, new Random(12)); Arithmetics.Normalize(probabilities); ProbabilitySelector.Incremental(probabilities); Assert.Equal( ProbabilitySelector.IndexOfSerial(probabilities, 0.5), ProbabilitySelector.IndexOfBinary(probabilities, 0.5) ); }