private List <string> GetTapeLibrary()
        {
            List <string> lstRetVal = new List <string>();

            if (m_lstTapeLibrary == null)
            {
                TuringMachine tm = this;

                foreach (TransitionFunction tf in tm.TransitionFunctions)
                {
                    foreach (string sz in tf.DomainHeadValues)
                    {
                        if (!lstRetVal.Contains(sz))
                        {
                            lstRetVal.Add(sz);
                        }
                    }

                    foreach (string sz in tf.RangeHeadWrite)
                    {
                        if (!lstRetVal.Contains(sz))
                        {
                            lstRetVal.Add(sz);
                        }
                    }
                }
            }
            else
            {
                lstRetVal = m_lstTapeLibrary.ToList();
            }


            return(lstRetVal.Where(x => x != END_SYMBOL && x != RETURN_SYMBOL && x != NULL_SYMBOL).ToList());
        }
        private List <TransitionFunction> GetPossibleTFs(State state)
        {
            TuringMachine             tm        = TM;
            List <TransitionFunction> lstRetVal = new List <TransitionFunction>();

            foreach (TransitionFunction tf in tm.TransitionFunctions)
            {
                bool found      = true;
                int  subsLength = 0;
                foreach (string subscript in state.SubScripts)
                {
                    subsLength += subscript.Length;
                }
                found &= tf.DomainState.Actual == state.Actual.Substring(0, state.Actual.Length - subsLength);

                for (int i = 0; i < state.SubScripts.Count; i++)
                {
                    found &= SymMap[tf.DomainHeadValues[i]] == state.SubScripts[i];
                }

                if (found)
                {
                    lstRetVal.Add(tf);
                }
            }

            return(lstRetVal);
        }
 /// <summary>
 /// Iteration is the number of the virtual head.
 /// If the flattening algorithm is being used for the first time,
 /// the virtual heads should all be 1s (as in 1 level down).
 /// Second Time -> 2s
 /// etc.
 /// </summary>
 /// <param name="iter"></param>
 public Flattener(TuringMachine tf, int iteration = 1)
 {
     ITERATION = iteration;
     SymMap    = new SampleCollection(ITERATION);
     NUM_TAPES = tf.TransitionFunctions.First().DomainHeadValues.Count;
     TM        = tf;
 }
        private List <State> GetStates()
        {
            TuringMachine tm     = TM;
            List <State>  states = new List <State>();

            foreach (TransitionFunction tf in tm.TransitionFunctions)
            {
                bool add = true;

                foreach (State state in states)
                {
                    add &= state.Actual != tf.DomainState.Actual;
                }
                if (add)
                {
                    states.Add(tf.DomainState);
                }

                bool addR = true;
                foreach (State state in states)
                {
                    addR &= state.Actual != tf.RangeState.Actual;
                }
                if (addR)
                {
                    states.Add(tf.RangeState);
                }
            }
            return(states);
        }
예제 #5
0
        // Methods
        public TuringMachineThread(TuringMachine tm, UpdateUICallback updateUIFunction)
        {
            this.tm       = tm;
            this.UpdateUI = updateUIFunction;
            TryUpdateUI();
            Thread thread = new Thread(new ThreadStart(TMThread));

            thread.IsBackground = true;

            thread.Start();
        }
예제 #6
0
        static void Main(string[] args)
        {
            TuringMachine tm = new TuringMachine();

            tm.GetInput("Input.txt");

            string    doc = "";
            Flattener fl  = new Flattener(tm);

            fl.Flatten(ref doc);

            tm.FinalizeOutput(ref doc);

            File.WriteAllText("Output.txt", doc);
            Console.WriteLine("Done");
            Console.ReadLine();
        }
        /// <summary>
        /// Generally, it takes each tape, puts them on a single tape separated by #s.
        /// The head then sweeps right and determines what symbol each "Simulated Head"
        /// lies on. Once each Simulated Head is determined, the head then sweeps left
        /// again to perform the changes to each simulated head.
        ///
        /// Each simulated head represents a head on another tape prior to flattenning.
        ///
        /// THIS IS THE ALGORITHM FOR NON BIDIRECTIONAL TURING MACHINE FLATTENNER
        ///
        /// Details:
        /// Transition functions must be constructed for each process in the algorithm.
        /// First thing that needs to be done is the sweep right to determine the
        /// states of the "simulated heads". This is done so the flat machine can determine
        /// which transition function to simulate from the original k-tape machine.
        /// While sweeping right, the transition functions operate on "Undetermined States"
        ///
        /// TODO: EXAMPLE HERE
        ///
        /// Once all simulated heads are determined, the state of the flat machine can
        /// be uniquely assigned to a transition function from the machine prior to
        /// flattenning. The states with this ability are called the  "Determined States"
        /// The determined state transition functions sweep left until they reach a
        /// simulated head. Once a simulated head is reached, the transition functions
        /// move the simulated head, then perform the change to that "simulated tape"
        /// The head then continues sweeping left for each simulated head.
        /// The machine then goes back to undetermined states and the process repeats
        /// until an accept or reject state is reached.
        ///
        /// Summary:
        /// 1: Create Transition Functions for sweeping right - undetermined states
        ///     a: Transition functions for going from no known simulated heads to the first.
        ///        - Sweep_Right_XthParm_MoveToNext
        ///     b: Transition functions for transitioning to the correct state that knows the
        ///         location of each head on the previous simulated tapes
        ///        - Sweep_Right_XthParm_FoundNextChangeToNextState
        ///     c: Transition functions for going from first to second.
        ///        - Sweep_Right_XthParm_MoveToNext
        ///     d: Transition functions for going from second to third... etc.
        ///        - Sweep_Right_XthParm_MoveToNext
        ///     e: Transition functions for going from the last head to a determined state.
        ///        - Sweep_Right_Complete_BeginActionStates
        /// 2: Create Transition Functions for sweeping left - determined states
        ///     a: Transition functions for going left until the last simulated head.
        ///        - Sweep_Right_Complete_BeginActionStates
        ///     b: Transition functions for transtioning to states that perform changes on the simulated heads.
        ///         i: Transition functions for performing the change on the simulated tape.
        ///            CAVEAT: For each write operation that can possible write on a blank space "_"
        ///                     include a transition function that can also write on "#". This is done
        ///                     in ii.
        ///            - Sweep_Left_ActorState_N_FoundPreviousWriteThenChangeToMoveHeadState
        ///        ii: Transition functions for extending the simulated tape if a write was
        ///              performed on a blank space that was actually a tape end "#". See the
        ///              function for extending the tape.
        ///       iii: Transition functions for moving the simulated head and returning to the sweep left.
        ///            - Sweep_Left_ActorState_N_MoveHeadThenChangeToNLess1MoveToPrevious
        ///     c: Transition functions for moving the the previous (N less 1) simulated head.
        ///            - Sweep_Left_ActorState_N_MoveToPrevious
        ///     d: Transition functions for meving from the first simulated head to a completed state.
        ///            - Sweep_Left_Complete_ActorState_0F_ChangeToNext
        /// 3: Create Transition functions for transition from determined states to undetermined states
        ///     that then perform another sweep right OR check for completion.
        ///            - Sweep_Left_Complete_ActorState_0F_ChangeToNext
        ///
        ///
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="leftSafety"></param>
        public void Flatten(ref string doc, bool leftSafety = false)
        {
            #region Setup
            TuringMachine tm = TM;
            Push_Symbols(ref TM.TransitionFunctions);
            int           NUM_TAPES   = tm.TransitionFunctions.First().DomainHeadValues.Count;
            List <string> TapeLibrary = GetTapeLibrary();
            TapeLibrary.Add("#");

            List <string> NonHeaderLibrary = TapeLibrary.ToList();
            List <string> HeaderLibrary    = NonHeaderLibrary.Select(x => SymMap[x]).ToList();
            TapeLibrary = TapeLibrary.Concat(HeaderLibrary).ToList();
            HeaderLibrary.Add("B");


            // Get the mkStates.
            List <State> mkStates = GetStates();

            // Record the Final States
            IEnumerable <State> OutputStates = new List <State>();

            // Record the Final Transition Functions
            List <TransitionFunction> OutputTFs = new List <TransitionFunction>();
            List <TransitionFunction> UnbranchedFinalizedList = null;
            #endregion Setup

            #region Iterate of States Of Multitape Machine Mk
            foreach (State mkState in mkStates)
            {
                //Build the states... These are really just used for counting.
                IEnumerable <TransitionFunction> TransitionFunction_State_ = Get_Domain_States(mkState, tm.TransitionFunctions);

                IEnumerable <State> undeterminedStates = Construct_Undetermined_States(mkState, TransitionFunction_State_)
                                                         .Distinct();
                IList <DeterminedState> determinedStates = Construct_Determined_States(mkState, TransitionFunction_State_).ToList();

                IEnumerable <DeterminedState> determinedStates_ActorStates_N_Write =
                    Construct_Transition_States_Primary(determinedStates);

                IEnumerable <DeterminedState> determinedStates_ActorStates_N_MoveHead =
                    Construct_Transition_States_Secondary(determinedStates_ActorStates_N_Write);

                IEnumerable <DeterminedState> determinedStates_ActorStates_0F_Complete =
                    Construct_Transition_Complete_States(determinedStates);

                //Build the transition functions.
                IEnumerable <TransitionFunction> tmp = new List <TransitionFunction>();
                IEnumerable <TransitionFunction> undeterminedStates_TransitionFunctions_XthParm_MoveToNextI =
                    Sweep_Right_XthParm_MoveToNext(undeterminedStates, NonHeaderLibrary);
                tmp = tmp.Concat(undeterminedStates_TransitionFunctions_XthParm_MoveToNextI).ToList();

                IEnumerable <TransitionFunction> undeterminedStates_TransitionFunction_XthParm_FoundNextChangeToNextI =
                    Sweep_Right_XthParm_FoundNextChangeToNextState(undeterminedStates);
                tmp = tmp.Concat(undeterminedStates_TransitionFunction_XthParm_FoundNextChangeToNextI);

                IEnumerable <TransitionFunction> determinedStates_TransitionFunction_ChangeToActionStates =
                    Sweep_Right_Complete_BeginActionStates(determinedStates, NonHeaderLibrary);
                tmp = tmp.Concat(determinedStates_TransitionFunction_ChangeToActionStates);

                IEnumerable <TransitionFunction> determinedStates_TransitionFunction_ActorState_N_MoveToPreviousAndWrite =
                    Sweep_Left_ActorState_N_MoveToPrevious(
                        determinedStates_ActorStates_N_Write,
                        NonHeaderLibrary);
                tmp = tmp.Concat(determinedStates_TransitionFunction_ActorState_N_MoveToPreviousAndWrite);

                IEnumerable <TransitionFunction> determinedState_TransitionFunction_ActorState_N_FoundPreviousWriteThenChangeToMoveHeadState =
                    Sweep_Left_ActorState_N_FoundPreviousWriteThenChangeToMoveHeadState(
                        determinedStates);
                tmp = tmp.Concat(determinedState_TransitionFunction_ActorState_N_FoundPreviousWriteThenChangeToMoveHeadState);

                IEnumerable <TransitionFunction> determinedState_TransitionFunction_ActorState_N_MoveHeadThenChangeToNLess1MoveToPrevious =
                    Sweep_Left_ActorState_N_MoveHeadThenChangeToNLess1MoveToPrevious(
                        determinedStates,
                        NonHeaderLibrary);
                tmp = tmp.Concat(determinedState_TransitionFunction_ActorState_N_MoveHeadThenChangeToNLess1MoveToPrevious);

                IEnumerable <TransitionFunction> determinedState_TransitionFunction_ActorState_0F_MoveToBeginning =
                    Sweep_Left_Complete_ActorState_0F_ChangeToNext(
                        determinedStates_ActorStates_0F_Complete,
                        TapeLibrary);
                tmp = tmp.Concat(determinedState_TransitionFunction_ActorState_0F_MoveToBeginning);

                OutputTFs = OutputTFs.Concat(tmp).ToList();
                //UniqueAdd(ref OutputTFs, tmp.ToList());
                Console.WriteLine("Presafety");
            }
            Console.WriteLine(OutputTFs.Count());
            List <TransitionFunction> withBranches = Include_Shift_Right_Safety(
                ref OutputTFs,
                TapeLibrary).ToList();
            Console.WriteLine("RightSafety");

            List <TransitionFunction> withBranchesRH;
            if (leftSafety)
            {
                withBranchesRH = Include_Shift_Left_Safety_RightHanded(
                    ref OutputTFs,
                    TapeLibrary).ToList();
                Console.WriteLine("LeftSafety");
                Console.WriteLine(withBranchesRH.Count());
            }
            else
            {
                withBranchesRH = new List <TransitionFunction>();
            }

            //UniqueAdd(ref withBranchesRH, withBranches);

            //UnbranchedFinalizedList = OutputTFs.Concat(withBranchesRH).ToList();
            UnbranchedFinalizedList = Unique(OutputTFs.Concat(withBranchesRH).Concat(withBranches).ToList());
            Console.WriteLine(UnbranchedFinalizedList.Count());
            for (int i = 0; i < UnbranchedFinalizedList.Count(); i++)
            {
                doc += UnbranchedFinalizedList[i].ToString();
                if (i % 100 == 0)
                {
                    Console.WriteLine(i + "/" + UnbranchedFinalizedList.Count());
                }
            }


            #endregion Iterate of States Of Multitape Machine Mk
        }