Пример #1
0
        private void Mutation(string key)
        {
            Random rand = new Random(i++);
            int    a    = rand.Next(32);
            int    b    = rand.Next(33);

            StringBuilder newKeyBuilder = new StringBuilder(key);
            char          temp          = newKeyBuilder[a];

            newKeyBuilder[a] = newKeyBuilder[b];
            newKeyBuilder[b] = temp;
            string newKey = newKeyBuilder.ToString();

            if (!AllKeys.ContainsKey(newKey))
            {
                AllKeys.Add(newKey, 0.0);
            }
        }
Пример #2
0
        private void Crossover(string parent1, string parent2)
        {
            Random rand = new Random(i++);

            int a = rand.Next(32);
            int b = rand.Next(a, 34);

            char[] child1 = parent1.ToCharArray();
            char[] child2 = parent2.ToCharArray();

            Dictionary <char, char> accordance1 = new Dictionary <char, char>();
            Dictionary <char, char> accordance2 = new Dictionary <char, char>();

            for (int i = a; i < b; i++)
            {
                child1[i] = parent2[i];
                child2[i] = parent1[i];
                accordance1.Add(parent1[i], parent2[i]);
                accordance2.Add(parent2[i], parent1[i]);
            }

            for (int i = 0; i < a; i++)
            {
                if (accordance1.ContainsKey(child1[i]))
                {
                    child1[i] = accordance1[child1[i]];
                }
                else if (accordance2.ContainsKey(child1[i]))
                {
                    child1[i] = accordance2[child1[i]];
                }

                if (accordance1.ContainsKey(child2[i]))
                {
                    child2[i] = accordance1[child2[i]];
                }
                else if (accordance2.ContainsKey(child2[i]))
                {
                    child2[i] = accordance2[child2[i]];
                }
            }

            for (int i = b; i < 33; i++)
            {
                if (accordance1.ContainsKey(child1[i]))
                {
                    child1[i] = accordance1[child1[i]];
                }
                else if (accordance2.ContainsKey(child1[i]))
                {
                    child1[i] = accordance2[child1[i]];
                }

                if (accordance1.ContainsKey(child2[i]))
                {
                    child2[i] = accordance1[child2[i]];
                }
                else if (accordance2.ContainsKey(child2[i]))
                {
                    child2[i] = accordance2[child2[i]];
                }
            }

            string child1str = new string(child1);
            string child2str = new string(child2);

            if (CheckKey(child1str))
            {
                if (!AllKeys.ContainsKey(child1str))
                {
                    AllKeys.Add(child1str, 0.0);
                }

                if (!AllKeys.ContainsKey(child2str))
                {
                    AllKeys.Add(child2str, 0.0);
                }
            }
        }