16.03.2021 Views

Advanced Deep Learning with Keras

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9

# UI to dump Q Table contents

def print_q_table(self):

print("Q-Table (Epsilon: %0.2f)" % self.epsilon)

print(self.q_table)

# update Exploration-Exploitation mix

def update_epsilon(self):

if self.epsilon > self.epsilon_min:

self.epsilon *= self.epsilon_decay

Listing 9.3.2, q-learning-9.3.1.py. The main Q-Learning loop. The agent's Q-Table

is updated every state, action, reward, and next state iteration:

# state, action, reward, next state iteration

for episode in range(episode_count):

state = q_world.reset()

done = False

print_episode(episode, delay=delay)

while not done:

action = q_world.act()

next_state, reward, done = q_world.step(action)

q_world.update_q_table(state, action, reward, next_state)

print_status(q_world, done, step, delay=delay)

state = next_state

# if episode is done, perform housekeeping

if done:

if q_world.is_in_win_state():

wins += 1

scores.append(step)

if wins > maxwins:

print(scores)

exit(0)

# Exploration-Exploitation is updated every episode

q_world.update_epsilon()

step = 1

else:

step += 1

print(scores)

q_world.print_q_table()

[ 285 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!