コード例 #1
0
ファイル: Minimizer.cs プロジェクト: KamushekDev/BOOM
 private void ImplicantExpansion()
 {//Последовательное расширение
     for (int i = 0; i < ExpansionBuffer.Count; i++)
     {
         StringBuilder sb                = new StringBuilder(ExpansionBuffer[i]);
         int           start             = rand.Next(Length);
         int           EllapsedChangings = 0;
         int           position          = start;
         do
         {
             if (sb[position] != '-')
             {
                 char buffer = sb[position];
                 sb[position] = '-';
                 if (IntersectFalse(sb.ToString()))
                 {
                     sb[position] = buffer;
                 }
                 else
                 {
                     EllapsedChangings++;
                     ExpansionBuffer[i] = sb.ToString();
                 }
             }
             position = (position + 1) % Length;
             if (position == (start - 1 + Length) % Length)
             {
                 if (EllapsedChangings == 0)
                 {
                     break;
                 }
                 else
                 {
                     EllapsedChangings = 0;
                 }
             }
         } while (true);
     }
     ExpansionBuffer = ExpansionBuffer.Distinct().ToList();
 }
コード例 #2
0
        public IEnumerator <VsExpansion> GetEnumerator()
        {
            IVsExpansionManager expansionManager;

            ErrorHandler.ThrowOnFailure(_textManager.GetExpansionManager(out expansionManager));

            IVsExpansionEnumeration enumerator;
            int onlyShortcut = (this.ShortcutOnly ? 1 : 0);

            ErrorHandler.ThrowOnFailure(expansionManager.EnumerateExpansions(_languageGuid, onlyShortcut, null, 0, 0, 0, out enumerator));

            ExpansionBuffer buffer = new ExpansionBuffer();
            GCHandle        handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try {
                int  hr = VSConstants.S_OK;
                uint fetched;
                while (VSConstants.S_OK == (hr = enumerator.Next(1, new IntPtr[] { handle.AddrOfPinnedObject() }, out fetched)))
                {
                    buffer = (ExpansionBuffer)handle.Target;
                    try {
                        handle.Free();
                        if (IntPtr.Zero != buffer.shortcutPtr)
                        {
                            VsExpansion expansion = new VsExpansion();
                            expansion.shortcut = Marshal.PtrToStringBSTR(buffer.shortcutPtr);
                            if (IntPtr.Zero != buffer.descriptionPtr)
                            {
                                expansion.description = Marshal.PtrToStringBSTR(buffer.descriptionPtr);
                            }
                            if (IntPtr.Zero != buffer.pathPtr)
                            {
                                expansion.path = Marshal.PtrToStringBSTR(buffer.pathPtr);
                            }
                            if (IntPtr.Zero != buffer.titlePtr)
                            {
                                expansion.title = Marshal.PtrToStringBSTR(buffer.titlePtr);
                            }
                            yield return(expansion);

                            handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                        }
                    } finally {
                        if (IntPtr.Zero != buffer.descriptionPtr)
                        {
                            Marshal.FreeBSTR(buffer.descriptionPtr);
                            buffer.descriptionPtr = IntPtr.Zero;
                        }
                        if (IntPtr.Zero != buffer.pathPtr)
                        {
                            Marshal.FreeBSTR(buffer.pathPtr);
                            buffer.pathPtr = IntPtr.Zero;
                        }
                        if (IntPtr.Zero != buffer.shortcutPtr)
                        {
                            Marshal.FreeBSTR(buffer.shortcutPtr);
                            buffer.shortcutPtr = IntPtr.Zero;
                        }
                        if (IntPtr.Zero != buffer.titlePtr)
                        {
                            Marshal.FreeBSTR(buffer.titlePtr);
                            buffer.titlePtr = IntPtr.Zero;
                        }
                    }
                }
                ErrorHandler.ThrowOnFailure(hr);
            } finally {
                if (handle.IsAllocated)
                {
                    handle.Free();
                }
            }
        }
コード例 #3
0
ファイル: Minimizer.cs プロジェクト: KamushekDev/BOOM
        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)]);
                }
            }
        }