-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_Qlearn.py
67 lines (45 loc) · 1.77 KB
/
run_Qlearn.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
from env import Environment
from agent import Q_algorithm
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.learning(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
env.final()
RL.print_table()
RL.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 = Q_algorithm(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()