def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots # self.enemies = [tank for tank in othertanks if tank.color != # self.constants['team']] self.ticks_passed += 1 self.ticks_since_turn += 1 update = False turn = False if self.ticks_passed >= 15: self.ticks_passed = 0 update = True if self.ticks_since_turn >= 100: self.ticks_since_turn = 0 turn = True self.commands = [] for tank in mytanks: self.execute_dumb_strategy(tank, False, turn, self.start_tank_angles.get(tank.index,0)) if (update): pos, occ_grid = self.bzrc.get_occgrid(tank.index) self.update_grid(pos, occ_grid) if update: gl.update_grid(self.grid) gl.draw_grid() results = self.bzrc.do_commands(self.commands)
def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.flags = flags self.shots = shots self.enemies = [ tank for tank in othertanks if tank.color != self.constants['team'] ] self.commands = [] for idx, tank in enumerate(mytanks): field = self.mytanks[idx].field role = self.mytanks[idx].role self.mytanks[idx] = tank self.mytanks[idx].role = role self.mytanks[idx].field = field # get the occgrid for only alive tanks if tank.status == self.constants['tankalive']: self.recalculate_grid(idx) if tank.status == self.constants['tankdead']: print 'tankdead' self.mytanks[idx].role = None self.mytanks[idx].field = None else: if self.mytanks[idx].role == None: self.assign_role(idx) self.mytanks[idx].role(self.mytanks[idx]) results = self.bzrc.do_commands(self.commands) update_grid(self.grid) draw_grid()
def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.flags = flags self.shots = shots self.enemies = [tank for tank in othertanks if tank.color != self.constants['team']] self.commands = [] for idx, tank in enumerate(mytanks): field = self.mytanks[idx].field role = self.mytanks[idx].role self.mytanks[idx] = tank self.mytanks[idx].role = role self.mytanks[idx].field = field # get the occgrid for only alive tanks if tank.status == self.constants['tankalive']: self.recalculate_grid(idx) if tank.status == self.constants['tankdead']: print 'tankdead' self.mytanks[idx].role = None self.mytanks[idx].field = None else: if self.mytanks[idx].role == None: self.assign_role(idx) self.mytanks[idx].role(self.mytanks[idx]) results = self.bzrc.do_commands(self.commands) update_grid(self.grid) draw_grid()
def update(self, tankNum): mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks[:1] self.flags = flags # print self.bzrc.get_occgrid(tankNum) try: sensorData = zip(*self.bzrc.get_occgrid(tankNum)[1]) except: print "error" start_x, start_y = self.bzrc.get_occgrid(tankNum)[0] # print "row length: "+str(len(self.bzrc.get_occgrid(0)[1][0])-1) # 99 # print "col length: "+str(len(self.bzrc.get_occgrid(0)[1])-1) # 99 xStart = start_x + int(self.constants['worldsize']) / 2 yStart = start_y + int(self.constants['worldsize']) / 2 try: # for row in range(len(self.bzrc.get_occgrid(0)[1][0])): # for point in range(len(self.bzrc.get_occgrid(0)[1])): y = 0 x = 0 for row in sensorData: for point in row: try: if self.chosenX == 0 and self.chosenY == 0: self.chosenY = yStart + y self.chosenX = xStart + x # Apply Bayes Rule on each point and get the new prior. prior = self.grid[yStart + y][xStart + x] # point=0 if point == 1: posterior = self.trueP * prior / ( self.trueP * prior + (1 - self.trueN) * (1 - prior)) # print prior else: posterior = (1 - self.trueP) * prior / ( (1 - self.trueP) * prior + self.trueN * (1 - prior)) self.grid[yStart + y][xStart + x] = posterior # print str(self.chosenY)+" "+str(self.chosenX) # if xStart+x==self.chosenX and yStart+y==self.chosenY: # print "posterior: "+str(posterior) # if point==1: # print "("+str(self.trueP)+"*"+str(prior)+"/("+str(self.trueP)+"*"+str(prior)+"(1-"+str(self.trueN)+")*(1-"+str(prior)+"))" # # print prior # print "posterior: "+str(self.grid[self.chosenY][self.chosenX]) x += 1 except IndexError: continue y += 1 x = 0 except ValueError: print "error" pass # try: # self.grid[yStart : yStart+len(self.bzrc.get_occgrid(0)[1][0]), xStart : xStart+len(self.bzrc.get_occgrid(0)[1])] = sensorData # except ValueError: # pass grid_filter_gl.update_grid(self.grid) grid_filter_gl.draw_grid()
def update(self, tankNum): mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks[:1] self.flags = flags # print self.bzrc.get_occgrid(tankNum) try: sensorData=zip(*self.bzrc.get_occgrid(tankNum)[1]) except: print "error" start_x, start_y = self.bzrc.get_occgrid(tankNum)[0] # print "row length: "+str(len(self.bzrc.get_occgrid(0)[1][0])-1) # 99 # print "col length: "+str(len(self.bzrc.get_occgrid(0)[1])-1) # 99 xStart=start_x+int(self.constants['worldsize'])/2 yStart=start_y+int(self.constants['worldsize'])/2 try: # for row in range(len(self.bzrc.get_occgrid(0)[1][0])): # for point in range(len(self.bzrc.get_occgrid(0)[1])): y=0 x=0 for row in sensorData: for point in row: try: if self.chosenX==0 and self.chosenY==0: self.chosenY=yStart+y self.chosenX=xStart+x # Apply Bayes Rule on each point and get the new prior. prior=self.grid[yStart+y][xStart+x] # point=0 if point==1: posterior=self.trueP*prior/(self.trueP*prior + (1-self.trueN)*(1-prior)) # print prior else: posterior=(1-self.trueP)*prior/((1-self.trueP)*prior + self.trueN*(1-prior)) self.grid[yStart+y][xStart+x]=posterior # print str(self.chosenY)+" "+str(self.chosenX) # if xStart+x==self.chosenX and yStart+y==self.chosenY: # print "posterior: "+str(posterior) # if point==1: # print "("+str(self.trueP)+"*"+str(prior)+"/("+str(self.trueP)+"*"+str(prior)+"(1-"+str(self.trueN)+")*(1-"+str(prior)+"))" # # print prior # print "posterior: "+str(self.grid[self.chosenY][self.chosenX]) x+=1 except IndexError: continue y+=1 x=0 except ValueError: print "error" pass # try: # self.grid[yStart : yStart+len(self.bzrc.get_occgrid(0)[1][0]), xStart : xStart+len(self.bzrc.get_occgrid(0)[1])] = sensorData # except ValueError: # pass grid_filter_gl.update_grid(self.grid) grid_filter_gl.draw_grid()
def tick(self, time_diff): """Some time has passed; decide what to do next.""" mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff() self.mytanks = mytanks self.othertanks = othertanks self.flags = flags self.shots = shots self.enemies = [tank for tank in othertanks if tank.color != self.constants['team']] self.commands = [] self.iterations += 1 print "Iteration %d" % self.iterations for tank in mytanks: # get the occgrid for that tank occgrid = self.bzrc.get_occgrid(tank.index) starting_point = occgrid[0] occgrid = occgrid[1] # so now we have a grid that starts at starting_point and goes up and right. The first point is the starting point, and then each point moves up the world for i in range(len(occgrid)): for j in range(len(occgrid[i])): observation = occgrid[i][j] # so we have our observation. Let's update the probabilities prior_probability = self.get_occupancy_grid_at_point(starting_point[0] + i, starting_point[1] + j) # p(observation = occupied) = p(observation = occupied | state = occupied) * p(state = occupied) + p(observation = occupied | state = unoccupied) * p(state = unnocupied) # so the observation probability is just the true positive rate times the prior plus the false positive rate times 1 - the prior if observation: observation_probability = self.constants["truepositive"] * prior_probability + self.constants["falsepositive"] * (1 - prior_probability) else: observation_probability = self.constants["truenegative"] * (1 - prior_probability) + self.constants["falsenegative"] * prior_probability # the likelihood just depends on what the observation actually was # if the observation is occupied, we want p(observation = occupied | state = occupied), or the true positive rate # if the observation is unoccupied, we want p(observation = unoccupied | state = occupied), or the false negative rate likelihood = self.constants["truepositive"] if observation else self.constants["falsenegative"] # p(state = occupied | observation = occupied) = p(observation = occupied | state = occupied) * p(state = occupied) / p(observation = occupied) # p(state = occupied | observation = unnoccupied) = p(observation = unoccupied | state = occupied) * p(satet = occupied) / p(observation = unoccupied) new_probability = likelihood * prior_probability / observation_probability # and finally, update the probability at that grid location self.update_occupancy_grid_at_point(starting_point[0] + i, starting_point[1] + j, new_probability) if self.iterations % 5 == 0: grid_filter_gl.update_grid(numpy.array(self.grid)) grid_filter_gl.draw_grid() if self.done_exploring(): sys.exit() # user potential fields to explore self.potential_fields = {} def attractive_fields_func(x, y, res): # determine how far from point to flags # calculate attractive fields to closest flag alpha = 10 closest_flag = None distance = float("inf") # find closest flag self.flags = [] # found_unexplored_point = False # for i in range(len(self.grid)): # for j in range(len(self.grid[i])): # if self.grid[i][j] > 0.2 and self.grid[i][j] < 0.8: # found_unexplored_point = True # closest_flag = (i - 400, j - 400) # distance = math.sqrt((i - 400 - x)**2 + (j - 400 - y)**2) # print "Moving to point (%d, %d)" % closest_flag # break # if found_unexplored_point: # break # for i in range(len(self.grid)): # self.flags = self.flags + [(i - 400 + (i * 20), j - 400 + (j * 20)) for j in range(len(self.grid[i])) if self.grid[i][j] > 0.2 and self.grid[i][j] < 0.8] # found_unexplored_point = False # for flag in self.flags: # distance_to_flag = math.sqrt((flag[0] - x)**2 + (flag[1] - y)**2) # if distance_to_flag < distance: # distance = distance_to_flag # closest_flag = flag if self.iterations % 100 == 0: # let it move toward the new point for two full iterations print "Selecting using random region." closest_flag = self.get_random_region() distance = math.sqrt((x - closest_flag[0])**2 + (y - closest_flag[1])**2) elif self.iterations % 50 == 0: print "Selecting using random unsearched region." closest_flag = self.get_random_unsearched_region() distance = math.sqrt((x - closest_flag[0])**2 + (y - closest_flag[1])**2) else : closest_flag, distance = self.get_nearest_unsearched_region_to_point(x, y) closest_flag = (closest_flag[0] - 400, closest_flag[1] - 400) print "Moving toward point (%d, %d)" % closest_flag # calculate angle between closest_flag and tank angle = math.atan2(closest_flag[1] - y, closest_flag[0] - x) # calculate dx and dy based off of distance and angle if distance < FLAGRADIUS: return 0,0 elif distance < FLAGRADIUS + FLAGSPREAD: return alpha * (distance - FLAGRADIUS) * math.cos(angle), alpha * (distance - FLAGRADIUS) * math.sin(angle) else: return alpha * FLAGSPREAD * math.cos(angle), alpha * FLAGSPREAD * math.sin(angle) def repulsive_fields_func(x, y, res): alpha = 10 INFINITY = 1000000 potential_fields = [] for i in range(len(self.grid)): #row for j in range(len(self.grid[i])): #point if self.grid[i][j] > .8: xavg = i - 400 yavg = j - 400 # for corner in obstacle: # xavg += corner[0] # yavg += corner[1] # xavg = xavg / len(obstacle) # yavg = yavg / len(obstacle) radius = 10 #todo frob this later distance = math.sqrt((xavg - x)**2 + (yavg - y)**2) #tank distance from center angle = math.atan2(yavg - y, xavg - x) if distance < radius + OBSTACLESPREAD: potential_fields.append((-alpha * (OBSTACLESPREAD + radius - distance) * math.cos(angle), -alpha * (OBSTACLESPREAD + radius - distance) * math.sin(angle))) # merge potential fields return self.merge_potential_fields(potential_fields) def tangential_fields_func(x, y, res): alpha = 1 potential_fields = [] for i in range(len(self.grid)): for j in range(len(self.grid[i])): if i > 795 or j > 795: continue if (self.grid[i][j] > 0.8 and self.grid[i+1][j] > 0.8 and self.grid[i+2][j] > 0.8 and self.grid[i+3][j] > 0.8) or (self.grid[i][j] > 0.8 and self.grid[i][j+1] > 0.8 and self.grid[i][j+2] > 0.8 and self.grid[i][j+3] > 0.8): xavg = i - 400 yavg = j - 400 radius = 10 # magic number - frob distance = math.sqrt((xavg - x)**2 + (yavg - y)**2) #tank distance from center angle = math.atan2(yavg - y, xavg - x) + math.pi/2 if distance < radius + OBSTACLESPREAD: potential_fields.append((-alpha * (OBSTACLESPREAD + radius - distance) * math.cos(angle), -alpha * (OBSTACLESPREAD + radius - distance) * math.sin(angle))) # for obstacle in self.obstacles: # xavg = 0 # yavg = 0 # for corner in obstacle: # xavg += corner[0] # yavg += corner[1] # xavg = xavg / len(obstacle) # yavg = yavg / len(obstacle) # radius = math.sqrt((obstacle[0][0] - xavg)**2 + (obstacle[0][1] - yavg)**2) # distance = math.sqrt((xavg - x)**2 + (yavg - y)**2) #tank distance from center # angle = math.atan2(yavg - y, xavg - x) + math.pi/2 # if distance < radius + OBSTACLESPREAD: # potential_fields.append((-alpha * (OBSTACLESPREAD + radius - distance) * math.cos(angle), -alpha * (OBSTACLESPREAD + radius - distance) * math.sin(angle))) return self.merge_potential_fields(potential_fields) def super_tab(x, y, res): potential_fields = [attractive_fields_func(x, y, res)] potential_fields = potential_fields + [repulsive_fields_func(x, y, res)] potential_fields = potential_fields + [tangential_fields_func(x, y, res)] merged = self.merge_potential_fields(potential_fields) return merged[0], merged[1] self.attractive_fields_func = attractive_fields_func self.repulsive_fields_func = repulsive_fields_func self.tangential_fields_func = tangential_fields_func self.super_tab = super_tab if self.iterations % 5 == 0 and (self.iterations % 50 != 5 and self.iterations % 50 != 10): for tank in mytanks: # self.potential_fields[tank.index] = self.calculate_attractive_fields(tank) # potential_fields = self.calculate_attractive_fields(tank) target_point = self.figure_out_where_to_go(tank.x, tank.y) self.move_to_position(tank, target_point[0], target_point[1]) # self.potential_fields[tank.index] = self.potential_fields[tank.index] + self.calculate_repulsive_fields(tank, self.obstacles, mytanks + othertanks) # self.potential_fields[tank.index] = self.potential_fields[tank.index] + self.calculate_tangential_fields(tank) # actually move the tanks # for key in self.potential_fields.keys(): # reduce potential fields to one # move in direction based off of dx and dy # self.potential_fields[key] = self.merge_potential_fields(self.potential_fields[key]) # for tank in mytanks: # self.move_to_position(tank, tank.x + self.potential_fields[tank.index][0], tank.y + self.potential_fields[tank.index][1]) results = self.bzrc.do_commands(self.commands)