Ejemplo n.º 1
0
        public void Run()
        {
            Boolean              terminate = false;
            DateTime             Begin     = DateTime.Now;
            ICipher              Cipher;
            IPopulationGenerator popGen;
            IFitness             fit;
            ISelection           selection;
            IGenetics            genetics;



            switch (this.cipher)
            {
            case 0:
                ChromosomeLength = 26;
                popGen           = new SubPopGen(PopulationSize, ChromosomeLength);
                Cipher           = new SubstitutionCipher();
                break;

            case 1:
                ChromosomeLength = 54;
                popGen           = new ZodiacPopGen(PopulationSize, ChromosomeLength);
                Cipher           = new Z408Cipher();
                break;

            case 2:
                ChromosomeLength = 63;
                popGen           = new ZodiacPopGen(PopulationSize, ChromosomeLength);
                Cipher           = new Z340Cipher();
                break;

            default:
                popGen = null;
                Cipher = null;
                break;
            }

            Population = popGen.GeneratePopulation();
            TimeTermination terminator = new TimeTermination(this.timeLimit);

            switch (this.fitness)
            {
            case 0:
                fit = new NGramFitness(Population, Cipher);
                break;

            case 1:
                fit = new BiGramFitness(Population, Cipher);
                break;

            case 2:
                fit = new TriGramFitness(Population, Cipher);
                break;

            case 3:
                fit = new ReducedFitness(Population, Cipher);
                break;

            default:
                fit = null;
                break;
            }

            while (terminate == false)
            {
                Population = fit.calcFitness();

                switch (this.selection)
                {
                case 0:
                    selection = new RouletteSelection(Population);
                    break;

                case 1:
                    selection = new StochasticSelection(Population);
                    break;

                case 2:
                    selection = new TournamentSelection(Population);
                    break;

                default:
                    selection = null;
                    break;
                }
                Intermediate = selection.selectAlleles();

                switch (this.cipher)
                {
                case 0:
                    genetics = new SubGenetics(0.6, 0.1, this.crossover, this.mutation);
                    break;

                case 1:
                    genetics = new ZodiacGenetics(0.6, 0.1, this.mutation, this.crossover);
                    break;

                case 2:
                    genetics = new ZodiacGenetics(0.6, 0.1, this.mutation, this.crossover);
                    break;

                default:
                    genetics = null;
                    break;
                }

                genetics.reproduce(Intermediate);

                terminate = terminator.doesTerminate(Begin);

                IChromosome generationMax = findMaxFitness();
                Output(generationMax.ToString());
                double mean = findMeanFitness();
                Output(mean.ToString());
            }

            IChromosome best      = findMaxFitness();
            String      plaintext = generateCipherText(best, Cipher);
            string      output    = best.ToString();

            Output(output);
            Output(plaintext);
        }
        public void Run()
        {
            Boolean terminate = false;
            DateTime Begin = DateTime.Now;
            ICipher Cipher;
            IPopulationGenerator popGen;
            IFitness fit;
            ISelection selection;
            IGenetics genetics;

            switch (this.cipher)
            {
                case 0:
                    ChromosomeLength = 26;
                    popGen = new SubPopGen(PopulationSize, ChromosomeLength);
                    Cipher = new SubstitutionCipher();
                    break;
                case 1:
                    ChromosomeLength = 54;
                    popGen = new ZodiacPopGen(PopulationSize, ChromosomeLength);
                    Cipher = new Z408Cipher();
                    break;
                case 2:
                    ChromosomeLength = 63;
                    popGen = new ZodiacPopGen(PopulationSize, ChromosomeLength);
                    Cipher = new Z340Cipher();
                    break;
                default:
                    popGen = null;
                    Cipher = null;
                    break;
            }

            Population = popGen.GeneratePopulation();
            TimeTermination terminator = new TimeTermination(this.timeLimit);

            switch (this.fitness)
            {
                case 0:
                    fit = new NGramFitness(Population, Cipher);
                    break;
                case 1:
                    fit = new BiGramFitness(Population, Cipher);
                    break;
                case 2:
                    fit = new TriGramFitness(Population, Cipher);
                    break;
                case 3:
                    fit = new ReducedFitness(Population, Cipher);
                    break;
                default:
                    fit = null;
                    break;
            }

            while (terminate == false)
            {

                Population = fit.calcFitness();

                switch (this.selection)
                {
                    case 0:
                        selection = new RouletteSelection(Population);
                        break;
                    case 1:
                        selection = new StochasticSelection(Population);
                        break;
                    case 2:
                        selection = new TournamentSelection(Population);
                        break;
                    default:
                        selection = null;
                        break;
                }
                Intermediate = selection.selectAlleles();

                switch (this.cipher)
                {
                    case 0:
                        genetics = new SubGenetics(0.6, 0.1, this.crossover, this.mutation);
                        break;
                    case 1:
                        genetics = new ZodiacGenetics(0.6, 0.1, this.mutation, this.crossover);
                        break;
                    case 2:
                        genetics = new ZodiacGenetics(0.6, 0.1, this.mutation, this.crossover);
                        break;
                    default:
                        genetics = null;
                        break;
                }

                genetics.reproduce(Intermediate);

                terminate = terminator.doesTerminate(Begin);

                IChromosome generationMax = findMaxFitness();
                Output(generationMax.ToString());
                double mean = findMeanFitness();
                Output(mean.ToString());
            }

            IChromosome best = findMaxFitness();
            String plaintext = generateCipherText(best, Cipher);
            string output = best.ToString();

            Output(output);
            Output(plaintext);
        }