-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_maze_RL.py
48 lines (37 loc) · 1.41 KB
/
run_maze_RL.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
from environment.maze_env import Maze
from agent.QLearn import QLearn
from agent.Sarsa import Sarsa
import argparse
import os
def update():
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(observation)
# RL take action and get next observation and reward
observation_, reward, done = env.step(action)
# RL learn from this transition
RL.learn(observation, action, reward, observation_)
# swap observation
observation = observation_
# break while loop when end of this episode
if done:
break
RL.save_table('Q_table','Q-learning')
# end of game
print('game over')
env.destroy()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--algorithm', default='Q-learning')
args = parser.parse_args()
env = Maze()
assert args.algorithm == 'Q-learning' or args.algorithm == 'Sarsa', 'Please type Q-learning or Sarsa'
if args.algorithm == 'Q-learning': RL = QLearn(actions=list(range(env.n_actions)))
if args.algorithm == 'Sarsa': RL = Sarsa(actions=list(range(env.n_actions)))
env.after(100, update)
env.mainloop()