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))
11 all_n = torch.arange(N)
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)
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]
28 result = torch.cat([x[:, None, :] for x in result], dim=1)
33 ######################################################################
35 if __name__ == "__main__":
38 tapes = generate_turing_sequences(1, nb_iter=10)
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}]")
46 print("".join([f"{x}" for x in r[i]]))