Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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());
        }