-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reinforcement_learning_FrozenLake.py
63 lines (46 loc) · 1.58 KB
/
Reinforcement_learning_FrozenLake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# The OpenAI-gym- for environment
# Python 3.11: Coursera Lab exercise.
# check readme file for more details
import numpy as np
env = gym.make('FrozenLake-v0')
#Initialize table with all zeros to be uniform
Q = np.zeros([env.observation_space.n, env.action_space.n])
# Learning parameters
alpha = 0.1
gamma = 0.95
num_episodes = 2000
# array of reward for each episode
rs = np.zeros([num_episodes])
for i in range(num_episodes):
# Set total reward and time to zero, done to False
r_sum_i = 0
t = 0
done = False
#Reset environment and get first new observation
s = env.reset()
while not done:
# Choose an action by greedily (with noise) from Q table
a = np.argmax(Q[s,:] + np.random.randn(1, env.action_space.n)*(1./(i/10+1)))
# Get new state and reward from environment
s1, r, done, _ = env.step(a)
# Update Q-Table with new knowledge
Q[s,a] = (1 - alpha)*Q[s,a] + alpha*(r + gamma*np.max(Q[s1,:]))
# Add reward to episode total
r_sum_i += r*gamma**t
# Update state and time
s = s1
t += 1
rs[i] = r_sum_i
## Plot reward vs episodes
# Sliding window average
r_cumsum = np.cumsum(np.insert(rs, 0, 0))
r_cumsum = (r_cumsum[50:] - r_cumsum[:-50]) / 50
# Plot
plt.plot(r_cumsum)
plt.show()
# Print number of times the goal was reached
N = len(rs)//10
num_Gs = np.zeros(10)
for i in range(10):
num_Gs[i] = np.sum(rs[i*N:(i+1)*N] > 0)
print("Rewards: {0}".format(num_Gs))