예제 #1
0
파일: Algorithm.cs 프로젝트: kylc/seve
        public Algorithm(int populationSize, BinarySerializer constructor, ICrossFunction crossFunction, IMutateFunction mutateFunction)
        {
            PopulationSize = populationSize;
            Constructor = constructor;
            CrossFunction = crossFunction;
            MutateFunction = mutateFunction;

            Random = new Random();

            MakeInitialPopulation();
        }
예제 #2
0
        public Individual Cross(Individual father, Individual mother, BinarySerializer serializer)
        {
            byte[] fatherData = serializer.ToBinary(father);
            byte[] motherData = serializer.ToBinary(mother);

            if (fatherData.Length != motherData.Length)
            {
                throw new ArgumentException("Individuals must compose to binary representations of the same length to be crossed.");
            }

            byte[] childData = new byte[fatherData.Length];

            var splitIndex = (long)(fatherData.Length * Ratio);
            Array.Copy(fatherData, childData, splitIndex);
            Array.Copy(motherData, splitIndex, childData, splitIndex, childData.Length - splitIndex);

            return serializer.FromBinary(childData);
        }