/
run.py
271 lines (186 loc) · 6.06 KB
/
run.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import random
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from Game2048 import Game
from state_manager import StateManager, State, RewardInfo
from nn import NeuralNetwork
from batch_manager import BatchManager
# small wrapper around state object, representing a suite of alike states by the rep
class Species (object):
def __init__ (self, rep):
self.rep = rep
# resets the rep for the species
def set_rep (self, rep):
self.rep = rep
# return the representative for this species
def get_rep (self):
return self.rep
# species are completely repped by self.rep, including hash and eq
def __hash__ (self):
return self.rep.__hash__ ()
def __eq__ (self, other):
return self.rep.__eq__ (other.get_rep ())
# manages job of finding species associated with various boards
class SpeciesManager (object):
num_species = 0
# if tf_nn not passed, then every unique board is a unique species
def __init__ (self, tf_nn=None):
self.species = {}
self.tf_nn = tf_nn
def find_species (self, state):
# use neural network to group states
in_ = state.get_state ()
# check state vs every known species (TODO optimize this somehow)
for species in self.species:
other_ = species.get_rep ().get_state ()
alike = self.tf_nn.compute (in_, other_)
# if state is alike, return that species as its similar enough
if alike:
return species
# return species associated with inputted state
return s
# given a state and reward info, inserts it into set
def insert (self, state, r_info):
s = self.find_species (state)
# only create new data if species not found
if not s:
self.species [s] = r_info
# given a state, output the reward info
def lookup (self, state):
# lookup state in species dict
spec = self.find_species (state)
# if not found, create a new one
if not spec:
spec = Species (state)
r = RewardInfo ()
self.species [s] = r
return spec, r
# return reward for inputted state
return spec, self.species [spec]
g = Game ()
sm = StateManager ()
scores = []
random_moves = []
informed_moves = []
dif_states = []
x_data = []
running_length = 500
averages = []
num_games = int (4 * 1e3)
print 'Playing %s games.' % num_games
while (num_games >= 0):
while (not g.is_stale ()):
# get current states reward info
s = State (g.get_state ())
# save current score
current_score = g.get_score ()
# get reward info for current state
r = sm.lookup (s)
#r = spm.lookup (s)
# get move decision
move = r.get_move ()
# make move
g.process_move (move)
# get new score
new_score = g.get_score ()
# calc score gain
gain = new_score - current_score
# update reward info
r.update (move, gain)
x_data += [len (x_data)]
scores += [g.get_score ()]
random_moves += [100.0 * RewardInfo.random_moves / RewardInfo.total_moves]
informed_moves += [100.0 * RewardInfo.informed_moves / RewardInfo.total_moves]
dif_states += [StateManager.dif_states]
if len (scores) % running_length == 0:
print 'Best score thus far: ', (np.max (scores))
print 'Running average: %s' % (sum (scores [-running_length:]) / running_length)
print 'Num species: %s' % (StateManager.dif_states)
print 'Total moves: %s' % (RewardInfo.total_moves)
print 'random moves: %s (%s)' % (RewardInfo.random_moves, 1.0 * RewardInfo.random_moves / RewardInfo.total_moves)
print 'informed moves: %s (%s)' % (RewardInfo.informed_moves, 1.0 * RewardInfo.informed_moves / RewardInfo.total_moves)
print 'games left: %s' % num_games
print ''
if len (scores) > 100:
averages += [sum (scores [-100:]) / 100]
else:
averages += [sum (scores) / len (scores)]
g = Game ()
num_games -= 1
states, rewards = sm.get_complete_pairs_prepped ()
bm = BatchManager (states, rewards)
print ''
print 'Completed states: %s' % len (states)
print 'Percent of all states: %s' % (1.0 * len (states) / StateManager.dif_states)
print ''
# train neural network
nn = NeuralNetwork ()
spm = SpeciesManager (nn)
nn.train (bm)
states, rewards = sm.get_complete_pairs ()
print states [40]
print rewards [40]
# insert pairs
for i in range (len (states)):
spm.insert (states [i], rewards [i])
scores = []
random_moves = []
informed_moves = []
dif_states = []
x_data = []
running_length = 10
averages = []
num_games = int (4 * 1e3)
print 'Playing %s games.' % num_games
while (num_games >= 0):
while (not g.is_stale ()):
# get current states reward info
s = State (g.get_state ())
# save current score
current_score = g.get_score ()
# get reward info for current state
s, r = spm.lookup (s)
# get move decision
move = r.get_move ()
print '\n' * 10
print 'Board:'
print g
print 'Species match rep:'
g_ = Game ()
g_.state = s.get_rep ().get_state ()
print g_
print 'R info: %s' % r.get_rewards ()
print 'TF jic: %s' % nn.compute (g.get_state (), g_.get_state ())
print 'Move: %s' % move
print 'New Board:'
# make move
g.process_move (move)
print g
w = raw_input ('Waiting...')
# get new score
new_score = g.get_score ()
# calc score gain
gain = new_score - current_score
# update reward info
r.update (move, gain)
x_data += [len (x_data)]
scores += [g.get_score ()]
random_moves += [100.0 * RewardInfo.random_moves / RewardInfo.total_moves]
informed_moves += [100.0 * RewardInfo.informed_moves / RewardInfo.total_moves]
dif_states += [StateManager.dif_states]
if len (scores) % running_length == 0:
print 'Best score thus far: ', (np.max (scores))
print 'Running average: %s' % (sum (scores [-running_length:]) / running_length)
print 'Num species: %s' % (SpeciesManager.num_species)
print 'Total moves: %s' % (RewardInfo.total_moves)
print 'random moves: %s (%s)' % (RewardInfo.random_moves, 1.0 * RewardInfo.random_moves / RewardInfo.total_moves)
print 'informed moves: %s (%s)' % (RewardInfo.informed_moves, 1.0 * RewardInfo.informed_moves / RewardInfo.total_moves)
print 'games left: %s' % num_games
print ''
if len (scores) > 100:
averages += [sum (scores [-100:]) / 100]
else:
averages += [sum (scores) / len (scores)]
g = Game ()
num_games -= 1