public Species Produce(Species parent1, Species parent2) { BitArray bitArray = new BitArray(parent1.Values.Length); for (var i = 0; i < bitArray.Count; i++) { bitArray[i] = _rnd.Next(0, 2) == 1 ? parent1.Values[i] : parent2.Values[i]; } var result = new Species(bitArray, _fitnessFun.Calculate(bitArray)); return(result); }
public Species Produce(Species parent1, Species parent2) { BitArray bitArray = new BitArray(parent1.Values.Length); var better = parent1.Fittness >= parent2.Fittness ? parent1 : parent2; var worse = better == parent1 ? parent2 : parent1; for (var i = 0; i < bitArray.Count; i++) { bitArray[i] = _rnd.NextDouble() >= 1 - _betterParentGeneSelectionProbability ? better.Values[i] : worse.Values[i]; } var result = new Species(bitArray, _fitnessFun.Calculate(bitArray)); return(result); }
private static Species[] GenerateInitialSpecies(int speciesNumber, int itemCount, IFitnessFun fitnessFun) { var rnd = new Random(); return(Enumerable.Range(1, speciesNumber) .Select(_ => { var vector = new BitArray(itemCount); for (var i = 0; i < vector.Count; i++) { bool value = rnd.Next(0, 2) == 1; vector[i] = value; } return vector; }) .Select(v => new Species(v, fitnessFun.Calculate(v))) .ToArray()); }