-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
175 lines (159 loc) · 6.34 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include <bits/stdc++.h>
using namespace std;
/*
memory buffer : turing number : tape description
design of program:
- divide the tape into 3 sections enstead of using 3 separate tapes (prob saves memory and increases efficiecy especially during 5 state busy beaver)
- wrong?
- Create a universal turing mahine class????
- have a simulate function
- the tape should be a vector<string>
- give some dedicated amount of space to the buffer at the start
- the buffer will store the state it is currently in
- for the 3 tape design (vector<string> or a set):
- tape for the buffer
- tape for the machine description (array inside of array?)
- tape for the tape description (where m's computation is performed)
- create a universalTuringMachine Class
- given some input simulate and give an output
- class variables (strings or long longs)
- states (array)
- Initial state
- Terminating State
- Blank Symbol
- tuple of permissable symbols
- array inside of array of each rule
- hard code the three permissable actions {L, N, R}
- init function
- the input tape
and class variables
- functions
- halt()
- return tape
- nextState()
- change tape of memory buffer
- change contents of the tape
- should you also impliment the functions in the paper? *dunno
- simulate() function outputs the resulting tape after computation (output)
*/
class universalTuringMachine{
vector<string> states;
string initialState;
string terminatingState;
string blankSymbol;
vector<string> permissableSymbols;
vector<vector<string>> machineDescriptionTape;
vector<string> permissableActions = {"L", "N", "R"};
string bufferTape[2];
//a vector is theoretically infinite????
//vector<string> tapeDescription = {"1", "1", "1"};
//vector<string> tapeDescription = {};
long long startIndex = 5000;
long long endIndex = 10000-1;
string tapeDescription[10000];
public:
void setValues(vector<string>, string, string, string, vector<string>, vector<vector<string>>);
void halt();
void simulate();
void printTape();
string returnVar(){
return machineDescriptionTape[1][1];
}
};
void universalTuringMachine::setValues (vector<string> States, string InitialState, string TerminatingState, string BlankSymbol,
vector<string> PermissableSymbols, vector<vector<string>> Rules){
states = States;
initialState = InitialState;
terminatingState = TerminatingState;
blankSymbol = BlankSymbol;
permissableSymbols = PermissableSymbols;
machineDescriptionTape = Rules;
for(int x=0; x<10000; x++){
tapeDescription[x] = "0";
}
/*
tapeDescription[startIndex] = "1";
tapeDescription[startIndex+1] = "1";
tapeDescription[startIndex+2] = "1";
*/
}
void universalTuringMachine::halt(){
for(int x=0; x<endIndex; x++){
cout << tapeDescription[x] << " ";
}
cout << endl;
}
void universalTuringMachine::printTape(){
for(int x=0; x<endIndex; x++){
cout << tapeDescription[x] << " ";
}
cout << endl;
}
void universalTuringMachine::simulate(){
bool HALT = false;
long long index = startIndex;
bufferTape[0] = initialState;
long long count = 0;
while(HALT == false){
//printTape();
for(int x=0; x<machineDescriptionTape.size(); x++){
if(machineDescriptionTape[x][0] == bufferTape[0] && machineDescriptionTape[x][1] == tapeDescription[index]){
if(machineDescriptionTape[x][4] == terminatingState){
tapeDescription[index] = machineDescriptionTape[x][2];
halt();
HALT = true;
break;
}
bufferTape[0] = machineDescriptionTape[x][4];
if(machineDescriptionTape[x][3] == "right"){
tapeDescription[index] = machineDescriptionTape[x][2];
index++;
break;
}else if(machineDescriptionTape[x][3] == "left"){
tapeDescription[index] = machineDescriptionTape[x][2];
index--;
break;
}else if(machineDescriptionTape[x][3] == "stay"){
tapeDescription[index] = machineDescriptionTape[x][2];
break;
}
}
}
count++;
}
}
int main() {
// Simple incrementer Turing machine
/*
universalTuringMachine test;
vector<string> state = {"q0", "qf"};
vector<string> symbols = {"0", "1"};
vector<vector<string>> rules = {{"q0", "1", "1", "right", "q0"}, {"q0", "0", "1", "stay", "qf"}};
test.setValues(state, "q0", "qf", "0", symbols, rules);
test.simulate();
*/
// 3-State Busy Beaver turing machine
universalTuringMachine threeStateBusyBeaver;
vector<string> state = {"a", "b", "c", "halt"};
vector<string> symbols = {"0", "1"};
vector<vector<string>> rules = {{"a", "0", "1", "right", "b"}, {"a", "1", "1", "left", "c"}, {"b", "0", "1", "left", "a"},
{"b", "1", "1", "right", "b"}, {"c", "0", "1", "left", "b"}, {"c", "1", "1", "stay", "halt"}};
threeStateBusyBeaver.setValues(state, "a", "halt", "0", symbols, rules);
threeStateBusyBeaver.simulate();
/*
// 5-state, 2-symbol probable Busy Beaver machine from wikipedia
universalTuringMachine fiveStateTwoSymbolBusyBeaver;
vector<string> state = {"A", "B", "C", "D", "E", "H"};
vector<string> symbols = {"0", "1"};
vector<vector<string>> rules = {{"A", "0", "1", "right", "B"}, {"A", "1", "1", "left", "C"}, {"B", "0", "1", "right", "C"},
{"B", "1", "1", "right", "B"}, {"C", "0", "1", "right", "D"}, {"C", "1", "0", "left", "E"}, {"D", "0", "1", "left", "A"},
{"D", "1", "1", "left", "D"}, {"E", "0", "1", "stay", "H"}, {"E", "1", "0", "left", "A"}};
fiveStateTwoSymbolBusyBeaver.setValues(state, "A", "H", "0", symbols, rules);
fiveStateTwoSymbolBusyBeaver.simulate();
*/
}