-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerranAgent.py
442 lines (385 loc) · 16.2 KB
/
TerranAgent.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
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
433
434
435
436
437
438
439
440
441
442
import sys
import math
import time
from numpy import *
from random import randrange
from fields import PotentialFieldPlotter
from grid_filter_gl import GridFilter
import threading
from bzrc import BZRC, Command, UnexpectedResponse, Location
class Marine(threading.Thread):
def __init__(self,marine, index, commandCenter,constants, count):
threading.Thread.__init__(self)
self.me = marine
self.myIndex = index
self.commandCenter = commandCenter
self.constants = constants
self.sensorTower = SensorTower(constants, commandCenter,self.myIndex,count)
self.commands = []
self.swapTeams = 0
self.shootTime = 0
self.recalculateTime = 0
self.moveTime = 0
#self.sensorTower.visualize_potential_field()
self.frozenTime = 0
self.calculateGridTime = 0
self.new = True
print "Marine " + str(index) + " ready to go!"
def move_next(self):
self.sensorTower.move_next()
print "Marine moving to new location"
def run(self):
prev_time = time.time()
while True:
time_diff = time.time() - prev_time
self.tick(time_diff)
def tick(self, time_diff):
if self.shootTime ==0 and self.recalculateTime == 0 and self.swapTeams == 0:
self.shootTime = time_diff
self.recalculateTime = time_diff
self.swapTeams = time_diff
if self.calculateGridTime == 0:
self.calculateGridTime = time_diff
self.commands = []
oldme = self.me
self.me = self.commandCenter.get_marine(self.myIndex)
# if time_diff - self.shootTime > 1:
# self.shoot()
# self.shootTime = time_diff
if self.me.x == oldme.x and self.me.y == oldme.y:
self.frozenTime+=1
else:
self.frozenTime = 0
if self.frozenTime > 100:
print "Marine " + str(self.myIndex) + " is stuck... moving to better location!"
self.frozenTime -=1
command = Command(self.me.index, 1, math.radians(randrange(0,180)), False)
self.commandCenter.do_commands([command])
if time_diff - self.recalculateTime > .10:
self.sensorTower.recalculate()
self.recalculateTime = time_diff
if time_diff - self.swapTeams > 35:
self.swapTeams = time_diff
self.move_next()
self.sensorTower.recalculate()
if time_diff - self.calculateGridTime > .1:
self.hitGrid()
self.calculateGridTime = time_diff
self.move_by_potential_field()
results = self.commandCenter.do_commands(self.commands)
def shoot(self):
command = Command(self.me.index, 0, 0,True)
self.commands.append(command)
def move_by_potential_field(self):
flag = self.me.flag != "-" and self.me.flag != self.constants["team"]
deltaX,deltaY = self.sensorTower.calculate_potential_field_value(self.me.x,self.me.y,flag)
velocity = math.sqrt(math.pow(deltaX,2) + math.pow(deltaY,2))
if velocity > 1:
velocity = 1
elif velocity < -1:
velocity = -1
angle = math.atan2(deltaY, deltaX)
angle = self.normalize_angle(angle - self.me.angle)
if math.fabs(angle) < .4:
angle = angle/2
if math.fabs(angle) < .1:
angle = 0
command = Command(self.me.index, 1, angle, False)
self.commands.append(command)
def normalize_angle(self, angle):
"""Make any angle be between +/- pi."""
angle -= 2 * math.pi * int (angle / (2 * math.pi))
if angle <= -math.pi:
angle += 2 * math.pi
elif angle > math.pi:
angle -= 2 * math.pi
return angle
def hitGrid(self):
if self.me.status == 'dead':
return
occGridPos, occGrid = self.commandCenter.get_occgrid(self.myIndex)
for i in range(0,len(occGrid)):
for j in range(0,len(occGrid[0])):
self.commandCenter.grid.hitSquare(occGrid[i][j] == 1, occGridPos[0] + i + 400,occGridPos[1] + j + 400)
self.commandCenter.gridPlotter.update_grid(self.commandCenter.grid.filterGrid)
self.commandCenter.gridPlotter.draw_grid()
class CommandCenter(object):
"""Class handles all command and control logic for a teams tanks."""
def __init__(self, bzrc, foodSupply, gridPlotter):
self.bzrc = bzrc
self.constants = self.bzrc.get_constants()
self.bases = self.bzrc.get_bases()
self.foodSupply = int(foodSupply)
self.army = []
self.prev_time = 0
self.lock = threading.Lock()
self.mytanks,self.othertanks,self.flags,self.shots = self.bzrc.get_lots_o_stuff()
self.grid = Grid(self.constants["worldsize"],float(self.constants['truepositive']),float(self.constants['truenegative']))
self.gridPlotter = gridPlotter
print "Command Center Beginning Attack"
print "Target firing on Location -350, -350"
print "Deploying " + str(min(self.foodSupply,len(self.mytanks))) + " marines"
for i in range(0,min(self.foodSupply,len(self.mytanks))):
marine = Marine(self.mytanks[i],i,self,self.constants,self.foodSupply)
marine.start()
self.army.append(marine)
self.prev_time1 = time.time()
def get_occgrid(self,index):
return self.use_bzrc('get_occgrid',index)
def get_marine(self, index):
return self.mytanks[index]
def get_teams(self):
return self.teams
def tick(self):
"""Some time has passed; decide what to do next."""
self.time_diff = time.time() - self.prev_time1
if self.prev_time == 0:
self.prev_time = self.time_diff
if self.time_diff - self.prev_time > .01:
self.prev_time = self.time_diff
self.mytanks,self.othertanks,self.flags,self.shots = self.use_bzrc('get_lots_o_stuff',None)
def use_bzrc(self,command,commands):
self.lock.acquire()
try:
if command == 'get_lots_o_stuff':
return self.bzrc.get_lots_o_stuff()
elif command == 'do_commands':
try:
if not commands == None and len(commands) > 0:
results = self.bzrc.do_commands(commands)
except UnexpectedResponse:
print "error"
elif command == 'get_occgrid':
return self.bzrc.get_occgrid(commands)
finally:
self.lock.release()
def do_commands(self,commands):
# self.commands.append(commands)
# if len(self.commands) > 10:
self.use_bzrc('do_commands',commands)
def get_team_index(self):
return self.index
def get_bases(self):
return self.bases
def get_obstacles(self):
return self.obstacles
def get_lots_o_stuff(self):
return self.mytanks,self.othertanks,self.flags, self.shots
class SensorTower(object):
"""Class handles all potential field logic for an agent"""
def __init__(self, constants, commandCenter,index,count):
self.constants = constants
self.commandCenter = commandCenter
self.bases = self.commandCenter.get_bases()
self.static_repulsive_field = []
self.plotter = PotentialFieldPlotter()
self.points = [(-350,-350),(-350,350),(350,350),(350,-350),(0,-350),(0,350),(0,0),(350,0),(-350,0),(-350,-350),(350,350),(-350,350),(350,-350)]
self.index = randrange(0,len(self.points)-1)
self.location = [-350,-350]
self.move = 50
self.count = count
self.m_index = index
self.gather = True
self.moveHorizontal = True
self.moveVertical = True
self.tick = 1
self.initialize_fields()
def initialize_fields(self):
self.recalculate()
def move_next(self):
if self.gather and self.moveHorizontal:
self.location[1] = self.location[1] + self.move * self.m_index
self.gather = False
elif self.moveHorizontal:
tempLoc = self.location
if self.tick % 2 != 0:
if self.location[0] < 0:
tempLoc[0] = self.location[0] + 700
else:
tempLoc[0] = self.location[0] - 700
else:
tempLoc[1] = self.location[1] + self.move*(self.count)/2
if abs(tempLoc[0]) > 400 or abs(tempLoc[1]) > 400:
self.location = [350,350]
self.moveHorizontal = False
self.gather = True
self.tick = 1
else:
self.location = tempLoc
self.tick+=1
elif self.gather and self.moveVertical:
self.location[0] = self.location[0] - self.move * self.m_index
self.gather = False
elif self.moveVertical:
tempLoc = self.location
if self.tick % 2 != 0:
if self.location[1] < 0:
tempLoc[1] = self.location[1] + 700
else:
tempLoc[1] = self.location[1] - 700
else:
tempLoc[0] = self.location[0] + self.move*(self.count)/2
if abs(tempLoc[0]) > 400 or abs(tempLoc[1]) > 400:
self.location = [-350,-350]
self.gather = True
self.moveHorizontal = True
self.moveVertical = True
self.tick = 1
else:
self.location = tempLoc
self.tick+=1
print "Marine Patrolling to location " + str(self.location[0]) + " " + str(self.location[1])
# self.index+=1;
# if(self.index == len(self.points)):
# print "Completed"
# self.index = 0
def recalculate(self):
self.dynamic_repulsive_field = []
self.enemy_flag = Location()
self.enemy_flag.middle_x = self.location[0]
self.enemy_flag.middle_y = self.location[1]
self.enemy_flag.radius = 25
self.enemy_flag.size = 50
self.enemy_flag.weight = 1000
def calculate_potential_field_value(self, x, y, flag):
sumDeltaX = 0
sumDeltaY = 0
attractive_field = []
attractive_field.append(self.enemy_flag)
for item in attractive_field:
goalX = item.middle_x
goalY = item.middle_y
goalRadius = item.radius
goalSize = item.size
goalWeight = item.weight
distance = math.sqrt(math.pow((goalX - x),2) + math.pow((goalY - y),2))
angle = math.atan2(goalY-y, goalX-x)
deltaX,deltaY = self.positive_potential_field_values(distance,goalRadius, angle, goalSize, goalWeight)
sumDeltaX += deltaX
sumDeltaY += deltaY
return sumDeltaX, sumDeltaY
def calculate_full_potential_field(self):
arr = []
for i in range(-int(self.constants["worldsize"])/2, int(self.constants["worldsize"])/2):
x = []
for j in range(-int(self.constants["worldsize"])/2, int(self.constants["worldsize"])/2):
deltaX,deltaY,angle = self.calculate_potential_field_value(i,j,False)
x.append(PotentialFieldValue(deltaX, deltaY, angle))
arr.append(x)
return arr
def positive_potential_field_values(self,distance,radius,angle,size,weight):
if distance < radius:
deltaX = 0
deltaY = 0
elif distance >= radius and distance <= size + radius:
deltaX = weight*(distance - radius)*math.cos(angle)
deltaY = weight*(distance - radius)*math.sin(angle)
else:
deltaX = weight*(size)*math.cos(angle)
deltaY = weight*(size)*math.sin(angle)
return deltaX, deltaY
def negative_potential_field_values(self,distance,radius,angle,size,weight):
if distance < radius:
deltaX = -float(1e3000)
deltaY = -float(1e3000)
elif distance >= radius and distance <= size + radius:
deltaX = -weight*(distance - radius + size)*math.cos(angle)
deltaY = -weight*(distance - radius + size)*math.sin(angle)
else:
deltaX = 0
deltaY = 0
return deltaX, deltaY
def negative_tangential_field_values(self,distance,radius,angle,size,weight):
angle = angle + math.radians(90)
if distance < radius:
deltaX = -float(1e3000)
deltaY = -float(1e3000)
elif distance >= radius and distance <= size + radius:
deltaX = -weight*(distance - radius + size)*math.cos(angle)
deltaY = -weight*(distance - radius + size)*math.sin(angle)
else:
deltaX = 0
deltaY = 0
return deltaX, deltaY
def visualize_potential_field(self):
print "Visualize it"
self.plotter.plot(self.calculate_potential_field_value,self.obstacles)
class PotentialFieldValue(object):
def __init__(self,deltaX,deltaY,angle):
self.deltaX = deltaX
self.deltaY = deltaY
self.angle = angle
def __str__(self):
return "(" + str(self.deltaX) + "," + str(self.deltaY) + "," + str(self.angle) + ")"
def __repr__(self):
return "(" + str(self.deltaX) + "," + str(self.deltaY) + "," + str(self.angle) + ")"
class Grid:
"""Class handles storing and manipulating the occupancy grid"""
def __init__(self, worldsize,true,false):
self.grid = []
self.true = true
self.false = false
self.filterGrid = zeros((int(worldsize),int(worldsize)))
self.print_times = 1
self.filterGrid.fill(.15)
for i in range(0,int(worldsize)):
self.grid.append([])
for j in range(0,int(worldsize)):
self.grid[i].append(OccupancySquare(self.true,self.false))
print self.grid[0][0].true,self.grid[0][0].truefalse,self.grid[0][0].false,self.grid[0][0].falsetrue
def hitSquare(self, isOccupied, x, y):
if len(self.grid) != 0:
#print str(x) + " " + str(y) + " " +str(isOccupied)
self.grid[y][x].hitSquare(isOccupied)
self.filterGrid[y][x] = self.grid[y][x].probabilityOfOccupied
if self.print_times % 1000000 == 1:
print self.filterGrid[y][x], x, y
self.print_times+=1
# if self.grid[y][x].probabilityOfOccupied > .7:
# self.filterGrid[y][x] = 1
# else:
# self.filterGrid[y][x] = 0
class OccupancySquare:
"""Class handles a single square in the occupancy grid"""
def __init__(self,true,false):
self.true = true
self.false = false
self.truefalse = 1 - true
self.falsetrue = 1 - false
"""Start with it having a probability of 0, then as hits happen,
calculate the new probability"""
self.probabilityOfOccupied = 0.15
def hitSquare(self, isOccupied):
if isOccupied:
self.probabilityOfOccupied = (self.true * self.probabilityOfOccupied) / ((self.true * self.probabilityOfOccupied) + (self.truefalse * (1 - self.probabilityOfOccupied)))
else:
self.probabilityOfOccupied = (self.truefalse * self.probabilityOfOccupied) / ((self.truefalse * self.probabilityOfOccupied) + (self.false * (1 - self.probabilityOfOccupied)))
def main():
# Process CLI arguments.
try:
execname, host, port, foodSupply = sys.argv
except ValueError:
execname = sys.argv[0]
print >>sys.stderr, '%s: incorrect number of arguments' % execname
print >>sys.stderr, 'usage: %s hostname port' % sys.argv[0]
sys.exit(-1)
# Connect.
#bzrc = BZRC(host, int(port), debug=True)
bzrc = BZRC(host, int(port))
gridPlotter = GridFilter()
cc = CommandCenter(bzrc, foodSupply,gridPlotter)
gridPlotter.init_window(800, 800,cc.tick)
prev_time = time.time()
# Run the agent
try:
while True:
time_diff = time.time() - prev_time
action = threading.Thread(target =cc.tick, args=[time_diff])
action.start()
action.join()
except KeyboardInterrupt:
print "Exiting due to keyboard interrupt."
bzrc.close()
if __name__ == '__main__':
main()
# vim: et sw=4 sts=4