def __init__(self, ants, logger): try: # Setup shared variables self.ants = ants self.logger = logger self.render = render(ants, self) self.turn = 0 self.diffuse_rate = DIFFUSE_RATE self.coefficient = COEFFICIENT # Time out setup total_time = self.ants.turntime safe_time = total_time * TIME_OUT_BUFFER self.time_out_vision = total_time - (TIME_OUT_VISION * safe_time) self.time_out_analysis = self.time_out_vision - (TIME_OUT_ANALYSIS * safe_time) self.time_out_diffuse = self.time_out_analysis - (TIME_OUT_DIFFUSE * safe_time) self.logger.debug("Time out total: %s , safe: %s", total_time, safe_time) self.logger.debug("Time out vision: %s", self.time_out_vision) self.logger.debug("Time out analysis: %s", self.time_out_analysis) self.logger.debug("Time out diffuse: %s", self.time_out_diffuse) self.kill_time = self.ants.turns * KILL_TIME self.logger.debug("Kill time: %s", self.kill_time) # Setup my ant variables self.my_hills = set() self.num_my_hills = 0 self.prev_my_hills = 0 self.my_dead_ants = [] self.my_ants = set() self.movable = set() self.num_my_ants = 0 # Setup enemy ant variables self.enemy_dead_ants = [] self.enemy_ants = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()] self.enemy_hills = [set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()] self.num_enemy_ants = 0 # Setup food variables self.food = set() self.desired = set() self.weakest = 0 self.target = 0 self.confidence = 0 self.attack_ant = None self.attack_target = None self.food_target = None self.path_max = PATH_MAX self.paths = [] self.map_scan = map_scan(ants, logger, self) # Setup bucket map self.size = BUCKET_SIZE self.bucket_rows = int(self.ants.rows / self.size) self.bucket_cols = int(self.ants.cols / self.size) self.find_bucket = [[(0,0)]*self.ants.cols for row in range(self.ants.rows)] for row in range(self.ants.rows): for col in range(self.ants.cols): b_col = int(col / self.size) b_row = int(row / self.size) self.find_bucket[row][col] = (b_row, b_col) self.bucket_map = [] for row in range(self.bucket_rows + 1): self.bucket_map.append([]) for col in range(self.bucket_cols + 1): self.bucket_map[row].append(bucket(self.size)) for row in range(self.bucket_rows): for col in range(self.bucket_cols): self.bucket_map[row][col].loc = (row * self.size, col * self.size) # Other maps self.occupied = [[False]*self.ants.cols for row in range(self.ants.rows)] self.mobile_map = [[False]*self.ants.cols for row in range(self.ants.rows)] self.food_map = [[False]*self.ants.cols for row in range(self.ants.rows)] self.seen = [[False]*self.ants.cols for row in range(self.ants.rows)] self.passable = [[True]*self.ants.cols for row in range(self.ants.rows)] self.vision = [[False]*self.ants.cols for row in range(self.ants.rows)] self.diffusion = [[0]*self.ants.cols for row in range(self.ants.rows)] self.new_diffusion = [[0]*self.ants.cols for row in range(self.ants.rows)] self.my_attack_map = [[0]*self.ants.cols for row in range(self.ants.rows)] self.enemy_attack_map = [[0]*self.ants.cols for row in range(self.ants.rows)] self.fighting_map = [[-999999]*self.ants.cols for row in range(self.ants.rows)] self.g_map = [[0]*self.ants.cols for row in range(self.ants.rows)] self.g_map = [[0]*self.ants.cols for row in range(self.ants.rows)] self.g_highest = 0 self.nodes = [] # Setup vision offset self.vision_offsets_2 = [] mx = int(sqrt(self.ants.viewradius2)) for d_row in range(-mx,mx+1): for d_col in range(-mx,mx+1): d = d_row**2 + d_col**2 if d <= self.ants.viewradius2: self.vision_offsets_2.append(( d_row%self.ants.rows-self.ants.rows, d_col%self.ants.cols-self.ants.cols )) # Setup attack offset radius = self.ants.attackradius2 * ATTACK_BUFFER self.enemy_attack_offsets_2 = [] mx = int(sqrt(radius)) for d_row in range(-mx,mx+1): for d_col in range(-mx,mx+1): d = d_row**2 + d_col**2 if d <= radius: self.enemy_attack_offsets_2.append(( d_row%self.ants.rows-self.ants.rows, d_col%self.ants.cols-self.ants.cols )) # Setup attack offset radius = self.ants.attackradius2 * DEFEND_BUFFER self.my_attack_offsets_2 = [] mx = int(sqrt(radius)) for d_row in range(-mx,mx+1): for d_col in range(-mx,mx+1): d = d_row**2 + d_col**2 if d <= radius: self.my_attack_offsets_2.append(( d_row%self.ants.rows-self.ants.rows, d_col%self.ants.cols-self.ants.cols )) # Full sized maps self.west_map = [] self.east_map = [] self.north_map = [] self.south_map = [] for row in range(self.ants.rows): self.west_map.append((row - 1) % self.ants.rows) self.east_map.append((row + 1) % self.ants.rows) for col in range(self.ants.cols): self.north_map.append((col - 1) % self.ants.cols) self.south_map.append((col + 1) % self.ants.cols) # Bucket sized maps self.west_bucket_map = [] self.east_bucket_map = [] self.north_bucket_map = [] self.south_bucket_map = [] for row in range(self.bucket_rows): self.west_bucket_map.append((row - 1) % self.bucket_rows) self.east_bucket_map.append((row + 1) % self.bucket_rows) for col in range(self.bucket_cols): self.north_bucket_map.append((col - 1) % self.bucket_cols) self.south_bucket_map.append((col + 1) % self.bucket_cols) self.diffuse_sections = [] self.bucket_sections = [] self.total_sections = int(1 / (SECTION * SECTION)) self.next_diffuse_section = 0 self.next_vision_section = 1 sections = int(1 / SECTION) for row in range(sections): for col in range(sections): left = int((row / sections) * self.ants.rows) top = int((col / sections) * self.ants.cols) right = int(((row + 1) / sections) * self.ants.rows) bottom = int(((col + 1) / sections) * self.ants.cols) section_bounds = bounds(left, top, right, bottom) self.diffuse_sections.append(section_bounds) left = int((row / sections) * self.bucket_rows) top = int((col / sections) * self.bucket_cols) right = int(((row + 1) / sections) * self.bucket_rows) bottom = int(((col + 1) / sections) * self.bucket_cols) section_bounds = bounds(left, top, right, bottom) self.bucket_sections.append(section_bounds) self.diffuse_sections.append(bounds(0, 0, 0, 0)) self.bucket_sections.append(bounds(0, 0, 0, 0)) #self.logger.debug("Diffuse Map size %s, %s", self.ants.rows, self.ants.cols) #for section in self.diffuse_sections: # self.logger.debug("Diffuse Section left %s, top %s, right %s, bottom %s", section.left, section.top, section.right, section.bottom) #self.logger.debug("Bucket Map size %s, %s", self.bucket_rows, self.bucket_cols) #for section in self.bucket_sections: # self.logger.debug("Bucket Section left %s, top %s, right %s, bottom %s", section.left, section.top, section.right, section.bottom) except: self.logger.error("Error game_state init: print_exc(): %s", traceback.format_exc())
def calcSpline(xdata,ydata,E0,segs): lowx=segs[0][1] order=segs[0][0] data=[] mat,vec=bounds(xdata,ydata,segs,E0) #handle singular matrices? try: #soln=scipy.linalg.basic.solve(mat,vec) soln=linalg.solve(array(mat),array(vec)) except linalg.LinAlgError: print "The spline matrix is singular. Using single line as polynomial estimate" #Get first and last marker position and corresponding indexes of the xdata lowx=segs[0][1] highx=segs[-1][2] lindex=xdata.index(lowx) hindex=xdata.index(highx) #get y-values of the markers lowy=ydata[lindex] highy=ydata[hindex] slope=(highy-lowy)/(highx-lowx) for i in range(len(xdata)): dx=xdata[i]-lowx dy=slope*dx data.append(lowy+dy) return data #make polynomials polys=[] offset=0 for seg in segs: order=seg[0] coeffs=soln[offset:offset+order].tolist() poly=Polynomial(coeffs) polys.append(poly) offset=offset+order+2 #generate individual spline data for pre-1st segment for i in range(0,xdata.index(lowx)): #interate through each point of pre-segment except last temp=polys[0].eval(xdata[i]) data.append(temp) offset=0 for i in range(len(segs)): #for each of the segments lowx=segs[i][1] highx=segs[i][2] lowindex=xdata.index(lowx) highindex=xdata.index(highx) #iterate through xdata bound by seg ranges for j in range(lowindex,highindex): #iterate through each point of segment except last one #for i in [0,1]: temp=polys[i].eval(xdata[j]) data.append(temp) highx=segs[-1][2] order=segs[-1][0] for i in range(xdata.index(highx),len(xdata)): temp=polys[-1].eval(xdata[i]) data.append(temp) #data.append(1) sp_E0=polys[0].eval(E0) return data,sp_E0