Main Page | Modules | Alphabetical List | Compound List | File List | Compound Members | Related Pages

state.hpp

00001 #ifndef ross_state_header
00002 #define ross_state_header
00003 
00004 /*Authored 2002    by  Kyle  Ross,  Undergraduate  Philosophy   Thesis
00005 (Advisor: Professor Bram van Heuveln)
00006 
00007 
00008 This  file is part of the   "Generic Turing Machine Implementation for
00009 Use in the Exploration of the Busy Beaver Problem".
00010 
00011 The "Generic Turing Machine Implementation for  Use in the Exploration
00012 of the Busy Beaver Problem" is free  software; you can redistribute it
00013 and/or modify it under the terms of the  GNU General Public License as
00014 published   by the Free Software Foundation;   either version 2 of the
00015 License, or (at your option) any later version.
00016 
00017 This programme is distributed in the hope that it  will be useful, but
00018 WITHOUT    ANY  WARRANTY;  without   even  the    implied warranty  of
00019 MERCHANTABILITY or FITNESS FOR  A  PARTICULAR  PURPOSE.  See  the  GNU
00020 General Public License for more details.
00021 
00022 You should have received   a copy of  the  GNU General Public  License
00023 along  with  this   programme; if  not,  write   to the Free  Software
00024 Foundation, Inc., 59  Temple Place, Suite  330, Boston, MA  02111-1307
00025 USA (or visit http://www.fsf.org/licenses/licenses.html)
00026 
00027 Created as part of the Busy Beaver Project:
00028 Bram van Heuveln <heuveb@rpi.edu>
00029 Boleshaw Szymanski <szymansk@cs.rpi.edu>
00030 Selmer Bringsjord <selmer@rpi.edu>
00031 Carlos Varela <cvarela@cs.rpi.edu>
00032 Owen Kellett <kelleo@rpi.edu>
00033 Shailesh Kelkar <kelkas@rpi.edu>
00034 Kyle Ross <rossk2@rpi.edu>
00035 Rensselaer Reasoning Group
00036 Department of Cognitive Science
00037 Department of Computer Science
00038 Rensselaer Polytechnic Institute
00039 Troy, New York 12180
00040 U.S.A.
00041 */
00042 //implements a finite-state automaton suitable for use in implementation of a four-tuple turing machine
00043 
00044 #include <map>
00045 #include <vector>
00046 #include "debug.hpp"
00047 #include "infinite_tape.hpp"
00048 
00053 template <typename T>
00054 class state
00055 {
00056 public:
00057         enum transition_result{run, halt};
00058 
00063         state()
00064         {
00065                 #ifdef ross_debug_mode
00066                         std::cout<<"["<<this<<"]state constructed"<<std::endl;
00067                 #endif
00068         }
00069         
00074         ~state()
00075         {
00076                 #ifdef ross_debug_mode
00077                         std::cout<<"["<<this<<"]state destructed"<<std::endl;
00078                 #endif
00079         }
00080 
00081         //user functionality associated with making transitions
00082         //create various transition types
00093         void create_transition(T read, T write, typename std::vector<state<T> >::size_type next_state, typename infinite_tape<T>::direction move)
00094         {
00095                 #ifdef kyle_debug_mode
00096                         if(transition_map.find(transition_struct(write, next_state, move))!=transition_map.end())
00097                         {
00098                                 std::cout<<"["<<this<<"]state::create-transition WARNING: transition has been replaced"<<std::endl;
00099                         }
00100                 #endif
00101                 transition_map[read]=transition_struct(write, next_state, move);
00102         }
00103 
00113         void create_write_transition(T read, T write, typename std::vector<state<T> >::size_type next_state)
00114         {
00115                 create_transition(read, write, next_state, infinite_tape<T>::do_nothing);
00116         }
00117 
00127         void create_move_transition(T read, typename std::vector<state<T> >::size_type next_state, typename infinite_tape<T>::direction move)
00128         {
00129                 create_transition(read, read, next_state, move);
00130         }
00131 
00141         transition_result perform_transition(infinite_tape<T> &tape, typename std::vector<state<T> >::size_type &state_index)
00142         {
00143                 typename std::map<T, transition_struct>::iterator lookup_result(transition_map.find(tape.read()));
00144                 if(lookup_result==transition_map.end())//no transition, halt
00145                 {
00146                         return halt; //return failure
00147                 }
00148                 else //process the transition
00149                 {
00150                         tape.write((*lookup_result).second.write); //write new symbol to tape
00151                         tape.move((*lookup_result).second.move); //move tape's head
00152                         state_index=(*lookup_result).second.next_state; //set the next state
00153                         return run; //return success
00154                 }
00155         }
00156 
00168         transition_result simulate_transition(T tape_read, T &tape_write, typename infinite_tape<T>::direction &tape_move, typename std::vector<state<T> >::size_type &state_index) const
00169         {
00170                 //lookup the transition for the current read symbol
00171                 typename std::map<T, transition_struct>::const_iterator lookup_result=transition_map.find(tape_read);
00172                 if(lookup_result==transition_map.end())//no transition, halt
00173                 {
00174                         return halt;
00175                 }
00176                 else //process the transition
00177                 {
00178                         tape_write=(*lookup_result).second.write; //the new tape symbol that would have been written
00179                         tape_move=(*lookup_result).second.move; //the head's move that would have been performed
00180                         state_index=(*lookup_result).second.next_state; //the state that would have been entered
00181                         return run; //return success
00182                 }
00183         }
00184         //displays the states of the set
00185         //preconditions: there are zero or more states defined
00186         //postconditions: "  < no transitions defined >" has been displayed if there are none defined, else each transition has been displayed in the format "  read("<symbol>") -> "<transition-struct-format-output><endline>
00187         //complexity: O(NM) where N is the number of states defined and M is the alphabet size
00194         void print(std::ostream & out) const
00195         {
00196                 if(transition_map.begin()==transition_map.end())
00197                 {
00198                         out<<"  < no transitions defined >"<<std::endl;
00199                 }
00200                 else
00201                 {
00202                         for(typename std::map<T, transition_struct>::const_iterator i=transition_map.begin(); i!=transition_map.end(); ++i)
00203                         {
00204                                 out<<"  read("<<(*i).first<<") -> ";
00205                                 (*i).second.print(out);
00206                                 out<<std::endl;
00207                         }
00208                 }
00209         }
00210 protected:
00214         struct transition_struct
00215         {
00220                 transition_struct()
00221                 {}
00222 
00230                 transition_struct(T new_write, typename std::vector<state<T> >::size_type new_next_state, typename infinite_tape<T>::direction new_move)
00231                 :write(new_write), next_state(new_next_state), move(new_move)
00232                 {}
00233 
00240                 void print(std::ostream &out) const
00241                 {
00242                         out<<"write("<<write<<") move(";
00243                         switch(move)
00244                         {
00245                                 case infinite_tape<T>::left:
00246                                         out<<"left";
00247                                         break;
00248                                 case infinite_tape<T>::right:
00249                                         out<<"right";
00250                                         break;
00251                                 case infinite_tape<T>::do_nothing:
00252                                         out<<"do_nothing";
00253                         }
00254                         out<<") next_state("<<next_state<<")";
00255                 }
00256                 T write;
00257                 typename std::vector<state<T> >::size_type next_state;
00258                 typename infinite_tape<T>::direction move;
00259 
00260         };
00264         typename std::map<T, transition_struct> transition_map;
00265 };
00266 
00267 
00275 template <typename T>
00276 std::ostream& operator<<(std::ostream &out, const state<T> &to_print)
00277 {
00278         to_print.print(out);
00279         return out;
00280 }
00281 
00282 #endif
00283 
00284 

Generated on Thu Nov 20 00:17:32 2003 for BusyBeaver by doxygen 1.3.3