Playing the Game

Now, once you have reset the game, all there is to do is play. You can feed your actions/responses to the game with,

action = 0
new_state, reward, done, info = env.step(action)
print((new_state, reward, done, info))

The env.step function accepts your response/action (move left or right)  and generates the new_state/orientation (x, x_dot, theta, theta_dot) of the Cart-Pole system. Along with the new state, the env.step function also returns the 'reward' which indicates the score you receive for the action you just took, 'done' which indicates if the game has finished, and 'info' which has system related information.

When the game begins, done is set to False. Only when the Cart-Pole orientation exceeds the game rules, done will be set to True, indicating that either the cart moved 2.4 units from the center or the pole was more than 45 degrees from the vertical.

As long as every step you take is within the 'game over' limits, the reward for that step will be 1 unit, otherwise zero.

 

Let's play the game by making random actions

def random_actions_game(episodes):
for episode in range(episodes):
state = env.reset() # reset environment
done = False # set done to False
score = 0
while not done:
#env.render() # Display cart pole game on the screen
action = random.choice([0,1]) # Choose between 0 or 1
new_state, reward, done, info = env.step(action) # perform the action
score+=1
print('Episode: {} Score: {}'.format(episode+1, score))

# play game
random_actions_game(10)
Figure  15.2:  Scores from random actions game 
Figure 15.3: Snapshot of Cart-Pole game that gets displayed on the screen when rendered
random.choice returns a randomly selected item from a non-empty sequence such as list/array. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.70.170