Beispiel #1
0
        public TuringMachineSingleBand(string name, uint stateCount, char[] inputAlphabet, char[] bandAlphabet, TuringTransformSingleBand transform, uint startState, char blankSymbol, params uint[] acceptedStates)
            : base(name, stateCount, inputAlphabet, bandAlphabet, startState, blankSymbol, acceptedStates)
        {
            Transforms = transform;

            CheckConstraints();
        }
Beispiel #2
0
        public static TuringMachineSingleBand GenerateRandom()
        {
            const byte MAX_STATES = 20;
            const byte MAX_CHAR   = 7;

            var rnd = Uni.Utils.RND;

            int stateCount = rnd.Next(1, MAX_STATES);

            char[] inputAlphabet = RandomGenerator.RandomAlphabet(1, MAX_CHAR);
            char[] bandAlphabet  = RandomGenerator.RandomAlphabet(1, MAX_CHAR, inputAlphabet.Append(BLANK));
            uint[] accState      = RandomGenerator.RandomAcceptedStates(1, stateCount / 3, stateCount);

            var t = new TuringTransformSingleBand();

            for (uint i = 0; i < stateCount; i++)
            {
                int transformsRnd = rnd.Next(0, stateCount);
                for (uint j = 0; j < transformsRnd; j++)
                {
                    var tk = new TuringKey(i, bandAlphabet.RndElement());
                    var tv = new TuringVal(j, bandAlphabet.RndElement(), TMDirection.Right);
                    t.TryAdd(tk, tv);
                }
            }

            var ret = new TuringMachineSingleBand("TM1_Random", (uint)stateCount, inputAlphabet, bandAlphabet, t, (uint)rnd.Next(0, stateCount), BLANK, accState);

            ret.Name = $"TM1_Random_{ret.GetHashCode()}";
            return(ret);
        }
 public TuringMachineSingleBand1659(string name, string[] states, char[] inputAlphabet, char[] bandAlphabet, TuringTransformSingleBand transform, uint startState, char blankSymbol, uint acceptedState, uint discardState)
     : base(name, states, inputAlphabet, bandAlphabet, startState, blankSymbol, new uint[] { acceptedState })
 {
     Transforms   = transform;
     DiscardState = discardState;
     CheckConstraints();
 }
 public void Insert(TuringTransformSingleBand transform, uint index)
 {
     foreach (var ti in transform)
     {
         var tk = new TuringKey(ti.Key.q + index, ti.Key.c);
         var tv = new TuringVal(ti.Value.qNext + index, ti.Value.c2, ti.Value.Direction);
         Add(tk, tv);
     }
 }
Beispiel #5
0
        public override IAutomat PurgeStates()
        {
            (uint[] translate, string[] names, uint[] aStates) = base.RemovedStateTranslateTables();

            var newT = new TuringTransformSingleBand();

            foreach (var t2 in Transforms)
            {
                if (translate.Contains(t2.Key.q))
                {
                    if (translate.Contains(t2.Value.qNext))
                    {
                        var tk = new TuringKey(translate.ArrayIndex(t2.Key.q), t2.Key.c);
                        var tv = new TuringVal(translate.ArrayIndex(t2.Value.qNext), t2.Value.c2, t2.Value.Direction);
                        newT.Add(tk, tv);
                    }
                }
            }

            return(new TuringMachineSingleBand($"{Name}_purged", (uint)names.Length, Alphabet, BandAlphabet, newT, translate.ArrayIndex(StartState), BlankSymbol, aStates)
            {
                DefaultAcceptance = this.DefaultAcceptance
            });
        }
Beispiel #6
0
        public static TuringMachineSingleBand FromBinString(string binString)
        {
            var transform = new TuringTransformSingleBand();

            const char S = '1'; // Separator
            const char C = '0'; // Counter

            const char SC = '0';

            int curPos = 0;

            char cmax = SC;
            uint qmax = 0;


            while (curPos < binString.Length)
            {
                uint q = 0;
                char c = SC;

                while (binString[curPos] == C)
                {
                    q++;
                    curPos++;
                }
                if (binString[curPos] != S)
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos++;
                }

                while (binString[curPos] == C)
                {
                    c++;
                    curPos++;
                }
                if (binString[curPos] != S)
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos++;
                }

                if (cmax < c)
                {
                    cmax = c;
                }
                if (qmax < q)
                {
                    qmax = q;
                }
                var tmkey = new TuringKey(q - 1, --c);

                uint qnext = 0;
                char c2    = SC;
                uint dir   = 0;

                while (binString[curPos] == C)
                {
                    qnext++;
                    curPos++;
                }
                if (binString[curPos] != S)
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos++;
                }

                while (binString[curPos] == C)
                {
                    c2++;
                    curPos++;
                }
                if (binString[curPos] != S)
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos++;
                }

                while (curPos < binString.Length && binString[curPos] == C)
                {
                    dir++;
                    curPos++;
                }

                if (curPos < binString.Length && binString.Substring(curPos, 2) != new string(S, 2))
                {
                    throw new Serpen.Uni.Automat.Exception("Format", null);
                }
                else
                {
                    curPos += 2;
                }

                if (cmax < c2)
                {
                    cmax = c2;
                }
                if (qmax < qnext)
                {
                    qmax = qnext;
                }
                var tmval = new TuringVal(qnext - 1, --c2, (TMDirection)(++dir));

                transform.Add(tmkey, tmval);
            }

            var alp = new char[cmax - SC];

            for (int i = 0; i < alp.Length; i++)
            {
                alp[i] = (char)(SC + i);
            }

            return(new TuringMachineSingleBand("uTM", qmax, alp.SkipLast(1).ToArray(), alp, transform, 0, alp[^ 1], 1));