forked from amanirmk/AMISTAD-intention-exp1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpSimulate.py
More file actions
432 lines (391 loc) · 15.7 KB
/
helpSimulate.py
File metadata and controls
432 lines (391 loc) · 15.7 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import pybullet as p
import pybullet_data
import helpScript as hsc
import helpUnity as hu
import magicVariables as mv
from classPrey import *
from classPredator import *
from classTerrain import *
from classFood import *
import copy
frameCount = 0
oldLineList = []
sightLineList = []
preyList = []
predatorList = []
foodList = []
terrainWallIDs = []
objIDToObject = {} #maps object IDs to objects
targetCounts = []
data = {} # data dictionary for a single run
def printProgressBar (iteration, total, prefix = 'Progress:', suffix = 'Complete', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
def createExpInputFile(inputToVary, startValue, endValue, stepValue):
"""Inputs:
inputToVary: String, the input to vary.
eg. "predSightDistance"
startValue: the starting value of input, inclusive
endingValue: the ending value of input, inclusive
stepValue: the stepValue for input
"""
filename = "experimentInput.txt"
file = open(filename, "w")
for targetedAware in [True, False]:
toWrite = "targetedAware " + str(targetedAware) + "\n"
for proximityAware in [True, False]:
saveToWrite = toWrite
toWrite += "proximityAware " + str(proximityAware) + "\n"
if not (targetedAware and not proximityAware):
if (inputToVary == "preySightDistance"):
for preySightDistance in range(startValue, endValue+1, stepValue):
file.write(toWrite)
file.write("preySightDistance " + str(preySightDistance) + "\n\n")
elif (inputToVary == "predSightDistance"):
for predSightDistance in range(startValue, endValue+1, stepValue):
file.write(toWrite)
file.write("predSightDistance " + str(predSightDistance) + "\n\n")
elif (inputToVary == "predSightAngle"):
for predSightAngle in range(startValue, endValue+1, stepValue):
file.write(toWrite)
file.write("predSightAngle " + str(predSightAngle) + "\n\n")
elif (inputToVary == "preyPredRatio"):
for preyPredRatio in range(startValue, endValue+1, stepValue):
file.write(toWrite)
file.write("preyPredRatio " + str(preyPredRatio) + "\n\n")
elif (inputToVary == "speedFrac"):
for speedFrac in range(startValue, endValue+1, stepValue):
speedFrac /= 10
file.write(toWrite)
file.write("speedFrac " + str(speedFrac) + "\n\n")
else:
print("ERROR: Invalid inputToVary.")
toWrite = saveToWrite
file.close()
return filename
def createCautiousFile(inputToVary, startValue, endValue, stepValue):
"""Inputs:
inputToVary: String, the input to vary.
eg. "predSightDistance"
startValue: the starting value of input, inclusive
endingValue: the ending value of input, inclusive
stepValue: the stepValue for input
"""
filename = "cautiousInput.txt"
file = open(filename, "w")
for targetedAware in [True]:
toWrite = "targetedAware " + str(True) + "\n"
toWrite += "proximityAware " + str(True) + "\n"
toWrite += "cautious " + str(True) + "\n"
if (inputToVary == "preySightDistance"):
for preySightDistance in range(startValue, endValue+1, stepValue):
file.write(toWrite)
file.write("preySightDistance " + str(preySightDistance) + "\n\n")
elif (inputToVary == "predSightDistance"):
for predSightDistance in range(startValue, endValue+1, stepValue):
file.write(toWrite)
file.write("predSightDistance " + str(predSightDistance) + "\n\n")
elif (inputToVary == "predSightAngle"):
for predSightAngle in range(startValue, endValue+1, stepValue):
file.write(toWrite)
file.write("predSightAngle " + str(predSightAngle) + "\n\n")
elif (inputToVary == "preyPredRatio"):
for preyPredRatio in range(startValue, endValue+1, stepValue):
file.write(toWrite)
file.write("preyPredRatio " + str(preyPredRatio) + "\n\n")
elif (inputToVary == "speedFrac"):
for speedFrac in range(startValue, endValue+1, stepValue):
speedFrac /= 10
file.write(toWrite)
file.write("speedFrac " + str(speedFrac) + "\n\n")
else:
print("ERROR: Invalid inputToVary.")
file.close()
return filename
def createSimInput(variableTupleList, fileName="inputFile.txt"):
""" Called from command line, gives our program the parameters to run the simulation on
Inputs:
variableTupleList = [(var, [value range]), (var, [value range])]
[min, max, step]
"""
# create a file to write to with specified name
file = open(fileName, "w")
# recurse!
createSimInputHelper(file, variableTupleList, "")
file.close()
return fileName
def createSimInputHelper(file, variableTupleListIn, toWrite):
"""Recursive method, called by createSimInput
file: the file object corresponding to the file we are writing to
variableTupleListIn: the current list of variable Tuples to recurse over
tuples: key, value.
value: list, one element list if a single value. [min, max, step] if a range.
key: string, the key
toWrite: the string to write to the file at this stage in the recursion
"""
if not variableTupleListIn:
file.write(toWrite + "\n")
else:
variableTuple = variableTupleListIn.pop(0)
key = variableTuple[0]
values = variableTuple[1]
if len(values) == 1:
toWrite += (str(key) + " " + str(values[0]) + "\n")
createSimInputHelper(file, variableTupleListIn, toWrite)
else:
num = values[0]
while num <= values[1]:
copyTupleList = copy.deepcopy(variableTupleListIn)
createSimInputHelper(file, copyTupleList, toWrite + (str(key) + " " + str(num) + "\n"))
num += values[2]
def createSeedListFromFile(filename):
seedFile = open(filename, "r")
lineList = seedFile.readlines()
seedFile.close()
lineList = [x.strip("\n") for x in lineList]
lineList = lineList[:-1]
standardSeed = {
"targetedAware" : True,
"proximityAware" : True,
"cautious" : False,
"preyPredRatio" : 4,
"preySightDistance" : 10,
"predSightDistance" : 20,
"predSightAngle" : 90,
"speedFrac": 0.8
}
seedList = []
preferences = copy.deepcopy(standardSeed)
for line in lineList:
if line == "":
seedList.append(preferences)
preferences = copy.deepcopy(standardSeed)
else:
key, value = line.split()
if key in preferences:
if value == "True":
preferences[key] = True
elif value == "False":
preferences[key] = False
else:
preferences[key] = float(value)
else:
raise Exception(key + " is not a value in preferences!")
seedList.append(preferences)
return seedList
def simulateManySetups(numSimulations, maxSteps, shouldMakeScript, seedList):
allData = []
numSeeds = len(seedList)
for i in range(numSeeds):
allData.append(batchSimulate(numSimulations, maxSteps, shouldMakeScript, seedList[i], [True, i, numSeeds]))
return allData
def batchSimulate(numSimulations, maxSteps, shouldMakeScript, preferences, manySetups=[False,0,0]):
"""runs simulate many times"""
batchData = copy.deepcopy(preferences) # holds runData, as well as averages for each set of parameters
runsData = [] # holds data dictionaries for runs with a given set of parameters. (greater number of runs = greater precision)
for i in range(numSimulations):
runsData.append(simulate(maxSteps, shouldMakeScript, preferences))
if manySetups[0]:
printProgressBar((i+1) + (manySetups[1] * numSimulations), numSimulations * manySetups[2])
batchData["runsData"] = runsData
addAverages(batchData)
return batchData
def addAverages(batchData):
"""computes averages form 'runsData' and adds them to batchData dict"""
runsData = batchData["runsData"]
numRuns = len(runsData) + 0.0
batchData["avgStepCount"] = 0
batchData["avgSurvivingPrey"] = 0
for run in runsData:
batchData["avgStepCount"] += run["stepCount"]/numRuns
batchData["avgSurvivingPrey"] += run["survivingPrey"]/numRuns
def simulate(maxSteps, shouldMakeScript, preferences):
"""runs the entire simulation
Input:
steps: int, number of steps to run the simulation for
makeScript: bool, whether to generate a c# script
"""
global data
initializeData(preferences)
if preferences != {}:
mv.redefineMagicVariables(preferences)
#if it is empty, will use defaults
#won't store pref details either, so best use a seed
startSimulation()
for step in range(maxSteps):
updateSimulation()
if len(preyList) == 0:
break
endSimulation(shouldMakeScript)
return data
def initializeData(preferences):
global data
data = copy.deepcopy(preferences)
data["stepCount"] = 0
data["preyCountOverTime"] = []
data["foodPerPrey"] = []
data["preyPerPred"] = []
data["survivingPrey"] = 0
data["scriptList"] = "no script generated"
def startSimulation():
"""connect to pybullet and do initial spawns"""
#reset variables
global frameCount, preyList, predatorList, foodList, terrainWallIDs, targetCounts, objIDToObject
frameCount = 0
preyList = []
predatorList = []
foodList = []
targetCounts = []
objIDToObject = {}
hsc.script.clear()
hsc.script.append([])
hsc.maxID[0] = 0
#start simulation
p.connect(p.DIRECT)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
terrain = Terrain(mv.TERRAIN_SIZE)
spawnPrey(mv.PREY_START_COUNT)
spawnPredators(mv.PREDATOR_START_COUNT)
spawnFood(mv.FOOD_START_COUNT)
def updateSimulation():
"""update the items of the game and proceed to next frame"""
updateFood()
updatePrey()
updatePredators()
collectTargetInfo()
nextStep()
global frameCount
frameCount += 1
def endSimulation(shouldMakeScript):
"""disconnect from pybullet and make c# script if desired"""
p.disconnect()
for prey in preyList:
data["foodPerPrey"].append(prey.foodTimeStamps)
targetCounts.append(prey.targetList)
for predator in predatorList:
data["preyPerPred"].append(predator.preyTimeStamps)
data["stepCount"] = frameCount
data["survivingPrey"] = len(preyList)
data["targetInfo"] = calcTargetInfo()
if shouldMakeScript:
data["scriptList"] = copy.deepcopy(hsc.script)
hsc.makeScript()
def nextStep():
"""carries out the next step in the simulation and info to unity code
"""
p.stepSimulation()
if mv.FRAME_RATIO != 0:
if frameCount % mv.FRAME_RATIO == 0:
hsc.script.append([])
for obj in preyList + predatorList + foodList:
hu.unityUpdateObj(obj.objID, obj.pos, obj.yaw)
global oldLineList
for i in range(len(oldLineList)):
hu.destroyLine(i)
oldLineList = [line for line in sightLineList]
for i in range(len(sightLineList)):
hu.drawLine(i, sightLineList[i])
sightLineList.clear()
def spawnPrey(count):
"""spawns 'count' number of prey"""
for i in range(count):
pos = mv.PREY_SPAWN_ALG(mv.PREY_SIZE/2)
preyList.append(Prey(pos))
def spawnPredators(count):
"""spawns 'count' number of predators"""
for i in range(count):
pos = mv.PREDATOR_SPAWN_ALG(mv.PREDATOR_SIZE/2)
predatorList.append(Predator(pos))
def spawnFood(count):
"""spawns 'count' number of food"""
for i in range(count):
pos = mv.FOOD_SPAWN_ALG(mv.FOOD_SIZE/2)
foodList.append(Food(pos))
def updatePrey():
"""updates all prey in simulation"""
data["preyCountOverTime"].append(len(preyList))
for prey in preyList:
prey.updateCharacter()
def updatePredators():
"""updates all predators in simulation"""
for predator in predatorList:
predator.updateCharacter()
def updateFood():
"""spawns food if it should"""
if mv.FOOD_SPAWN_RATE != 0 and len(foodList) < mv.FOOD_MAX_COUNT:
if frameCount % mv.FOOD_SPAWN_RATE == 0:
spawnFood(1)
def create(prefab, filename, objPos, objYaw, scale=1):
"""creates an object in both pybullet and unity
input:
prefab: string
filename: string
objPos: vector (list)
objRot: quaternion (list)
scale: int, defaulted to 1
output:
objID: the ID of the created pybullet object
"""
objRot = alg.quatFromYawRad(objYaw)
if prefab == "food":
objID = p.loadURDF(filename, objPos, objRot, useFixedBase=1, globalScaling=scale)
else:
objID = p.loadURDF(filename, objPos, objRot, globalScaling=scale)
hu.unitySpawn(objID, prefab, objPos, objYaw, scale)
return objID
def destroy(objID):
"""destroys an object in both pybullet and unity
input:
objID: int
"""
p.removeBody(objID)
hu.unityDestroy(objID)
def addToLineList(line):
sightLineList.append(line)
def collectTargetInfo():
for i in range(len(predatorList)):
predator = predatorList[i]
if predator.lastTargetedPrey:
prey = objIDToObject[predator.lastTargetedPrey]
predTargetInfo = prey.targetList[i]
lastTargeted = len(predTargetInfo) - 1
predTargetInfo += [0]*(frameCount - 1 - lastTargeted) + [1]
def calcTargetInfo():
durationInfo = []
probTargetInfo = []
for preyTargetList in targetCounts:
preyTargetInfo = []
for predTargetInfo in preyTargetList:
targetDuration = 0
infoLength = len(predTargetInfo)
for i in range(infoLength):
timestep = predTargetInfo[i]
if timestep == 1:
targetDuration += 1
if i == infoLength - 1:
preyTargetInfo.append(targetDuration)
else:
if targetDuration > 0:
preyTargetInfo.append(targetDuration)
targetDuration = 0
p = len(preyTargetInfo)/frameCount
probTargetInfo.append(p)
durationInfo += preyTargetInfo
return [probTargetInfo, durationInfo]