-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
148 lines (92 loc) · 3.5 KB
/
bot.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
142
143
144
145
146
147
148
import threes
import pygame
import time
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
model = tf.keras.Sequential([
tf.keras.layers.Dense(units = 182, activation = None),
tf.keras.layers.Dense(units = 32, activation = 'relu'),
tf.keras.layers.Dense(units = 4, activation= 'relu')
])
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
class memory():
def __init__(self):
self.clear()
def clear(self):
self.observations = []
self.actions = []
self.rewards = []
def update(self, obs, action, reward):
self.observations.append(obs)
self.actions.append(action)
self.rewards.append(reward)
def normalize(x):
x = x.astype(np.float32)
x -= np.mean(x)
x /= np.std(x)
return x.astype(np.float32)
def discount_rewards(rewards):
discounted_rewards = np.zeros_like(rewards)
R = 0
gamma = 0.8
for i in reversed(range(len(rewards))):
R = R*gamma + rewards[i]
discounted_rewards[i] = R
return normalize(discounted_rewards)
def choose_action(model, observation):
logits = model(obs.astype(np.float32))
action_tensor = tf.random.categorical(logits, num_samples = 1)
action_number = action_tensor[0].numpy()[0]
if action_number == 0:
action = 'left'
elif action_number == 1:
action = 'right'
elif action_number == 2:
action = 'up'
elif action_number == 3:
action = 'down'
return action, action_number
def compute_loss(observations, actions, discounted_rewards):
logits = model(observations.astype(np.float32))
neg_logprop = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits = logits, labels = actions)
loss = tf.reduce_mean(neg_logprop * discounted_rewards)
return loss
def train(model, optimizer, obs, actions, discounted_rewards):
with tf.GradientTape() as tape:
loss = compute_loss(obs, actions, discounted_rewards)
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
epochs = 30000
max_N = 1000
point_list = []
rewars_list = []
for j in tqdm(range(epochs)):
board_memory = memory()
board = threes.board(600, 300, 4)
i = 0
while True:
obs = board.create_observation()
obs = obs / (3*2 ** 4)
obs = np.expand_dims(obs, axis = 0)
action, action_number = choose_action(model, obs)
board.input(action)
reward = board.reward
board_memory.update(obs, action_number, reward)
i +=1
if i == max_N:
board.running = False
if board.running == False:
total_reward = np.sum(board_memory.rewards)
discounted = discount_rewards(board_memory.rewards)
train(model, optimizer, np.vstack(board_memory.observations), np.array(board_memory.actions), discounted)
point_list.append(board.points)
rewars_list.append(total_reward)
break
pygame.quit()
random_data = np.loadtxt('random_points.dat')
plt.plot(np.linspace(0, epochs, epochs), np.mean(random_data)*np.ones(epochs))
plt.plot(np.convolve(np.array(point_list), np.ones(1000)/1000, mode='valid'))
plt.show()