-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.py
141 lines (114 loc) · 4.26 KB
/
sim.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.optimizers import Adam
import time
from Buffer import Buffer
from bot import Botenv
from plot_trajectory import plot_trajectory
from networks import Actor, Critic
ENV_SIZE = 10
VELOCITY = 5
NUM_EPS = 1000
SHOW = False
# Discount Factor
GAMMA = 0.99
# Used to update Target Network
TAU = 0.005
EPS = 0.7
EPS_DECAY = 0.99
bot = Botenv(10, 0.01, ENV_SIZE, VELOCITY)
state_size = bot.state.shape[0]
action_size = 1
actor = Actor(state_size, action_size)
critic = Critic(state_size, action_size)
actor_target = Actor(state_size, action_size)
critic_target = Critic(state_size, action_size)
actor_target.set_weights(actor.get_weights())
critic_target.set_weights(critic.get_weights())
# Actor/Critic Learning Rates
actor_lr = 1e-4
critic_lr = 1e-3
actor_optimizer = Adam(actor_lr)
critic_optimizer = Adam(critic_lr)
buffer_size = 64
buffer_capacity = 1000000
buffer_file = None
buffer = Buffer(buffer_file, buffer_size, buffer_capacity, state_size, action_size)
def update(state_batch, next_state_batch,
action_batch, reward_batch):
# Critic Loss
with tf.GradientTape() as tape:
target_actions = actor_target(next_state_batch, training=True)
target_q = critic_target([next_state_batch, target_actions], training=True)
y = reward_batch + GAMMA * target_q
cur_q = critic([state_batch, action_batch], training=True)
critic_loss = tf.math.reduce_mean(tf.math.square(y - cur_q))
critic_grad = tape.gradient(critic_loss, critic.trainable_variables)
critic_optimizer.apply_gradients(zip(critic_grad, critic.trainable_variables))
# Actor Loss
with tf.GradientTape() as tape:
actions = actor(state_batch, training=True)
q_value = critic([state_batch, actions], training=True)
actor_loss = -tf.math.reduce_mean(q_value)
actor_grad = tape.gradient(actor_loss, actor.trainable_variables)
actor_optimizer.apply_gradients(zip(actor_grad, actor.trainable_variables))
def learn():
r = min(buffer.buffer_counter, buffer_size)
batch_indices = np.random.choice(r, buffer_size)
state_batch = tf.convert_to_tensor(buffer.state_buffer[batch_indices])
next_state_batch = tf.convert_to_tensor(buffer.next_state_buffer[batch_indices])
action_batch = tf.convert_to_tensor(buffer.action_buffer[batch_indices])
reward_batch = tf.convert_to_tensor(buffer.reward_buffer[batch_indices])
reward_batch = tf.cast(reward_batch, dtype=tf.float32)
update(state_batch, next_state_batch,
action_batch, reward_batch)
def policy(state):
sampled_actions = tf.squeeze(actor(state))
sampled_actions = sampled_actions.numpy()
legal_action = np.clip(sampled_actions, -1, 1)
return [np.squeeze(legal_action)]
# Update Target Network slowly
def update_target(target_weights, weights, tau):
for (a, b) in zip(target_weights, weights):
a.assign(b * tau + a * (1 - tau))
episode_rewards = []
action = 0
TAU = 0.005
for ep in range(NUM_EPS):
if ep%50 == 0:
trajectory = {'x': [], 'y': []}
show = True
else:
show = False
bot.reset()
score = 0
while(bot.T > 0):
cur_state = bot.state
tf_cur_state = tf.expand_dims(tf.convert_to_tensor(cur_state), 0)
if np.random.random() > EPS:
action = policy(tf_cur_state)
else:
action = [np.random.uniform(-1, 1)]
action = tf.convert_to_tensor(action)
next_state, reward, done = bot.action(action[0])
obs = (cur_state, action, next_state, reward)
buffer.record(obs)
score += reward
learn()
update_target(actor_target.variables, actor.variables, TAU)
update_target(critic_target.variables, critic.variables, TAU)
if show:
trajectory['x'].append(bot.x)
trajectory['y'].append(bot.y)
plot_trajectory(SIZE=ENV_SIZE, agent=bot,
trajectory=trajectory)
if done:
break
episode_rewards.append(score)
print(f"Score for Episode #{ep} = {score}")
plt.close()
filename = f"replay_{int(time.time())}"
buffer.save(filename)
actor.save("actor.h5")
critic.save("critic.h5")