Exemple #1
0
        public static Report Calculate(
            int n, List <int> a, List <int> b, int B,
            SelectionType selectionType, LocalOptimization optimization,
            int numberOfIterations = 10, int populationSize = 15)
        {
            if (numberOfIterations < 3)
            {
                numberOfIterations = 3;
            }
            NumberOfGenes = populationSize;
            RecordsForMax = new List <int>();
            int iterationsForMax = 0, recordForMax = int.MinValue;

            CreatePopulation(n, a, b, B);

            do
            {
                switch (selectionType)
                {
                case SelectionType.Tournament: MakeTournamentSelectionForMax();
                    break;

                case SelectionType.FullyRandom: MakeFullyRandomSelection();
                    break;

                case SelectionType.PartlyRandom: MakeSelectionWithMaxAndRandom();
                    break;

                case SelectionType.OutBreeding: MakeOutBreedingSelection();
                    break;
                }

                FirstChild  = Gene.Recombinate(FirstParent.Chromosomes, SecondParent.Chromosomes, n);
                SecondChild = Gene.Recombinate(FirstParent.Chromosomes, SecondParent.Chromosomes, n);

                FirstChild.Mutate(a, b);
                SecondChild.Mutate(a, b);

                FirstChild.Reanimate(a, b, B);
                SecondChild.Reanimate(a, b, B);

                switch (optimization)
                {
                case LocalOptimization.Optimize:
                    FirstChild.OptimizeForMax(a, b, B);
                    SecondChild.OptimizeForMax(a, b, B);
                    break;
                }

                UpdatePopulationForMax();

                if (recordForMax < Population[GetIndexOfMaxGen()].Fitness)
                {
                    recordForMax = Population[GetIndexOfMaxGen()].Fitness;
                }

                RecordsForMax.Add(recordForMax);
            } while (!isReadyToStop(RecordsForMax, ++iterationsForMax, numberOfIterations));

            CreatePopulation(n, a, b, B);

            RecordsForMin = new List <int>();

            int recordForMin = int.MaxValue, iterationsForMin = 0;

            do
            {
                switch (selectionType)
                {
                case SelectionType.Tournament: MakeTournamentSelectionForMin();
                    break;

                case SelectionType.FullyRandom: MakeFullyRandomSelection();
                    break;

                case SelectionType.PartlyRandom: MakeSelectionWithMinAndRandom();
                    break;

                case SelectionType.OutBreeding: MakeOutBreedingSelection();
                    break;
                }

                FirstChild  = Gene.Recombinate(FirstParent.Chromosomes, SecondParent.Chromosomes, n);
                SecondChild = Gene.Recombinate(FirstParent.Chromosomes, SecondParent.Chromosomes, n);

                FirstChild.Mutate(a, b);
                SecondChild.Mutate(a, b);

                FirstChild.Reanimate(a, b, B);
                SecondChild.Reanimate(a, b, B);

                switch (optimization)
                {
                case LocalOptimization.Optimize:
                    FirstChild.OptimizeForMin(a, b, B);
                    SecondChild.OptimizeForMin(a, b, B);
                    break;
                }

                UpdatePopulationForMin();

                if (recordForMin > Population[GetIndexOfMinGen()].Fitness)
                {
                    recordForMin = Population[GetIndexOfMinGen()].Fitness;
                }

                RecordsForMin.Add(recordForMin);
            } while (!isReadyToStop(RecordsForMin, ++iterationsForMin, numberOfIterations));

            return(new Report(
                       (recordForMax, iterationsForMax, RecordsForMax),
                       (recordForMin, iterationsForMin, RecordsForMin)));
        }