示例#1
0
 public void Dispose()
 {
     ImplicantBuffer.Dispose();
 }
示例#2
0
        private void CD_Search()
        {
            long     iteration = 0, ellapsedIterations = 0, lastChangeIteration = 0, implicantsCount = 0;
            DateTime startTime = DateTime.Now;

            while (true)
            {
                if (ellapsedIterations == long.MaxValue || iteration == long.MaxValue || (DateTime.Now - startTime).TotalSeconds > secondsForCDSearch)
                {
                    break;
                }
                iteration++; ellapsedIterations++;
                CD_SearchOnce();
                if (ImplicantBuffer.Count != implicantsCount)
                {
                    lastChangeIteration = iteration;
                    ellapsedIterations  = 0;
                    implicantsCount     = ImplicantBuffer.Count;
                    iteration           = 0;
                }
                if (ellapsedIterations / 3 > lastChangeIteration)
                {
                    break;
                }
            }
            void CD_SearchOnce()
            {
                long[,] varFrequences = new long[2, Length];
                string t = new string(Enumerable.Repeat('-', Length).ToArray()); ///текущая импликанта

                List <string> trueReduced = True.ToList();                       ///сокращённый истинный набор

                do
                {
                    t = new string(Enumerable.Repeat('-', Length).ToArray());
                    do
                    {
                        if (!t.Contains('-'))
                        {
                            t = new string(Enumerable.Repeat('-', Length).ToArray());
                        }
                        t = MostFrequenceVariable();
                    } while (IntersectFalse(t));
                    ReduceTrue(trueReduced, t);
                    if (ImplicantBuffer.Add(t))
                    {
                        ExpansionBuffer.Add(t);
                    }
                } while (trueReduced.Count > 0);
                //todo вернуть H (сгенерированные импликанты)

                string MergeTerms(string first, string second)
                {
                    if (first.Length != second.Length)
                    {
                        throw new ArgumentException("Строки разной длины.");
                    }
                    StringBuilder sb = new StringBuilder("");

                    for (int i = 0; i < first.Length; i++)
                    {
                        if (first[i] == '-' && second[i] == '-')
                        {
                            sb.Append('-');
                        }
                        else if ((first[i] == '0' && second[i] == '-') || (first[i] == '-' && second[i] == '0') || (first[i] == '0' && second[i] == '0'))
                        {
                            sb.Append('0');
                        }
                        else if ((first[i] == '1' && second[i] == '-') || (first[i] == '-' && second[i] == '1') || (first[i] == '1' && second[i] == '1'))
                        {
                            sb.Append('1');
                        }
                        else
                        {
                            throw new ArgumentException("Входные строки не соответствуют требованию");
                        }
                    }
                    return(sb.ToString());
                }

                string MostFrequenceVariable()
                {
                    long max      = -1;
                    int  countMax = 0;

                    for (int i = 0; i < 2; i++)
                    {
                        for (int k = 0; k < Length; k++)
                        {
                            varFrequences[i, k] = 0;
                        }
                    }
                    ///Составление массива частоты вхождений переменных
                    for (int i = 0; i < Length; i++)
                    {
                        for (int k = 0; k < trueReduced.Count; k++)
                        {
                            if (t[i] == '-')
                            {
                                if (trueReduced[k][i] == '0')
                                {
                                    varFrequences[0, i]++;
                                    if (varFrequences[0, i] > max)
                                    {
                                        max      = varFrequences[0, i];
                                        countMax = 1;
                                    }
                                    else if (varFrequences[0, i] == max)
                                    {
                                        countMax++;
                                    }
                                }
                                else if (trueReduced[k][i] == '1')
                                {
                                    varFrequences[1, i]++;
                                    if (varFrequences[1, i] > max)
                                    {
                                        max      = varFrequences[1, i];
                                        countMax = 1;
                                    }
                                    else if (varFrequences[1, i] == max)
                                    {
                                        countMax++;
                                    }
                                }
                            }
                        }
                    }

                    ///обработка массива частоты вхождений переменных
                    List <string> mostFrequencesVariables = new List <string>(countMax); //Набор импликант
                    StringBuilder sb = new StringBuilder(new string(Enumerable.Repeat('-', Length).ToArray()));
                    int           countImplicants = 0;
                    bool          mutation        = (rand.Next(0, 100) > 96);

                    if (mutation)
                    {
                        while (true)
                        {
                            int start = rand.Next(Length);
                            if (varFrequences[1, start] > 0)
                            {
                                sb[start] = '1';
                                string implicant = MergeTerms(t, sb.ToString());
                                if (!IntersectFalse(implicant))
                                {
                                    countImplicants++;
                                }
                                mostFrequencesVariables.Add(implicant);
                                sb[start] = '-';
                                break;
                            }
                            else if (varFrequences[0, start] > 0)
                            {
                                sb[start] = '0';
                                string implicant = MergeTerms(t, sb.ToString());
                                if (!IntersectFalse(implicant))
                                {
                                    countImplicants++;
                                }
                                mostFrequencesVariables.Add(implicant);
                                sb[start] = '-';
                                break;
                            }
                            start = (start + 1) % Length;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            for (int k = 0; k < Length; k++)
                            {
                                if (varFrequences[i, k] == max)
                                {
                                    sb[k] = i.ToString()[0];
                                    string implicant = MergeTerms(t, sb.ToString());
                                    if (!IntersectFalse(implicant))
                                    {
                                        countImplicants++;
                                    }
                                    mostFrequencesVariables.Add(implicant);
                                    sb[k] = '-';
                                }
                            }
                        }
                    }

                    if (countImplicants > 0)
                    {
                        for (int i = 0; i < mostFrequencesVariables.Count; i++)
                        {
                            if (IntersectFalse(mostFrequencesVariables[i]))
                            {
                                mostFrequencesVariables.RemoveAt(i--);
                            }
                        }
                    }
                    return(mostFrequencesVariables[rand.Next(mostFrequencesVariables.Count)]);
                }
            }
        }