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

non_halt_data_structures.hpp

00001 #ifndef kellett_non_halt_data_structures_header
00002 #define kellett_non_halt_data_structures_header
00003 
00004 /*Authored 2003    by  Owen Kellett,  UnderGraduate Research
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 
00043 #include <fstream>
00044 #include <iostream>
00045 #include <iterator>
00046 #include <list>
00047 #include <vector>
00048 #include <map>
00049 
00050 #include "infinite_tape.hpp"
00051 #include "state.hpp"
00052 #include "turing_machine.hpp"
00053 
00054 typedef unsigned short d_type;
00055 
00060 //Begin simpleLoopCheck data structures
00061 
00066 template <typename T>
00067 class stateSymbolPair
00068 {
00069 public:
00074         stateSymbolPair()
00075         :stateIndicator(0), symbol(0)
00076         {}
00077 
00085         stateSymbolPair(typename std::vector<state<T> >::size_type sI, T s)
00086         :stateIndicator(sI), symbol(s)
00087         {}
00088 
00094         friend bool operator < <> (const stateSymbolPair<T> &lhs, const stateSymbolPair<T> &rhs);
00095 
00096         typename std::vector<state<T> >::size_type stateIndicator;
00097         T symbol;
00098 };
00099 
00100 
00107 template <typename T>
00108 bool operator < (const stateSymbolPair<T> &lhs, const stateSymbolPair<T> &rhs)
00109 {
00110         if(lhs.stateIndicator == rhs.stateIndicator)
00111                 return lhs.symbol < rhs.symbol;
00112         return lhs.stateIndicator < rhs.stateIndicator;
00113 }
00114 
00122 template <typename T>
00123 class tapeBoundsTriple
00124 {
00125 public:
00134         tapeBoundsTriple(infinite_tape<T> &t, typename std::list<T>::iterator l, typename std::list<T>::iterator r)
00135         :tape(t), leftBoundOriginal(l), rightBoundOriginal(r)
00136         {
00137                 typename std::list<T>::iterator lTempOriginal = t.begin();
00138                 typename std::list<T>::iterator rTempOriginal = t.begin();
00139                 leftBound = tape.begin();
00140                 rightBound = tape.begin();
00141 
00142                 //position the bounds on tape to match their positions on t
00143                 while(lTempOriginal != leftBoundOriginal)
00144                 {
00145                         ++lTempOriginal;
00146                         ++leftBound;
00147                 }
00148                 while(rTempOriginal != rightBoundOriginal)
00149                 {
00150                         ++rTempOriginal;
00151                         ++rightBound;
00152                 }
00153         }
00154 
00159         tapeBoundsTriple(const tapeBoundsTriple<T> &rhs)
00160         :tape(rhs.tape), leftBoundOriginal(rhs.leftBoundOriginal), rightBoundOriginal(rhs.rightBoundOriginal)
00161         {
00162                 typename std::list<T>::const_iterator lTempOriginal = rhs.tape.begin();
00163                 typename std::list<T>::const_iterator rTempOriginal = rhs.tape.begin();
00164                 leftBound = tape.begin();
00165                 rightBound = tape.begin();
00166 
00167                 //position the bounds on tape to match their positions on t
00168                 while(lTempOriginal != rhs.leftBound)
00169                 {
00170                         ++lTempOriginal;
00171                         ++leftBound;
00172                 }
00173                 while(rTempOriginal != rhs.rightBound)
00174                 {
00175                         ++rTempOriginal;
00176                         ++rightBound;
00177                 }
00178         }
00179 
00180         infinite_tape<T> tape;
00181         typename std::list<T>::iterator leftBound;
00182         typename std::list<T>::iterator rightBound;
00183 
00184         typename std::list<T>::iterator leftBoundOriginal;
00185         typename std::list<T>::iterator rightBoundOriginal;
00186 };
00187 
00188 //end simpleLoopCheck data structures
00189 
00190 //begin backTrack data structures
00191 
00196 template <typename T>
00197 class owenTransition
00198 {
00199 public:
00204         owenTransition()
00205         {}
00206 
00217         owenTransition(T newRead, T newWrite, typename std::vector<state<T> >::size_type newNextState, typename std::vector<state<T> >::size_type newOriginalState, typename infinite_tape<T>::direction newMove)
00218         :read(newRead), write(newWrite), originalState(newOriginalState), nextState(newNextState), move(newMove)
00219         {}
00220 
00221         T read;
00222         T write;
00223         typename std::vector<state<T> >::size_type originalState;
00224         typename std::vector<state<T> >::size_type nextState;
00225         typename infinite_tape<T>::direction move;
00226 };
00227 
00232 template <typename T>
00233 class localTape
00234 {
00235 public:
00240         localTape()
00241         {}
00242 
00250         localTape(T onlySymbol)
00251         {
00252                 tapeConfig.push_back(onlySymbol);
00253                 currentPosition = 0;
00254         }
00255 
00264         localTape(T desiredSymbol, owenTransition<T> &transition)
00265         {
00266                 switch(transition.move)
00267                 {
00268                         case infinite_tape<T>::left:
00269                                 tapeConfig.push_back(desiredSymbol);
00270                                 tapeConfig.push_back(transition.read);
00271                                 currentPosition = 1;
00272                                 break;
00273                         case infinite_tape<T>::right:
00274                                 tapeConfig.push_back(transition.read);
00275                                 tapeConfig.push_back(desiredSymbol);
00276                                 currentPosition = 0;
00277                                 break;
00278                         case infinite_tape<T>::do_nothing:
00279                                 tapeConfig.push_back(transition.read);
00280                                 currentPosition = 0;
00281                                 break;
00282                 }
00283         }
00284 
00293         localTape(localTape<T> &desiredTape, owenTransition<T> &transition)
00294         {
00295                 switch(transition.move)
00296                 {
00297                         case infinite_tape<T>::left:
00298                                 for(int j = 0; j <= desiredTape.tapeConfig.size(); j++)
00299                                 {
00300                                         if(j == desiredTape.currentPosition + 1)
00301                                         {
00302                                                 tapeConfig.push_back(transition.read);
00303                                                 currentPosition = tapeConfig.size()-1;
00304                                         }
00305                                         else if(j == desiredTape.tapeConfig.size())
00306                                                 continue;
00307                                         else
00308                                                 tapeConfig.push_back(desiredTape.tapeConfig[j]);
00309                                 }
00310                                 break;
00311                         case infinite_tape<T>::right:
00312                                 if(desiredTape.currentPosition == 0)
00313                                 {
00314                                         tapeConfig.push_back(transition.read);
00315                                         currentPosition = tapeConfig.size()-1;
00316                                 }
00317                                 for(int j = 0; j < desiredTape.tapeConfig.size(); j++)
00318                                 {
00319                                         if(j == desiredTape.currentPosition - 1)
00320                                         {
00321                                                 tapeConfig.push_back(transition.read);
00322                                                 currentPosition = tapeConfig.size()-1;
00323                                         }
00324                                         else
00325                                                 tapeConfig.push_back(desiredTape.tapeConfig[j]);
00326                                 }
00327                                 break;
00328                         case infinite_tape<T>::do_nothing:
00329                                 for(int j = 0; j < desiredTape.tapeConfig.size(); j++)
00330                                 {
00331                                         if(j == desiredTape.currentPosition)
00332                                         {
00333                                                 tapeConfig.push_back(transition.read);
00334                                                 currentPosition = tapeConfig.size()-1;
00335                                         }
00336                                         else
00337                                                 tapeConfig.push_back(desiredTape.tapeConfig[j]);
00338                                 }
00339                                 break;
00340                 }
00341         }
00342 
00351         bool simulateTransition(localTape<T> &desiredTape, owenTransition<T> &transition)
00352         {
00353                 std::vector<T> tempTapeConfig(tapeConfig);
00354                 int tempCurrentPosition = currentPosition;
00355                 int tempCounter, desiredCounter;
00356 
00357                 if(tempTapeConfig[tempCurrentPosition] == transition.read)
00358                 {
00359                         //simulate the transition on a temporary copy of this tape
00360                         tempTapeConfig[tempCurrentPosition] = transition.write;
00361                         switch(transition.move)
00362                         {
00363                                 case infinite_tape<T>::left:
00364                                         tempCurrentPosition--;
00365                                         break;
00366                                 case infinite_tape<T>::right:
00367                                         tempCurrentPosition++;
00368                                         break;
00369                         }
00370 
00371                         //check if the tapes are equivalent to the right of the read head
00372                         for(tempCounter = tempCurrentPosition, desiredCounter = desiredTape.currentPosition;
00373                                 tempCounter < tempTapeConfig.size() && desiredCounter < desiredTape.tapeConfig.size();
00374                                 tempCounter++, desiredCounter++)
00375                         {
00376                                 if(tempTapeConfig[tempCounter] != desiredTape.tapeConfig[desiredCounter])
00377                                 {
00378                                         return false;
00379                                 }
00380                         }
00381                         //check if the tapes are equivalent to the left of the read head
00382                         for(tempCounter = tempCurrentPosition, desiredCounter = desiredTape.currentPosition;
00383                                 tempCounter >= 0 && desiredCounter >= 0;
00384                                 tempCounter--, desiredCounter--)
00385                         {
00386                                 if(tempTapeConfig[tempCounter] != desiredTape.tapeConfig[desiredCounter])
00387                                 {
00388                                         return false;
00389                                 }
00390                         }
00391                         return true;
00392                 }
00393                 else
00394                         return false;
00395         }
00396 
00402         bool allOneValue(T value)
00403         {
00404                 for(int j = 0; j < tapeConfig.size(); j++)
00405                         if(tapeConfig[j] != value)
00406                                 return false;
00407                 return true;
00408         }
00409 
00415         void print()
00416         {
00417                 for(int j = 0; j < tapeConfig.size(); j++)
00418                 {
00419                         if(j == currentPosition)
00420                                 std::cout << "->" << tapeConfig[j] << "<-";
00421                         else
00422                                 std::cout << tapeConfig[j];
00423                 }
00424         }
00425 
00426         std::vector<T> tapeConfig;
00427         int currentPosition;
00428 };
00429 
00430 //end backTrack data structures
00431 
00432 //begin christmasTree data structures
00433 
00438 template <typename T>
00439 class tapeChunk
00440 {
00441 public:
00446         tapeChunk()
00447         {}
00448 
00454         tapeChunk(const tapeChunk<T> &tape)
00455         {
00456                 for(int j = 0; j < tape.tapeConfig.size(); j++)
00457                         tapeConfig.push_back(tape.tapeConfig[j]);
00458         }
00459 
00469         tapeChunk(infinite_tape<T> &tape, typename std::list<T>::iterator start, typename std::list<T>::iterator end)
00470         {
00471                 for(typename std::list<T>::iterator index = start; index != end; index++)
00472                         tapeConfig.push_back(*index);
00473                 tapeConfig.push_back(*end);
00474         }
00475 
00485         tapeChunk(tapeChunk<T> &tape, int start, int end)
00486         {
00487                 for(int j = start; j < end; j++)
00488                         tapeConfig.push_back(tape.tapeConfig[j]);
00489                 tapeConfig.push_back(tape.tapeConfig[end]);
00490         }
00491 
00501         void fillTape(infinite_tape<T> &tape, typename std::list<T>::iterator start, typename std::list<T>::iterator end)
00502         {
00503                 for(typename std::list<T>::iterator index = start; index != end; index++)
00504                                         tapeConfig.push_back(*index);
00505                 tapeConfig.push_back(*end);
00506         }
00507 
00517         void fillTape(const tapeChunk<T> &tape, int start, int end)
00518         {
00519                 for(int j = start; j < end; j++)
00520                         tapeConfig.push_back(tape.tapeConfig[j]);
00521                 tapeConfig.push_back(tape.tapeConfig[end]);
00522         }
00523 
00529         void emptyTape()
00530         {
00531                 while(tapeConfig.size() > 0)
00532                         tapeConfig.pop_back();
00533         }
00534 
00540         bool compareNextChunk(const tapeChunk<T> & tape, int &start)
00541         {
00542                 for(int j = 0; j < tape.tapeConfig.size(); j++)
00543                 {
00544                         if(start >= tapeConfig.size() || tape.tapeConfig[j] != tapeConfig[start])
00545                                 return false;
00546                         start++;
00547                 }
00548                 return true;
00549         }
00550 
00556         bool isEmpty()
00557         {
00558                 return tapeConfig.size() == 0;
00559         }
00560 
00566         int length()
00567         {
00568                 return tapeConfig.size();
00569         }
00570 
00575         void print()
00576         {
00577                 for(int j = 0; j < tapeConfig.size(); j++)
00578                         std::cout << tapeConfig[j] << ' ';
00579                 std::cout << std::endl;
00580         }
00581 
00586         tapeChunk<T> operator + (const tapeChunk<T> &rhs)
00587         {
00588                 tapeChunk<T> returnValue;
00589                 returnValue.fillTape(*this, 0, tapeConfig.size()-1);
00590                 returnValue.fillTape(rhs, 0, rhs.tapeConfig.size()-1);
00591                 return returnValue;
00592         }
00593 
00599         bool operator == (const tapeChunk<T> &rhs)
00600         {
00601                 if(tapeConfig.size() != rhs.tapeConfig.size())
00602                         return false;
00603                 for(int j = 0; j < tapeConfig.size(); j++)
00604                         if(tapeConfig[j] != rhs.tapeConfig[j])
00605                                 return false;
00606                 return true;
00607         }
00608 
00614         bool operator != (const tapeChunk<T> &rhs)
00615         {
00616                 if(tapeConfig.size() != rhs.tapeConfig.size())
00617                         return true;
00618                 for(int j = 0; j < tapeConfig.size(); j++)
00619                         if(tapeConfig[j] != rhs.tapeConfig[j])
00620                                 return true;
00621                 return false;
00622         }
00623 
00624         std::vector<T> tapeConfig;
00625 };
00626 
00627 
00628 
00629 //end christmasTree data structures
00630 
00631 //being multisweep data structures
00632 
00637 template <typename T>
00638 class sweepStats
00639 {
00640 public:
00641         tapeChunk<T> U;
00642         tapeChunk<T> V;
00643         tapeChunk<T> X;
00644 
00645         tapeChunk<T> Xp;
00646         tapeChunk<T> Y;
00647         tapeChunk<T> Yp;
00648         tapeChunk<T> Z;
00649         tapeChunk<T> Vp;
00650         tapeChunk<T> Vpp;
00651         tapeChunk<T> Up;
00652 
00653         typename std::vector<state<T> >::size_type q;
00654         typename std::vector<state<T> >::size_type r;
00655         typename std::vector<state<T> >::size_type t;
00656 
00657         void fill(tapeChunk<T> myU,
00658                         tapeChunk<T> myV,
00659                         tapeChunk<T> myX,
00660                         tapeChunk<T> myXp,
00661                         tapeChunk<T> myY,
00662                         tapeChunk<T> myYp,
00663                         tapeChunk<T> myZ,
00664                         tapeChunk<T> myVp,
00665                         tapeChunk<T> myVpp,
00666                         tapeChunk<T> myUp,
00667                         typename std::vector<state<T> >::size_type myq,
00668                         typename std::vector<state<T> >::size_type myr,
00669                         typename std::vector<state<T> >::size_type myt)
00670         {
00671                 U = myU;
00672                 V = myV;
00673                 X = myX;
00674                 Xp = myXp;
00675                 Y = myY;
00676                 Yp = myYp;
00677                 Z = myZ;
00678                 Vp = myVp;
00679                 Vpp = myVpp;
00680                 Up = myUp;
00681                 q = myq;
00682                 r = myr;
00683                 t = myt;
00684         }
00685 
00686         void populate(tapeChunk<T> &myU,
00687                         tapeChunk<T> &myV,
00688                         tapeChunk<T> &myX,
00689                         tapeChunk<T> &myXp,
00690                         tapeChunk<T> &myY,
00691                         tapeChunk<T> &myYp,
00692                         tapeChunk<T> &myZ,
00693                         tapeChunk<T> &myVp,
00694                         tapeChunk<T> &myVpp,
00695                         tapeChunk<T> &myUp,
00696                         typename std::vector<state<T> >::size_type &myq,
00697                         typename std::vector<state<T> >::size_type &myr,
00698                         typename std::vector<state<T> >::size_type &myt)
00699         {
00700                 myU = U;
00701                 myV = V;
00702                 myX = X;
00703                 myXp = Xp;
00704                 myY = Y;
00705                 myYp = Yp;
00706                 myZ = Z;
00707                 myVp = Vp;
00708                 myVpp = Vpp;
00709                 myUp = Up;
00710                 myq = q;
00711                 myr = r;
00712                 myt = t;
00713         }
00714 };
00715 
00716 //end multisweep data structures
00717 
00718 #endif

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