You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
action_size=env.action_space.nstate_size=env.observation_space.nprint("Action Size: " , action_size, "(0: LEFT - 1: DOWN - 2: RIGHT - 3: UP)","\nState Size: ", state_size)
Action Size: 4 (0: LEFT - 1: DOWN - 2: RIGHT - 3: UP)
State Size: 16
# Create our Q table with state_size rows and action_size columns (qtable=np.zeros((state_size, action_size))
print("Each row is State & Each column is Action\n------------------\n", qtable)
total_episodes=20000# Total episodeslearning_rate=0.7# Learning ratemax_steps=99# Max steps per episodegamma=0.95# Discounting rate# Exploration parametersepsilon=1.0# Exploration ratemax_epsilon=1.0# Exploration probability at startmin_epsilon=0.01# Minimum exploration probability decay_rate=0.005# Exponential decay rate for exploration prob
Step 4: The Q-learning algorithm
# 1. List of rewardsrewards= []
# 2. Until learning is stoppedStart=time.time()
forepisodeinrange(100):
print(f"\n---------- Episode: {episode} ----------\n")
# Reset the environmentstate=env.reset()
step=0done=Falsetotal_rewards=0forstepinrange(max_steps):
print(f"Step: {step}")
# 3. Choose an action a in the current world state (s)## First we randomize a numberexp_exp_tradeoff=random.uniform(0, 1)
## If this number > greater than epsilon --> exploitation (taking the biggest Q value for this state)ifexp_exp_tradeoff>epsilon:
action=np.argmax(qtable[state,:])
#print(exp_exp_tradeoff, "action", action)# Else doing a random choice --> explorationelse:
action=env.action_space.sample()
#print("action random", action)# Take the action (a) and observe the outcome state(s') and reward (r)new_state, reward, done, info=env.step(action)
# Update Q(s,a):= Q(s,a) + lr [R(s,a) + gamma * max Q(s',a') - Q(s,a)]# qtable[new_state,:] : all the actions we can take from new stateqtable[state, action] =qtable[state, action] +learning_rate* (reward+gamma*np.max(qtable[new_state, :]) -qtable[state, action])
total_rewards+=reward# Our new state is statestate=new_state# If done (if we're dead) : finish episodeifdone==Trueandreward==1:
print("WIN")
breakifdone==True:
print("Dead")
break# Reduce epsilon (because we need less and less exploration) # https://towardsdatascience.com/learning-rate-schedules-and-adaptive-learning-rate-methods-for-deep-learning-2c8f433990d1epsilon=min_epsilon+ (max_epsilon-min_epsilon)*np.exp(-decay_rate*episode) # https://machinelearningmastery.com/using-learning-rate-schedules-deep-learning-models-python-keras/ rewards.append(total_rewards)
End=time.time()
Time=End-Startprint ("Score over time: ", sum(rewards)/total_episodes)
print('Execution time: {:.3f}'.format(Time), 'seconds')
float_formatter="{:.9f}".format# https://stackoverflow.com/questions/21008858/formatting-floats-in-a-numpy-arraynp.set_printoptions(formatter={'float_kind': float_formatter})
print("\n------------------------------------------\n")
print(qtable)
env.reset()
max_steps=99n=100win=0loss=0forepisodeinrange(n):
state=env.reset()
step=0done=Falseprint("****************************************************")
print("EPISODE: ", episode)
forstepinrange(max_steps):
# Take the action (index) that have the maximum expected future reward given that stateaction=np.argmax(qtable[state,:])
new_state, reward, done, info=env.step(action)
ifdone:
# Here, we decide to only print the last state (to see if our agent is on the goal or fall into an hole)env.render()
ifnew_state==15:
win+=1print(">> We reached our Goal")
else:
loss+=1print(">> We fell into a hole")
# We print the number of step it took.print("Number of steps: ", step)
breakstate=new_stateenv.close()
print("****************************************************")
print(f"\nTotal Episode: {n}\nMax Step per Episode: {max_steps}\nWin: {win}\nLoss: {loss}")
****************************************************
EPISODE: 0
>> We fell into a hole
Number of steps: 23
****************************************************
EPISODE: 1
>> We fell into a hole
Number of steps: 17
****************************************************
EPISODE: 2
>> We fell into a hole
Number of steps: 33
****************************************************
EPISODE: 3
>> We fell into a hole
Number of steps: 9
****************************************************
EPISODE: 4
>> We fell into a hole
Number of steps: 18
****************************************************
EPISODE: 5
>> We reached our Goal
Number of steps: 8
****************************************************
EPISODE: 6
>> We fell into a hole
Number of steps: 9
****************************************************
EPISODE: 7
>> We reached our Goal
Number of steps: 16
****************************************************
EPISODE: 8
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 9
>> We fell into a hole
Number of steps: 9
****************************************************
EPISODE: 10
>> We fell into a hole
Number of steps: 15
****************************************************
EPISODE: 11
>> We fell into a hole
Number of steps: 24
****************************************************
EPISODE: 12
>> We reached our Goal
Number of steps: 14
****************************************************
EPISODE: 13
>> We fell into a hole
Number of steps: 22
****************************************************
EPISODE: 14
>> We fell into a hole
Number of steps: 9
****************************************************
EPISODE: 15
>> We reached our Goal
Number of steps: 8
****************************************************
EPISODE: 16
>> We fell into a hole
Number of steps: 12
****************************************************
EPISODE: 17
>> We fell into a hole
Number of steps: 17
****************************************************
EPISODE: 18
>> We fell into a hole
Number of steps: 15
****************************************************
EPISODE: 19
>> We fell into a hole
Number of steps: 30
****************************************************
EPISODE: 20
>> We reached our Goal
Number of steps: 11
****************************************************
EPISODE: 21
>> We fell into a hole
Number of steps: 34
****************************************************
EPISODE: 22
>> We reached our Goal
Number of steps: 45
****************************************************
EPISODE: 23
>> We fell into a hole
Number of steps: 43
****************************************************
EPISODE: 24
>> We reached our Goal
Number of steps: 25
****************************************************
EPISODE: 25
>> We fell into a hole
Number of steps: 25
****************************************************
EPISODE: 26
>> We fell into a hole
Number of steps: 8
****************************************************
EPISODE: 27
>> We fell into a hole
Number of steps: 6
****************************************************
EPISODE: 28
>> We fell into a hole
Number of steps: 15
****************************************************
EPISODE: 29
>> We fell into a hole
Number of steps: 4
****************************************************
EPISODE: 30
>> We fell into a hole
Number of steps: 5
****************************************************
EPISODE: 31
>> We fell into a hole
Number of steps: 42
****************************************************
EPISODE: 32
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 33
>> We fell into a hole
Number of steps: 25
****************************************************
EPISODE: 34
>> We fell into a hole
Number of steps: 25
****************************************************
EPISODE: 35
>> We fell into a hole
Number of steps: 7
****************************************************
EPISODE: 36
>> We fell into a hole
Number of steps: 16
****************************************************
EPISODE: 37
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 38
>> We fell into a hole
Number of steps: 18
****************************************************
EPISODE: 39
>> We fell into a hole
Number of steps: 20
****************************************************
EPISODE: 40
>> We fell into a hole
Number of steps: 47
****************************************************
EPISODE: 41
>> We reached our Goal
Number of steps: 31
****************************************************
EPISODE: 42
>> We reached our Goal
Number of steps: 9
****************************************************
EPISODE: 43
>> We fell into a hole
Number of steps: 16
****************************************************
EPISODE: 44
>> We fell into a hole
Number of steps: 27
****************************************************
EPISODE: 45
>> We reached our Goal
Number of steps: 7
****************************************************
EPISODE: 46
>> We fell into a hole
Number of steps: 8
****************************************************
EPISODE: 47
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 48
>> We fell into a hole
Number of steps: 19
****************************************************
EPISODE: 49
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 50
>> We fell into a hole
Number of steps: 10
****************************************************
EPISODE: 51
>> We fell into a hole
Number of steps: 14
****************************************************
EPISODE: 52
>> We reached our Goal
Number of steps: 11
****************************************************
EPISODE: 53
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 54
>> We fell into a hole
Number of steps: 8
****************************************************
EPISODE: 55
>> We reached our Goal
Number of steps: 15
****************************************************
EPISODE: 56
>> We fell into a hole
Number of steps: 8
****************************************************
EPISODE: 57
>> We reached our Goal
Number of steps: 22
****************************************************
EPISODE: 58
>> We fell into a hole
Number of steps: 5
****************************************************
EPISODE: 59
>> We reached our Goal
Number of steps: 13
****************************************************
EPISODE: 60
>> We fell into a hole
Number of steps: 32
****************************************************
EPISODE: 61
>> We fell into a hole
Number of steps: 9
****************************************************
EPISODE: 62
>> We fell into a hole
Number of steps: 8
****************************************************
EPISODE: 63
>> We reached our Goal
Number of steps: 14
****************************************************
EPISODE: 64
>> We fell into a hole
Number of steps: 28
****************************************************
EPISODE: 65
>> We fell into a hole
Number of steps: 10
****************************************************
EPISODE: 66
>> We fell into a hole
Number of steps: 10
****************************************************
EPISODE: 67
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 68
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 69
>> We fell into a hole
Number of steps: 13
****************************************************
EPISODE: 70
>> We fell into a hole
Number of steps: 6
****************************************************
EPISODE: 71
>> We fell into a hole
Number of steps: 29
****************************************************
EPISODE: 72
>> We fell into a hole
Number of steps: 12
****************************************************
EPISODE: 73
>> We fell into a hole
Number of steps: 15
****************************************************
EPISODE: 74
>> We fell into a hole
Number of steps: 6
****************************************************
EPISODE: 75
>> We reached our Goal
Number of steps: 27
****************************************************
EPISODE: 76
>> We reached our Goal
Number of steps: 29
****************************************************
EPISODE: 77
>> We fell into a hole
Number of steps: 31
****************************************************
EPISODE: 78
>> We fell into a hole
Number of steps: 7
****************************************************
EPISODE: 79
>> We fell into a hole
Number of steps: 3
****************************************************
EPISODE: 80
>> We fell into a hole
Number of steps: 35
****************************************************
EPISODE: 81
>> We fell into a hole
Number of steps: 18
****************************************************
EPISODE: 82
>> We fell into a hole
Number of steps: 14
****************************************************
EPISODE: 83
>> We fell into a hole
Number of steps: 18
****************************************************
EPISODE: 84
>> We fell into a hole
Number of steps: 10
****************************************************
EPISODE: 85
>> We fell into a hole
Number of steps: 9
****************************************************
EPISODE: 86
>> We fell into a hole
Number of steps: 9
****************************************************
EPISODE: 87
>> We fell into a hole
Number of steps: 36
****************************************************
EPISODE: 88
>> We fell into a hole
Number of steps: 3
****************************************************
EPISODE: 89
>> We fell into a hole
Number of steps: 18
****************************************************
EPISODE: 90
>> We fell into a hole
Number of steps: 46
****************************************************
EPISODE: 91
>> We fell into a hole
Number of steps: 11
****************************************************
EPISODE: 92
>> We fell into a hole
Number of steps: 30
****************************************************
EPISODE: 93
>> We fell into a hole
Number of steps: 12
****************************************************
EPISODE: 94
>> We fell into a hole
Number of steps: 5
****************************************************
EPISODE: 95
>> We fell into a hole
Number of steps: 20
****************************************************
EPISODE: 96
>> We fell into a hole
Number of steps: 16
****************************************************
EPISODE: 97
>> We fell into a hole
Number of steps: 21
****************************************************
EPISODE: 98
>> We fell into a hole
Number of steps: 33
****************************************************
EPISODE: 99
>> We fell into a hole
Number of steps: 15
****************************************************
Total Episode: 100
Max Step per Episode: 99
Win: 17
Loss: 83