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))
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,))
19 for _ in range(nb_iter):
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]
26 result = torch.cat([x[:, None, :] for x in result], dim=1)
31 ######################################################################
33 if __name__ == "__main__":
36 tapes = generate_turing_sequences(5)
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}]")
44 print("".join([f"{x}" for x in r[i]]))