Skip to content

Instantly share code, notes, and snippets.

@SolClover
Created October 15, 2022 10:44
Show Gist options
  • Save SolClover/79761956bf9df19648591a77731f4476 to your computer and use it in GitHub Desktop.
Save SolClover/79761956bf9df19648591a77731f4476 to your computer and use it in GitHub Desktop.
Agent performing random actions around the environment
# Reset environment to initial state
state, info = env.reset()
# Cycle through 30 random steps redering and displaying the agent inside the environment each time
for _ in range(30):
# Render and display current state of the environment
plt.imshow(env.render()) # render current state and pass to pyplot
plt.axis('off')
display.display(plt.gcf()) # get current figure and display
display.clear_output(wait=True) # clear output before showing the next frame
# Sample a random action from the entire action space
random_action = env.action_space.sample()
# Pass the random action into the step function
state, reward, done, _, info = env.step(random_action)
# Reset environment when done=True, i.e., when the agent falls into a Hole (H) or reaches the Goal (G)
if done:
# Render and display current state of the environment
plt.imshow(env.render()) # render current state and pass to pyplot
plt.axis('off')
display.display(plt.gcf()) # get current figure and display
display.clear_output(wait=True) # clear output before showing the next frame
# Reset environment
state, info = env.reset()
# Close environment
env.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment