// ----------------------------------------------------------------------------------------------------
        //                                      Function Maximize related functions
        // ----------------------------------------------------------------------------------------------------

        public static int[,] DoCrossover(int[,] intialChromosome, ref DataTable mainTable)
        {
            matingPool     = new string[4, DataStorage.initialPopCount];
            crossoverPoint = new string[4, DataStorage.initialPopCount];
            offspring      = new string[4, DataStorage.initialPopCount];
            updatedXVal    = new int[4, DataStorage.initialPopCount];
            fitnessVal     = new int[4, DataStorage.initialPopCount];

            // Chromosomes that are selected for crossover
            for (int gen = 0; gen < 4; gen++)
            {
                for (int i = 0; i < DataStorage.initialPopCount; i++)
                {
                    matingPool[gen, i] = Utility.DecToBin(intialChromosome[gen, i]);
                }
            }

            PerformCrossoverRateFunctionMaximize(DataStorage.crossoverRate, ref matingPool, ref offspring, ref crossoverPoint);

            for (int gen = 0; gen < 4; gen++)
            {
                for (int i = 0; i < DataStorage.initialPopCount; i++)
                {
                    updatedXVal[gen, i] = Utility.BinToDec(offspring[gen, i]);
                    fitnessVal[gen, i]  = FunctionParser.calculate(DataStorage.fitnessFunction, updatedXVal[gen, i]);
                }
            }

            // Updating the crosover table
            UpdateTableValue(ref mainTable);

            return(updatedXVal);
        }
        public static void PerformCrossoverRateFunctionMaximize(int rate, ref string[,] inputchromosome, ref string[,] outputchromosome, ref string[,] crossPoint)
        {
            for (int gen = 0; gen < 4; gen++)
            {
                int      length         = inputchromosome.GetLength(1);
                int[]    tempFitness    = new int[length];
                string[] tempChromosome = new string[length];

                // Converting the chromosomes into there fitnessValue
                for (int i = 0; i < length; i++)
                {
                    tempFitness[i] = FunctionParser.calculate(DataStorage.fitnessFunction, Utility.BinToDec(inputchromosome[gen, i]));
                }

                // Sorting chromosomes based on their fitness level and also keeping track of thier chromosomes
                Utility.SortChromosomes(ref tempFitness, ref tempChromosome);

                // Initially, inputChomosomes and outputChromosomes are same and sorted based in thier fitness value
                for (int i = 0; i < length; i++)
                {
                    inputchromosome[gen, i] = outputchromosome[gen, i] = tempChromosome[i];
                }

                // selectedValues determines how many chromosomes will undergo crossover
                int selectedvalues = (int)((rate / 100f) * length);
                if ((selectedvalues % 2) == 1)
                {
                    selectedvalues--;
                }

                // Only first selectedValues number of chromsomes undergo crossover
                int chromosomeLength = Utility.CountMaxBits(DataStorage.UpperBound);
                for (int i = 0; i < selectedvalues; i += 2)
                {
                    if (DataStorage.crossoverType == DataStorage.crossoverSchemes[0])
                    {
                        crossPoint[gen, i] = crossPoint[gen, i + 1] = SinglePointCrossoverFunctionMaximize(new string[] { inputchromosome[gen, i], inputchromosome[gen, i + 1] }, out outputchromosome[gen, i], out outputchromosome[gen, i + 1], chromosomeLength);
                    }
                    else
                    {
                        crossPoint[gen, i] = crossPoint[gen, i + 1] = TwoPointCrossoverFunctionMaximize(new string[] { inputchromosome[gen, i], inputchromosome[gen, i + 1] }, out outputchromosome[gen, i], out outputchromosome[gen, i + 1], chromosomeLength);
                    }
                }

                // For non-crossovered chromosomes, their crossoverPoint is nill
                for (int i = selectedvalues; i < DataStorage.initialPopCount; i++)
                {
                    crossPoint[gen, i] = "Nil";
                }
            }
        }
Beispiel #3
0
        public static int[,] DoMutation(int[,] initialChromosome, ref DataTable mainTable, ref int[] maxima)
        {
            offspringBefore    = new string[4, DataStorage.initialPopCount];
            mutationChromosome = new string[4, DataStorage.initialPopCount];
            offspringAfter     = new string[4, DataStorage.initialPopCount];
            xVal       = new int[4, DataStorage.initialPopCount];
            fitnessVal = new int[4, DataStorage.initialPopCount];

            for (int gen = 0; gen < 4; gen++)
            {
                for (int i = 0; i < DataStorage.initialPopCount; i++)
                {
                    mutationChromosome[gen, i] = string.Empty;
                    offspringBefore[gen, i]    = Utility.DecToBin(initialChromosome[gen, i]);
                }
            }

            PerformMutationrRate(DataStorage.mutationRate, ref offspringBefore, ref offspringAfter, ref mutationChromosome);

            for (int gen = 0; gen < 4; gen++)
            {
                for (int i = 0; i < DataStorage.initialPopCount; i++)
                {
                    xVal[gen, i]       = Utility.BinToDec(offspringAfter[gen, i]);
                    fitnessVal[gen, i] = FunctionParser.calculate(DataStorage.fitnessFunction, xVal[gen, i]);
                }
            }

            for (int gen = 0; gen < 4; gen++)
            {
                maxima[gen] = Utility.GetMax(xVal, gen);
            }

            // Updating mutation table
            UpdateTableValue(ref mainTable);

            return(xVal);
        }
Beispiel #4
0
        private static void PerformInitialCalulation(int[,] xVal, ref DataTable mainTable, ref DataTable additionalTable)
        {
            xValue        = new int[4, DataStorage.initialPopCount];
            fitnessVal    = new int[4, DataStorage.initialPopCount];
            prob          = new double[4, DataStorage.initialPopCount];
            probPercent   = new double[4, DataStorage.initialPopCount];
            expectedCount = new double[4, DataStorage.initialPopCount];
            actualCount   = new int[4, DataStorage.initialPopCount];

            additionalTableFitness       = new int[4, 3];
            additionalTableProb          = new double[4, 3];
            additionalTableProbPercent   = new double[4, 3];
            additionalTableExpectedCount = new double[4, 3];
            additionalTableActualCount   = new int[4, 3];

            for (int gen = 0; gen < 4; gen++)
            {
                for (int i = 0; i < DataStorage.initialPopCount; i++)
                {
                    fitnessVal[gen, i] = FunctionParser.calculate(DataStorage.fitnessFunction, xVal[gen, i]);
                    xValue[gen, i]     = xVal[gen, i];
                }
            }

            int[] sumFitness = new int[DataStorage.initialPopCount];
            int[] avgFitness = new int[DataStorage.initialPopCount];
            for (int gen = 0; gen < 4; gen++)
            {
                sumFitness[gen] = Utility.GetSum(fitnessVal, gen);
                avgFitness[gen] = Utility.GetAvg(fitnessVal, gen);
            }

            for (int gen = 0; gen < 4; gen++)
            {
                for (int i = 0; i < DataStorage.initialPopCount; i++)
                {
                    prob[gen, i]          = Math.Round((double)fitnessVal[gen, i] / sumFitness[gen], 2);
                    probPercent[gen, i]   = prob[gen, i] * 100;
                    expectedCount[gen, i] = Math.Round((double)fitnessVal[gen, i] / avgFitness[gen], 2);
                    actualCount[gen, i]   = (int)Math.Round(expectedCount[gen, i], 0);
                }
            }

            for (int gen = 0; gen < 4; gen++)
            {
                // Calculation for data in additional selection table
                additionalTableFitness[gen, 0]       = Utility.GetSum(fitnessVal, gen);
                additionalTableFitness[gen, 1]       = Utility.GetAvg(fitnessVal, gen);
                additionalTableFitness[gen, 2]       = Utility.GetMax(fitnessVal, gen);
                additionalTableProb[gen, 0]          = Utility.GetSum(prob, gen);
                additionalTableProb[gen, 1]          = Utility.GetAvg(prob, gen);
                additionalTableProb[gen, 2]          = Utility.GetMax(prob, gen);
                additionalTableProbPercent[gen, 0]   = Utility.GetSum(probPercent, gen);
                additionalTableProbPercent[gen, 1]   = Utility.GetAvg(probPercent, gen);
                additionalTableProbPercent[gen, 2]   = Utility.GetMax(probPercent, gen);
                additionalTableExpectedCount[gen, 0] = Utility.GetSum(expectedCount, gen);
                additionalTableExpectedCount[gen, 1] = Utility.GetAvg(expectedCount, gen);
                additionalTableExpectedCount[gen, 2] = Utility.GetMax(expectedCount, gen);
                additionalTableActualCount[gen, 0]   = Utility.GetSum(actualCount, gen);
                additionalTableActualCount[gen, 1]   = Utility.GetAvg(actualCount, gen);
                additionalTableActualCount[gen, 2]   = Utility.GetMax(actualCount, gen);
            }
        }