Skip to content

Instantly share code, notes, and snippets.

@lorenzotinfena
Last active April 13, 2022 17:20
Show Gist options
  • Save lorenzotinfena/7af0e0763497c5767d7a18d2cc6759f2 to your computer and use it in GitHub Desktop.
Save lorenzotinfena/7af0e0763497c5767d7a18d2cc6759f2 to your computer and use it in GitHub Desktop.
1.code inside "A formal introduction to Deep Reinforcement Learning"
import gym
env = gym.make('CartPole-v1')
# initialize metrics
total_reward = 0
steps = 0
current_state = env.reset() # obtain first state
done = False
while not done: # when done is True the episode ends
action = env.action_space.sample() # get a random action from A of the environment
next_state, reward, done, _ = env.step(action) # perform the action
print(f'Transition from state {current_state} to state {next_state}, '
+ f'I earned reward: {reward} and now the episode is done is {done}')
# update metrics
total_reward += reward
steps += 1
current_state = next_state # update current_state
print(f'Episode done in {steps} steps, total reward {total_reward}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment