Пример #1
0
        private static void Crossover(Hromozome firstParent, Hromozome secondParent)
        {
            var randomIndex         = RAND.Next(0, firstParent.Representation.Count());
            var childRepresentation = (bool[])firstParent.Representation.Clone();

            Array.Copy(secondParent.Representation, randomIndex, childRepresentation, randomIndex, childRepresentation.Count() - randomIndex);
            CreateChild(childRepresentation);
        }
Пример #2
0
        private static void Mutate(Hromozome parent)
        {
            var randomIndex         = RAND.Next(0, parent.Representation.Count());
            var childRepresentation = (bool[])parent.Representation.Clone();

            childRepresentation[randomIndex] = !parent.Representation[randomIndex];
            CreateChild(childRepresentation);
        }
Пример #3
0
        private static void Select(Hromozome firstParent, Hromozome secondParent)
        {
            var childRepresentation = (bool[])firstParent.Representation.Clone();

            for (int i = 0; i < childRepresentation.Count(); i++)
            {
                childRepresentation[i] &= secondParent.Representation[i];
            }

            CreateChild(childRepresentation);
        }
Пример #4
0
        private static void CreateChild(bool[] representation)
        {
            var hromozome = new Hromozome
            {
                Representation = representation,
                Weight         = 0,
                Value          = 0
            };

            CalculateWeightAndValue(hromozome);
        }
Пример #5
0
        private static void CalculateWeightAndValue(Hromozome hromozome)
        {
            for (int j = 0; j < hromozome.Representation.Count(); j++)
            {
                if (hromozome.Representation[j])
                {
                    hromozome.Weight += Weights[j];
                    hromozome.Value  += Values[j];
                }
            }

            if (hromozome.Weight <= MaxWeight)
            {
                population.Add(hromozome);
            }
        }