-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
210 lines (159 loc) · 7.26 KB
/
script.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**********************************************************************************************************************************************
** ___ __ __ .___________. ______ .___ ___. ___ ______ ______ __ .__ __. **
** / \ | | | | | |/ __ \ | \/ | / \ / | / __ \ | | | \ | | **
** / ^ \ | | | | `---| |----| | | | | \ / | / ^ \ | ,----'| | | | | | | \| | **
** / /_\ \ | | | | | | | | | | | |\/| | / /_\ \ | | | | | | | | | . ` | **
** / _____ \ | `--' | | | | `--' | | | | | / _____ \ | `----.| `--' | | | | |\ | **
** /__/ \__\ \______/ |__| \______/ |__| |__| /__/ \__\ \______| \______/ |__| |__| \__| **
** ______ ______ .___ ___. .______ __ __ .___________._______ __ ___ _______ .______ .__ __. _______ __ **
** / | / __ \ | \/ | | _ \ | | | | | | ____| | |/ / | ____|| _ \ | \ | | | ____|| | **
** | ,----'| | | | | \ / | | |_) | | | | | `---| |----| |__ | ' / | |__ | |_) | | \| | | |__ | | **
** | | | | | | | |\/| | | ___/ | | | | | | | __| | < | __| | / | . ` | | __| | | **
** | `----.| `--' | | | | | | | | `--' | | | | |____ | . \ | |____ | |\ \----| |\ | | |____ | `----. **
** \______| \______/ |__| |__| | _| \______/ |__| |_______| |__|\__\ |_______|| _| `._____|__| \__| |_______||_______| **
** **
***********************************************************************************************************************************************/
/** A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic */
const BigNumber = require('bignumber.js');
/** all the step of computation till to runtime */
function allstep(table, control, head, tape, blank, halt, runtime) {
while (runtime > 0) {
tape, runtime = onestep(table, control, head, tape, blank, halt, runtime)
runtime--;
}
}
/** This function define the behaviour of what is a single step of a turing machine computation */
function onestep(table, control, head, tape, blank, halt, runtime) {
let result = table[control][tape[head]];
// the triple which represents a move is [control, write, dir]
tape[head] = result[1]
control = result[0];
// 0 represents the halting interrupt
if (result[0] === halt) {
return { tape, runtime: -1 };
}
if (result[2] > 1) {
if (head === tape.length - 1) {
tape.push(blank);
}
head++;
} else {
if (head === 0) {
tape.unshift(blank);
}
}
return { tape, runtime };
}
/************************************************************************************************/
/** This function define the behaviour of what is a single step of a turing machine computation */
function step(table, control, head, tape, blank, halt, runtime) {
let result = table[control][tape[head]];
// the triple which represents a move is [control, write, dir]
tape[head] = result[1]
control = result[0];
// 0 represents the halting interrupt
if (result[0] === halt || runtime === 0) {
return tape;
}
if (result[2] > 1) {
if (head === tape.length - 1) {
tape.push(blank);
}
head++;
} else {
if (head === 0) {
tape.unshift(blank);
}
}
// 1 represents the output 'ready' to new moves.
return step(table, control, head, tape, blank, halt, --runtime);
}
/** This function summarizes the preparatory settings one should set to properly configure a machine */
function boot(number, colors, states) {
if (number.toString().length > states * colors) {
throw new Error(`ERROR: number ${number} doesn't represent a TM in the space D(${states},${colors})`);
}
/** The actual model of a complete Turing Machine */
let table = [];
const base = colors * ((2 * states) + 1);
let r;
let gr;
for (let i = 0; i < states; i++) {
table.push([]);
for (let j = 0; j < colors; j++) {
table[i].push([null, null, null]);
}
}
// the triple which represents a move is [control, write, dir]
for (i = states - 1; i >= 0; i--) {
for (j = 0; j < colors; j++) {
gr = number.modulo(base);
number = number.minus(gr).idiv(base);
r = gr.toNumber();
if (r < colors) {
table[i][j][0] = -1; //halting state
table[i][j][1] = r;
table[i][j][2] = 1;
} else {
r = r - colors;
table[i][j][1] = (r % colors);
r = Math.floor(r / colors);
table[i][j][2] = ((r % 2) == 0 ? 2 : 0);
r = Math.floor(r / 2);
table[i][j][0] = (r % states);
}
}
}
return table;
}
/** This routine executes a batch of turing machine and gives back to the caller the array of output tapes */
function compute(states, colors, runtime, currentTM, lastTM, quietMode) {
// Arguments:
// - <states>
// - <colors>
// - max runtime
// - first TM
// - last TM
let tape;
let head;
let table;
let output = [];
const blank = 0;
const init = 0;
const halt = -1;
runtime = parseInt(runtime);
states = parseInt(states);
colors = parseInt(colors);
let current = new BigNumber(currentTM);
let last = new BigNumber(lastTM);
start = Date.now();
if (quietMode != 1) {
console.log("\n[[Automacoin Kernel]]", `computing turing machines in the space D(${states},${colors}) in the interval (${currentTM},${lastTM}) with max runtime ${runtime}.\n`);
}
while (current.isLessThanOrEqualTo(last)) {
// The TM's Tape, blank at start
tape = [blank];
// the computation starts at position zero
head = 0;
// the triple [table, control, halting] represents a complete turing machine
table = boot(current, colors, states);
console.log(`\n\n\nTable of TM ${current}: \n`, table, "\n\n\n");
// the actual computation until runtime is reached or the machine halts
allstep(table, init, head, tape, blank, halt, runtime);
if (quietMode != 1) {
console.log(`\n[[Automacoin Kernel]] turing machine ${current.toString()} has been executed with output tape: `, tape.join(''));
}
output.push(tape.join(''));
current = current.plus(1);
}
return {
states,
colors,
runtime,
interval: [currentTM, lastTM],
duration: Date.now() - start,
tapes: output
}
}
console.log(boot(new BigNumber(10), 2, 2))
module.exports = { compute };