Let's define a function that when called will return the action that needs to be taken for that specific state.
def agent_action(model, epsilon, state, actions):
"""Define action to be taken."""
if np.random.rand() <= epsilon:
act = random.randrange(actions)
else:
act = np.argmax(model.predict(state)[0])
return act
For any value from the uniform distribution (between 0 and 1), less than or equal to epsilon, the action returned will be random. For any value greater than epsilon, the action chosen will be that predicted by the agent we have defined above.
1) numpy.random.rand function generates a random number from a uniform distribution over 0 and 1. 2) numpy.argmax returns the index of the maximum value in the sequence. 3) random.randrange returns a randomly selected item from a range().