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) ); }
public override double[] Probabilities(Population <TGene, TAllele> population, int count) { var fitness = new double[population.Count]; // Copy the fitness values to probabilities arrays. fitness[0] = population[0].GetFitness().ToDouble(null); var min = fitness[0]; var max = fitness[0]; for (var i = 1; i < fitness.Length; ++i) { fitness[i] = population[i].GetFitness().ToDouble(null); if (fitness[i] < min) { min = fitness[i]; } else if (fitness[i] > max) { max = fitness[i]; } } var diff = max - min; if (ProbabilitySelector.Eq(diff, 0.0)) { // Set equal probabilities if diff (almost) zero. //Array.Fill(fitness, 1.0 / fitness.Length); for (int index = 0; index < fitness.Length; index++) { fitness[index] = 1.0 / fitness.Length; } } else { // Scale fitness values to avoid overflow. for (var i = fitness.Length; --i >= 0;) { fitness[i] = (fitness[i] - min) / diff; } // Apply the "Boltzmann" function. for (var i = fitness.Length; --i >= 0;) { fitness[i] = Math.Exp(_b * fitness[i]); } } return(Arithmetics.Normalize(fitness)); }