-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathrun_agent.py
80 lines (59 loc) · 2.3 KB
/
run_agent.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
71
72
73
74
75
76
77
78
79
80
# File: run_agent.py
# Description: Running algorithm
# Environment: PyCharm and Anaconda environment
#
# MIT License
# Copyright (c) 2018 Valentyn N Sichkar
# github.com/sichkar-valentyn
#
# Reference to:
# Valentyn N Sichkar. Reinforcement Learning Algorithms for global path planning // GitHub platform. DOI: 10.5281/zenodo.1317899
# Importing classes
from env import Environment
from agent_brain import QLearningTable
def update():
# Resulted list for the plotting Episodes via Steps
steps = []
# Summed costs for all episodes in resulted list
all_costs = []
for episode in range(5000):
# Initial Observation
observation = env.reset()
# Updating number of Steps for each Episode
i = 0
# Updating the cost for each episode
cost = 0
while True:
# Refreshing environment
env.render()
# RL chooses action based on observation
action = RL.choose_action(str(observation))
# RL takes an action and get the next observation and reward
observation_, reward, done = env.step(action)
# RL learns from this transition and calculating the cost
cost += RL.learn(str(observation), action, reward, str(observation_))
# Swapping the observations - current and next
observation = observation_
# Calculating number of Steps in the current Episode
i += 1
# Break while loop when it is the end of current Episode
# When agent reached the goal or obstacle
if done:
steps += [i]
all_costs += [cost]
break
# Showing the final route
env.final()
# Showing the Q-table with values for each action
RL.print_q_table()
# Plotting the results
RL.plot_results(steps, all_costs)
# Commands to be implemented after running this file
if __name__ == "__main__":
# Calling for the environment
env = Environment()
# Calling for the main algorithm
RL = QLearningTable(actions=list(range(env.n_actions)))
# Running the main loop with Episodes by calling the function update()
env.after(100, update) # Or just update()
env.mainloop()