Esempio n. 1
0
        public List <PossibleKey> Breed(PossibleKey key1, PossibleKey key2)
        {
            var childrens = new List <PossibleKey>();
            var probOfCrossover = rand.NextDouble();
            var keyLength = key1.Key.Length;
            var childKey1 = new char[keyLength]; var childKey2 = new char[keyLength];

            // Crossover
            for (int i = 0; i < keyLength; i++)
            {
                childKey1[i] = key1.Key[i];
                childKey2[i] = key2.Key[i];
            }
            //if (probOfCrossover < ProbabilityOfCrossover)
            //{
            //    int randIndex = rand.Next(1, keyLength);
            //    for (int i = 0; i < randIndex; i++)
            //    {
            //        childKey1[i] = key1.Key[i];
            //        childKey2[i] = key2.Key[i];
            //    }
            //    for (int j = randIndex; j < keyLength; j++)
            //    {
            //        childKey1[j] = key2.Key[j];
            //        childKey2[j] = key1.Key[j];
            //    }

            //    var key1String = new String(childKey1);
            //    var key2String = new String(childKey2);
            //    var dupCharsKey1 = key1String.GroupBy(x => x)
            //                                .Where(x => x.Count() > 1)
            //                                .Select(x => x.Key);
            //    var dupCharsKey2 = key2String.GroupBy(x => x)
            //                                .Where(x => x.Count() > 1)
            //                                .Select(x => x.Key);
            //    var notInChild1 = ALPHABET.Except(childKey1).ToList();
            //    int cnt = 0;
            //    foreach (var dupChar in dupCharsKey1)
            //    {
            //        childKey1[key1String.IndexOf(dupChar)] = notInChild1[cnt];
            //        cnt++;
            //    }

            //    var notInChild2 = ALPHABET.Except(childKey2).ToList();
            //    cnt = 0;
            //    foreach (var dupChar in dupCharsKey2)
            //    {
            //        childKey2[key2String.IndexOf(dupChar)] = notInChild2[cnt];
            //        cnt++;
            //    }


            //    //var randPositions = new List<int>();
            //    //for(int i = 0; i < CrossoverPoints; i++)
            //    //{
            //    //    int index = -1;
            //    //    do
            //    //    {
            //    //        index = rand.Next(0, keyLength);
            //    //    }
            //    //    while (randPositions.Contains(index));
            //    //    randPositions.Add(index);
            //    //}

            //    //List<char> remainingCharacters1 = new List<char>(key1.Key.ToCharArray());
            //    //List<char> remainingCharacters2 = new List<char>(key2.Key.ToCharArray());

            //    //foreach (int point in randPositions)
            //    //{
            //    //    char c1 = key1.Key[point];
            //    //    char c2 = key2.Key[point];
            //    //    childKey1[point] = c1;
            //    //    childKey2[point] = c2;
            //    //    remainingCharacters1.Remove(c1);
            //    //    remainingCharacters2.Remove(c2);
            //    //}
            //    //int j = 0;
            //    //for (int i = 0; i < keyLength; i++)
            //    //{
            //    //    if (!randPositions.Contains(i))
            //    //    {
            //    //        childKey1[i] = remainingCharacters1[j];
            //    //        childKey2[i] = remainingCharacters2[j];
            //    //        j++;
            //    //    }
            //    //}
            //}
            //else
            //{
            //    for (int i = 0; i < keyLength; i++)
            //    {
            //        childKey1[i] = key1.Key[i];
            //        childKey2[i] = key2.Key[i];
            //    }
            //}

            // Mutation
            var probOfKey1Mutate = rand.NextDouble();
            var probOfKey2Mutate = rand.NextDouble();

            if (probOfKey1Mutate < ProbabilityOfMutation)
            {
                int oldPosition = rand.Next(0, keyLength);
                int newPosition = -1;
                do
                {
                    newPosition = rand.Next(0, keyLength);
                } while (oldPosition == newPosition);
                char oldChar = childKey1[oldPosition];
                childKey1[oldPosition] = childKey1[newPosition];
                childKey1[newPosition] = oldChar;
            }

            if (probOfKey2Mutate < ProbabilityOfMutation)
            {
                int oldPosition = rand.Next(0, keyLength);
                int newPosition = -1;
                do
                {
                    newPosition = rand.Next(0, keyLength);
                } while (oldPosition == newPosition);
                char oldChar = childKey2[oldPosition];
                childKey2[oldPosition] = childKey2[newPosition];
                childKey2[newPosition] = oldChar;
            }

            childrens.Add(new PossibleKey(new String(childKey1), EncryptedText));
            childrens.Add(new PossibleKey(new String(childKey2), EncryptedText));

            return(childrens);
        }
Esempio n. 2
0
 private int ComparatorForSorting(PossibleKey key1, PossibleKey key2)
 {
     return(-key1.Fitness.CompareTo(key2.Fitness));
 }