in source/MXNetEnv/heuristics_utils.py [0:0]
def simulate(env, net, heuristics, number_of_snakes):
'''
Helper functions to simulate the snakes moving with BattlesnakeGym.
Pseudo code is:
Until only 1 snake is alive:
1. Get actions from the neural network
2. Run heuristisc
3. Feed into the battlesnake gym
'''
Memory = namedtuple("Memory", "state turn_count health")
state, _, _, infos = env.reset()
rgb_arrays = [env.render(mode="rgb_array")]
infos_array = [infos]
actions_array = [[4, 4, 4, 4]]
json_array = [env.get_json()]
heuristics_log_array = [{k: "" for k in range(number_of_snakes)}]
memory = Memory(state=np.zeros(state.shape), turn_count=infos["current_turn"], health=infos["snake_health"])
while True:
infos["current_turn"] += 1
heuristics_log = {}
actions = []
for i in range(number_of_snakes):
action = get_action(net, state, snake_id=i,
turn_count=infos["current_turn"]+1,
health=infos["snake_health"],
memory=memory)
snake_list = make_snake_lists(env)
map_size = env.map_size
json = convert_state_into_json(map_size, state, snake_list, snake_id=i,
turn_count=infos["current_turn"]+1,
health=infos["snake_health"])
# Add heuristics
if is_snake_alive(env, i):
action, heuristics_log_string = heuristics.run(
state, snake_id=i,
turn_count=infos["current_turn"]+1,
health=infos["snake_health"],
json=json,
action=action)
else:
action = np.argmax(action[0])
heuristics_log_string = "Dead"
heuristics_log[i] = heuristics_log_string
actions.append(action)
memory = Memory(state=state, turn_count=infos["current_turn"],
health=infos["snake_health"])
next_state, reward, dones, infos = env.step(np.array(actions))
rgb_array = env.render(mode="rgb_array")
rgb_arrays.append(rgb_array.copy())
infos_array.append(infos)
actions_array.append(actions)
heuristics_log_array.append(heuristics_log)
json_array.append(env.get_json())
# Check if only 1 snake remains
number_of_snakes_alive = sum(list(dones.values()))
if number_of_snakes - number_of_snakes_alive <= 1:
done = True
else:
done = False
state = next_state
if done:
print("Completed")
break
return infos_array, rgb_arrays, actions_array, heuristics_log_array, json_array