-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_universe.py~
170 lines (140 loc) · 5.13 KB
/
testing_universe.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
import gym
import universe # register the universe environments
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import RMSprop
from IPython.display import clear_output
import random
import numpy as np
import pickle
from skimage.color import rgb2gray
from skimage.transform import resize
from keras.models import load_model
epochs = 100
gamma = 0.99
epsilon = 1
games = 0 #always init. to 0
done_n = [False]
DEATH_COST = -1/600
LOAD = False
PLAY_AFTER = True
BATCH_SIZE = 10
def makeMove(state, action):
mousePositions = []
for i in range(8):
mousePositions.append((100 * np.cos(2 * np.pi / 8 * i), 100 * np.sin(2 * np.pi / 8 * i)))
if action < 8:
action_n = [[('PointerEvent', mousePositions[int(action)//2][0] + 265, mousePositions[int(action)//2][1] + 235, False)]]
else:
action_n = [[('PointerEvent', mousePositions[int(action)//2][0] + 265, mousePositions[int(action)//2][1] + 235, True)]]
return env.step(action_n)
def simplify(data):
data = np.array(data)[0:530,0:470,0:3]
data = rgb2gray(data)
data = resize(data, (53, 47))
return np.array(data)
if not LOAD:
model = Sequential()
model.add(Dense(800, init='lecun_uniform', input_shape=(53*47,)))
model.add(Activation('relu'))
#model.add(Dropout(0.2)) I'm not using dropout, but maybe you wanna give it a try?
model.add(Dense(800, init='lecun_uniform'))
model.add(Activation('relu'))
#model.add(Dropout(0.2))
model.add(Dense(16, init='lecun_uniform'))
model.add(Activation('linear')) #linear output so we can have range of real-valued outputs
rms = RMSprop()
model.compile(loss='mse', optimizer=rms)\
else:
model = load_model('model250.h5')
try:
pelletsEarnedList = pickle.load(open('pelletsearned.p', 'rb'))
pelletsEarnedList.append([])
except:
pelletsEarnedList = [[]]
env = gym.make('internet.SlitherIO-v0')
env.configure(remotes=1) # automatically creates a local docker container
state = env.reset()
while games < epochs:
pelletsEarned = 0
rounds = 0 #keep track of how long snake is alive
while True: #we need to call an action to get the state to update
action_n = [[('PointerEvent', 200, 200, False)]]
state, reward_n, done_n, info = env.step(action_n)
env.render()
try:
state[0]['vision']
break
except:
pass
state = simplify(state[0]['vision'])
#game still in progress
while not done_n[0]:
f
#Let's run our Q function on S to get Q values for all possible actions
qval = model.predict(state.reshape(1,53*47), batch_size=1)
if (random.random() < epsilon): #choose random action
action = np.random.randint(0,16)
else: #choose best action from Q(s,a) values
action = (np.argmax(qval))
print(action)
expected = qval[0][action]
#Take action, observe new state S'
new_state, reward, done_n, info = makeMove(state, action)
pelletsEarned += reward[0]
if done_n[0]:
new_state = state
else:
new_state = simplify(new_state[0]['vision'])
env.render()
#Observe reward
#Get max_Q(S',a)
newQ = model.predict(new_state.reshape(1,53*47), batch_size=1)
maxQ = np.max(newQ)
y = np.zeros((1,16))
y[:] = qval[:]
if not done_n[0]: #non-terminal state
update = ((reward[0]) + (gamma * maxQ))
if action >= 8:
update -= 0.1 #Penalize for boosting
else: #terminal state
update = (DEATH_COST * rounds + (gamma * maxQ))
y[0][action] = update#target output
model.fit(state.reshape(1,53*47), y, batch_size=1, nb_epoch=1, verbose=0)
state = new_state
clear_output(wait=True)
rounds += 1
pelletsEarnedList[len(pelletsEarnedList)-1].append(pelletsEarned)
if epsilon > 0.1:
epsilon -= (1/(epochs)) #we may want to change this later
games += 1
if games % 10 == 0:
model.save('model250.h5')
pickle.dump(pelletsEarnedList, open('pelletsearned.p', 'wb'))
pickle.dump(pelletsEarnedList, open('pelletsearned.p', 'wb'))
while PLAY_AFTER:
while True: #we need to call an action to get the state to update
action_n = [[('PointerEvent', 200, 200, False)]]
state, reward_n, done_n, info = env.step(action_n)
env.render()
try:
state[0]['vision']
break
except:
pass
state = simplify(state[0]['vision'])
#while game still in progress
while(not done_n[0]):
#We are in state S
#Let's run our Q function on S to get Q values for all possible actions
qval = model.predict(state.reshape(1,53*47), batch_size=1)
action = (np.argmax(qval))
print("Action:", action, "QVAL:", qval)
new_state, reward, done_n, info = makeMove(state, action)
if done_n[0]:
new_state = state
else:
new_state = simplify(new_state[0]['vision'])
env.render()
state = new_state
clear_output(wait=True)