-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunText.py
70 lines (52 loc) · 1.71 KB
/
runText.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
64
65
66
67
68
69
70
from mazeEnv import Maze
from RLQLearing import QLearningTable
import numpy as np
import matplotlib.pyplot as plt
count = 0
def update():
global count
totleStepCount = []
successStepCount = []
errorStepCount = []
for episode in range(1000):
# initial observation
observation = env.reset()
while True:
# fresh env
env.render()
# RL choose action based on observation
action = RL.choose_action(str(observation))
# RL take action and get next observation and reward
observation_, reward, done = env.step(action)
# RL learn from this transition
RL.learn(str(observation), action, reward, str(observation_))
# swap observation
observation = observation_
count += 1
# break while loop when end of this episode
if done:
totleStepCount.append(count)
if reward == 1:
successStepCount.append(count)
elif reward == -1:
errorStepCount.append(count)
count = 0
break
# end of game
print('game over')
env.destroy()
plt.figure(1)
plt.plot(np.arange(len(totleStepCount)), totleStepCount)
plt.title('totleStepCount')
plt.figure(2)
plt.plot(np.arange(len(successStepCount)), successStepCount)
plt.title('successStepCount')
plt.figure(3)
plt.plot(np.arange(len(errorStepCount)), errorStepCount)
plt.title('errorStepCount')
plt.show()
if __name__ == "__main__":
env = Maze()
RL = QLearningTable(actions=list(range(env.n_actions)))
env.after(100, update)
env.mainloop()