예제 #1
0
 private double CalculateFitness(NGramFrequencies nGramFrequencies, string encryptedText, string chromosone)
 {
     double currentFitness = 0;
     string decryptedText = SubstitutionCipher.DecryptText(encryptedText, chromosone);
     MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(decryptedText));
     NGramFrequencies decryptedTextFrequencies = NGramFrequencies.Analyse(new StreamReader(stream), nGramFrequencies.Length);
     foreach (string ngram in decryptedTextFrequencies)
     {
         double trainedFrequency = nGramFrequencies.FrequencyOf(ngram);
         if (trainedFrequency != 0)
         {
             currentFitness += decryptedTextFrequencies.FrequencyOf(ngram) * Math.Log(trainedFrequency, FITNESS_LOGARITHM_BASE);
         }
     }
     return currentFitness;
 }
 public Environment(string trainingFilePath, string plainText, string encryptedText,
     int numberOfGenerations, int populationSize,
     double percentageOfElitism, int tournamentSize,
     double tournamentWinnerProbability, double probabilityOfCrossover,
     int numberOfCrossoverPoints, double probabilityOfMutation)
 {
     this.nGramFrequencies = NGramFrequencies.Analyse(new StreamReader(trainingFilePath), NGRAM_LENGTH);
     this.trainedLetterFrequencies = LetterFrequencies.Analyse(new StreamReader(trainingFilePath));
     this.cipherLetterFrequencies = LetterFrequencies.Analyse(new StreamReader(new MemoryStream(ASCIIEncoding.Default.GetBytes(encryptedText))));
     this.randomNumberGenerator = new Random(1337);
     this.decryptedText = plainText.Trim().ToUpper();
     this.encryptedText = encryptedText.Trim().ToUpper();
     this.numberOfGenerations = numberOfGenerations;
     this.populationSize = populationSize;
     this.percentageOfElitism = percentageOfElitism;
     this.tournamentSize = tournamentSize;
     this.tournamentWinnerProbability = tournamentWinnerProbability;
     this.probabilityOfCrossover = probabilityOfCrossover;
     this.numberOfCrossoverPoints = numberOfCrossoverPoints;
     this.probabilityOfMutation = probabilityOfMutation;
     this.population = new List<Individual>();
     this.currentGeneration = 0;
     CreateInitialPopulation();
 }
예제 #3
0
 public Individual(NGramFrequencies nGramFrequencies, string encryptedText, string chromosone)
 {
     this.chromosone = chromosone;
     this.fitness = CalculateFitness(nGramFrequencies, encryptedText, chromosone);
 }