-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainingSnake.py
113 lines (87 loc) · 2.56 KB
/
TrainingSnake.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from Environments.Snake import Env
from RLClasses import epsilonByFrame, ReplayBuffer, DQNAgent, DuelingDQNAgent, DQNHelper, DDQNHelper
env = Env()
action_size = env.action_size
obs_size = env.observation_size
rb = ReplayBuffer(Capacity=1000)
agent = DQNAgent(obs_size, action_size)
def dqnTraining():
helper = DQNHelper(agent, action_size)
max_frames = 10000
batch_size = 100
episode_reward = 0
total_rewards = []
state = env.reset()
for frame in range(max_frames):
epsilon = epsilonByFrame(frame)
action = helper.act(epsilon, state)
next_state, reward, done = env.step(action)
rb.push(state, action, reward, next_state, done)
state = next_state
episode_reward += reward
if done:
total_rewards.append(episode_reward)
episode_reward = 0
state = env.reset()
if len(rb) > batch_size:
helper.compute_loss(batch_size, rb)
else:
helper.compute_loss(len(rb), rb)
helper.plot(total_rewards)
def ddqnTraining():
target_agent = DQNAgent(obs_size, action_size)
helper = DDQNHelper(agent, target_agent, action_size)
max_frames = 10000
batch_size = 100
episode_reward = 0
total_rewards = []
state = env.reset()
for frame in range(max_frames):
epsilon = epsilonByFrame(frame)
action = helper.act(epsilon, state)
next_state, reward, done = env.step(action)
rb.push(state, action, reward, next_state, done)
state = next_state
episode_reward += reward
if done:
total_rewards.append(episode_reward)
episode_reward = 0
state = env.reset()
if len(rb) > batch_size:
helper.compute_loss(batch_size, rb)
else:
helper.compute_loss(len(rb), rb)
if frame%100 == 0:
helper.update_target_network()
helper.plot(total_rewards)
def duelingdqnTraining():
agent = DuelingDQNAgent(obs_size, action_size)
target_agent = DuelingDQNAgent(obs_size, action_size)
helper = DDQNHelper(agent, target_agent, action_size)
max_frames = 10000
batch_size = 100
episode_reward = 0
total_rewards = []
state = env.reset()
for frame in range(max_frames):
epsilon = epsilonByFrame(frame)
action = helper.act(epsilon, state)
next_state, reward, done = env.step(action)
rb.push(state, action, reward, next_state, done)
state = next_state
episode_reward += reward
if done:
total_rewards.append(episode_reward)
episode_reward = 0
state = env.reset()
if len(rb) > batch_size:
helper.compute_loss(batch_size, rb)
else:
helper.compute_loss(len(rb), rb)
if frame%100 == 0:
helper.update_target_network()
helper.plot(total_rewards)
if __name__ == "__main__":
# dqnTraining()
# ddqnTraining()
duelingdqnTraining()