Exemplo n.º 1
0
        public static bool TestEqualWithWords(IAcceptWord A1, IAcceptWord A2, int initialCount = 100) {
            int onceTrue = 0, onceFalse = 0;
            int passLevel = System.Math.Min(initialCount / 10, 5);

            string[] words = System.Array.Empty<string>();
            int count = 0;

            if (A1 is CFGrammer cfg1) A1 = cfg1.ToChomskyNF();
            if (A2 is CFGrammer cfg2) A2 = cfg2.ToChomskyNF();

            while ((onceTrue < passLevel | onceFalse < passLevel) && count < initialCount * 2) {
                words = A1.GetRandomWords(initialCount / 2, 1, Serpen.Uni.Utils.Sqrt(initialCount), words);
                foreach (string w in words) {
                    try {
                        var erg1 = A1.AcceptWord(w);
                        var erg2 = A2.AcceptWord(w);

                        if (erg1) onceTrue++;
                        else onceFalse++;

                        if (erg1 != erg2)
                            throw new Automat.Exception($"{count}. word '{w}' divides Automates", A1, A2);

                        Utils.DebugMessage($"{count}. word '{w}' passes {erg1}", Uni.Utils.eDebugLogLevel.Verbose, A1, A2);
                        count++;
                    } catch (TuringCycleException) {
                    } catch (PDAStackException) { }
                }
            }
            if (onceTrue >= passLevel && onceFalse >= passLevel) {
                Utils.DebugMessage($"{count} words passed ({onceTrue}/{onceFalse})", Uni.Utils.eDebugLogLevel.Verbose, A1, A2);
                return true;
            } else {
                if (A1.Equals(A2)) {
                    Utils.DebugMessage($"{count} words passed, but not both tested ({onceTrue}/{onceFalse}), but Equals works", Uni.Utils.eDebugLogLevel.Verbose, A1, A2);
                    return true;
                } else {
                    Utils.DebugMessage($"{count} words passed, but not both tested ({onceTrue}/{onceFalse}), Equals not working", Uni.Utils.eDebugLogLevel.Normal, A1, A2);
                    return true;
                }

            }
        }
Exemplo n.º 2
0
        public static bool InMyhillNerodeRelation(string w1, string w2, IAcceptWord automat, int count = 50) {
            var words = automat.GetRandomWords(count, 0, Serpen.Uni.Utils.Sqrt(count), System.Array.Empty<string>());

            foreach (string w in words) {
                bool? erg1 = null, erg2 = null;

                try {
                    erg1 = automat.AcceptWord(w1 + w);
                } catch (Turing.TuringCycleException) {
                } catch (ContextFree.PDAStackException) { }
                try {
                    erg2 = automat.AcceptWord(w2 + w);
                } catch (Turing.TuringCycleException) {
                } catch (ContextFree.PDAStackException) { }

                if (erg1 != erg2) {
                    Utils.DebugMessage($"word {w} divides {w1},{w2}", automat, Uni.Utils.eDebugLogLevel.Verbose);
                    return false;
                }
            }
            return true;
        }