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=3, nb_symbols=4, 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     position = torch.zeros(N, dtype=torch.int64)
17     state = torch.zeros(N, dtype=torch.int64)
18
19     result = []
20
21     for _ in range(nb_iter):
22         result.append(tape.clone())
23         current_symbol = tape[all_n, position]
24         tape[all_n, position] = next_symbol[all_n, state, current_symbol]
25         position = (position + next_move[all_n, state, current_symbol] - 1) % tape_size
26         state = next_state[all_n, state, current_symbol]
27
28     result = torch.cat([x[:, None, :] for x in result], dim=1)
29
30     return result
31
32
33 ######################################################################
34
35 if __name__ == "__main__":
36     print("Basic check.")
37
38     tapes = generate_turing_sequences(1, nb_iter=10)
39
40     for i in range(tapes.size(1)):
41         # print(f"- {i:03d} ------------------------")
42         # for s, h, r in zip(state, position, tape):
43         # print("".join([f"{x}" for x in r]))
44         # print(" " * h + f"^[{s}]")
45         for r in tapes:
46             print("".join([f"{x}" for x in r[i]]))