Update.
[picoclvr.git] / turing.py
1 #!/usr/bin/env python
2
3 import torch
4
5
6 def generate_turing_sequences(N, nb_iter=5, nb_states=4, nb_symbols=2, tape_size=5):
7     next_state = torch.randint(nb_states, (N, nb_states, nb_symbols))
8     next_symbol = torch.randint(nb_symbols, (N, nb_states, nb_symbols))
9     next_move = torch.randint(3, (N, nb_states, nb_symbols))
10
11     all_n = torch.arange(N)
12
13     tape = torch.randint(nb_symbols, (N, tape_size))
14     position = torch.randint(tape_size, (N,))
15     state = torch.randint(nb_states, (N,))
16
17     result = []
18
19     for _ in range(nb_iter):
20         result.append(tape)
21         current_symbol = tape[all_n, position]
22         tape[all_n, position] = next_symbol[all_n, state, current_symbol]
23         position = (position + next_move[all_n, state, current_symbol] - 1) % tape_size
24         state = next_state[all_n, state, current_symbol]
25
26     result = torch.cat([x[:, None, :] for x in result], dim=1)
27
28     return result
29
30
31 ######################################################################
32
33 if __name__ == "__main__":
34     print("Basic check.")
35
36     tapes = generate_turing_sequences(5)
37
38     for i in range(tapes.size(1)):
39         print(f"- {i:03d} ------------------------")
40         # for s, h, r in zip(state, position, tape):
41         # print("".join([f"{x}" for x in r]))
42         # print(" " * h + f"^[{s}]")
43         for r in tapes:
44             print("".join([f"{x}" for x in r[i]]))