예제 #1
0
        private void GenerateSequence(ref int x0, ref int a, ref int b, ref int c) // returns also the Optmal PSNR value
        {
            // Defining genes Domains
            BoundPair[] parameterDomain = new BoundPair[4];

            //Domain of x0
            parameterDomain[0] = new BoundPair(0, imageWidth * imageHeight);
            //Domain of a
            parameterDomain[1] = new BoundPair(1, imageWidth * imageHeight);
            //Domain of b
            parameterDomain[2] = new BoundPair(1, imageWidth * imageHeight);
            //Domain of c
            parameterDomain[3] = new BoundPair(1, imageWidth * imageHeight);

            GeneticAlgorithm GA = new GeneticAlgorithm(
                30,                                            // Population Size
                4,                                             // Chromosome Length
                100,                                           // Number of Generations
                0.85,                                          // Crossover Ratio
                0.25,                                          // Mutation Ratio
                new GeneticAlgorithm.EvaluationDelegate(PSNR), // Fitness Function
                parameterDomain,                               // Domain of each Parameter
                1,                                             // Elitism Factor
                GeneticAlgorithm.SelectionMode.RouletteWheel   //Selection Method
                );

            ImageHiding.GA.Organism OptimalSequence = GA.Run(encryptWorker, encryptEvent); // Run the Genetic Algorithm

            //Get the Best Result
            x0 = OptimalSequence.chromosome[0];
            a  = OptimalSequence.chromosome[1];
            b  = OptimalSequence.chromosome[2];
            c  = OptimalSequence.chromosome[3];
            double trivial = PSNR(0, 1, 1, 1);
            double opt     = PSNR(OptimalSequence.chromosome);
            int    x       = 0;
        }
        private void crossover(ref Organism parent1, ref Organism parent2, out Organism child1, out Organism child2)
        {
            int pivot = GARandomGenerator.Next(0, chromosomeLength - 1);

            for (int i = pivot; i < chromosomeLength; i++)
            {
                int temp = parent1.chromosome[i];
                parent1.chromosome[i] = parent2.chromosome[i];
                parent2.chromosome[i] = temp;
            }
            child1 = parent1;
            child2 = parent2;
        }