-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathextras.py
276 lines (216 loc) · 9.64 KB
/
extras.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
272
273
274
275
276
import multiprocessing as mp
import os
import time
from multiprocessing import set_start_method
import inspect
import math
import gym
import numpy as np
from tpg.trainer import Trainer
from tpg.utils import getLearners, getTeams, learnerInstructionStats, actionInstructionStats, pathDepths
"""
Transform visual input from ALE to flat vector.
inState should be made int32 before passing in.
"""
def getStateALE(inState):
# each row is all 1 color
rgbRows = np.reshape(inState,(len(inState[0])*len(inState), 3)).T
# add each with appropriate shifting
# get RRRRRRRR GGGGGGGG BBBBBBBB
return np.add(np.left_shift(rgbRows[0], 16),
np.add(np.left_shift(rgbRows[1], 8), rgbRows[2]))
"""
Run each agent in this method for parallization.
See example in tpg_examples.ipynb.
Args:
args: (TpgAgent, envName, scoreList, numEpisodes, numFrames)
"""
def runAgentParallel(args):
try:
agent = args[0] # the agent
envName = args[1] # name of OpenAI environment
scoreList = args[2] # track scores of all agents
numEpisodes = args[3] # number of times to repeat game
numFrames = args[4] # frames to play for
nRandFrames = args[5]
do_real = args[6]
agent.configFunctionsSelf()
# skip if task already done by agent
if agent.taskDone(envName):
print('Agent #' + str(agent.agentNum) + ' can skip.')
scoreList.append((agent.team.id, agent.team.outcomes))
return
env = gym.make(envName)
valActs = range(env.action_space.n) # valid actions, some envs are less
acts = env.action_space.n
scoreTotal = 0 # score accumulates over all episodes
if do_real:
for ep in range(numEpisodes): # episode loop
state = env.reset()
scoreEp = 0
for i in range(numFrames): # frame loop
if i < nRandFrames:
env.step(env.action_space.sample())
continue
act = agent.act(getStateALE(np.array(state, dtype=np.int32)))
act = int(math.floor(act[1]) % acts)
#print(act)
# feedback from env
state, reward, isDone, debug = env.step(act)
scoreEp += reward # accumulate reward in score
if isDone:
break # end early if losing state
print('Agent #' + str(agent.agentNum) +
' | Ep #' + str(ep) + ' | Score: ' + str(scoreEp))
scoreTotal += scoreEp
else:
for ep in range(numEpisodes): # episode loop
state = env.reset()
scoreEp = 0
for i in range(numFrames): # frame loop
if i < nRandFrames:
env.step(env.action_space.sample())
continue
act = agent.act(getStateALE(np.array(state, dtype=np.int32)))
# feedback from env
state, reward, isDone, debug = env.step(act)
scoreEp += reward # accumulate reward in score
if isDone:
break # end early if losing state
#print('Agent #' + str(agent.agentNum) +
#' | Ep #' + str(ep) + ' | Score: ' + str(scoreEp))
scoreTotal += scoreEp
scoreTotal /= numEpisodes
env.close()
agent.reward(scoreTotal, envName)
scoreList.append((agent.team.id, agent.team.outcomes))
except Exception as playException:
print("Exception occured while Agent {} was playing {}".format(args[0].agentNum, args[1] ))
raise playException
"""
Uses the runAgentParallel function to run a whole population of TPG agents
for however many generations on the supplied environmental parameters.
On an OpenAI gym environment.
"""
def runPopulationParallel(envName="Boxing-v0", gens=1000, popSize=360, reps=3,
frames=18000, processes=4, nRandFrames=30, rootBasedPop=True,
memType=None, operationSet="full", rampancy=(5,5,5), traversal="team",
do_real=False):
tStart = time.time()
'''
Python really is something special... sometimes it just deadlocks...¯\_(ツ)_/¯
https://pythonspeed.com/articles/python-multiprocessing/
'''
set_start_method("spawn")
print("creating atari environment")
# get num actions
env = gym.make(envName)
acts = env.action_space.n
del env
print("creating trainer")
if do_real:
trainer = Trainer(actions=[1,1], teamPopSize=popSize, rootBasedPop=rootBasedPop,
memType=memType, operationSet=operationSet, rampancy=rampancy,
traversal=traversal)
else:
trainer = Trainer(actions=acts, teamPopSize=popSize, rootBasedPop=rootBasedPop,
memType=memType, operationSet=operationSet, rampancy=rampancy,
traversal=traversal)
trainer.configFunctions()
#print(1/0)
man = mp.Manager()
pool = mp.Pool(processes=processes, maxtasksperchild=1)
allScores = [] # track all scores each generation
print("running generations")
for gen in range(gens): # do generations of training
print("doing generation {}".format(gen))
scoreList = man.list()
agents = trainer.getAgents() # swap out agents only at start of generation
agent = agents[0]
try:
# run the agents
pool.map(runAgentParallel,
[(agent, envName, scoreList, reps, frames, nRandFrames, do_real)
for agent in agents]
)
except Exception as mpException:
print("Exception occured while running multiprocessing via pool.map!")
print(mpException)
raise mpException
# prepare population for next gen
print("Applying gen {} scores to agents".format(gen))
teams = trainer.applyScores(scoreList)
print("Getting champion")
champ = trainer.getAgents(sortTasks=[envName])[0].team
print("Evolving population")
trainer.evolve(tasks=[envName]) # go into next gen
# track stats
scoreStats = trainer.fitnessStats
allScores.append((scoreStats['min'], scoreStats['max'], scoreStats['average']))
#print('Time Taken (Hours): ' + str((time.time() - tStart)/3600))
#print('Gen: ' + str(gen))
#print('Results so far: ' + str(allScores))
print("teams: {}, rTeams: {}, learners: {}, Champ Teams: {}, Champ Learners: {}, Champ Instructions: {}."
.format(len(trainer.teams), len(trainer.rootTeams), len(trainer.learners),
len(getTeams(champ)), len(getLearners(champ)), learnerInstructionStats(getLearners(champ), trainer.operations)))
#print(actionInstructionStats(getLearners(champ), trainer.operations))
#print(1/0)
print(f"Gen: {gen}, Best Score: {scoreStats['max']}, Avg Score: {scoreStats['average']}, Time: {str((time.time() - tStart)/3600)}")
print(pathDepths(champ))
print('Time Taken (Hours): ' + str((time.time() - tStart)/3600))
print('Results:\nMin, Max, Avg')
for score in allScores:
print(score[0],score[1],score[2])
return trainer, allScores[-1]
def runPopulation(envName="Boxing-v0", gens=1000, popSize=360, reps=3,
frames=18000, nRandFrames=30):
# get num actions
env = gym.make(envName)
acts = env.action_space.n
trainer = Trainer(actions=acts, teamPopSize=popSize, memType=None,
operationSet="full", rampancy=(5,5,10), traversal="learner")
tStart = time.time()
allScores = [] # track scores per gen
for gen in range(gens): # do generations of training
agents = trainer.getAgents()
while True: # loop through agents of current generation
if len(agents) == 0:
break
agent = agents.pop()
team = agent.team
#print("in runPopulation!")
#print(inspect.getsource(team.act))
#print(1/0)
if agent.taskDone(envName):
continue
score = 0
for i in range(reps): # repetitions of game
state = env.reset()
for j in range(frames): # frames of game
# start random for stochasticity
if j < nRandFrames:
state, reward, isDone, debug = env.step(env.action_space.sample())
continue
act = agent.act(getStateALE(np.array(state, dtype=np.int32)))
state, reward, isDone, debug = env.step(act)
score += reward # accumulate reward in score
if isDone:
break # end early if losing state
agent.reward(score/reps, envName)
print('Agent #' + str(agent.agentNum) +
' | Score: ' + str(score/reps))
# current generation done
trainer.evolve(tasks=[envName])
# track stats
scoreStats = trainer.fitnessStats
allScores.append((scoreStats['min'], scoreStats['max'], scoreStats['average']))
print('Time Taken (Hours): ' + str((time.time() - tStart)/3600))
print('Gen: ' + str(gen))
print('Results so far: ' + str(allScores))
print('Time Taken (Hours): ' + str((time.time() - tStart)/3600))
print('Results:\nMin, Max, Avg')
for score in allScores:
print(score[0],score[1],score[2])
return trainer, allScores[-1]
if __name__ == "__main__":
runPopulationParallel(envName="BankHeist-v0", do_real=False, popSize=360, memType=None, gens=1000, reps=5, processes=23)