public IEnumerable <T> GenerateOffspring(IEnumerable <T> candidates, int amount, IRNG random) { if (this.crossover != null) { candidates = this.Crossover(candidates, random); } return(candidates.Select(Mutate).Take(amount)); T Mutate(T candidate) { foreach (var mutator in this.mutators) { if (random.Flip(mutator.Chance)) { return(mutator.Mutate(candidate, random)); } } return(candidate); } }
public static T Choose <T>(this IRNG rng, T left, T right, float probability = 0.5f) { return(rng.Flip(probability) ? left : right); }